一、普通对象转Json
1、继承BaseTypeHandler
覆盖setNonNullParameter(),getNullableResult(),getNullableResult(),getNullableResult()方法
public class ObjectJsonHandler<T> extends BaseTypeHandler<T> { private Class<T> clazz;
/**构造函数*/ public ObjectJsonHandler(Class<T> clazz) { if (clazz == null) { throw new IllegalArgumentException("Type argument cannot be null"); } this.clazz = clazz; } private NameFilter nameFilter = (object, name, value) -> name; /** * 设置非空参数 */ @Override public void setNonNullParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException { ps.setString(i, JSON.toJSONString(parameter, nameFilter)); } /** * 根据列名,获取可以为空的结果 */ @Override public T getNullableResult(ResultSet rs, String columnName) throws SQLException { String sqlJson = rs.getString(columnName); if (null != sqlJson) { return JSONObject.parseObject(sqlJson, clazz); } return null; } /** * 根据列索引,获取可以为空的结果 */ @Override public T getNullableResult(ResultSet rs, int columnIndex) throws SQLException { String sqlJson = rs.getString(columnIndex); if (null != sqlJson) { return JSONObject.parseObject(sqlJson, clazz); } return null; } @Override public T getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { String sqlJson = cs.getString(columnIndex); if (null != sqlJson) { return JSONObject.parseObject(sqlJson, clazz); } return null; } }
2、继承ObjectJsonHandler
/** * @Author jijiecong * @Date 2021/4/20 11:49 * @Version 1.0 * @Description 评价扩展字段json处理器 */ public class EvaluationAttributesJsonHandler extends ObjectJsonHandler<EvaluationAttributesEntity> { public EvaluationAttributesJsonHandler() { super(EvaluationAttributesEntity.class); } }
二、List对象转JsonArray
1、继承BaseTypeHandler
覆盖setNonNullParameter(),getNullableResult(),getNullableResult(),getNullableResult()方法
** * ObjectJsonArrayHandler * JSONArray处理类 * * @version v1.0.0 * @date 2020/7/23 */ public class ObjectJsonArrayHandler<T> extends BaseTypeHandler<List<T>> { private final Class<T> clazz; public ObjectJsonArrayHandler(Class<T> clazz) { if (clazz == null) { throw new IllegalArgumentException("Type argument cannot be null"); } else { this.clazz = clazz; } } @Override public void setNonNullParameter(PreparedStatement ps, int i, List<T> ts, JdbcType jdbcType) throws SQLException { ps.setString(i, JSONArray.toJSONString(ts)); } @Override public List<T> getNullableResult(ResultSet rs, String s) throws SQLException { String jsonArrayStr = rs.getString(s); if (null != jsonArrayStr) { return JSONArray.parseArray(jsonArrayStr).toJavaList(clazz); } return null; } @Override public List<T> getNullableResult(ResultSet rs, int i) throws SQLException { String jsonArrayStr = rs.getString(i); if (null != jsonArrayStr) { return JSONArray.parseArray(jsonArrayStr).toJavaList(clazz); } return null; } @Override public List<T> getNullableResult(CallableStatement cs, int i) throws SQLException { String jsonArrayStr = cs.getString(i); if (null != jsonArrayStr) { return JSONArray.parseArray(jsonArrayStr).toJavaList(clazz); } return null; } }
2、继承ObjectJsonArrayHandler
/** * StringJsonArrayHandler * * @version v1.0.0 * @date 2020/8/11 */ public class StringJsonArrayHandler extends ObjectJsonArrayHandler<String> { public StringJsonArrayHandler(Class<String> clazz) { super(String.class); } }