MyBatisPlus
在
ruoyi
模块中锁定版本,在ruoyi-common
模块中添加mybatisplus
依赖<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <!-- 禁止升级 避免版本冲突 --> <version>3.5.0</version> </dependency>
1
2
3
4
5
6修改
mybatis
配置为mybatis-plus
# MyBatis Plus配置 mybatis-plus: # 搜索指定包别名 typeAliasesPackage: com.ruoyi.**.domain # 配置mapper的扫描,找到所有的mapper.xml映射文件 mapperLocations: classpath*:mapper/**/*Mapper.xml # 加载全局的配置文件 configLocation: classpath:mybatis/mybatis-config.xml global-config: db-config: # 数据库ID自增 id-type: auto
1
2
3
4
5
6
7
8
9
10
11
12删除
ruoyi-framework
模块中MyBatisConfig.java
文件,添加MyBatis Plus
配置package com.ruoyi.framework.config; import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.transaction.annotation.EnableTransactionManagement; /** * Mybatis Plus 配置 * * @author ruoyi */ @EnableTransactionManagement(proxyTargetClass = true) @Configuration public class MybatisPlusConfig { @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); // 分页插件 interceptor.addInnerInterceptor(paginationInnerInterceptor()); // 乐观锁插件 interceptor.addInnerInterceptor(optimisticLockerInnerInterceptor()); // 阻断插件 interceptor.addInnerInterceptor(blockAttackInnerInterceptor()); return interceptor; } /** * 分页插件,自动识别数据库类型 https://baomidou.com/guide/interceptor-pagination.html */ public PaginationInnerInterceptor paginationInnerInterceptor() { PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(); // 设置数据库类型为mysql paginationInnerInterceptor.setDbType(DbType.MYSQL); // 设置最大单页限制数量,默认 500 条,-1 不受限制 paginationInnerInterceptor.setMaxLimit(-1L); return paginationInnerInterceptor; } /** * 乐观锁插件 https://baomidou.com/guide/interceptor-optimistic-locker.html */ public OptimisticLockerInnerInterceptor optimisticLockerInnerInterceptor() { return new OptimisticLockerInnerInterceptor(); } /** * 如果是对全表的删除或更新操作,就会终止该操作 https://baomidou.com/guide/interceptor-block-attack.html */ public BlockAttackInnerInterceptor blockAttackInnerInterceptor() { return new BlockAttackInnerInterceptor(); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# 配置自动填充功能
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
try {
this.strictInsertFill(metaObject, "createBy", String.class, SecurityUtils.getUsername());
} catch (ServiceException e) {
log.info(e.getMessage());
}
this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
}
@Override
public void updateFill(MetaObject metaObject) {
try {
this.strictUpdateFill(metaObject, "updateBy", String.class, SecurityUtils.getUsername());
} catch (ServiceException e) {
log.info(e.getMessage());
}
this.strictUpdateFill(metaObject, "updateTime", Date.class, new Date());
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/** 搜索值 */
@TableField(exist = false)
private String searchValue;
/** 创建者 */
@TableField(exist = false, fill = FieldFill.INSERT)
private String createBy;
/** 创建时间 */
@TableField(exist = false, fill = FieldFill.INSERT)
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date createTime;
/** 更新者 */
@TableField(exist = false, fill = FieldFill.UPDATE)
private String updateBy;
/** 更新时间 */
@TableField(exist = false, fill = FieldFill.UPDATE)
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date updateTime;
/** 备注 */
@TableField(exist = false)
private String remark;
/** 请求参数 */
@TableField(exist = false)
private Map<String, Object> params;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 修改代码生成模板
mapper.java.vm
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
1extends BaseMapper<${ClassName}>
1service.java.vm
import com.baomidou.mybatisplus.extension.service.IService;
1extends IService<${ClassName}>
1serviceImpl.java.vm
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
1extends ServiceImpl<${ClassName}Mapper, ${ClassName}>
1
编辑 (opens new window)
上次更新: 2025-01-11 19:08:58