|
@@ -12,6 +12,16 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import org.apache.ibatis.type.BaseTypeHandler;
|
|
import org.apache.ibatis.type.BaseTypeHandler;
|
|
import org.apache.ibatis.type.JdbcType;
|
|
import org.apache.ibatis.type.JdbcType;
|
|
|
|
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.sql.CallableStatement;
|
|
|
|
+import java.sql.PreparedStatement;
|
|
|
|
+import java.sql.ResultSet;
|
|
|
|
+import java.sql.SQLException;
|
|
|
|
+import java.util.List;import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
+import org.apache.ibatis.type.BaseTypeHandler;
|
|
|
|
+import org.apache.ibatis.type.JdbcType;
|
|
|
|
+
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
import java.sql.CallableStatement;
|
|
import java.sql.CallableStatement;
|
|
import java.sql.PreparedStatement;
|
|
import java.sql.PreparedStatement;
|
|
@@ -24,28 +34,28 @@ public class JsonTypeHandler<T> extends BaseTypeHandler<List<T>> {
|
|
private static final ObjectMapper objectMapper = new ObjectMapper();
|
|
private static final ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
|
|
@Override
|
|
@Override
|
|
- public void setNonNullParameter(PreparedStatement ps, int i, List<T> parameter, JdbcType jdbcType) throws SQLException {
|
|
|
|
|
|
+ public void setNonNullParameter(PreparedStatement preparedStatement, int i, List<T> list, JdbcType jdbcType) throws SQLException {
|
|
try {
|
|
try {
|
|
- String json = objectMapper.writeValueAsString(parameter);
|
|
|
|
- ps.setString(i, json);
|
|
|
|
|
|
+ String json = objectMapper.writeValueAsString(list);
|
|
|
|
+ preparedStatement.setString(i, json);
|
|
} catch (JsonProcessingException e) {
|
|
} catch (JsonProcessingException e) {
|
|
- throw new SQLException("Error converting object to JSON string", e);
|
|
|
|
|
|
+ throw new SQLException("Error converting object to JSON", e);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
- public List<T> getNullableResult(ResultSet rs, String columnName) throws SQLException {
|
|
|
|
- return parseJson(rs.getString(columnName));
|
|
|
|
|
|
+ public List<T> getNullableResult(ResultSet resultSet, String s) throws SQLException {
|
|
|
|
+ return parseJson(resultSet.getString(s));
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
- public List<T> getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
|
|
|
|
- return parseJson(rs.getString(columnIndex));
|
|
|
|
|
|
+ public List<T> getNullableResult(ResultSet resultSet, int i) throws SQLException {
|
|
|
|
+ return parseJson(resultSet.getString(i));
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
- public List<T> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
|
|
|
|
- return parseJson(cs.getString(columnIndex));
|
|
|
|
|
|
+ public List<T> getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
|
|
|
|
+ return parseJson(callableStatement.getString(i));
|
|
}
|
|
}
|
|
|
|
|
|
private List<T> parseJson(String json) throws SQLException {
|
|
private List<T> parseJson(String json) throws SQLException {
|
|
@@ -55,7 +65,7 @@ public class JsonTypeHandler<T> extends BaseTypeHandler<List<T>> {
|
|
try {
|
|
try {
|
|
return objectMapper.readValue(json, List.class);
|
|
return objectMapper.readValue(json, List.class);
|
|
} catch (IOException e) {
|
|
} catch (IOException e) {
|
|
- throw new SQLException("Error converting JSON string to object", e);
|
|
|
|
|
|
+ throw new SQLException("Error converting JSON to object", e);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|