Mybatis自定义TypeHandler简介说明
下文笔者讲述Mybatis中自定义TypeHandler简介说明,如下所示
例:继承BaseTypeHandler这个抽象类
TypeHandler简介说明
类型处理器,将入参和结果转换为所需要的类型
类型处理器这个接口其实很简单
总共四个方法
一个方法将入参的Java类型的数据转换为JDBC类型
三个方法将返回结果转换为Java类型
TypeHandler源码简介
public interface TypeHandler<T> {
//设置参数,java类型转换为jdbc类型
void setParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException;
//将查询的结果转换为java类型
T getResult(ResultSet rs, String columnName) throws SQLException;
T getResult(ResultSet rs, int columnIndex) throws SQLException;
T getResult(CallableStatement cs, int columnIndex) throws SQLException;
}
自定义并使用TypeHandler的示例
前端传来的年龄是
男, 女
但是数据库定义的字段却是int 类型(1男2女)
可自定义一个年龄的类型处理器
自定义TYpeHandler的实现思路
方式1:实现TypeHandle接口 方式2:继承BaseTypeHandler抽象类
例:继承BaseTypeHandler这个抽象类
定义一个年龄的类型处理器
@MappedJdbcTypes(JdbcType.INTEGER)
@MappedTypes(String.class)
public class GenderTypeHandler extends BaseTypeHandler {
//设置参数,这里将Java的String类型转换为JDBC的Integer类型
@Override
public void setNonNullParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType) throws SQLException {
ps.setInt(i, StringUtils.equals(parameter.toString(),"男")? 1:2);
}
// 以下三个参数都是将查询的结果转换
@Override
public Object getNullableResult(ResultSet rs, String columnName) throws SQLException {
return rs.getInt(columnName)==1?"男":"女";
}
@Override
public Object getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
return rs.getInt(columnIndex)==1?"男":"女";
}
@Override
public Object getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
return cs.getInt(columnIndex)==1?"男":"女";
}
}
这里涉及到两个注解,如下:
@MappedTypes :
指定与其关联的 Java 类型列表。
如果在javaType属性中也同时指定,则注解上的配置将被忽略。
@MappedJdbcTypes :
指定与其关联的JDBC类型列表。
如果在jdbcType 属性中也同时指定,则注解上的配置将被忽略。
将其添加到Mybatis中
第一种:只需要在配置文件application.properties 中添加一行配置即可,如下:
## 设置自定义的Typehandler所在的包,启动的时候会自动扫描配置到Mybatis中
mybatis.type-handlers-package=com.java265.demo.typehandler
第二种:其实任何框架与Springboot整合之后,只要配置文件中能够配置的,
在配置类中都可以配置(除非有特殊定制,否则不要轻易覆盖自动配置)
如下:
@Bean("sqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPER_LOCATOIN));
org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
// 自动将数据库中的下划线转换为驼峰格式
configuration.setMapUnderscoreToCamelCase(true);
configuration.setDefaultFetchSize(100);
configuration.setDefaultStatementTimeout(30);
sqlSessionFactoryBean.setConfiguration(configuration);
// 将typehandler注册到mybatis
GenderTypeHandler genderTypeHandler = new GenderTypeHandler();
TypeHandler[] typeHandlers=new TypeHandler[]{genderTypeHandler};
sqlSessionFactoryBean.setTypeHandlers(typeHandlers);
return sqlSessionFactoryBean.getObject();
}
XML文件中如何指定TypeHandler
<insert id="insertUser">
insert into user_info(user_id,his_id,name,gender,password,create_time)
values(#{userId,jdbcType=VARCHAR},#{hisId,jdbcType=VARCHAR},#{name,jdbcType=VARCHAR},
#{gender,jdbcType=INTEGER,
typeHandler=com.java265.demo.typehandler.GenderT ypeHandler},
#{password,jdbcType=VARCHAR},now())
</insert>
查询:查询的时候类型处理会将JDBC类型的转化为Java类型,因此也是需要指定typeHandler
,需要在resultMap 中指定typeHandler 这个属性,值为 全类名,如下:
<resultMap id="userResultMap" type="com.java265.demo.domain.UserInfo">
<id column="id" property="id"/>
<result column="user_id" property="userId"/>
<result column="his_id" property="hisId"/>
<!-- 指定typeHandler属性为全类名-->
<result column="gender" property="gender"
typeHandler="com.java265.demo.typehandler.GenderTypeHandler"/>
<result column="name" property="name"/>
<result column="password" property="password"/>
</resultMap>
<select id="selectlist" resultMap="userResultMap">
select * from user_info where status=1 and user_id in
<foreach collection="userIds" item="item" open="(" separator="," close=")" >
#{item}
</foreach>
</select>
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


