jiangping
2025-01-08 a35b8eec96aac2793de732c3fb7513cd6c5b2e4c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package com.doumee.config.mybatis;
 
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;
 
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
 
/**
 * @author T14
 */
public class JSONObjectHandler implements TypeHandler<JSONObject> {
    @Override
    public void setParameter(PreparedStatement preparedStatement, int i, JSONObject o, JdbcType jdbcType) throws SQLException {
        preparedStatement.setString(i,toString());
    }
 
    @Override
    public JSONObject getResult(ResultSet resultSet, String s) throws SQLException {
        String str = resultSet.getString(s);
        if (StringUtils.isBlank(str)){
            return null;
        }
        if (!JSONUtil.isJsonObj(str)){
            throw new SQLException("解析数据格式有误");
        }
        return (JSONObject)JSONObject.parse(str);
    }
 
    @Override
    public JSONObject getResult(ResultSet resultSet, int i) throws SQLException {
        String str = resultSet.getString(i);
        if (StringUtils.isBlank(str)){
            return null;
        }
        if (!JSONUtil.isJsonObj(str)){
            throw new SQLException("解析数据格式有误");
        }
        return (JSONObject)JSONObject.parse(str);
    }
 
    @Override
    public JSONObject getResult(CallableStatement callableStatement, int i) throws SQLException {
        String str = callableStatement.getString(i);
        if (StringUtils.isBlank(str)){
            return null;
        }
        if (!JSONUtil.isJsonObj(str)){
            throw new SQLException("解析数据格式有误");
        }
        return (JSONObject)JSONObject.parse(str);
    }
}