mybatis存取oracle长数据clob方法

1.重写mybatis的sql执行器

package com.ai.gridoperation;

import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;

import java.sql.*;


/**
 * 解决保存数据字段超过4000 char 问题
 * @author yuans
 * @create 2020-06-02-14:38
 */
public class OracleClobTypeHandler implements TypeHandler<Object> {

    @Override
    public void setParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType) throws SQLException {
        Clob clob = ps.getConnection().createClob();
        clob.setString(1, (String) parameter);
        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 {
        return null;
    }

    @Override
    public Object getResult(CallableStatement cs, int columnIndex) throws SQLException {
        return null;
    }
}

2.保存使用

  INSERT INTO 
    GR_MICROGRID(example)
    VALUES(
      <if test=" shape !=null and shape !='' ">
           #{shape,typeHandler=com.ai.gridoperation.OracleClobTypeHandler}
      </if>)

3.查询使用

 <resultMap id="microFormatsBorderId" type="java.util.HashMap">
        <result  column="polygon" property="polygon" 		       typeHandler="com.ai.gridoperation.OracleClobTypeHandler"/>
 </resultMap>

 <select id="microFormatsBorderQuery" parameterType="java.util.Map" resultMap="microFormatsBorderId">
        select polygon from table
 </select>
posted @ 2020-06-21 22:04  微凉微  阅读(1981)  评论(0编辑  收藏  举报