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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package com.cesgroup.config;
import com.cesgroup.entity.ContentJson;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import java.io.IOException;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* 自定义类型处理器
* Created by chenpt on 2019/12/2.
*/
@MappedJdbcTypes(JdbcType.VARCHAR)
@MappedTypes(ContentJson.class)
@Slf4j
public class JSONHandler<T extends Object> extends BaseTypeHandler<T> {
private static final ObjectMapper mapper = new ObjectMapper();
private Class<T> clazz;
static {
mapper.configure(JsonParser.Feature.ALLOW_MISSING_VALUES, false);
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
}
public JSONHandler(Class<T> clazz) {
if (clazz == null) {
throw new NullPointerException("Type argument cannot be null");
}
this.clazz = clazz;
}
/**
* object转json string
* @param object
* @return
*/
private String toJSON(T object) {
try {
String string = mapper.writeValueAsString(object);
log.info(">>>> json handler string:{} <<<<",string);
return string;
} catch (Exception e) {
log.error(">>>> covert object to json string failed, error message: <<<<",e.getMessage());
}
return null;
}
/**
* json转object
* @param json
* @param clazz
* @return
* @throws IOException
*/
private T toObject(String json, Class<T> clazz) throws IOException {
if (json != null && json != "") {
return mapper.readValue(json,clazz);
}
return null;
}
@Override
public void setNonNullParameter(PreparedStatement preparedStatement, int i, T t, JdbcType jdbcType) throws SQLException {
try {
preparedStatement.setString(i,toJSON(t));
} catch (Exception e) {
log.error(">>>> preparedStatement set string failed, error message:{} <<<<",e.getMessage());
}
}
@Override
public T getNullableResult(ResultSet resultSet, String s) throws SQLException {
try {
return toObject(resultSet.getString(s), clazz);
} catch (IOException e) {
log.error(">>>> convert json string to object failed, error message:{} <<<<",e.getMessage());
}
return null;
}
@Override
public T getNullableResult(ResultSet resultSet, int i) throws SQLException {
try {
return toObject(resultSet.getString(i), clazz);
} catch (IOException e) {
log.error(">>>> convert json string to object failed, error message:{} <<<<",e.getMessage());
}
return null;
}
@Override
public T getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
try {
return toObject(callableStatement.getString(i), clazz);
} catch (IOException e) {
log.error(">>>> convert json string to object failed, error message:{} <<<<",e.getMessage());
}
return null;
}
}