Skip to content

Commit bb8d5ba

Browse files
committed
feat: 完善 plugin mybatis
1 parent 208f4c1 commit bb8d5ba

20 files changed

Lines changed: 147 additions & 95 deletions

File tree

application/src/main/java/top/cadecode/uniboot/controller/system/SysLogController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
import org.springframework.web.bind.annotation.RequestMapping;
1313
import org.springframework.web.bind.annotation.RestController;
1414
import top.cadecode.uniboot.common.core.annotation.ApiFormat;
15-
import top.cadecode.uniboot.common.core.mybatis.handler.BoolToIntTypeHandler;
1615
import top.cadecode.uniboot.common.core.response.PageResult;
16+
import top.cadecode.uniboot.common.plugin.mybatis.converter.BoolToIntTypeHandler;
1717
import top.cadecode.uniboot.framework.bean.po.SysLog;
1818
import top.cadecode.uniboot.framework.bean.vo.SysLogVo.SysLogPageVo;
1919
import top.cadecode.uniboot.framework.convert.SysLogConvert;

application/src/main/resources/application.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ pagehelper:
8080
# mybatis plus 配置
8181
mybatis-plus:
8282
mapper-locations: classpath*:mapper/**/*.xml
83-
type-aliases-package: top.cadecode.**.bean,top.cadecode.**.mybatis.handler
83+
type-aliases-package: top.cadecode.**.bean,top.cadecode.**.mybatis.converter
8484
type-enums-package: top.cadecode.**.enums
8585
configuration:
8686
map-underscore-to-camel-case: true

common/core/pom.xml

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,21 +53,6 @@
5353
<groupId>org.apache.commons</groupId>
5454
<artifactId>commons-pool2</artifactId>
5555
</dependency>
56-
<!--mybatis-->
57-
<dependency>
58-
<groupId>org.mybatis.spring.boot</groupId>
59-
<artifactId>mybatis-spring-boot-starter</artifactId>
60-
</dependency>
61-
<!--pagehelper-->
62-
<dependency>
63-
<groupId>com.github.pagehelper</groupId>
64-
<artifactId>pagehelper-spring-boot-starter</artifactId>
65-
</dependency>
66-
<!--mybatis plus-->
67-
<dependency>
68-
<groupId>com.baomidou</groupId>
69-
<artifactId>mybatis-plus-boot-starter</artifactId>
70-
</dependency>
7156
<!--spring retry-->
7257
<dependency>
7358
<groupId>org.springframework.retry</groupId>

