Mybaits 自定义TypeHandler 处理枚举

1、定义枚举基类

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、基类子类实现

  

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处理类

  

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)

    

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

  

  4.2、springBoot 注册方式2(推荐

  

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

  

posted on 2020-12-02 12:44  滚动的蛋  阅读(368)  评论(0编辑  收藏  举报

导航