首先,在知道怎么定义类型处理器之前,了解一下mybaties的类型处理器是什么,有什么作用?
mybaties的类型处理器,也就是处理JDBC类型与Java类型转换的处理器,mybaties默认是提供给了我们很多处理器。BooleanTypeHandler,ByteTypeHandler等等。
自定义类型处理器:
可以通过TypeHandler BaseTypeHandler 接口来实现
@MappedTypes(value = {Video.class,Blog.class})
public class ClobTypeHandler implements TypeHandler<Object> {
@Override
public void setParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType) throws SQLException {
Clob clob = new SerialClob(parameter.toString().toCharArray());
ps.setClob(i, clob);
}
@Override
public Object getResult(ResultSet rs, String columnName) throws SQLException {
Clob clob = rs.getClob(columnName);
return (clob == null || clob.length() == 0) ? null : clob.getSubString((long) 1, (int) clob.length());
}
@Override
public Object getResult(ResultSet rs, int columnIndex) throws SQLException {
Clob clob = rs.getClob(columnIndex);
return (clob == null || clob.length() == 0) ? null : clob.getSubString((long) 1, (int) clob.length());
}
@Override
public Object getResult(CallableStatement cs, int columnIndex) throws SQLException {
Clob clob = cs.getClob(columnIndex);
return (clob == null || clob.length() == 0) ? null : clob.getSubString((long) 1, (int) clob.length());
}
}
在springboot中使用类型处理器:
1.在 applicationl.properties 中配置 mybatis.type.handlers.pachage = com.tj.dc.web.core.mybatis.handler
2. 在自定义的类型处理器上指定 需要转换的Java类。@MappedTypes(value = {Video.class,Blog.class})
在spring中使用类型处理器:
1.
<!-- mybatis-config.xml --> <typeHandlers> <typeHandler handler="org.mybatis.example.ExampleTypeHandler"/> </typeHandlers>
2.在自定义的类型处理器上指定 需要转换的Java类。@MappedTypes(value = {Video.class,Blog.class})
MyBatis类型处理器详解
本文深入解析MyBatis类型处理器的作用与实现方式,包括如何自定义类型处理器以处理JDBC与Java类型间的转换,以及在SpringBoot和Spring中的配置方法。
1141

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



