springMVC与MyBatis中绑定枚举类型数据的转换

最近项目中遇到这样一个:后台Controller参数是一个对象,对象里面有个枚举类型的属性,前台提交过来的数据里面这个枚举该怎么接收呢,如何写进数据库?
数据库用的MySQL,
该枚举字段为:
这里写图片描述
实际存储为:
这里写图片描述
解决方案如下:

实体类:
这里写图片描述

枚举类:
这里写图片描述
set与get方法省略……
这里写图片描述

接下来是springMVC 中枚举的转换类(Converter)

public class StringToEnumConverter implements ConverterFactory<String,ExceptionTypeEnum> {

    @Override
    public <T extends ExceptionTypeEnum> Converter<String, T> getConverter(Class<T> aClass) {
        return new StringToEnum(aClass);
    }
    private class StringToEnum<T extends Enum> implements Converter<String, T> {

        private final Class<T> enumType;

        public StringToEnum(Class<T> enumType) {
            this.enumType = enumType;
        }

        public T convert(String source) {
            if (source.length() == 0) {
                return null;
            }
            return (T) Enum.valueOf(this.enumType, source.trim());
        }
    }

}

spring配置:

<!--自定义枚举类封装  -->
    <beans:bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
        <beans:property name="converters">
            <beans:set>
                <beans:bean class="com.breadtree.management.vo.StringToEnumConverter" />
            </beans:set>
        </beans:property>
    </beans:bean>
<!--另外这里加上,记得一定要加上-->
<mvc:annotation-driven  conversion-service="conversionService"/>

Mybatis自定义转换类型:

public class EnumKeyTypeHandler extends BaseTypeHandler<ExceptionTypeEnum>{
    private  Class<ExceptionTypeEnum> type;

    private final ExceptionTypeEnum[] enums;

    /**
     * 设置配置文件设置的转换类以及枚举类内容,供其他方法更便捷高效的实现
     * @param type 配置文件中设置的转换类
     */
    public EnumKeyTypeHandler(Class<ExceptionTypeEnum> 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, ExceptionTypeEnum parameter, JdbcType jdbcType) throws SQLException {
        // baseTypeHandler已经帮我们做了parameter的null判断
        ps.setInt(i, parameter.getIndex());
    }

    @Override
    public ExceptionTypeEnum getNullableResult(ResultSet rs, String s) throws SQLException {
        return convert(rs.getInt(s));
    }

    @Override
    public ExceptionTypeEnum getNullableResult(ResultSet rs, int i) throws SQLException {
        return convert(rs.getInt(i));
    }

    @Override
    public ExceptionTypeEnum getNullableResult(CallableStatement cs, int i) throws SQLException {
        return convert(cs.getInt(i));
    }



    private ExceptionTypeEnum convert(int status) {
        ExceptionTypeEnum[] objs = type.getEnumConstants();
        for (ExceptionTypeEnum em : objs) {
            if (em.getIndex() == status) {
                return em;
            }
        }
        return null;
    }

mapper.xml里配置如下:

<resultMap id="BaseExcResultMap" type="XXX.XX.ExceptionEntity">
……省略其他属性配置  
<result column="exception_type" jdbcType="VARCHAR" property="exception_type" typeHandler="com.breadtree.management.vo.EnumKeyTypeHandler" />
……省略其他属性配置                

以上配置完,查询基本就没问题了,页面效果如下,已经正常显示枚举的VALUE值了:
这里写图片描述

对应的存储数据库字段里的值:
这里写图片描述

对比上面的枚举类,一 一对应,没毛病!!!

insert或update时(要注意这里!!!!!!)
赋值的时候
#{exception_type.index}
不要写成: #{exception_type} 这样写存进数据库的是前台传过来的枚举属性,不是想要的效果!

用谷歌postman测试这个接口时,刚开始有点纠结这个枚举属性exception_type,该如何给其赋值,其实得这样如下:

这里写图片描述

在看看上面的枚举类:
这里写图片描述

对应controller接口,里面只用一个对象接口:

@RequestMapping(value = "/addException", method = RequestMethod.POST)
public ExecuteResult addException(ExceptionEntity exceptionEntity)

断点调试,刚进此方法时:
这里写图片描述

我这里做的是insert,最后存储到数据库如下:
这里写图片描述

大致就这样了!!!暂时就只用了,不知道还有没有其他方法!
参考网上的一些文章:

http://xiuluocd.iteye.com/blog/2302501
http://blog.csdn.net/china_bobo/article/details/43016415
http://elim.iteye.com/blog/1860732

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值