介绍
mp是mybatis-plus的增强工具,提高开发效率,不用在写各种dao层设施,自动生成
pom依赖
<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.4.1</version></dependency>
配置properties/yml
mybatis-plus.mapper-locations=classpath:mapper/*Mapper.xmlmybatis-plus.configuration.map-underscore-to-camel-case=truemybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
mapper、service配置
通用mappermapper 继承 baseMapper<T>通用serviceservice 继承 Iservice<T> - @Service注解serviceImpl 继承ServiceImpl<T,T> 实现 Service
启动类
启动类加入MapperScan 或 接入加入@mapper注解
mp的注解
- @TableId:专门给主键使用进行映射
- type:生成主键策略
IdType.ASSIGN_ID默认值,雪花idIdType.AUTO自增,需要数据表设置自增字段IdType.INPUT手动输入IdType.ASSIGN_IDid为空时,自动填充雪花id,主键类型number或stringIdType.ASSIGN_UUID主键为空时,自动填充UUID,主键必须是String
- type:生成主键策略
- @TableName:关联数据库表名
- value:默认字段名,实体对于表名
- @TableField:关联数据库字段名
- value:默认字段名,实体对于表名
- 当
mapUnderscoreToCamelCase为true时:数据库字段值.replace("_","").toUpperCase() == 实体属性名.toUpperCase()
- 当
mapUnderscoreToCamelCase为false时:数据库字段值.toUpperCase() == 实体属性名.toUpperCase()
- 当
- exist:默认true,是否为数据库字段
- select:默认true,是否进行select查询
- fill:字段自动填充策略
- DELETE:默认不填充
- INSERT:表示在插入操作时赋值(create_time)
- UPDATE:在更新操作时,更新值
- INSERT_UPDATE:插入和更新操作都会更新字段(update_time)
- value:默认字段名,实体对于表名
- @TableLogic:将删除操作变成修改操作
mp的插件
```java 在configuration注解中安装乐观锁插件 @Configuration public class MybatisConfig { @Bean public MybatisPlusInterceptor a(){
} } 分页插件 IPageMybatisPlusInterceptor Interceptor = new MybatisPlusInterceptor();Interceptor.addInnerInterceptor(new PaginationInnerInterceptor());// 安装分页插件Interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());// 安装乐观锁插件return Interceptor ;
userIPage = new Page (){{ setSize(2); // 获取两个数据 setCurrent(2); // 从第二个开始 }}; IPage userIPageResult = userMapper.selectPage(userIPage, null); // 使用分页插件,获取分页对象 List records = userIPageResult.getRecords(); // 获取数据
乐观锁插件 //在某字段中设置乐观锁注解 @Version // 使用乐观锁插件 private Integer version; ```
Wrapper
wrapper是mybatis-plus的条件拼装查询器
构造器关系

Wrapper : 条件构造抽象类,最顶端父类
AbstractWrapper 查询条件封装,⽤于⽣成 sql 中的 where 语句。

