Springboot集成MyBatisPlus

一、MyBatisPlus简介

       MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。

1、功能

  1. 单表CURD(简单 +  批量)操作,自动完成;
  2. 分页插件,Count  查询 自动 或 自定义SQL 查询;
  3. Spring 根据不同环境加载不同配置支持。

2、特性

  1. 无侵入:只做增强不做改变,引入它不会对现有工程产生影响,如丝般顺滑;
  2. 损耗小:启动即会自动注入基本 CURD,性能基本无损耗,直接面向对象操作;
  3. 强大的 CRUD 操作:内置通用 Mapper、通用 Service,仅仅通过少量配置即可实现单表大部分 CRUD 操作,更有强大的条件构造器,满足各类使用需求;
  4. 支持 Lambda 形式调用:通过 Lambda 表达式,方便的编写各类查询条件,无需再担心字段写错;
  5. 支持多种数据库:支持 MySQL、MariaDB、Oracle、DB2、H2、HSQL、SQLite、Postgre、SQLServer2005、SQLServer 等多种数据库;
  6. 支持主键自动生成:支持多达 4 种主键策略(内含分布式唯一 ID 生成器 - Sequence),可自由配置,完美解决主键问题;
  7. 支持 XML 热加载:Mapper 对应的 XML 支持热加载,对于简单的 CRUD 操作,甚至可以无 XML 启动;
  8. 支持 ActiveRecord 模式:支持 ActiveRecord 形式调用,实体类只需继承 Model 类即可进行强大的 CRUD 操作;
  9. 支持自定义全局通用操作:支持全局通用方法注入( Write once, use anywhere );
  10. 支持关键词自动转义:支持数据库关键词(order、key......)自动转义,还可自定义关键词;
  11. 内置代码生成器:采用代码或者 Maven 插件可快速生成 Mapper 、 Model 、 Service 、 Controller 层代码,支持模板引擎,更有超多自定义配置等您来使用;
  12. 内置分页插件:基于 MyBatis 物理分页,开发者无需关心具体操作,配置好插件之后,写分页等同于普通 List 查询;
  13. 内置性能分析插件:可输出 Sql 语句以及其执行时间,建议开发测试时启用该功能,能快速揪出慢查询;
  14. 内置全局拦截插件:提供全表 delete 、 update 操作智能分析阻断,也可自定义拦截规则,预防误操作;
  15. 内置 Sql 注入剥离器:支持 Sql 注入剥离,有效预防 Sql 注入攻击。

3、架构

Springboot集成MyBatisPlus_第1张图片

4、代码及文档发布地址

  • 官方地址:http://mp.baomidou.com
  • Github代码地址: https://github.com/baomidou/mybatis-plus
  • Gitee代码地址:  https://gitee.com/baomidou/mybatis-plus
  • API地址:http://mp.baomidou.com/guide/

二、MyBatis Plus

1、配置 MapperScan 注解

 启动类中配置:

@SpringBootApplication
@MapperScan("com.example.demo.*.mapper")
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(QuickStartApplication.class, args);
    }
}

自定义配置类中配置: 

@EnableTransactionManagement
@Configuration
@MapperScan("com.example.demo.*.mapper")
public class MybatisPlusConfig {

}

