自定义集合类型转换器, 将sql字符串和对象中的集合相互转换
/** 自定义类型转换器, 将sql字符串和对象中的集合相互转换
* @author zjl
* @date 2023/11/10
*/
@MappedJdbcTypes(JdbcType.VARCHAR)
@MappedTypes(java.util.List.class)
public class ListToStringTypeHandle extends BaseTypeHandler<List<String>> {
private static final ObjectMapper mapper = new ObjectMapper();
private String toJson(List<String> params) {
try {
return mapper.writeValueAsString(params);
} catch (Exception e) {
e.printStackTrace();
}
return "[]";
}
private List<String> toObject(String content) {
if (content != null && !content.isEmpty()) {
try {
return (List<String>) mapper.readValue(content, List.class);
} catch (Exception e) {
throw new RuntimeException(e);
}
} else {
return null;
}
}
@Override
public void setParameter(PreparedStatement preparedStatement, int i, List<String> parameter, JdbcType jdbcType) throws SQLException {
preparedStatement.setString(i, toJson(parameter));
}
@Override
public List<String> getResult(ResultSet resultSet, String columnName) throws SQLException {
return this.toObject(resultSet.getString(columnName));
}
@Override
public List<String> getResult(ResultSet resultSet, int columnName) throws SQLException {
return this.toObject(resultSet.getString(columnName));
}
@Override
public List<String> getResult(CallableStatement callableStatement, int columnName) throws SQLException {
return this.toObject(callableStatement.getString(columnName));
}
@Override
public void setNonNullParameter(PreparedStatement preparedStatement, int i, List<String> parameter, JdbcType jdbcType) throws SQLException {
preparedStatement.setString(i, toJson(parameter));
}
@Override
public List<String> getNullableResult(ResultSet resultSet, String columnName) throws SQLException {
return this.toObject(resultSet.getString(columnName));
}
@Override
public List<String> getNullableResult(ResultSet resultSet, int columnName) throws SQLException {
return this.toObject(resultSet.getString(columnName));
}
@Override
public List<String> getNullableResult(CallableStatement callableStatement, int columnName) throws SQLException {
return this.toObject(callableStatement.getString(columnName));
}
}
自定义数组类型转换器, 将sql字符串和对象中的数组相互转换
/**
* @author zjl
* @date 2023/11/10
*/
@MappedJdbcTypes(JdbcType.VARCHAR)
@MappedTypes(java.lang.String[].class)
public class ArrayToStringTypeHandler extends BaseTypeHandler<String[]> {
private static final ObjectMapper mapper = new ObjectMapper();
@Override
public void setNonNullParameter(PreparedStatement ps, int i, String[] parameter, JdbcType jdbcType) throws SQLException {
ps.setString(i, toJson(parameter));
}
@Override
public String[] getNullableResult(ResultSet rs, String columnName) throws SQLException {
return this.toObject(rs.getString(columnName));
}
@Override
public String[] getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
return this.toObject(rs.getString(columnIndex));
}
@Override
public String[] getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
return this.toObject(cs.getString(columnIndex));
}
private String toJson(String[] params) {
try {
return mapper.writeValueAsString(params);
} catch (Exception e) {
e.printStackTrace();
}
return "[]";
}
private String[] toObject(String content) {
if (content != null && !content.isEmpty()) {
try {
return (String[]) mapper.readValue(content, String[].class);
} catch (Exception e) {
throw new RuntimeException(e);
}
} else {
return null;
}
}
}
以上两种typeHandle用法如下:
可使用注解模式
@TableField(value = "names" , jdbcType = JdbcType.VARCHAR ,typeHandler = ListToStringTypeHandle.class)
private List<String> names;
或xml中(注:两种typeHandle的javaType都为java.util.List)
<resultMap id="na" type="对象的相对路径">
<result typeHandler="ListToStringTypeHandle"
column="names"
javaType="java.util.List"
jdbcType="VARCHAR"
property="names"/>
</resultMap>
<select id="selectNames" resultMap="na">
SELECT * FROM sysName
</select>