mybaties 自定义 类型处理器

首先,在知道怎么定义类型处理器之前,了解一下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})

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值