2、Mapper CRUD 接口(Mapper接口需要继承BaseMapper

  • int insert(T entity); //插入一条记录,返回插入成功记录数
  • int deletebyId(Serializable id); //根据 ID 删除,返回删除成功记录数
  • int deleteByMap(@Param(Constants.COLUMN_MAP) Map columnMap);  //根据 columnMap 条件,删除记录,返回删除成功记录数
  • int delete(@Param(Constants.WRAPPER) Wrapper queryWrapper); //根据 entity 条件,删除记录,返回删除成功记录数
  • int deleteBatchIds(@Param(Constants.COLLECTION) Collection idList); // 删除(根据ID 批量删除),返回删除成功记录数
  • int updateById(@Param(Constants.ENTITY) T entity); // 根据 ID 修改,返回修改记录数
  • int update(@Param(Constants.ENTITY) T entity, @Param(Constants.WRAPPER) Wrapper updateWrapper); // 根据 whereEntity 条件,更新记录,返回记录数
  • T selectById(Serializable id);  // 根据 ID 查询
  • List selectBatchIds(@Param(Constants.COLLECTION) Collection idList); //  查询(根据ID 批量查询)
  • List selectByMap(@Param(Constants.COLUMN_MAP) Map columnMap);// 查询(根据 columnMap 条件)
  • T selectOne(@Param(Constants.WRAPPER) Wrapper queryWrapper); //根据 entity 条件,查询一条记录
  • Integer selectCount(@Param(Constants.WRAPPER) Wrapper queryWrapper); // 根据 Wrapper 条件,查询总记录数
  • List selectList(@Param(Constants.WRAPPER) Wrapper queryWrapper); // 根据 entity 条件,查询全部记录
  • List> selectMaps(@Param(Constants.WRAPPER) Wrapper queryWrapper); //根据 Wrapper 条件,查询全部记录
  • List selectObjs(@Param(Constants.WRAPPER) Wrapper queryWrapper); //根据 Wrapper 条件,查询全部记录,只返回第一个字段的值
  • IPage selectPage(IPage page, @Param(Constants.WRAPPER) Wrapper queryWrapper); //根据 entity 条件,查询全部记录(并翻页)
  • IPage> selectMapsPage(IPage page, @Param(Constants.WRAPPER) Wrapper queryWrapper); // 根据 Wrapper 条件,查询全部记录(并翻页)
  • 3、Service CRUD 接口(Service接口需要继承IService,实现类继承ServiceImp

    • boolean save(T entity); // 插入一条记录(选择字段,策略插入)
    • boolean save(T entity); // 插入一条记录(选择字段,策略插入)
    • boolean saveOrUpdateBatch(Collection entityList); //批量修改插入
    • boolean saveOrUpdateBatch(Collection entityList, int batchSize); //批量修改插入
    • boolean removeById(Serializable id); // 根据 ID 删除
    • boolean removeByMap(Map columnMap); //根据 columnMap 条件,删除记录
    • boolean remove(Wrapper queryWrapper); // 根据 entity 条件,删除记录
    • boolean removeByIds(Collection idList); //删除(根据ID 批量删除)
    • boolean updateById(T entity); // 根据 ID 选择修改
    • boolean update(T entity, Wrapper updateWrapper); //根据 whereEntity 条件,更新记录
    • boolean updateBatchById(Collection entityList, int batchSize); //根据ID 批量更新
    • boolean saveOrUpdate(T entity); //TableId 注解存在更新记录,否插入一条记录
    • T getById(Serializable id); //根据 ID 查询
    • Collection listByIds(Collection idList); //查询(根据ID 批量查询)
    • Collection listByMap(Map columnMap); // 查询(根据 columnMap 条件)
    • T getOne(Wrapper queryWrapper, boolean throwEx); //根据 Wrapper,查询一条记录
    • Map getMap(Wrapper queryWrapper); //根据 Wrapper,查询一条记录
    • Object getObj(Wrapper queryWrapper); // 根据 Wrapper,查询一条记录
    • int count(Wrapper queryWrapper); //根据 Wrapper 条件,查询总记录数
    • List list(Wrapper queryWrapper); //查询列表
    • IPage page(IPage page, Wrapper queryWrapper); //  翻页查询
    • List> listMaps(Wrapper queryWrapper); // 查询列表
    • List listObjs(Wrapper queryWrapper); //根据 Wrapper 条件,查询全部记录
    • IPage> pageMaps(IPage page, Wrapper queryWrapper); // 翻页查询
    • 4、条件构造器(AbstractWrapper-->QueryWrapper&UpdateWrapper

      • allEq:全部eq(或个别isNull)    
      •      例1: allEq({id:1,name:"老王",age:null})--->id = 1 and name = '老王' and age is null
      •      例2: allEq({id:1,name:"老王",age:null}, false)--->id = 1 and name = '老王'
      • eq:等于 =
      •      例1: allEq((k,v) -> k.indexOf("a") > 0, {id:1,name:"老王",age:null})--->name = '老王' and age is null
      •      例2: allEq((k,v) -> k.indexOf("a") > 0, {id:1,name:"老王",age:null}, false)--->name = '老王'
      • ne:不等于 <>
      •      例: ne("name", "老王")--->name <> '老王'
      • gt:大于 >
      •      例: gt("age", 18)--->age > 18
      • ge:大于等于 >=
      •      例: ge("age", 18)--->age >= 18
      • lt:小于 <
      •      例: lt("age", 18)--->age < 18
      • le:小于等于 <=
      •      例: le("age", 18)--->age <= 18
      • between:BETWEEN 值1 AND 值2
      •      例: between("age", 18, 30)--->age between 18 and 30
      • notBetween:NOT BETWEEN 值1 AND 值2
      •      例: notBetween("age", 18, 30)--->age not between 18 and 30
      • like:LIKE '%值%'
      •       例: like("name", "王")--->name like '%王%'
      • notLike:NOT LIKE '%值%'
      •       例: notLike("name", "王")--->name not like '%王%'
      • likeLeft:LIKE '%值'
      •       例: likeLeft("name", "王")--->name like '%王'
      • likeRight:LIKE '值%'
      •       例: likeRight("name", "王")--->name like '王%'
      • isNull:字段 IS NULL
      •       例: isNull("name")--->name is null
      • isNotNull:字段 IS NULL
      •       例: isNotNull("name")--->name is not null
      • in:字段 IN (value.get(0), value.get(1), ...)
      •       例: in("age",{1,2,3})--->age in (1,2,3)
      • notIn:字段 IN (value.get(0), value.get(1), ...)
      •       例: notIn("age",{1,2,3})--->age not in (1,2,3)
      • inSql:字段 IN ( sql语句 )
      •       例: inSql("age", "1,2,3,4,5,6")--->age in (1,2,3,4,5,6)
      •       例: inSql("id", "select id from table where id < 3")--->id in (select id from table where id < 3)
      • notInSql:字段 NOT IN ( sql语句 )
      •       例: notInSql("age", "1,2,3,4,5,6")--->age not in (1,2,3,4,5,6)
      •       例: notInSql("id", "select id from table where id < 3")--->age not in (select id from table where id < 3)
      • groupBy:分组:GROUP BY 字段, ...
      •       例: groupBy("id", "name")--->group by id,name
      • orderByAsc:排序:ORDER BY 字段, ... ASC
      •       例: orderByAsc("id", "name")--->order by id ASC,name ASC
      • orderByDesc:排序:ORDER BY 字段, ... DESC
      •       例: orderByDesc("id", "name")--->order by id DESC,name DESC
      • orderBy:排序:ORDER BY 字段, ...
      •       例: orderBy(true, true, "id", "name")--->order by id ASC,name ASC
      • having:HAVING ( sql语句 )
      •       例: having("sum(age) > 10")--->having sum(age) > 10
      •       例: having("sum(age) > {0}", 11)--->having sum(age) > 11
      • or:拼接 OR 。注意事项:主动调用or表示紧接着下一个方法不是用and连接!(不调用or则默认为使用and连接)
      •       例: eq("id",1).or().eq("name","老王")--->id = 1 or name = '老王'
      •       例: or(i -> i.eq("name", "李白").ne("status", "活着"))--->or (name = '李白' and status <> '活着')
      • and:AND 嵌套
      •       例: and(i -> i.eq("name", "李白").ne("status", "活着"))--->and (name = '李白' and status <> '活着')
      • apply:拼接 sql 。 注意事项:该方法可用于数据库函数 动态入参的params对应前面sqlHaving内部的{index}部分.这样是不会有sql注入风险的,反之会有!
      •      例: apply("id = 1")--->having sum(age) > 10
      •      例: apply("date_format(dateColumn,'%Y-%m-%d') = '2008-08-08'")--->date_format(dateColumn,'%Y-%m-%d') = '2008-08-08'")
      •      例: apply("date_format(dateColumn,'%Y-%m-%d') = {0}", "2008-08-08")--->date_format(dateColumn,'%Y-%m-%d') = '2008-08-08'")
      • last:无视优化规则直接拼接到 sql 的最后。注意事项:只能调用一次,多次调用以最后一次为准 有sql注入的风险,请谨慎使用
      •      例: last("limit 1")
      • exists:拼接 EXISTS ( sql语句 )
      •      例: exists("select id from table where age = 1")--->exists (select id from table where age = 1)
      • notExists:拼接 NOT EXISTS ( sql语句 )
      •      例: notExists("select id from table where age = 1")--->not exists (select id from table where age = 1)
      • nested:正常嵌套 不带 AND 或者 OR
      •      例: nested(i -> i.eq("name", "李白").ne("status", "活着"))--->(name = '李白' and status <> '活着')

      注意:  使用的是数据库字段,不是 Java 属性。

      5、分页插件

      spring XML方式配置:

      
      
          
              
              
          
      

      springboot Bean的配置方式:

      @Bean
      public PaginationInterceptor paginationInterceptor() {
         return new PaginationInterceptor();
      }

      6、逻辑删除

      @Bean
      public ISqlInjector sqlInjector() {
           return new LogicSqlInjector();
      }

         逻辑删除:  并不会真正的从数据库中将数据删除掉,而是将当前被删除的这条数据中的一个逻辑删除字段置为删除状态。             即:tbl_user   logic_flag = 1    →  -1 

      • logicDeleteValue   逻辑删除全局值 
      • logicNotDeleteValue  逻辑未删除全局值
      • 在 POJO 的逻辑删除字段  添加  @TableLogic 注解
      • 会在 mp 自带查询和更新方法的 sql 后面,追加『逻辑删除字段』 = 『LogicNotDeleteValue默认值』 删除方法:deleteById()和其他 delete 方法,  底层 SQL 调用的是 update tbl_xxx  set  『逻辑删除字段』=『logicDeleteValue 默认值』

      7、执行分析插件

      @Bean
      public PerformanceInterceptor performanceInterceptor() {
             PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();
             performanceInterceptor.setFormat(true);
             return performanceInterceptor;
      }

      功能: 

      • SQL 执行分析拦截器,只支持 MySQL5.6.3 以上版本;
      • 该插件的作用是分析  DELETE   UPDATE 语句,防止小白或者恶意进行 DELETE  UPDATE 全表操作;
      • 只建议在开发环境中使用,不建议在生产环境使用;
      • 在插件的底层  通过 SQL 语句分析命令:Explain分析当前的 SQL 语句, 根据结果集中的 Extra 列来断定当前是否全表操作。

      8、性能分析插件

      spring XML配置方式:

      
      
          
          
              
              
              
          
      

      springboot bean的方式:

      @Bean
      @Profile({"dev","test"})// 设置 dev test 环境开启
      public PerformanceInterceptor performanceInterceptor() {
            return new PerformanceInterceptor();
      }

      功能: 

      • 性能分析拦截器,用于输出每条  SQL  语句及其执行时间 ;
      • SQL 性能执行分析,开发环境使用,超过指定时间,停止运行,有助于发现问题。

      9、乐观锁插件

      spring XML配置方式:

      springboot bean配置方式:

      @Bean
      public OptimisticLockerInterceptor optimisticLockerInterceptor() {
          return new OptimisticLockerInterceptor();
      }

      乐观锁实现方式:

      • 取出记录时,获取当前version
      • 更新时,带上这个version
      • 执行更新时, set version = newVersion where version = oldVersion
      • 如果version不对,就更新失败
      • 注解实体字段 @Version 必须要的
      • 支持的数据类型只有:int,Integer,long,Long,Date,Timestamp,LocalDateTime

      10、ActiveRecord(活动记录)

              Active  Record(活动记录),是一种领域模型模式,特点是一个模型类对应关系型数据库中的一个表,而模型类的一个实例对应表中的一行记录;

              ActiveRecord 一直广受动态语言(PHP 、Ruby等)的喜爱,而  Java  作为准静态语言,对于ActiveRecord往往只能感叹其优雅,所以 MP 也在  AR  道路上进行了一定的探索;

      使用 AR 模式:仅仅需要让实体类继承 Model 类且实现主键指定方法,即可开启 AR 之旅。

      public class User extends Model {
      
          private Integer id;
      
          ......
      
          @Override
          protected Serializable pkVal() {
              return id;
          }
      }

      三、代码生成器

      package com.example.demo.automatic;
      
      import com.baomidou.mybatisplus.annotation.DbType;
      import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
      import com.baomidou.mybatisplus.core.toolkit.StringPool;
      import com.baomidou.mybatisplus.core.toolkit.StringUtils;
      import com.baomidou.mybatisplus.generator.AutoGenerator;
      import com.baomidou.mybatisplus.generator.InjectionConfig;
      import com.baomidou.mybatisplus.generator.config.*;
      import com.baomidou.mybatisplus.generator.config.po.TableInfo;
      import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
      import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
      import lombok.extern.slf4j.Slf4j;
      
      import java.util.ArrayList;
      import java.util.List;
      import java.util.Scanner;
      
      import static com.baomidou.mybatisplus.generator.config.rules.DateType.ONLY_DATE;
      
      /**
       * 类名:CodeGenerator.java
       * 功能:执行 main 方法控制台输入模块表名回车自动生成对应项目目录中
       * 描述:
       * 创建人:typ
       * 创建时间:2018/11/20 15:34
       * 修改人:
       * 修改描述:
       * 修改时间:
       */
      @Slf4j
      public class CodeGenerator {
      
          /**
           * 方法名:
           * 功能:读取控制台内容
           * 描述:
           * 创建人:typ
           * 创建时间:2018/11/20 15:34
           * 修改人:
           * 修改描述:
           * 修改时间:
           */
          public static String scanner(String tip) {
              Scanner scanner = new Scanner(System.in);
              StringBuilder help = new StringBuilder();
              help.append("请输入" + tip + ":");
              System.out.println(help.toString());
              if (scanner.hasNext()) {
                  String ipt = scanner.next();
                  if (StringUtils.isNotEmpty(ipt)) {
                      return ipt;
                  }
              }
              throw new MybatisPlusException("请输入正确的" + tip + "!");
          }
      
          /**
           * 方法名:
           * 功能:代码生成
           * 描述:
           * 创建人:typ
           * 创建时间:2018/11/20 15:35
           * 修改人:
           * 修改描述:
           * 修改时间:
           */
          public static void main(String[] args) {
              // 代码生成器
              AutoGenerator mpg = new AutoGenerator();
      
              // 1.全局配置
              GlobalConfig gc = new GlobalConfig();
              String projectPath = "F:/workingExperience/workspace/workIeda/springboot-master/springboot-mybatisplus";
              gc.setOutputDir(projectPath + "/src/main/java");
              gc.setAuthor("typ"); // 注释作者
              gc.setDateType(ONLY_DATE); // 注释时间类型
              gc.setOpen(false);
              gc.setActiveRecord(true); //是否需要ActiveRecord特性
              gc.setEnableCache(false); // mapper.xml中开启二级缓存
              gc.setBaseResultMap(true); // mapper.xml 中使用ResultMap
              gc.setBaseColumnList(true); // mapper.xml中使用columList
              gc.setKotlin(true); //是否生成kotlin代码
              gc.setControllerName("%sController"); //自定义controoler类的命名格式,列如:XxxController.java
              gc.setServiceName("%sService"); //自定义service接口的命名格式,列如:XxxService.java
              gc.setServiceImplName("%sServiceImpl"); //自定义service实现类的命名格式,列如:XxxServiceImpl.java
              gc.setMapperName("%sMapper"); //自定义mapper接口的命名格式,列如:XxxMapper.java
              gc.setXmlName("%sMapper"); //自定义mapper.xml的命名格式,列如:XxxMapper.xml
              mpg.setGlobalConfig(gc);
      
              // 2.数据源配置
              DataSourceConfig dsc = new DataSourceConfig();
              dsc.setDbType(DbType.MYSQL); // 配置数据库类型
              dsc.setUrl("jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&useSSL=false&characterEncoding=utf8");
              dsc.setDriverName("com.mysql.jdbc.Driver");
              dsc.setUsername("root");
              dsc.setPassword("admin");
              mpg.setDataSource(dsc);
      
              // 4.包配置
              PackageConfig pc = new PackageConfig();
              pc.setModuleName(scanner("模块名")); // 控制台输入方式
              pc.setParent("com.example.demo");
      //        pc.setController("controller");
      //        pc.setEntity("entity");
      //        pc.setService("service");
      //        pc.setServiceImpl("impl");
              mpg.setPackageInfo(pc);
      
              // 5.自定义配置
              InjectionConfig cfg = new InjectionConfig() {
                  @Override
                  public void initMap() {
                      // to do nothing
                  }
              };
      
              // 6.自定mapper.xml生成路径
              List focList = new ArrayList<>();
              focList.add(new FileOutConfig("/templates/mapper.xml.ftl") {
                  @Override
                  public String outputFile(TableInfo tableInfo) {
                      // 自定义输入文件名称
                      return projectPath + "/src/main/resources/mapper/" + pc.getModuleName()
                              + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
                  }
              });
              cfg.setFileOutConfigList(focList);
              mpg.setCfg(cfg);
              mpg.setTemplate(new TemplateConfig().setXml(null));
      
              // 7.策略配置
              StrategyConfig strategy = new StrategyConfig();
      //        strategy.setCapitalMode(true); // 全局大写命名
      //        strategy.setTablePrefix(new String[]{"tb_","tbs_"}); // 数据库表前缀
              strategy.setNaming(NamingStrategy.underline_to_camel); // 表名生成策略
              strategy.setColumnNaming(NamingStrategy.underline_to_camel);
              strategy.setEntityLombokModel(true);
              strategy.setRestControllerStyle(true);
              strategy.setInclude(scanner("表名")); // 需要生成的表名
              strategy.setSuperEntityColumns("id");
              strategy.setControllerMappingHyphenStyle(true);
              strategy.setTablePrefix(pc.getModuleName() + "_");
              mpg.setStrategy(strategy);
              mpg.setTemplateEngine(new FreemarkerTemplateEngine());
              mpg.execute();
          }
      
      }

       四、集成Springboot

      1、添加依赖

      特别说明:  Mybatis 及 Mybatis-Spring 依赖请勿加入项目配置,以免引起版本冲突。Mybatis-Plus 会自动帮你维护!

      
      		
      			org.springframework.boot
      			spring-boot-starter
      		
      		
      			org.springframework.boot
      			spring-boot-starter-web
      		
      		
      			org.springframework.boot
      			spring-boot-starter-test
      			test
      		
      		
      			mysql
      			mysql-connector-java
      			5.1.46
      		
      		
      			com.alibaba
      			druid
      		
      		
      			org.projectlombok
      			lombok
      			provided
      		
      		
      			com.alibaba
      			fastjson
      		
      		
      			com.baomidou
      			mybatis-plus-boot-starter
      			3.0.1
      		
      		
      		
      			com.baomidou
      			dynamic-datasource-spring-boot-starter
      			2.4.2
      		
      		
      		
      			org.apache.velocity
      			velocity-engine-core
      			2.0
      		
      		
      		
      			org.freemarker
      			freemarker
      			2.3.28
      		
      	

      2、配置文件

      application.yml

      server:
        port: 8081
      
      # mysql
      spring:
          profiles:
              active: dev
          datasource:
              type: com.alibaba.druid.pool.DruidDataSource
              driverClassName: com.mysql.jdbc.Driver
          jackson:
              time-zone: GMT+8
              date-format: yyyy-MM-dd HH:mm:ss
      
      # 主键策略配置
      mybatis-plus:
        global-config:
          db-config:
            id-type: auto

      application-dev.yml

      spring:
          datasource:
            url: jdbc:mysql://127.0.0.1:3306/test?autoReconnect=true&useUnicode=true&characterEncoding=utf-8&autoReconnect=true
            username: root
            password: admin
            #  连接池的配置信息
            initialSize: 10
            minIdle: 10
            maxActive: 100
            maxWait: 60000
            timeBetweenEvictionRunsMillis: 300000
            minEvictableIdleTimeMillis: 3600000
            validationQuery: SELECT 1 FROM DUAL
            testWhileIdle: true
            testOnBorrow: false
            testOnReturn: false
            poolPreparedStatements: true
            maxPoolPreparedStatementPerConnectionSize: 20

      3、MybatisPlus配置

      package com.example.demo.config;
      
      import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
      import com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor;
      import org.mybatis.spring.annotation.MapperScan;
      import org.springframework.context.annotation.Bean;
      import org.springframework.context.annotation.Configuration;
      import org.springframework.transaction.annotation.EnableTransactionManagement;
      
      /**
       * 路径:com.example.demo.config
       * 类名:
       * 功能:MybatisPlus配置文件
       * 备注:
       * 创建人:typ
       * 创建时间:2018/11/23 15:56
       * 修改人:
       * 修改备注:
       * 修改时间:
       */
      @EnableTransactionManagement
      @Configuration
      @MapperScan("com.example.demo.*.mapper")
      public class MybatisPlusConfig {
      
          /**
           * 方法名:
           * 功能:mybatis-plus分页插件
           * 描述:
           * 创建人:typ
           * 创建时间:2018/11/23 15:57
           * 修改人:
           * 修改描述:
           * 修改时间:
           */
          @Bean
          public PaginationInterceptor paginationInterceptor() {
              return new PaginationInterceptor();
          }
      
          /**
           * 方法名:
           * 功能:打印 sql
           * 描述:SQL执行效率插件【生产环境可以关闭】
           * 创建人:typ
           * 创建时间:2018/11/27 9:58
           * 修改人:
           * 修改描述:
           * 修改时间:
           */
          @Bean
          public PerformanceInterceptor performanceInterceptor() {
              PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();
              performanceInterceptor.setFormat(true);
              return performanceInterceptor;
          }
      
      
      
      }
      

      4、编码

      实体类

      package com.example.demo.crud.entity;
      
      import com.baomidou.mybatisplus.extension.activerecord.Model;
      import lombok.Data;
      
      import java.io.Serializable;
      
      /**
       * 路径:com.example.demo.crud.entity
       * 类名:
       * 功能:1.使用mybatis plus的一般形式
       *      2.使用myBatis Plus继承Model的形式
       * 备注:
       * 创建人:typ
       * 创建时间:2018/11/23 14:09
       * 修改人:
       * 修改备注:
       * 修改时间:
       */
      //1.使用mybatis plus的一般形式
      /*@Data
      public class User {
      
          private Integer id;
      
          private String username;
      
          private String password;
      
      }*/
      
      //2.使用myBatis Plus继承Model的形式
      @Data
      public class User extends Model {
      
          private Integer id;
      
          private String username;
      
          private String password;
      
          @Override
          protected Serializable pkVal() {
              return id;
          }
      }
      

      mapper接口

      package com.example.demo.crud.mapper;
      
      import com.baomidou.mybatisplus.core.mapper.BaseMapper;
      import com.example.demo.crud.entity.User;
      
      /**
       * 路径:com.example.demo.crud.mapper
       * 类名:
       * 功能:《用一句描述一下》
       * 备注:
       * 创建人:typ
       * 创建时间:2018/11/23 14:09
       * 修改人:
       * 修改备注:
       * 修改时间:
       */
      public interface UserMapper extends BaseMapper {
      }
      

      service接口

      package com.example.demo.crud.service;
      
      import com.baomidou.mybatisplus.core.conditions.Wrapper;
      import com.baomidou.mybatisplus.core.metadata.IPage;
      import com.baomidou.mybatisplus.extension.service.IService;
      import com.example.demo.crud.entity.User;
      
      import java.io.Serializable;
      import java.util.Collection;
      import java.util.List;
      import java.util.Map;
      
      /**
       * 路径:com.example.demo.crud.service
       * 类名:
       * 功能:《用一句描述一下》
       * 备注:
       * 创建人:typ
       * 创建时间:2018/11/23 16:21
       * 修改人:
       * 修改备注:
       * 修改时间:
       */
      public interface UserService extends IService {
      }
      

      service实现类

      package com.example.demo.crud.service.impl;
      
      import com.baomidou.mybatisplus.core.conditions.Wrapper;
      import com.baomidou.mybatisplus.core.enums.SqlMethod;
      import com.baomidou.mybatisplus.core.metadata.IPage;
      import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
      import com.example.demo.crud.entity.User;
      import com.example.demo.crud.mapper.UserMapper;
      import com.example.demo.crud.service.UserService;
      import org.apache.ibatis.session.SqlSession;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.stereotype.Service;
      
      import java.io.Serializable;
      import java.util.Collection;
      import java.util.List;
      import java.util.Map;
      
      /**
       * 路径:com.example.demo.crud.service.impl
       * 类名:
       * 功能:《用一句描述一下》
       * 备注:
       * 创建人:typ
       * 创建时间:2018/11/23 16:23
       * 修改人:
       * 修改备注:
       * 修改时间:
       */
      @Service
      public class UserServiceImpl extends ServiceImpl implements UserService {
      }
      

      controller层

      package com.example.demo.crud.controller;
      
      import com.alibaba.druid.support.json.JSONUtils;
      import com.alibaba.fastjson.JSON;
      import com.baomidou.mybatisplus.core.conditions.Wrapper;
      import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
      import com.baomidou.mybatisplus.core.metadata.IPage;
      import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
      import com.example.demo.crud.entity.User;
      import com.example.demo.crud.service.UserService;
      import lombok.extern.slf4j.Slf4j;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.web.bind.annotation.GetMapping;
      import org.springframework.web.bind.annotation.RequestMapping;
      import org.springframework.web.bind.annotation.ResponseBody;
      import org.springframework.web.bind.annotation.RestController;
      
      import java.security.PublicKey;
      
      /**
       * 路径:com.example.demo.crud.controller
       * 类名:
       * 功能:《用一句描述一下》
       * 备注:
       * 创建人:typ
       * 创建时间:2018/11/23 16:29
       * 修改人:
       * 修改备注:
       * 修改时间:
       */
      @Slf4j
      @RestController
      @RequestMapping("/crud")
      public class UserController {
      
          @Autowired
          private UserService userService;
      
          @GetMapping("/getById")
          public String getById(Integer id) {
              log.info("find start id:{}", id);
              User user = userService.getById(id);
              log.info("find end result:{}", JSON.toJSON(user));
              return user.toString();
          }
      
          @ResponseBody
          @GetMapping("/find")
          public IPage find(Integer pageNum, Integer pageSize, String name) {
              log.info("find start pageNum:{},pageSize:{},name:{}", pageNum, pageSize, name);
              QueryWrapper queryWrapper = new QueryWrapper<>();
              queryWrapper.eq("username", name);
      
              IPage page = userService.page(new Page<>(pageNum, pageSize), queryWrapper);
              log.info("find end result:{}", JSON.toJSON(page));
              return page;
          }
      }
      

      5、MyBatisPlus功能测试

      package com.example.demo.crud;
      
      import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
      import com.baomidou.mybatisplus.core.metadata.IPage;
      import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
      import com.example.demo.crud.entity.User;
      import com.example.demo.crud.mapper.UserMapper;
      import org.junit.Test;
      import org.junit.runner.RunWith;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.boot.test.context.SpringBootTest;
      import org.springframework.test.context.junit4.SpringRunner;
      
      import java.util.Arrays;
      import java.util.HashMap;
      import java.util.List;
      import java.util.Map;
      
      /**
       * 路径:com.example.demo.crud
       * 类名:
       * 功能:使用mybatis plus的一般形式
       * 备注:
       * 创建人:typ
       * 创建时间:2018/11/23 14:13
       * 修改人:
       * 修改备注:
       * 修改时间:
       */
      @RunWith(SpringRunner.class)
      @SpringBootTest
      public class UserMapperTest {
      
          @Autowired
          UserMapper userMapper;
      
          /**
           * 方法名:
           * 功能:查询
           * 描述:
           * 创建人:typ
           * 创建时间:2018/11/23 15:07
           * 修改人:
           * 修改描述:
           * 修改时间:
           */
          @Test
          public void  select(){
              //------------------selectById------------------------------
              User user = userMapper.selectById(1);
              System.out.println("user:"+user.toString());
      
              //--------------------selectList--------------------
              List list = userMapper.selectList(null);
              System.out.println("list:"+ Arrays.toString(list.toArray()));
      
              //------------------------selectByMap----------------
              Map map = new HashMap<>();
              map.put("id",1);
              map.put("username","root");
              List maps = userMapper.selectByMap(map);
              System.out.println("map:"+maps.toString());
      
              //------------------selectPage------------------------------------
              User u = new User();
              u.setUsername("AAA");
              QueryWrapper query = new QueryWrapper<>(u);
              IPage pages = userMapper.selectPage(new Page<>(0,10),query);
              System.out.println("pages:"+pages.getRecords());
      
              //---------------------------------selectMapsPage-------------------------------------
              User uMap = new User();
              uMap.setUsername("AAA");
              QueryWrapper queryMap = new QueryWrapper<>(uMap);
              IPage>  mapPages = userMapper.selectMapsPage(new Page<>(0,10),queryMap);
              System.out.println("mapPages:"+mapPages.getRecords());
          }
      
          /**
           * 方法名:
           * 功能:插入操作
           * 描述:
           * 创建人:typ
           * 创建时间:2018/11/23 14:41
           * 修改人:
           * 修改描述:
           * 修改时间:
           */
          @Test
          public void insert(){
              User user = new User();
              user.setUsername("AAA");
              user.setPassword("AAAAA");
              Integer result = userMapper.insert(user);
              // 插入成功放回值
              System.out.println("result:"+result);
          }
      
          /**
           * 方法名:
           * 功能:修改
           * 描述:
           * 创建人:typ
           * 创建时间:2018/11/23 15:01
           * 修改人:
           * 修改描述:
           * 修改时间:
           */
          @Test
          public void update(){
              User user = new User();
              user.setId(8);
              user.setUsername("AAA");
              user.setPassword("AAAAA");
              Integer result = userMapper.updateById(user);
              System.out.println("result:"+result);
          }
      
          /**
           * 方法名:
           * 功能:删除
           * 描述:
           * 创建人:typ
           * 创建时间:2018/11/23 15:05
           * 修改人:
           * 修改描述:
           * 修改时间:
           */
          @Test
          public void del(){
              Integer row = userMapper.deleteById(8);
              System.out.println("result:"+row);
          }
      
      }
      
      ActiveRecord特性测试,实体类需要集成Model,泛型为当前实体类
      package com.example.demo.crud;
      
      import com.baomidou.mybatisplus.core.metadata.IPage;
      import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
      import com.example.demo.crud.entity.User;
      import org.junit.Test;
      import org.junit.runner.RunWith;
      import org.springframework.boot.test.context.SpringBootTest;
      import org.springframework.test.context.junit4.SpringRunner;
      
      /**
       * 路径:com.example.demo.crud
       * 类名:
       * 功能:使用myBatis Plus继承Model的形式
       * 备注:
       * 创建人:typ
       * 创建时间:2018/11/27 10:04
       * 修改人:
       * 修改备注:
       * 修改时间:
       */
      @RunWith(SpringRunner.class)
      @SpringBootTest
      public class UserMapperModelTest {
      
          /**
           * 方法名:
           * 功能:insert
           * 描述:
           * 创建人:typ
           * 创建时间:2018/11/27 10:06
           * 修改人:
           * 修改描述:
           * 修改时间:
           */
          @Test
          public void testInsert(){
              User user = new User();
              //注意查询语法,与普通的有所不一样
              user.setId(12);
              user.setUsername("dhsfg");
              user.setPassword("11111");
              Boolean result = user.insert();
              System.out.println(result);
          }
      
          /**
           * 方法名:
           * 功能:select
           * 描述:
           * 创建人:typ
           * 创建时间:2018/11/27 10:06
           * 修改人:
           * 修改描述:
           * 修改时间:
           */
          @Test
          public void testSelect(){
              User user = new User();
              //注意查询语法,与普通的有所不一样
              user.setId(12);
              User result = user.selectById();
              System.out.println(result);
          }
      
          /**
           * 方法名:
           * 功能:selectPage
           * 描述:
           * 创建人:typ
           * 创建时间:2018/11/27 10:06
           * 修改人:
           * 修改描述:
           * 修改时间:
           */
          @Test
          public void testSelectPage(){
              User user = new User();
              //注意查询语法,与普通的有所不一样
              user.setId(12);
              IPage result = user.selectPage(new Page(1,2),null);
              System.out.println(result.getRecords());
          }
      }
      

      更多内容请阅读MyBatis-Plus官网!

       

      欢迎关注

      Springboot集成MyBatisPlus_第2张图片

       

      你可能感兴趣的:(springcloud,MyBatisPlus)