|
@@ -0,0 +1,55 @@
|
|
|
+package com.fdkankan.fusion.typehandle;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+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.sql.CallableStatement;
|
|
|
+import java.sql.PreparedStatement;
|
|
|
+import java.sql.ResultSet;
|
|
|
+import java.sql.SQLException;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 存储到数据库, 将JSON数组对象转换成字符串;
|
|
|
+ * 从数据库获取数据, 将字符串转为JSON数组对象.
|
|
|
+ */
|
|
|
+@MappedTypes({JSONObject.class})
|
|
|
+@MappedJdbcTypes({JdbcType.VARCHAR})
|
|
|
+public class JsonObjTypeHandler extends BaseTypeHandler<JSONObject> {
|
|
|
+ @Override
|
|
|
+ public void setNonNullParameter(PreparedStatement ps, int i, JSONObject parameter,
|
|
|
+ JdbcType jdbcType) throws SQLException {
|
|
|
+ ps.setString(i, JSONObject.toJSONString(parameter));
|
|
|
+ }
|
|
|
+ @Override
|
|
|
+ public JSONObject getNullableResult(ResultSet rs, String columnName)
|
|
|
+ throws SQLException {
|
|
|
+ return JSONObject.parseObject(rs.getString(columnName));
|
|
|
+ }
|
|
|
+ @Override
|
|
|
+ public JSONObject getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
|
|
|
+ return parseArray(rs.getString(columnIndex));
|
|
|
+ }
|
|
|
+ @Override
|
|
|
+ public JSONObject getNullableResult(CallableStatement cs, int columnIndex)
|
|
|
+ throws SQLException {
|
|
|
+ return parseArray(cs.getString(columnIndex));
|
|
|
+ }
|
|
|
+
|
|
|
+ public static JSONObject parseArray(String content) {
|
|
|
+ if(StringUtils.isBlank(content)){
|
|
|
+ return new JSONObject();
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ return JSONObject.parseObject(content);
|
|
|
+ } catch (Exception e) {
|
|
|
+ return new JSONObject();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|