Newer
Older
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
package com.cesgroup.practice.handler;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.alibaba.fastjson.JSONObject;
public class JSONHandler extends BaseTypeHandler<JSONObject> {
@Override
public void setNonNullParameter(PreparedStatement preparedStatement, int i, JSONObject jsonObject, JdbcType jdbcType) throws SQLException {
try {
String userInfoString= JSONObject.toJSONString(jsonObject);
preparedStatement.setString(i, userInfoString);
} catch (Exception e) {
System.out.println("----将对象转化为字符串失败----");
}
}
@Override
public JSONObject getNullableResult(ResultSet resultSet, String s) throws SQLException {
String infoJsonString= resultSet.getString(s);
JSONObject infObject=null;
try {
infObject = JSONObject.parseObject(infoJsonString);
//System.out.println("----将json串转成json对象成功!----");
} catch (Exception e) {
System.err.println("将json串转成json对象失败!");;
}
return infObject;
}
@Override
public JSONObject getNullableResult(ResultSet resultSet, int i) throws SQLException {
return null;
}
@Override
public JSONObject getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
return null;
}
}