构建Enum解析器:
/**
* Auther: Charles.Chen <br>
* Description: 类型标识接口
* Date: Create in 10:32 2018/5/25
**/
public interface BaseEnumValue {
String getEnumValue();
}
/**
* Auther: Charles.Chen <br>
* Description: 自定义枚举解析类
* 用于在Mappering.xml中使用枚举常量,便于状态值的数据库和代码中保持同步
* 暂只实现由 JAVA 转 DB,且只支持转换为JdbcType = INTEGER\VARCHAR
* Date: Create in 10:24 2018/5/25
**/
@MappedTypes({com.clyy.core.config.mybatis.handlers.BaseEnumValue.class})
public class EnumValueTypeHandler<E extends Enum<E> & BaseEnumValue> extends BaseTypeHandler<BaseEnumValue> {
/**
* 用于定义设置参数时,该如何把Java类型的参数转换为对应的数据库类型
* @param preparedStatement
* @param i
* @param e
* @param jdbcType
* @throws SQLException
*/
@Override
public void setNonNullParameter(PreparedStatement preparedStatement, int i, BaseEnumValue e, JdbcType jdbcType) throws SQLException {
if(jdbcType == JdbcType.INTEGER) {
preparedStatement.setInt(i, Integer.parseInt(e.getEnumValue()));
} else {
preparedStatement.setString(i, e.getEnumValue());
}
}
/**
* 用于定义通过字段名称获取字段数据时,如何把数据库类型转换为对应的Java类型
* @param resultSet
* @param s
* @return
* @throws SQLException
*/
@Override
public E getNullableResult(ResultSet resultSet, String s) throws SQLException {
// TODO
return null;
}
/**
* 用于定义通过字段索引获取字段数据时,如何把数据库类型转换为对应的Java类型
* @param resultSet
* @param i
* @return
* @throws SQLException
*/
@Override
public E getNullableResult(ResultSet resultSet, int i) throws SQLException {
// TODO
return null;
}
/**
* 用定义调用存储过程后,如何把数据库类型转换为对应的Java类型
* @param callableStatement
* @param i
* @return
* @throws SQLException
*/
@Override
public E getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
// TODO
return null;
}
}
配置Enum解析器:
方式一:配置文件指定Mybatis需要扫描的Handlers目录
spring.mybatis.bss.type-handlers-package=com.clyy.core.config.mybatis.handlers;
方式二:配置@Bean加载属性(Spring boot)
@Bean
public SqlSessionFactory sqlSessionFactory(DataSource bssDataSource, TypeHandler<?>[] typeHandlers) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(bssDataSource);
if(typeHandlers!= null && typeHandlers.length > 0) {
bean.setTypeHandlers(typeHandlers);
}
// 或 同方式一
// bean.setTypeHandlersPackage("com.clyy.core.config.mybatis.handlers");
return bean.getObject();
}
@Bean
public EnumValueTypeHandler getEnumValueTypeHandler(){
return new EnumValueTypeHandler();
}
使用Enum解析器:
/**
* Auther: Charles.Chen <br>
* Description:
* Date: Create in 8:47 2018/6/8
**/
public enum DataStatus implements BaseEnumValue {
UPDATE("0"),
DELETE("1");
private String value;
private DataStatus(String value) {
this.value = value;
}
public String getValue() {
return this.value;
}
@Override
public String getEnumValue() {
return this.getValue();
}
}
// Mapper.xml
// 非对象式使用Enum值
// 待优化:在加载Mybatis配置时,扫描所有继承BaseEnumValue的枚举数据,并以全局变量的方式配置到Mybatis中,既可避免每次都需要使用<bind>标签
<select id="selectUser" resultMap="BaseResultMap">
<bind name="status" value="@com.clyy.core.enums.DataStatus@DELETE"/>
select * from user
where `status` = #{status, jdbcType=VARCHAR, typeHandler=com.clyy.core.config.mybatis.handlers.EnumValueTypeHandler}
</select>
// Mapper.xml
// 对象式使用,原文:https://my.oschina.net/SEyanlei/blog/188919
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.sg.bean.User">
<resultMap type="User" id="userMap">
<id column="id" property="id"/>
<result column="accountID" property="accountID"/>
<result column="userName" property="userName"/>
<result column="statusDef" property="statusDef"/>
<result column="statusOrdinal" property="statusOrdinal" typeHandler="org.apache.ibatis.type.EnumOrdinalTypeHandler"/>
<result column="statusCustom" property="statusCustom" typeHandler="com.sg.util.typehandler.EnumStatusHandler"/>
</resultMap>
<select id="selectUser" resultMap="userMap">
select * from t_user where id = #{id}
</select>
<insert id="insertUser" parameterType="User">
insert into t_user(id,accountID,userName,statusDef,statusOrdinal,statusCustom)
values(
#{id}, #{accountID}, #{userName},
#{statusDef},
#{statusOrdinal, typeHandler=org.apache.ibatis.type.EnumOrdinalTypeHandler},
#{statusCustom, typeHandler=com.sg.util.typehandler.EnumStatusHandler}
)
</insert>
</mapper>
扩展资料:
Mybatis自定义枚举转换 --> http://www.jb51.net/article/121684.htm
Mybatis使用常量或枚举 --> https://wenku.baidu.com/view/e707038470fe910ef12d2af90242a8956becaa27.html
2150

被折叠的 条评论
为什么被折叠?



