springboot搭建通用mapper

对于搭建一个小项目自己测试玩如果采用传统的SSM框架配置起来太过于繁琐,使用springboot简化配置再搭配通用mapper简直不要太方便,话不多说,直接上代码。

  • 首先是pom文件,直接去spring官网找,或者通过idea工具打开。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.0.4.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.1</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--oracle -->
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>jdbc</artifactId>
            <version>11.2.0.3.0</version>
        </dependency>
        <!--mapper -->
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>2.0.4</version>
        </dependency>
        <!-- page-helper-spring-boot-starter -->
        <dependency>
            <!-- <scope>provided</scope> -->
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.9</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.10</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
  • 配置文件application.yml,这里使用的是oracle数据库。
spring:
  datasource:
    url: jdbc:oracle:thin:@*******
    username: ****
    password: ****
    #使用druid数据源
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: oracle.jdbc.driver.OracleDriver
    #可以使用监控过滤器,配置监控统计的filters,去掉后监控界面sql将无法统计,‘wall’用于防火墙
    filters: stat,wall.log4j
    #最大活跃数
    maxActive: 20
    #初始化数量
    initialSize: 1
    maxWait: 60000
    #通过connectionProperties属性来打开mergeSql功能;慢查询记录
    #connectionProperties:druid.stat.mergeSql=true
    #druid.stat.slowSqlMillis=5000
    minIdle: 1
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: select ‘x‘
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    #打开pscache
    poolPreparedStatements: true
    maxOpenPreparedStatements: 20


mybatis:
  type-aliases-package: com.example.demo.pojo
  mapper-locations: classpath:mapper/*.xml
  configuration:
    map-underscore-to-camel-case: true
  • 到这里就已经配置好了,接下来可以写三层架构了

controller层:

package com.example.demo.controller;

import com.example.demo.pojo.LoanInfo;
import com.example.demo.service.LoanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class LoanController {

    @Autowired
    private LoanService loanService;

    @RequestMapping("/test/get")
    public Object getLoanInfo(){
        List<LoanInfo> loanInfoList = loanService.getLoanInfo();
        return loanInfoList;
    }
    @RequestMapping("/test/get2")
    public Object getLoanInfo2(){
        List<LoanInfo> loanInfoList = loanService.getLoanInfo2();
        return loanInfoList;
    }
    @RequestMapping("/test/get3")
    public Object getLoanInfo3(){
        List<LoanInfo> loanInfoList = loanService.getLoanInfo3();
        return loanInfoList;
    }
}

service层:

package com.example.demo.service;

import com.example.demo.mapper.LoanMapper;
import com.example.demo.pojo.LoanInfo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@SuppressWarnings("ALL")
@Service
@Slf4j
public class LoanService {
    @Autowired
    private LoanMapper loanMapper;

    public List<LoanInfo> getLoanInfo() {
        PageHelper.startPage(1, 3);
        List<LoanInfo> loanInfos = loanMapper.selectAll();
        PageInfo<LoanInfo> pageInfo = new PageInfo<>(loanInfos);
        long total = pageInfo.getTotal();
        List<LoanInfo> list = pageInfo.getList();
        log.info("total:"+total);
        return list;
    }

    public List<LoanInfo> getLoanInfo2() {
        Example example = new Example(LoanInfo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.orEqualTo("loaninfoid", 1);
        example.and(criteria);
        List<LoanInfo> loanInfos = loanMapper.selectByExample(example);
        return loanInfos;
    }

    public List<LoanInfo> getLoanInfo3() {
        List<LoanInfo> loanInfos = loanMapper.queryLoaninfos(13);
        return loanInfos;
    }
}

dao层:这里使用通用mapper只需要写一个接口继承Mapper<T> (T是自定义的javabean)

package com.example.demo.mapper;

import com.example.demo.pojo.LoanInfo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

public interface LoanMapper extends Mapper<LoanInfo> {
@Select(
"select * from table") List<LoanInfo> queryLoaninfos(@Param("loaninfoid") Integer loaninfoid); }

pojo类:

package com.example.demo.pojo;

import lombok.Data;

import javax.persistence.Table;
import javax.persistence.Transient;

@Data
//对应数据库table名 @Table(name
= "LOANINFO") public class LoanInfo { private Integer loaninfoid; private Integer loaninfonum; private Float loaninfomoney; private Integer loaninfodate; private String loaninfoname; private Integer loaninfocardtype; private String loaninfocardnum; private String loaninfocreatetime;
   //用transient关键字标记的成员变量不参与序列化过程。 @Transient
private String partnername; }

至此就已经搭建完成,可以完成简单的CRUD,不需要写xml文件,如果需要多表查询写sql语句也可以通过@Select注解来实现。

通用mapper中提供了很多方法很好用,以下内容转载自https://blog.csdn.net/Code_shadow/article/details/81837602

Select
    方法:List<T> select(T record);
    说明:根据实体中的属性值进行查询,查询条件使用等号

    方法:T selectByPrimaryKey(Object key);
    说明:根据主键字段进行查询,方法参数必须包含完整的主键属性,查询条件使用等号

    方法:List<T> selectAll();
    说明:查询全部结果,select(null)方法能达到同样的效果

    方法:T selectOne(T record);
    说明:根据实体中的属性进行查询,只能有一个返回值,有多个结果是抛出异常,查询条件使用等号

    方法:int selectCount(T record);
    说明:根据实体中的属性查询总数,查询条件使用等号

Insert
    方法:int insert(T record);
    说明:保存一个实体,null的属性也会保存,不会使用数据库默认值

    方法:int insertSelective(T record);
    说明:保存一个实体,null的属性不会保存,会使用数据库默认值

Update
    方法:int updateByPrimaryKey(T record);
    说明:根据主键更新实体全部字段,null值会被更新

    方法:int updateByPrimaryKeySelective(T record);
    说明:根据主键更新属性不为null的值

Delete
    方法:int delete(T record);
    说明:根据实体属性作为条件进行删除,查询条件使用等号

    方法:int deleteByPrimaryKey(Object key);
    说明:根据主键字段进行删除,方法参数必须包含完整的主键属性

    Example方法
    方法:List<T> selectByExample(Object example);
    说明:根据Example条件进行查询
    重点:这个查询支持通过Example类指定查询列,通过selectProperties方法指定查询列

    方法:int selectCountByExample(Object example);
    说明:根据Example条件进行查询总数

    方法:int updateByExample(@Param("record") T record, @Param("example") Object example);
    说明:根据Example条件更新实体record包含的全部属性,null值会被更新

    方法:int updateByExampleSelective(@Param("record") T record, @Param("example") Object example);
    说明:根据Example条件更新实体record包含的不是null的属性值

    方法:int deleteByExample(Object example);
    说明:根据Example条件删除数据

条件查询
// 创建Example
Example example = new Example(TestTableVO.class);
// 创建Criteria
Example.Criteria criteria = example.createCriteria();
// 添加条件
criteria.andEqualTo("isDelete", 0);
criteria.andLike("name", "%abc123%");
List<TestTableVO> list = testTableDao.selectByExample(example);

 

springboot搭建通用mapper

上一篇:Android中的Environment.getExternalStorageState使用


下一篇:移动端的那些坑