MyBatis Plus 2.3 个人笔记-04-配置文件与插件使用
2024-09-07 16:15:49
- 接入 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)
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();
} }
最新文章
- 刚知道的android属性
- order_by_、group_by_、having的用法区别
- 台湾辅仁大学的python教程笔记
- Dynamic CRM 2013学习笔记(三十七)自定义审批流7 - 初始化(整套审批流下载、安装)
- setInterval 实时驱动界面改变
- jquery判断自己是父节点的第几个子节点
- redis入门指南学习笔记
- Simple Daemon Shell
- MySQL索引及查询优化总结
- 学JAVA第十七天,接口与抽象方法
- (爬虫)requests库
- 2018-2019-2 网络对抗技术 20165237 Exp6 信息搜集与漏洞扫描
- 本地图片上传与H5适配知识
- python学习之numpy.ewaxis
- Java集合实现
- 去掉点击a标签时产生的虚线框
- springMVC整理01--搭建普通的工程
- linux下vim的安装及其设置细节
- TOP100summit:【分享实录-猫眼电影】业务纵横捭阖背后的技术拆分与融合
- (转)2018几大主流的UI/JS框架——前端框架 [Vue.js(目前市场上的主流)]
热门文章
- C语言中sizeof()的用法
- 用图帮你了解https的原理
- 【C#硬件角度理解代码】函数
- Solving Large-Scale Granular Resource Allocation Problems Efficiently with POP(2021-POP-SOSP-文献阅读笔记)
- Hive数子IP与字符串IP之间的转换
- 【Oracle】PLSQL如何更新、提交数据库中数据
- Weblogic补丁升级常见问题
- Shell编程四剑客包括:find、sed、grep、awk
- 《Symfony 5全面开发》教程04、Symfony处理http请求的流程
- Java:List(二)——List、ArrayList、LinkedList