- 接入 springboot application.yml配置
1.mapper 扫描
mybatis-plus: # 如果是放在src/main/java目录下 classpath:/com/yourpackage/*/mapper/*Mapper.xml # 如果是放在resource目录 classpath:/mapper/*Mapper.xml mapper-locations: classpath:/com/huarui/mybatisplus/mapper/*Mapper.xml #实体扫描,多个package用逗号或者分号分隔 typeAliasesPackage: com.huarui.mybatisplus.entity
@SpringBootApplication
@MapperScan("com.huarui.mybatisplus.mapper")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
2.自定义公共字段填充处理器
当我们新增或修改时需要给某个字段 赋值默认值
@TableName("tbl_user") public class User extends Model<User> { private static final long serialVersionUID = 1L; @TableId(value = "id", type = IdType.ID_WORKER) private Long id; /**
* * 新增 修改时 字段自动填充 */ @TableField(fill = FieldFill.INSERT_UPDATE) private String name; }
package com.huarui.mybatisplus.configuration; import com.baomidou.mybatisplus.mapper.MetaObjectHandler; import org.apache.ibatis.reflection.MetaObject; /** * Created by lihui on 2019/2/17. * 自定义公共字段填充处理器 */ public class MyMetaObjectHandler extends MetaObjectHandler { /** * 插入操作 自动填充 */ @Override public void insertFill(MetaObject metaObject) { //获取到需要被填充的字段的值 Object fieldValue = getFieldValByName("name", metaObject); if(fieldValue == null) { System.out.println("*******插入操作 满足填充条件*********"); setFieldValByName("name", "youxiu326", metaObject); } } /** * 修改操作 自动填充 */ @Override public void updateFill(MetaObject metaObject) { Object fieldValue = getFieldValByName("name", metaObject); if(fieldValue == null) { System.out.println("*******修改操作 满足填充条件*********"); setFieldValByName("name", "youxiu326", metaObject); } } }
mybatis-plus:
# 如果是放在src/main/java目录下 classpath:/com/yourpackage/*/mapper/*Mapper.xml
# 如果是放在resource目录 classpath:/mapper/*Mapper.xml
mapper-locations: classpath:/com/huarui/mybatisplus/mapper/*Mapper.xml
#实体扫描,多个package用逗号或者分号分隔
typeAliasesPackage: com.huarui.mybatisplus.entity
global-config:
#主键类型 0:"数据库ID自增", 1:"用户输入ID",2:"全局唯一ID (数字类型唯一ID)", 3:"全局唯一ID UUID";
id-type: 2
#自定义填充策略接口实现
meta-object-handler: com.huarui.mybatisplus.configuration.MyMetaObjectHandler
3.逻辑删除
@TableName("tbl_user")
public class User extends Model<User> {
private static final long serialVersionUID = 1L;
@TableId(value = "id", type = IdType.ID_WORKER)
private Long id;
@TableField("deleteFlag")
@TableLogic //逻辑删除标志
private Integer deleteFlag;
}
mybatis-plus:
# 如果是放在src/main/java目录下 classpath:/com/yourpackage/*/mapper/*Mapper.xml
# 如果是放在resource目录 classpath:/mapper/*Mapper.xml
mapper-locations: classpath:/com/huarui/mybatisplus/mapper/*Mapper.xml
#实体扫描,多个package用逗号或者分号分隔
typeAliasesPackage: com.huarui.mybatisplus.entity
global-config:
#逻辑删除配置(下面3个配置)
logic-delete-value: -1 #删除状态
logic-not-delete-value: 1 #未删除状态
sql-injector: com.baomidou.mybatisplus.mapper.LogicSqlInjector
4.分页插件 与 乐观锁插件使用
@EnableTransactionManagement @Configuration @MapperScan("com.huarui.mybatisplus.mapper.*") public class MybatisPlusConfig { /** * 分页插件 */ @Bean public PaginationInterceptor paginationInterceptor() { return new PaginationInterceptor(); } /** * 乐观锁插件 */ @Bean public OptimisticLockerInterceptor optimisticLockerInterceptor() { return new OptimisticLockerInterceptor(); } }
分页插件配置即可使用
乐观锁实现方式:
- 取出记录时,获取当前version
- 更新时,带上这个version
- 执行更新时, set version = yourVersion+1 where version = yourVersion
- 如果version不对,就更新失败
@TableName("tbl_employee")
public class Employee extends Model<Employee> {
private static final long serialVersionUID = 1L;
/*
* @TableId:
* value: 指定表中的主键列的列名, 如果实体属性名与列名一致,可以省略不指定.
* type: 指定主键策略. ID_WORKER 全局唯一ID,内容为空自动填充(默认配置)
*/
@TableId(value = "id", type = IdType.ID_WORKER)
private Long id;
/**
* 声明该属性不是数据库中字段
*/
@TableField(exist = false)
private String notExist;
/**
* 乐观锁版本号
* 仅支持int,Integer,long,Long,Date,Timestamp
*/
@Version
private Integer version;
}
5.性能分析插件
spring: profiles: #spring boot application.properties文件中引用maven profile节点的值 active: dev #指定dev环境
package com.huarui.mybatisplus.configuration; import com.baomidou.mybatisplus.plugins.OptimisticLockerInterceptor; import com.baomidou.mybatisplus.plugins.PaginationInterceptor; import com.baomidou.mybatisplus.plugins.PerformanceInterceptor; import com.baomidou.mybatisplus.plugins.SqlExplainInterceptor; import org.mybatis.spring.annotation.MapperScan; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; import org.springframework.transaction.annotation.EnableTransactionManagement; /** * Created by lihui on 2019/2/17. */ @EnableTransactionManagement @Configuration @MapperScan("com.huarui.mybatisplus.mapper.*") public class MybatisPlusConfig { /** * 分页插件 */ @Bean public PaginationInterceptor paginationInterceptor() { return new PaginationInterceptor(); } /** * 性能分析插件 */ @Bean @Profile({"dev","test"})// 设置 dev test 环境开启 public PerformanceInterceptor performanceInterceptor () { PerformanceInterceptor p = new PerformanceInterceptor (); p.setFormat(true); p.setMaxTime(200);
//参数:maxTime SQL 执行最大时长,超过自动停止运行,有助于发现问题。
//参数:format SQL SQL是否格式化,默认false。
return p; } /** * 乐观锁插件 */ @Bean public OptimisticLockerInterceptor optimisticLockerInterceptor() { return new OptimisticLockerInterceptor(); } }