天天看点

如何在Mybatis中优雅的使用枚举如何在Mybatis中优雅的使用枚举如何在MyBatis中优雅的使用枚举

如何在Mybatis中优雅的使用枚举

问题

在编码过程中,经常会遇到用某个数值来表示某种状态、类型或者阶段的情况,比如有这样一个枚举:

public class QuestionnaireEnum {

    /**
     * 学历
     */
    public enum Education{
        Education_Junior(0, "初中"),
        Education_Senior(1, "高中"),
        Education_College(2, "本科/大专"),
        Education_Master(3, "硕士"),
        Education_Doctor(4, "博士"),
        Education_Other(5, "其他");
        private Integer value;
        private String label;

        /*省略get和set代码*/
    }

    /**
     * 目前的职业
     */
    public enum Job{
        Job_Teacher(0, "教师"),
        Job_Medic(1, "医护"),
        Job_Civil(2, "公务员"),
        Job_Worker(3, "务工"),
        Job_Soldier(4, "军人"),
        Job_Other(5, "其他");
        /*省略get和set代码*/
}
           

通常我们希望将表示状态的数值存入数据库,即

ComputerState.OPEN

存入数据库取值为

10

探索

首先,我们先看看MyBatis是否能够满足我们的需求。

MyBatis内置了两个枚举转换器分别是:

org.apache.ibatis.type.EnumTypeHandler

org.apache.ibatis.type.EnumOrdinalTypeHandler

EnumTypeHandler

这是默认的枚举转换器,该转换器将枚举实例转换为实例名称的字符串,即将

ComputerState.OPEN

转换

OPEN

EnumOrdinalTypeHandler

顾名思义这个转换器将枚举实例的ordinal属性作为取值,即

ComputerState.OPEN

转换为

,

ComputerState.CLOSE

转换为

1

使用它的方式是在MyBatis配置文件中定义:

<typeHandlers>
    <typeHandler handler="org.apache.ibatis.type.EnumOrdinalTypeHandler" javaType="com.example.entity.enums.ComputerState"/>
</typeHandlers>
           

以上的两种转换器都不能满足我们的需求,所以看起来要自己编写一个转换器了。

方案

MyBatis提供了

org.apache.ibatis.type.BaseTypeHandler

类用于我们自己扩展类型转换器,上面的

EnumTypeHandler

EnumOrdinalTypeHandler

也都实现了这个接口。

1. 定义接口

我们需要一个接口来确定某部分枚举类的行为。如下:

public interface IBaseEnum{
    Integer getValue();
    String getLabel();
}
           

该接口只有一个返回编码的方法,返回值将被存入数据库。

2. 改造枚举

就拿上面的

ComputerState

来实现

BaseCodeEnum

接口:

public class QuestionnaireEnum {

    /**
     * 学历
     */
    public enum Education implements IBaseEnum {
        Education_Junior(0, "初中"),
        Education_Senior(1, "高中"),
        Education_College(2, "本科/大专"),
        Education_Master(3, "硕士"),
        Education_Doctor(4, "博士"),
        Education_Other(5, "其他");
        /*省略get和set代码*/
    }

    /**
     * 目前的职业
     */
    public enum Job implements IBaseEnum {
        Job_Teacher(0, "教师"),
        Job_Medic(1, "医护"),
        Job_Civil(2, "公务员"),
        Job_Worker(3, "务工"),
        Job_Soldier(4, "军人"),
        Job_Other(5, "其他");
        /*省略get和set代码*/
}
           

3. 编写一个转换工具类

现在我们能顺利的将枚举转换为某个数值了,还需要一个工具将数值转换为枚举实例。

public class QuestEnumUtil {
    public static <E extends Enum<?> & IBaseEnum> E codeOf(Class<E> enumClass, int value) {
        E[] enumConstants = enumClass.getEnumConstants();
        for (E e : enumConstants) {
            if (e.getValue() == value)
                return e;
        }
        return null;
    }
}
           

4. 自定义类型转换器

准备工作做的差不多了,是时候开始编写转换器了。

BaseTypeHandler<T>

 一共需要实现4个方法:

  1. void setNonNullParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType)

用于定义设置参数时,该如何把Java类型的参数转换为对应的数据库类型

  1. T getNullableResult(ResultSet rs, String columnName)

用于定义通过字段名称获取字段数据时,如何把数据库类型转换为对应的Java类型

  1. T getNullableResult(ResultSet rs, int columnIndex)

用于定义通过字段索引获取字段数据时,如何把数据库类型转换为对应的Java类型

  1. T getNullableResult(CallableStatement cs, int columnIndex)

用定义调用存储过程后,如何把数据库类型转换为对应的Java类型

我是这样实现的:

public class QuestEnumTypeHandler<E extends Enum<?> & IBaseEnum> extends BaseTypeHandler<IBaseEnum> {

    private Class<E> type;
    private E[] enums;
    public QuestEnumTypeHandler(Class<E> type) {
        if (type == null) {
            throw new IllegalArgumentException("Type argument cannot be null");
        }
        this.type = type;
    }

    private E codeOf(int code){
        try {
            return QuestEnumUtil.codeOf(type, code);
        } catch (Exception ex) {
            throw new IllegalArgumentException("Cannot convert " + code + " to " + type.getSimpleName() + " by code value.", ex);
        }
    }

    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, IBaseEnum iBaseEnum, JdbcType jdbcType) throws SQLException {
        preparedStatement.setInt(i,iBaseEnum.getValue());
    }

