mybatis处理枚举类的简单方法
mybatis自带对枚举的处理类
org.apache.ibatis.type.EnumOrdinalTypeHandler
但是给转换仅仅是将对应的枚举转换为其索引位置,也就是"ordinal()"方法获取到的值。对应自定义的int值,该类无能为力。
org.apache.ibatis.type.EnumTypeHandler
对于想将枚举在数据库中存储为对应的int值的情况,该类没办法实现。
基于以上mybatis提供的两个枚举处理类的能力有限,因此只能自己定义对枚举的转换了。
自定义mybatis的枚举处理类EnumValueTypeHandler
该类需要继承org.apache.ibatis.type.BaseTypeHandler
importjava.sql.CallableStatement;
importjava.sql.PreparedStatement;
importjava.sql.ResultSet;
importjava.sql.SQLException;
importorg.apache.ibatis.type.MappedTypes;
importorg.apache.ibatis.type.BaseTypeHandler;
importorg.apache.ibatis.type.JdbcType;
/**
*处理实现了{@linkEsnBaseEnum}接口的枚举类
*@authorfollowtry
*@time2016年8月16日下午8:06:49
*@since2016年8月16日下午8:06:49
*/
//在xml中添加该TypeHandler时需要使用该注解
@MappedTypes(value={
QcListTypeEnum.class,
SellingQcBizTypeEnum.class
})
publicclassEnumValueTypeHandlerextendsBaseTypeHandler{
privateClasstype;
privatefinalE[]enums;
publicEnumValueTypeHandler(Classtype){
if(type==null){
thrownewIllegalArgumentException("Typeargumentcannotbenull");
}
this.type=type;
this.enums=type.getEnumConstants();
if(this.enums==null){
thrownewIllegalArgumentException(type.getSimpleName()+"doesnotrepresentanenumtype.");
}
}
@Override
publicvoidsetNonNullParameter(PreparedStatementps,inti,Eparameter,JdbcTypejdbcType)throwsSQLException{
//获取非空的枚举的int值并设置到statement中
ps.setInt(i,parameter.getValue());
}
@Override
publicEgetNullableResult(ResultSetrs,StringcolumnName)throwsSQLException{
inti=rs.getInt(columnName);
if(rs.wasNull()){
returnnull;
}else{
try{
returngetEnumByValue(i);
}catch(Exceptionex){
thrownewIllegalArgumentException(
"Cannotconvert"+i+"to"+type.getSimpleName()+"byordinalvalue.",ex);
}
}
}
/**
*通过枚举类型的int值,获取到对应的枚举类型
*@authorjingzz
*@parami
*/
protectedEgetEnumByValue(inti){
for(Ee:enums){
if(e.getValue()==i){
returne;
}
}
returnnull;
}
@Override
publicEgetNullableResult(ResultSetrs,intcolumnIndex)throwsSQLException{
inti=rs.getInt(columnIndex);
if(rs.wasNull()){
returnnull;
}else{
try{
returngetEnumByValue(i);
}catch(Exceptionex){
thrownewIllegalArgumentException(
"Cannotconvert"+i+"to"+type.getSimpleName()+"byordinalvalue.",ex);
}
}
}
@Override
publicEgetNullableResult(CallableStatementcs,intcolumnIndex)throwsSQLException{
inti=cs.getInt(columnIndex);
if(cs.wasNull()){
returnnull;
}else{
try{
returngetEnumByValue(i);
}catch(Exceptionex){
thrownewIllegalArgumentException(
"Cannotconvert"+i+"to"+type.getSimpleName()+"byordinalvalue.",ex);
}
}
}
}
该处理器是处理继承了EsnBaseEnum接口的枚举类,因为该接口中定义了获取自定义int值的方法。
如果在mybatis的xml中配置该typehandler,则需要添加注解@MappedTypes。在添加typeHandler注册时使用具体的实现类注册。
配置文件如下:
自定义的Enum需要实现的接口
/**
*@authorjingzz
*@time2016年8月17日上午9:22:46
*@since2016年8月17日上午9:22:46
*/
publicinterfaceEsnBaseEnum{
publicintgetValue();
}
而实现了EsnBaseEnum的枚举示例如下:
/**
*同步的类型
*@authorjingzz
*@time2016年8月3日上午11:13:06
*/
publicenumSyncTypeimplementsEsnBaseEnum{
DEPT(3),//部门
PERSON(1);//人员
privateintvalue;
privateSyncType(intvalue){
this.value=value;
}
@Override
publicintgetValue(){
returnthis.value;
}
}
只有在实现了EsnBaseEnum的接口,EnumValueTypeHandler才能通过接口的getValue方法获取到对应枚举的值。
到此,对于枚举的简单处理逻辑已经实现完成了,接下来就是如何配置来使用该自定义枚举处理逻辑
配置对枚举的处理
首先,mybatis中对于处理逻辑的设置是在sql的映射文件中,如EsnSyncLogMapper.xml。
关键的设置枚举处理的位置如下:
其中type列设置了属性typeHandler,其值为自定义的枚举处理逻辑。
而在具体sql中也需要使用typeHandler属性,如:
#{type,typeHandler=com.test.common.EnumValueTypeHandler},
在mybatis处理到该位置时,就会调用typeHandler指定的处理类来处理枚举类型。
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对毛票票的支持。