码迷,mamicode.com
首页 > 数据库 > 详细

json存数据库时处理器,jsonHandler/jsonArrayHandler

时间:2021-06-10 18:44:33      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:覆盖   ali   arrays   jdb   str   ring   div   att   resultset   

一、普通对象转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);
    }
}

 

json存数据库时处理器,jsonHandler/jsonArrayHandler

标签:覆盖   ali   arrays   jdb   str   ring   div   att   resultset   

原文地址:https://www.cnblogs.com/jijiecong/p/14871051.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!