支持对mybatis转对象过程当中枚举类型自动转换. java
声明: 最近发布的文章都是从已经上线的项目中分离的, 绝对经得起考验. 若有问题, 欢迎留言. 谢谢!mysql
EnumTypeHandler.javagit
package com.xxx.xxx.config.mybatis; import com.xxx.xxx.enums.BaseEnum; import lombok.extern.slf4j.Slf4j; import org.apache.ibatis.type.*; import java.sql.CallableStatement; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; /** * mybatis枚举类型处理 * org.apache.ibatis.type.EnumOrdinalTypeHandler * org.apache.ibatis.type.EnumTypeHandler * @author Yehun */ @Slf4j @MappedJdbcTypes(value = {JdbcType.NUMERIC, JdbcType.INTEGER}) public class EnumTypeHandler<E extends Enum<?> & BaseEnum<Integer>> extends BaseTypeHandler<BaseEnum<Integer>> { private Class<E> type; public EnumTypeHandler() { } /** * 设置配置文件设置的转换类以及枚举类内容 * @param type 配置文件中设置的转换类 */ public EnumTypeHandler(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, BaseEnum<Integer> parameter, JdbcType jdbcType) throws SQLException { Integer code = parameter.getCode(); if (jdbcType == null && code != null) { ps.setInt(i, code); } else { assert jdbcType != null; ps.setObject(i, parameter.getCode(), jdbcType.TYPE_CODE); // see r3589 } } @Override public E getNullableResult(ResultSet rs, String columnName) throws SQLException { Integer i = rs.getInt(columnName); return rs.wasNull() ? null : getEnum(i); } @Override public E getNullableResult(ResultSet rs, int columnIndex) throws SQLException { Integer i = rs.getInt(columnIndex); return rs.wasNull() ? null : getEnum(i); } @Override public E getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { Integer i = cs.getInt(columnIndex); return cs.wasNull() ? null : getEnum(i); } /** * 枚举类型转换,因为构造函数获取了枚举的子类enums * @param value 数据库中存储的自定义value属性 * @return value对应的枚举类 */ private E getEnum(Integer value) { log.debug("enum is [{}]", this.type.getName()); E[] enums = this.type.getEnumConstants(); if(enums != null) { for (E e : enums) { if (e.getCode().equals(value)) { return e; } } } throw new IllegalArgumentException(String.format("未知的枚举类型:%s,请核对%s", value, type.getSimpleName())); } }
在mybatis-config.xml中置顶转换github
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 全局参数 --> <settings> <!-- 使全局的映射器启用或禁用缓存。 --> <setting name="cacheEnabled" value="true"/> <!-- 全局启用或禁用延迟加载。当禁用时,全部关联对象都会即时加载。 --> <setting name="lazyLoadingEnabled" value="true"/> <!-- 当启用时,有延迟加载属性的对象在被调用时将会彻底加载任意属性。不然,每种属性将会按须要加载。 --> <setting name="aggressiveLazyLoading" value="true"/> <!-- 是否容许单条sql 返回多个数据集 (取决于驱动的兼容性) default:true --> <setting name="multipleResultSetsEnabled" value="true"/> <!-- 是否能够使用列的别名 (取决于驱动的兼容性) default:true --> <setting name="useColumnLabel" value="true"/> <!-- 容许JDBC 生成主键。须要驱动器支持。若是设为了true,这个设置将强制使用被生成的主键,有一些驱动器不兼容不过仍然能够执行。 default:false --> <setting name="useGeneratedKeys" value="true"/> <!-- 指定 MyBatis 如何自动映射 数据基表的列 NONE:不隐射 PARTIAL:部分 FULL:所有 --> <setting name="autoMappingBehavior" value="PARTIAL"/> <!-- 这是默认的执行类型 (SIMPLE: 简单; REUSE: 执行器可能重复使用prepared statements语句;BATCH: 执行器能够重复执行语句和批量更新) --> <setting name="defaultExecutorType" value="SIMPLE"/> <!-- 使用驼峰命名法转换字段。 --> <setting name="mapUnderscoreToCamelCase" value="true"/> <!-- 设置本地缓存范围 session:就会有数据的共享 statement:语句范围 (这样就不会有数据的共享 ) defalut:session --> <setting name="localCacheScope" value="SESSION"/> <!-- 设置但JDBC类型为空时,某些驱动程序 要指定值,default:OTHER,插入空值时不须要指定类型 --> <setting name="jdbcTypeForNull" value="NULL"/> </settings> <typeHandlers> <typeHandler handler="com.xxx.xxx.config.mybatis.EnumTypeHandler" jdbcType="NUMERIC" javaType="com.xxx.xxx.enums.StatusEnum" /> </typeHandlers> <plugins> <plugin interceptor="com.github.pagehelper.PageHelper"> <property name="dialect" value="mysql"/> <property name="offsetAsPageNum" value="false"/> <property name="rowBoundsWithCount" value="false"/> <property name="pageSizeZero" value="true"/> <property name="reasonable" value="false"/> <property name="supportMethodsArguments" value="false"/> <property name="returnPageInfo" value="none"/> </plugin> </plugins> </configuration>
oksql