common/core/src/main/java/top/cadecode/uniboot/common/core/convertor/EnumConvertor.java renamed to common/core/src/main/java/top/cadecode/uniboot/common/core/convertor/ParamEnumConvertor.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,11 @@
66
* @author Cade Li
77
* @date 2022/5/28
88
*/
9-
public interface EnumConvertor {
9+
public interface ParamEnumConvertor {
1010

1111
/**
1212
* 返回枚举元素的对应标记
1313
*/
1414
String convertBy();
1515

16-
/**
17-
* 返回用于数据库持久化的字段内容
18-
* 数据库使用数值类型存储,如 tinyint unsigned 0~255
19-
*/
20-
Integer persistBy();
21-
2216
}

common/core/src/main/java/top/cadecode/uniboot/common/core/convertor/EnumConvertorFactory.java renamed to common/core/src/main/java/top/cadecode/uniboot/common/core/convertor/ParamEnumConvertorFactory.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616
* @author Cade Li
1717
* @date 2022/6/14
1818
*/
19-
public class EnumConvertorFactory implements ConverterFactory<String, EnumConvertor> {
19+
public class ParamEnumConvertorFactory implements ConverterFactory<String, ParamEnumConvertor> {
2020

2121
private static final Map<Class<?>, Converter<String, ?>> CONVERTER_MAP = new ConcurrentHashMap<>();
2222

2323
@SuppressWarnings("unchecked")
2424
@Override
25-
public <T extends EnumConvertor> Converter<String, T> getConverter(Class<T> targetType) {
25+
public <T extends ParamEnumConvertor> Converter<String, T> getConverter(Class<T> targetType) {
2626
Converter<String, ?> stringConverter = CONVERTER_MAP.get(targetType);
2727
if (Objects.isNull(stringConverter)) {
2828
stringConverter = new StringEnumConvertor<>(targetType);
@@ -34,13 +34,13 @@ public <T extends EnumConvertor> Converter<String, T> getConverter(Class<T> targ
3434
/**
3535
* 字符串转枚举转换器
3636
*/
37-
public static class StringEnumConvertor<T extends EnumConvertor> implements Converter<String, T> {
37+
public static class StringEnumConvertor<T extends ParamEnumConvertor> implements Converter<String, T> {
3838

3939
private final Map<String, T> enumMap;
4040

4141
public StringEnumConvertor(Class<T> targetType) {
4242
enumMap = Arrays.stream(targetType.getEnumConstants())
43-
.collect(toMap(EnumConvertor::convertBy, o -> o, (p, n) -> n));
43+
.collect(toMap(ParamEnumConvertor::convertBy, o -> o, (p, n) -> n));
4444
}
4545

4646
@Override

common/plugin/log/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,9 @@
1616
<groupId>top.cadecode</groupId>
1717
<artifactId>uni-boot-common-core</artifactId>
1818
</dependency>
19+
<dependency>
20+
<groupId>top.cadecode</groupId>
21+
<artifactId>uni-boot-common-plugin-mybatis</artifactId>
22+
</dependency>
1923
</dependencies>
2024
</project>

common/plugin/mybatis/pom.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,25 @@
1111

1212
<artifactId>uni-boot-common-plugin-mybatis</artifactId>
1313

14+
<dependencies>
15+
<dependency>
16+
<groupId>top.cadecode</groupId>
17+
<artifactId>uni-boot-common-core</artifactId>
18+
</dependency>
19+
<!--mybatis-->
20+
<dependency>
21+
<groupId>org.mybatis.spring.boot</groupId>
22+
<artifactId>mybatis-spring-boot-starter</artifactId>
23+
</dependency>
24+
<!--mybatis plus-->
25+
<dependency>
26+
<groupId>com.baomidou</groupId>
27+
<artifactId>mybatis-plus-boot-starter</artifactId>
28+
</dependency>
29+
<!--pagehelper-->
30+
<dependency>
31+
<groupId>com.github.pagehelper</groupId>
32+
<artifactId>pagehelper-spring-boot-starter</artifactId>
33+
</dependency>
34+
</dependencies>
1435
</project>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package top.cadecode.uniboot.common.plugin.mybatis.config;
2+
3+
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
4+
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.context.annotation.Configuration;
7+
8+
/**
9+
* mybatis 配置类
10+
*
11+
* @author Cade Li
12+
* @date 2022/2/16
13+
*/
14+
@Configuration
15+
public class MybatisConfig {
16+
17+
/**
18+
* Mybatis Plus 插件配置
19+
*/
20+
@Bean
21+
public MybatisPlusInterceptor mybatisPlusInterceptor() {
22+
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
23+
// 添加分页插件,动态获取数据库类型
24+
interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
25+
return interceptor;
26+
}
27+
}

common/core/src/main/java/top/cadecode/uniboot/common/core/mybatis/handler/BoolToIntTypeHandler.java renamed to common/plugin/mybatis/src/main/java/top/cadecode/uniboot/common/plugin/mybatis/converter/BoolToIntTypeHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package top.cadecode.uniboot.common.core.mybatis.handler;
1+
package top.cadecode.uniboot.common.plugin.mybatis.converter;
22

33
import cn.hutool.core.util.ObjectUtil;
44
import org.apache.ibatis.type.BaseTypeHandler;

common/core/src/main/java/top/cadecode/uniboot/common/core/mybatis/DefaultEnumTypeHandler.java renamed to common/plugin/mybatis/src/main/java/top/cadecode/uniboot/common/plugin/mybatis/converter/DefaultEnumTypeHandler.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
package top.cadecode.uniboot.common.core.mybatis;
1+
package top.cadecode.uniboot.common.plugin.mybatis.converter;
22

33
import org.apache.ibatis.type.BaseTypeHandler;
44
import org.apache.ibatis.type.JdbcType;
5-
import top.cadecode.uniboot.common.core.convertor.EnumConvertor;
65

76
import java.sql.CallableStatement;
87
import java.sql.PreparedStatement;
@@ -16,7 +15,8 @@
1615

1716
/**
1817
* MyBatis 枚举类解析器,需要配置成默认枚举解析器使用,
19-
* 枚举类实现 EnumConvertor 接口可自定义持久化属性,没有实现 EnumConvertor 的普通枚举将使用 ordinal 进行持久化
18+
* 枚举类实现 EnumDbConvertor 接口可自定义持久化属性,没有实现 EnumDbConvertor 的普通枚举将使用 ordinal 进行持久化
19+
* 使用 mybatis.configuration.default-enum-type-handler 来配置
2020
*
2121
* @author Cade Li
2222
* @date 2022/6/16
@@ -37,9 +37,9 @@ public DefaultEnumTypeHandler(Class<E> type) {
3737
throw new IllegalArgumentException(type.getSimpleName() + " is not an enum type");
3838
}
3939
enumMap = Arrays.stream(enums).collect(toMap(o -> {
40-
// 实现 EnumConvertor 的枚举根据 persistBy 持久化
41-
if (o instanceof EnumConvertor) {
42-
return ((EnumConvertor) o).persistBy();
40+
// 实现 EnumDbConvertor 的枚举根据 persistBy 持久化
41+
if (o instanceof EnumDbConvertor) {
42+
return ((EnumDbConvertor) o).persistBy();
4343
}
4444
return o.ordinal();
4545
}, o -> o, (p, n) -> p));
@@ -100,8 +100,8 @@ private E enumFromValue(Integer value) {
100100
* 从 enum 元素中获取 value
101101
*/
102102
private Integer valueFromEnum(E e) {
103-
if (e instanceof EnumConvertor) {
104-
return ((EnumConvertor) e).persistBy();
103+
if (e instanceof EnumDbConvertor) {
104+
return ((EnumDbConvertor) e).persistBy();
105105
}
106106
return e.ordinal();
107107
}

0 commit comments

Comments
 (0)