MyBatisPlus2.3个人笔记-04-配置文件与插件使用
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
MyBatisPlus2.3个⼈笔记-04-配置⽂件与插件使⽤接⼊ 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);
}
}
}
MyMetaObjectHandler.java
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)
/**
* 声明该属性不是数据库中字段
*/
@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();
}
}。