Mybaits 自定义TypeHandler 处理枚举

1、定义枚举基类

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
package com.ne.scrm.content.constant;
 
 
import org.springframework.util.ReflectionUtils;
 
import java.lang.reflect.Field;
 
public interface BaseDBEnum {
     String DEFAULT_VALUE_NAME = "value";
     String DEFAULT_DESC_NAME = "desc";
 
     default Integer getValue() {
          Field field = ReflectionUtils.findField(this.getClass(), DEFAULT_VALUE_NAME);
          if (field == null) {
               return null;
          }
          try {
               field.setAccessible(true);
               return Integer.parseInt(field.get(this).toString());
          } catch (IllegalAccessException e) {
               throw new RuntimeException(e);
          }
     }
 
     default String getDesc() {
          Field field = ReflectionUtils.findField(this.getClass(), DEFAULT_DESC_NAME);
          if (field == null) {
               return null;
          }
          try {
               field.setAccessible(true);
               return field.get(this).toString();
          } catch (IllegalAccessException e) {
               throw new RuntimeException(e);
          }
     }
}

  

2、基类子类实现

  

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
package com.ne.scrm.content.enums;
 
import com.fasterxml.jackson.annotation.JsonValue;
import com.ne.scrm.content.constant.BaseDBEnum;
import lombok.Getter;
 
/**
 * @author Yungui.Zheng
 * @date 2020/12/01
 */
@Getter
public enum PublishTypeDBEnum implements BaseDBEnum {
    /**
     * 0-立即发布、1-定时发布
     */
 
    INSTANT_PUB(0, "立即发布"),
    TIMING_PUB(1, "定时发布");
 
    Integer value;
    @JsonValue
    String desc;
 
    PublishTypeDBEnum(Integer value, String desc) {
        this.value = value;
        this.desc = desc;
    }
 
}

  

3、自定义TypeHandler处理类

  

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
63
64
65
66
package com.ne.scrm.content.persist.mybatis.handler;
 
import com.ne.scrm.content.constant.BaseDBEnum;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
 
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
 
/**
 * 通用枚举转化handler
 * 用于转化BaseDBEnum子类
 *
 * @author : Yungui.zheng
 * @date : 20201202
 **/
@MappedJdbcTypes(value = JdbcType.TINYINT, includeNullJdbcType = true)
public class DefaultEnumTypeHandler extends BaseTypeHandler<BaseDBEnum> {
 
    private Class<BaseDBEnum> type;
 
    public DefaultEnumTypeHandler() {
        System.out.println("init DefaultEnumTypeHandler no args");
    }
 
    public DefaultEnumTypeHandler(Class<BaseDBEnum> type) {
        System.out.println("init DefaultEnumTypeHandler with args");
        if (type == null) {
            throw new IllegalArgumentException("Type argument cannot be null");
        }
        this.type = type;
    }
 
    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, BaseDBEnum parameter, JdbcType jdbcType) throws SQLException {
        ps.setInt(i, parameter.getValue());
    }
 
    @Override
    public BaseDBEnum getNullableResult(ResultSet rs, String columnName) throws SQLException {
        return convert(rs.getInt(columnName));
    }
 
    @Override
    public BaseDBEnum getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        return convert(rs.getInt(columnIndex));
    }
 
    @Override
    public BaseDBEnum getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        return convert(cs.getInt(columnIndex));
    }
 
    private BaseDBEnum convert(int status) {
        BaseDBEnum[] objs = type.getEnumConstants();
        for (BaseDBEnum em : objs) {
            if (em.getValue() == status) {
                return em;
            }
        }
        return null;
    }
}

  

4、注册Typehandler

  4.1、springBoot注册方式1(configuration)

    

1
2
3
mybatis:
  configuration:
    default-enum-type-handler: com.ne.scrm.content.persist.mybatis.handler.DefaultEnumTypeHandler

  

  4.2、springBoot 注册方式2(推荐

  

1
2
mybatis:
  type-handlers-package: com.ne.scrm.content.persist.mybatis.handler

  

posted on   滚动的蛋  阅读(370)  评论(0编辑  收藏  举报

编辑推荐:
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
阅读排行:
· 【.NET】调用本地 Deepseek 模型
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
历史上的今天:
2019-12-02 [jenkins] 启动错误 Failed to start LSB: Jenkins Automation Server.

导航

< 2025年2月 >
26 27 28 29 30 31 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 1
2 3 4 5 6 7 8
点击右上角即可分享
微信分享提示