MyBatis-Plus
函数式编程
Lambda
Lambda 是 JDK8 中的一个语法糖,它课可以对某些匿名内部类的写法进行简化。
任何接口只包含唯一一个抽象方法,那么它就是一个函数式接口,则可以使用 Lambda 表达式代替。
1 2 3 4 5 6 7 8 9 10
   |  new Thread(new Runnable() {     @Override     public void run() {         System.out.println("new Runnable");     } }).start();
 
  new Thread(()-> System.out.println("Lambda")).start();
 
  | 
 
Stream 流
Optional
MyBatis-Plus
使用方法
官网
MyBatis-Plus官网:https://www.baomidou.com/
依赖
1 2 3 4 5 6
   |  <dependency>     <groupId>com.baomidou</groupId>     <artifactId>mybatis-plus-boot-starter</artifactId>     <version>3.5.3.1</version> </dependency>
 
  | 
 
配置
1
   | @MapperScan("top/ruoxijun/mapper")
  | 
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
   | spring:   datasource:     type: com.alibaba.druid.pool.DruidDataSource     driver-class-name: com.mysql.cj.jdbc.Driver     url: jdbc:mysql://localhost:3306/db_user?serverTimezone=GMT%2B8&characterEncoding=utf8&useSSL=false&useUnicode=true     username: root     password: 88888888
  mybatis-plus:   global-config:     db-config:       id-type: auto        logic-delete-field: deleted        logic-delete-value: 1        logic-not-delete-value: 0    configuration:     map-underscore-to-camel-case: true      auto-mapping-behavior: full      log-impl: org.apache.ibatis.logging.stdout.StdOutImpl    mapper-locations: classpath*:/mapper/**/*.xml
   | 
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
   | @Component public class MyMetaObjectHandler implements MetaObjectHandler {
      @Override     public void insertFill(MetaObject metaObject) {         this.strictInsertFill(metaObject, "createTime", () -> LocalDateTime.now(), LocalDateTime.class);     }
      @Override     public void updateFill(MetaObject metaObject) {         this.strictInsertFill(metaObject, "createTime", () -> LocalDateTime.now(), LocalDateTime.class);         this.strictUpdateFill(metaObject, "updateTime", () -> LocalDateTime.now(), LocalDateTime.class);     }
  }
   | 
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
   | @Configuration @MapperScan("scan.your.mapper.package") public class MybatisPlusConfig {
      @Bean     public MybatisPlusInterceptor mybatisPlusInterceptor() {         MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();                  interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));                  interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());         return interceptor;     }
      
 
 
 
      @Bean     public ConfigurationCustomizer configurationCustomizer() {         return configuration -> configuration.setUseDeprecatedExecutor(false);     }
  }
   | 
 
常用注解
表名
1
   | @TableName(value="sys_user", resultMap="user")
   | 
 
主键
1
   | @TableId(value="id", type="IdType.AUTO")
   | 
 
字段
1 2 3 4 5
   | @TableField(   value = "name",    exist = true, // 该字段表中是否存在   fill = "INSERT_UPDATE" // 插入和更新时填充字段(CURRENT_TIMESTAMP) )
   | 
 
乐观锁
枚举类
逻辑处理
1
   | @TableLogic(value="1", delval="0")
   | 
 
默认排序
Service 接口
save 插入
1 2 3 4
   | boolean save boolean saveBatch boolean saveOrUpdate boolean saveOrUpdateBatch
   | 
 
remove 删除
1 2 3 4
   | boolean remove boolean removeById boolean removeByMap boolean removeByIds
   | 
 
update 更新
1 2 3
   | boolean update boolean updateById boolean updateBatchById
   | 
 
get 查询
1 2 3 4
   | T getById T getOne Map<String, Object> getMap <V> V getObj
   | 
 
list 查询
1 2 3 4 5
   | List<T> list Collection<T> listByIds Collection<T> listByMap List<Map<String, Object>> listMaps List<Object> listObjs
   | 
 
page 分页
1 2
   | IPage<T> page IPage<Map<String, Object>> pageMaps
   | 
 
查询记录数
Chain 链式操作
1 2 3 4 5 6 7 8 9
   |  QueryChainWrapper<T> query();
  LambdaQueryChainWrapper<T> lambdaQuery();
 
  UpdateChainWrapper<T> update();
  LambdaUpdateChainWrapper<T> lambdaUpdate();
 
  | 
 
Mapper 接口
增删改
1 2 3 4 5 6 7 8 9 10 11
   | int insert(T entity); 
 
  int delete int deleteBatchIds int deleteById int deleteByMap
 
  int update int updateById
   | 
 
查询
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
   | T selectById T selectOne List<T> selectBatchIds List<T> selectList List<T> selectByMap List<Map<String, Object>> selectMaps
 
  List<Object> selectObjs
 
  IPage<T> selectPage IPage<Map<String, Object>> selectMapsPage
  Integer selectCount
   | 
 
条件构造器
AbstractWrapper
QueryWrapper(LambdaQueryWrapper) 和 UpdateWrapper(LambdaUpdateWrapper) 的父类
条件语句
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
   | allEq eq ne  gt  ge  lt  le  between notBetween
  or and
  isNull isNotNull in notIn inSql notInSql
   | 
 
模糊查询
1 2 3 4 5 6
   | like  notLike likeLeft notLikeLeft likeRight notLikeRight
   | 
 
集合处理
1 2 3 4 5
   | groupBy orderByAsc orderByDesc orderBy having
   | 
 
sql 处理
1 2 3 4 5 6
   | func nested apply last exists notExists
   |