MyBatis-Plus
函数式编程
Lambda
Lambda 是 JDK8 中的一个语法糖,它课可以对某些匿名内部类的写法进行简化。
任何接口只包含唯一一个抽象方法,那么它就是一个函数式接口,则可以使用 Lambda 表达式代替。
| 12
 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/
依赖
| 12
 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")
 | 
| 12
 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
 
 | 
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 
 | @Componentpublic 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);
 }
 
 }
 
 | 
| 12
 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")
 | 
字段
| 12
 3
 4
 5
 
 | @TableField(value = "name",
 exist = true, // 该字段表中是否存在
 fill = "INSERT_UPDATE" // 插入和更新时填充字段(CURRENT_TIMESTAMP)
 )
 
 | 
乐观锁
枚举类
逻辑处理
| 1
 | @TableLogic(value="1", delval="0")
 | 
默认排序
Service 接口
save 插入
| 12
 3
 4
 
 | boolean saveboolean saveBatch
 boolean saveOrUpdate
 boolean saveOrUpdateBatch
 
 | 
remove 删除
| 12
 3
 4
 
 | boolean removeboolean removeById
 boolean removeByMap
 boolean removeByIds
 
 | 
update 更新
| 12
 3
 
 | boolean updateboolean updateById
 boolean updateBatchById
 
 | 
get 查询
| 12
 3
 4
 
 | T getByIdT getOne
 Map<String, Object> getMap
 <V> V getObj
 
 | 
list 查询
| 12
 3
 4
 5
 
 | List<T> listCollection<T> listByIds
 Collection<T> listByMap
 List<Map<String, Object>> listMaps
 List<Object> listObjs
 
 | 
page 分页
| 12
 
 | IPage<T> pageIPage<Map<String, Object>> pageMaps
 
 | 
查询记录数
Chain 链式操作
| 12
 3
 4
 5
 6
 7
 8
 9
 
 | QueryChainWrapper<T> query();
 
 LambdaQueryChainWrapper<T> lambdaQuery();
 
 
 UpdateChainWrapper<T> update();
 
 LambdaUpdateChainWrapper<T> lambdaUpdate();
 
 | 
Mapper 接口
增删改
| 12
 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
 
 | 
查询
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 
 | T selectByIdT 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) 的父类
条件语句
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 
 | allEqeq
 ne
 gt
 ge
 lt
 le
 between
 notBetween
 
 or
 and
 
 isNull
 isNotNull
 in
 notIn
 inSql
 notInSql
 
 | 
模糊查询
| 12
 3
 4
 5
 6
 
 | like notLike
 likeLeft
 notLikeLeft
 likeRight
 notLikeRight
 
 | 
集合处理
| 12
 3
 4
 5
 
 | groupByorderByAsc
 orderByDesc
 orderBy
 having
 
 | 
sql 处理
| 12
 3
 4
 5
 6
 
 | funcnested
 apply
 last
 exists
 notExists
 
 |