    @Override
    public IBaseEnum getNullableResult(ResultSet resultSet, String s) throws SQLException {
        int anInt = resultSet.getInt(s);
        return resultSet.wasNull()?null:codeOf(anInt);
    }

    @Override
    public IBaseEnum getNullableResult(ResultSet resultSet, int i) throws SQLException {
        int anInt = resultSet.getInt(i);
        return resultSet.wasNull()?null:codeOf(anInt);
    }

    @Override
    public IBaseEnum getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        int anInt = callableStatement.getInt(i);
        return callableStatement.wasNull()?null:codeOf(anInt);
    }
}
           

5. 使用

接下来需要指定哪个类使用我们自己编写转换器进行转换,在MyBatis配置文件中配置如下:

<typeHandlers>
    <typeHandler handler="xxx.xxx.xxx.QuestEnumTypeHandler" javaType="xxx.xxx.xxx.QuestionnaireEnum"/>
</typeHandlers>
           

搞定! 经测试

ComputerState.OPEN

被转换为

10

,

ComputerState.UNKNOWN

被转换为

255

,达到了预期的效果。

6. 优化

在第5步时,我们在MyBatis中添加

typeHandler

用于指定哪些类使用我们自定义的转换器,一旦系统中的枚举类多了起来,MyBatis的配置文件维护起来会变得非常麻烦,也容易出错。如何解决呢?

Spring

中我们可以使用

JavaConfig

方式来干预

SqlSessionFactory

的创建过程,来完成转换器的指定。

思路

  1. 再写一个能自动匹配转换行为的转换器
  2. 通过

    sqlSessionFactory.getConfiguration().getTypeHandlerRegistry()

    取得类型转换器注册器
  3. 再使用

    typeHandlerRegistry.setDefaultEnumTypeHandler(Class<? extends TypeHandler> typeHandler)

    将第一步的转换器注册成为默认的

首先,我们需要一个能确定转换行为的转换器:

AutoEnumTypeHandler.java

public class AutoEnumTypeHandler<E extends Enum<E>> extends BaseTypeHandler<E> {

    private BaseTypeHandler typeHandler = null;

    public AutoEnumTypeHandler(Class<E> type) {
        if (type == null) {
            throw new IllegalArgumentException("Type argument cannot be null");
        }
        if(IBaseEnum.class.isAssignableFrom(type)){
            // 如果实现了 BaseCodeEnum 则使用我们自定义的转换器
            typeHandler = new QuestEnumTypeHandler(type);
        }else {
            // 默认转换器 也可换成 EnumOrdinalTypeHandler
            typeHandler = new EnumTypeHandler<>(type);
        }
    }


    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, E e, JdbcType jdbcType) throws SQLException {
        typeHandler.setNonNullParameter(preparedStatement, i, e, jdbcType);
    }

    @Override
    public E getNullableResult(ResultSet resultSet, String s) throws SQLException {
        return (E) typeHandler.getNullableResult(resultSet, s);
    }

    @Override
    public E getNullableResult(ResultSet resultSet, int i) throws SQLException {
        return (E) typeHandler.getNullableResult(resultSet, i);
    }

    @Override
    public E getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        return (E) typeHandler.getNullableResult(callableStatement,i);
    }
}
           

接下来,我们需要干预

SqlSessionFactory

的创建过程,将刚刚的转换器指定为默认的:

@Configuration
@ConfigurationProperties(prefix = "mybatis")
public class MyBatisConfig {
    private String configLocation;

    private String mapperLocations;

    public String getConfigLocation() {
        return configLocation;
    }

    public void setConfigLocation(String configLocation) {
        this.configLocation = configLocation;
    }

    public String getMapperLocations() {
        return mapperLocations;
    }

    public void setMapperLocations(String mapperLocations) {
        this.mapperLocations = mapperLocations;
    }

    @Bean
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
        SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
        factory.setDataSource(dataSource);


        // 设置配置文件及mapper文件地址
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        factory.setConfigLocation(resolver.getResource(configLocation));
        factory.setMapperLocations(resolver.getResources(mapperLocations));


        SqlSessionFactory sqlSessionFactory = factory.getObject();


        // 取得类型转换注册器
        TypeHandlerRegistry typeHandlerRegistry = sqlSessionFactory.getConfiguration().getTypeHandlerRegistry();
        // 注册默认枚举转换器
        typeHandlerRegistry.setDefaultEnumTypeHandler(AutoEnumTypeHandler.class);

        return sqlSessionFactory;
    }
}
           

搞定! 这样一来,如果枚举实现了

BaseCodeEnum

接口就使用我们自定义的

CodeEnumTypeHandler

,如果没有实现

BaseCodeEnum

接口就使用默认的。再也不用写MyBatis的配置文件了!

就以为这样子就解决了吗?别忘了在相关的mybati.xml文件设置映射

<resultMap type="" id="">
<result column="q_education" property="QEducation" typeHandler="com.hjr.enums.QuestEnumTypeHandler"/>
<result column="q_job" property="QJob" typeHandler="com.hjr.enums.QuestEnumTypeHandler"/>
</resultMap>
           

如何在MyBatis中优雅的使用枚举

请根据自己的配置修改