mybatis处理枚举类

栏目: Java · 发布时间: 4年前

内容简介:但是给转换仅仅是将对应的枚举转换为其索引位置,也就是"ordinal()"方法获取到的值。对应自定义的int值,该类无能为力。对于想将枚举在数据库中存储为对应的int值的情况,该类没办法实现。基于以上mybatis提供的两个枚举处理类的能力有限,因此只能自己定义对枚举的转换了。

mybatis自带对枚举的处理类

  • org.apache.ibatis.type.EnumOrdinalTypeHandler<E> :该类实现了枚举类型和Integer类型的相互转换。

但是给转换仅仅是将对应的枚举转换为其索引位置,也就是"ordinal()"方法获取到的值。对应自定义的int值,该类无能为力。

  • org.apache.ibatis.type.EnumTypeHandler<E> :该类实现了枚举类型和String类型的相互转换。

对于想将枚举在数据库中存储为对应的int值的情况,该类没办法实现。

基于以上mybatis提供的两个枚举处理类的能力有限,因此只能自己定义对枚举的转换了。

自定义mybatis的枚举处理类 EnumValueTypeHandler

该类需要继承 org.apache.ibatis.type.BaseTypeHandler<E> ,然后在重定义的方法中实现自有逻辑。

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.ibatis.type.MappedTypes;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

/**
 * 处理实现了{@link EsnBaseEnum}接口的枚举类
 * @author followtry
 * @time 2016年8月16日 下午8:06:49
 * @since 2016年8月16日 下午8:06:49
 */
 //在 xml 中添加该 TypeHandler 时需要使用该注解
@MappedTypes(value = {
        QcListTypeEnum.class,
        SellingQcBizTypeEnum.class
})
public class EnumValueTypeHandler<E extends EsnBaseEnum> extends BaseTypeHandler<E> {

    private Class<E> type;

    private final E[] enums;

    public EnumValueTypeHandler(Class<E> type) {
        if (type == null) {
            throw new IllegalArgumentException("Type argument cannot be null");
        }
        this.type = type;
        this.enums = type.getEnumConstants();
        if (this.enums == null) {
            throw new IllegalArgumentException(type.getSimpleName() + " does not represent an enum type.");
        }
    }

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, E parameter, JdbcType jdbcType) throws SQLException {
        //获取非空的枚举的int值并设置到statement中
        ps.setInt(i, parameter.getValue());
    }

    @Override
    public E getNullableResult(ResultSet rs, String columnName) throws SQLException {
        int i = rs.getInt(columnName);
        if (rs.wasNull()) {
            return null;
        } else {
            try {
                return getEnumByValue(i);
            } catch (Exception ex) {
                throw new IllegalArgumentException(
                        "Cannot convert " + i + " to " + type.getSimpleName() + " by ordinal value.", ex);
            }
        }
    }

    /**
     * 通过枚举类型的int值,获取到对应的枚举类型
     * @author jingzz
     * @param i
     */
    protected E getEnumByValue(int i) {
        for (E e : enums) {
            if (e.getValue() == i) {
                return e;
            }
        }
        return null;
    }

    @Override
    public E getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        int i = rs.getInt(columnIndex);
        if (rs.wasNull()) {
            return null;
        } else {
            try {
                return getEnumByValue(i);
            } catch (Exception ex) {
                throw new IllegalArgumentException(
                        "Cannot convert " + i + " to " + type.getSimpleName() + " by ordinal value.", ex);
            }
        }
    }

    @Override
    public E getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        int i = cs.getInt(columnIndex);
        if (cs.wasNull()) {
            return null;
        } else {
            try {
                return getEnumByValue(i);
            } catch (Exception ex) {
                throw new IllegalArgumentException(
                        "Cannot convert " + i + " to " + type.getSimpleName() + " by ordinal value.", ex);
            }
        }
    }

}

该处理器是处理继承了 EsnBaseEnum 接口的枚举类,因为该接口中定义了获取自定义int值的方法。

如果在 mybatis 的 xml 中配置 该 typehandler,则需要添加注解 @MappedTypes 。在添加 typeHandler 注册时使用具体的实现类注册。

配置文件如下:

<typeAliases>
<!-- 为自定义的 TypeHandler 指定别名,在  sql  的 xml 中即可使用别名访问了 -->
    <typeAlias type="cn.followtry.mybatis.EnumValueTypeHandler" alias="enumValueTypeHandler"/>
</typeAliases>
<typeHandlers>
    <!--处理枚举的值转换-->
    <typeHandler handler="cn.followtry.mybatis.EnumValueTypeHandler"/>
</typeHandlers>

自定义的 Enum 需要实现的接口

/**
 * @author jingzz
 * @time 2016年8月17日 上午9:22:46
 * @since 2016年8月17日 上午9:22:46
 */
public interface EsnBaseEnum {
    
    public int getValue();
}

而实现了 EsnBaseEnum 的枚举示例如下:

/**
 * 同步的类型
 * @author jingzz
 * @time 2016年8月3日 上午11:13:06
 */
public enum SyncType implements EsnBaseEnum {
    
    DEPT(3),//部门
    PERSON(1);//人员
    
    private int value;
    
    private SyncType(int value) {
        
        this.value = value;
    }
    
    @Override
    public int getValue(){
        return this.value;
    }
}

只有在实现了 EsnBaseEnum 的接口, EnumValueTypeHandler 才能通过接口的getValue方法获取到对应枚举的值。

到此,对于枚举的简单处理逻辑已经实现完成了,接下来就是如何配置来使用该自定义枚举处理逻辑

配置对枚举的处理

首先,mybatis中对于处理逻辑的设置是在sql的映射文件中,如 EsnSyncLogMapper.xml

关键的设置枚举处理的位置如下:

<resultMap id="BaseResultMap" type="com.test.dao.model.EsnSyncLog" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="sync_time" property="syncTime" jdbcType="TIMESTAMP" />
    <result column="total_num" property="totalNum" jdbcType="INTEGER" />
    <result column="success_total_num" property="successTotalNum" jdbcType="INTEGER" />
    <result column="type" property="type" typeHandler="com.test.common.EnumValueTypeHandler" />
    <result column="msg" property="msg" jdbcType="VARCHAR" />
  </resultMap>

其中type列设置了属性 typeHandler ,其值为自定义的枚举处理逻辑。

而在具体sql中也需要使用 typeHandler 属性,如:

<if test="type != null" >
    #{type, typeHandler=com.test.common.EnumValueTypeHandler},
  </if>

在mybatis处理到该位置时,就会调用typeHandler指定的处理类来处理枚举类型。


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

雷军

雷军

蔡艳鹏 / 2012-12 / 29.80元

《雷军:人因梦想而伟大》内容简介:人生充满着期待,梦想连接着未来。雷军一直有个梦,就是建一个受世人尊敬的企业。他不仅建立了属于自己的受人尊敬的企业,也在帮助别人实现心中的梦想。雷军可以说是创业者、职场人奋斗的榜样,从他在金山的不折不挠,在投资界的百投百中,到小米的成功……无不充满传奇,让无数人争相效仿。一起来看看 《雷军》 这本书的介绍吧!

URL 编码/解码
URL 编码/解码

URL 编码/解码

MD5 加密
MD5 加密

MD5 加密工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具