码迷,mamicode.com
首页 > 其他好文 > 详细

MyBatis里字段到枚举类的映射

时间:2016-09-27 20:19:47      阅读:560      评论:0      收藏:0      [点我收藏+]

标签:

一、简介

  我们在用MyBatis里,很多时间有这样一个需求:bean里有个属性是枚举,在DB存储时我们想存的枚举的代号,从DB拿出来时想直接映射成目标枚举类型,也即代号字段与Java枚举类的相互类型转换

  当然,你可以为每个枚举写一个MyEnumTypeHandler,但问题是要为每个类都写一个TypeHandler,过于繁琐。

  有了泛型,一个通用的TypeHandler直接搞定。

二、源码

源码详见:spring-mybatis-test

package com.adu.spring_test.mybatis.typehandler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

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

import com.adu.spring_test.mybatis.enums.EnumTrait;
import com.adu.spring_test.mybatis.util.EnumTraitUtil;

/**
 * mapper里字段到枚举类的映射。
 * 用法:
 * 入库:#{item.myEnum, typeHandler=com.adu.spring_test.mybatis.typehandler.EnumTraitTypeHandler}
 * 出库:
 * <resultMap>
 * <result property="enumDataField" column="enum_data_field" javaType="com.xxx.MyEnum" typeHandler="com.adu.spring_test.mybatis.typehandler.EnumTraitTypeHandler"/>
 * </resultMap>
 */
public class EnumTraitTypeHandler<E extends EnumTrait> extends BaseTypeHandler<EnumTrait> {

    private Class<E> type;

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

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, EnumTrait parameter, JdbcType jdbcType)
            throws SQLException {
        ps.setInt(i, parameter.getCode());
    }

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

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

    private E fromCode(int code) {
        return EnumTraitUtil.codeOf(type, code);
    }
}

  

MyBatis里字段到枚举类的映射

标签:

原文地址:http://www.cnblogs.com/waterystone/p/5913953.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!