步骤1:实现TypeHandler接口
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import org.apache.ibatis.type.TypeHandler;
/**
* @author
* java中的boolean和jdbc中的char之间转换;true-Y;false-N
*/
//方式一:
@MappedJdbcTypes(JdbcType.CHAR)
@MappedTypes(Boolean.class)
public class BooleanTypeHandler implements TypeHandler {
/* (non-Javadoc)
* @see org.apache.ibatis.type.TypeHandler#getResult(java.sql.ResultSet, java.lang.String)
*/
@Override
public Object getResult(ResultSet resultSet, String columnLabel) throws SQLException {
String str = resultSet.getString(columnLabel);
Boolean rt = Boolean.FALSE;
if (str.equalsIgnoreCase("Y")){
rt = Boolean.TRUE;
}
return rt;
}
@Override
public Object getResult(ResultSet resultSet, int columnIndex) throws SQLException {
String str = resultSet.getString(columnIndex);
Boolean rt = Boolean.FALSE;
if (str.equalsIgnoreCase("Y")){
rt = Boolean.TRUE;
}
return rt;
}
/* (non-Javadoc)
* @see org.apache.ibatis.type.TypeHandler#getResult(java.sql.CallableStatement, int)
*/
@Override
public Object getResult(CallableStatement arg0, int arg1)
throws SQLException {
Boolean b = arg0.getBoolean(arg1);
return b == true ? "Y" : "N";
}
/* (non-Javadoc)
* @see org.apache.ibatis.type.TypeHandler#setParameter(java.sql.PreparedStatement, int, java.lang.Object, org.apache.ibatis.type.JdbcType)
*/
@Override
public void setParameter(PreparedStatement arg0, int arg1, Object arg2,
JdbcType arg3) throws SQLException {
Boolean b = (Boolean) arg2;
String value = (Boolean) b == true ? "Y" : "N";
arg0.setString(arg1, value);
}
}
步骤2:在Mybatis配置中注册该TypeHandler
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath:dao/mapper/*.xml"></property>
<property name="typeHandlers" >
<array>
<bean class="com.sankuai.travel.csc.test.dao.typehandler.BooleanTypeHandler" />
</array>
</property>
<property name="plugins">
<array>
<bean class="com.sankuai.travel.csc.test.dao.interceptor.SqlMonitorInterceptor" />
</array>
</property>
</bean>
步骤3:在映射配置文件中使用该TypeHander(如果第一步使用了注解,此处可以省略)
3.1在resultMap的定义中对对应列定义typeHandler
<resultMap id="BaseResultMap" type="com.sankuai.travel.csc.test.dao.domain.CategoryDO" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="category_type" property="categoryType" jdbcType="INTEGER" />
<result column="category_name" property="categoryName" jdbcType="VARCHAR" />
<result column="allocate_time" property="allocateTime" jdbcType="INTEGER" />
<result column="promise_time" property="promiseTime" jdbcType="INTEGER" />
<result column="last_operator" property="lastOperator" jdbcType="VARCHAR" />
<result column="update_time" property="updateTime" jdbcType="TIMESTAMP" />
<!--写在这里,仅对select语句有效,typeHandler="com.sankuai.travel.csc.test.dao.typehandler.BooleanTypeHandler" -->
<result column="is_cancel" property="isCancel" jdbcType="CHAR" typeHandler="com.sankuai.travel.csc.test.dao.typehandler.BooleanTypeHandler"/>
<result column="status1" property="status1" jdbcType="CHAR" />
<result column="status2" property="status2" jdbcType="CHAR" />
<result column="phone_list" property="phoneList" jdbcType="VARCHAR" />
<result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
</resultMap>
这里只能是在select的时候才会使用自定义的TypeHandler处理对应的映射关系,如果要在insert或者update时使用则需要在sql定义中添加相应的内容
3.2在resultMap的定义中对对应列定义typeHandler
<insert id="insert" parameterType="com.sankuai.travel.csc.test.dao.domain.CategoryDO" >
<selectKey resultType="java.lang.Integer" keyProperty="id" order="AFTER" >
SELECT LAST_INSERT_ID()
</selectKey>
insert into hotel_order_category (category_type, category_name, allocate_time,
promise_time, last_operator, update_time,
is_cancel, status1, status2,
phone_list, create_time)
values (#{categoryType,jdbcType=INTEGER}, #{categoryName,jdbcType=VARCHAR}, #{allocateTime,jdbcType=INTEGER},
#{promiseTime,jdbcType=INTEGER}, #{lastOperator,jdbcType=VARCHAR}, #{updateTime,jdbcType=TIMESTAMP},
<!--写在这里,仅对insert有效,注意,没有双引号: typeHandler=com.sankuai.travel.csc.test.dao.typehandler.BooleanTypeHandler -->
#{isCancel,jdbcType=CHAR,typeHandler=com.sankuai.travel.csc.test.dao.typehandler.BooleanTypeHandler},
#{status1,jdbcType=CHAR}, #{status2,jdbcType=CHAR},
#{phoneList,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP})
</insert>
#mybatis-enumtypehandler和enumordinaltypehandler的区别
mybatis对枚举类型提供了两种类型支持:EnumTypeHandler和EnumOrdinalTypeHandler。
- EnumTypeHandler是mybatis默认的枚举类型转换器,如果pojo类中使用了枚举类型,而配置文件没有指定类型转换类,mybatis将使用EnumTypeHandler处理枚举属性。EnumTypeHandler的将把枚举类的name进行存储,枚举类的name即枚举类名。参考:http://mybatis.github.io/mybatis-3/zh/configuration.html
- EnumOrdinalTypeHandler是mybatis提供的另一种转换器,顾名思义这个转换类使用了枚举类的ordinal属性作为数据库存储信息,由于ordinal属性是int类型的,按照官网的说明数据库中对应资源应该是int或double类型的,但是个人测试过程中MYSQL的varchar字段也可以存储。
- 总结:EnumTypeHandler和EnumOrdinalTypeHandler的区别主要是数据库中存储字段的类型差别,由于EnumOrdinalTypeHandler使用枚举类型的ordinal作为存储,所以必须使用数字类型字段存储。
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath:dao/mapper/*.xml"></property>
<property name="typeHandlers" >
<array>
<bean class="com.sankuai.travel.csc.test.dao.typehandler.BooleanTypeHandler" />
<bean class="com.sankuai.travel.csc.test.dao.typehandler.StringArrayTypeHandler" />
<!--不需要EnumTypeHandler,默认包含 -->
<bean class="org.apache.ibatis.type.EnumTypeHandler" >
<constructor-arg name="type" value="com.sankuai.travel.csc.test.enums.SimpleStatusEnum" />
</bean>
<!--需要EnumOrdinalTypeHandler,默认包含 -->
<bean class="org.apache.ibatis.type.EnumOrdinalTypeHandler" >
<constructor-arg name="type" value="com.sankuai.travel.csc.test.enums.SimpleStatusEnum" />
</bean>
</array>
</property>
<property name="plugins">
<array>
<bean class="com.sankuai.travel.csc.test.dao.interceptor.SqlMonitorInterceptor" />
</array>
</property>
</bean>
<resultMap id="BaseResultMap" type="com.sankuai.travel.csc.test.dao.domain.CategoryDO" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="category_type" property="categoryType" jdbcType="INTEGER" />
<result column="category_name" property="categoryName" jdbcType="VARCHAR" />
<result column="allocate_time" property="allocateTime" jdbcType="INTEGER" />
<result column="promise_time" property="promiseTime" jdbcType="INTEGER" />
<result column="last_operator" property="lastOperator" jdbcType="VARCHAR" />
<result column="update_time" property="updateTime" jdbcType="TIMESTAMP" />
<!--方式二:写在这里,仅对select语句有效,typeHandler="com.sankuai.travel.csc.test.dao.typehandler.BooleanTypeHandler" -->
<result column="is_cancel" property="isCancel" jdbcType="CHAR" />
<!--EnumTypeHandler默认处理类,不需要配置 -->
<result column="status1" property="status1" jdbcType="CHAR" />
<!--EnumOrdinalTypeHandler需要单独配置 -->
<result column="status2" property="status2" jdbcType="CHAR" typeHandler="org.apache.ibatis.type.EnumOrdinalTypeHandler" />
<result column="phone_list" property="phoneList" jdbcType="VARCHAR" />
<result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
</resultMap>
参考文章
https://my.oschina.net/amoshuang/blog/134199
mybatis-enumtypehandler和enumordinaltypehandler的区别