Mybatis How to deal with clob type data
- 2021-09-24 22:29:04
- OfStack
Mybatis handles clob type data
When the sql statement is executed and the map type is returned, such as
public List<Map<String, Object>> query(@Param("sql") String sql)
The resulting data is List < Map > Type data, in this case, if there is an clob field returned, the data is oracle. sql. CLOB @ 63636de0. Obviously, this is not what I want, what I need is string data
So how to handle the clob field is very simple
Is to define a type handler that handles the Clob field and converts the Clob data to string data to be returned
@MappedJdbcTypes(JdbcType.CLOB)
public class ClobTypeHandle extends BaseTypeHandler<Object> {
@Override
public void setNonNullParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType)
throws SQLException {
ps.setObject(i, parameter);
}
@Override
public Object getNullableResult(ResultSet rs, String columnName) throws SQLException {
return rs.getString(columnName);
}
@Override
public Object getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
return rs.getString(columnIndex);
}
@Override
public Object getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
return cs.getString(columnIndex);
}
}
Mybatis Large Field clob Processing
1. Entity class View
package com.ttt.sysManager.po;
public class View {
private String VIEW_NAME;
private String TEXT_LENGTH;
private String TEXT;<span style="white-space:pre"> </span>// clob Large field
private String EDITIONING_VIEW;
private String READ_ONLY;
// ... ( get , set Method)
}
2. Dao
List<View> getViewByPage(Map<String, Object> map);
3. XML
<resultMap id="viewResultMap" type="com.ttt.sysManager.po.View">
<result property="VIEW_NAME" column="VIEW_NAME"/>
<result property="TEXT_LENGTH" column="TEXT_LENGTH"/>
<result property="TEXT" column="TEXT" javaType="String" jdbcType="VARBINARY"/>
<result property="EDITIONING_VIEW" column="EDITIONING_VIEW" />
<result property="READ_ONLY" column="READ_ONLY" />
</resultMap>
<select id="getViewByPage" parameterType="map" resultMap="viewResultMap" >
SELECT VIEW_NAME,TEXT_LENGTH,TEXT,EDITIONING_VIEW,READ_ONLY FROM (
SELECT A.*, ROWNUM RN
FROM (SELECT * FROM user_views) A
WHERE ROWNUM <![CDATA[<=]]> #{endRow}
) WHERE RN <![CDATA[>]]> #{startRow}
</select>
Step 4 Write
Write large fields and ordinary write no difference, but the database field type needs to be clob, such as blob will report an error, temporarily not resolved.