|
@@ -0,0 +1,65 @@
|
|
|
+package org.hsweb.web.mybatis.handler;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import org.apache.ibatis.type.*;
|
|
|
+
|
|
|
+import java.sql.CallableStatement;
|
|
|
+import java.sql.PreparedStatement;
|
|
|
+import java.sql.ResultSet;
|
|
|
+import java.sql.SQLException;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Created by zhouhao on 16-5-14.
|
|
|
+ */
|
|
|
+@Alias("jsonMapHandler")
|
|
|
+@MappedTypes({Map.class})
|
|
|
+@MappedJdbcTypes({JdbcType.VARCHAR, JdbcType.CLOB})
|
|
|
+public class JsonMapHandler extends BaseTypeHandler<Map<String, Object>> {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Map<String, Object> getResult(ResultSet rs, int columnIndex) throws SQLException {
|
|
|
+ String s = rs.getString(columnIndex);
|
|
|
+ return JSON.parseObject(s);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Map<String, Object> getResult(ResultSet rs, String columnName) throws SQLException {
|
|
|
+ String s = rs.getString(columnName);
|
|
|
+ return JSON.parseObject(s);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Map<String, Object> getResult(CallableStatement cs, int columnIndex) throws SQLException {
|
|
|
+ String s = cs.getString(columnIndex);
|
|
|
+ return JSON.parseObject(s);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void setParameter(PreparedStatement ps, int i, Map<String, Object> parameter, JdbcType jdbcType) throws SQLException {
|
|
|
+ ps.setString(i, JSON.toJSONString(parameter));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void setNonNullParameter(PreparedStatement ps, int i, Map<String, Object> parameter, JdbcType jdbcType) throws SQLException {
|
|
|
+ ps.setString(i, "{}");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Map<String, Object> getNullableResult(ResultSet rs, String columnName) throws SQLException {
|
|
|
+ return new HashMap<>();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Map<String, Object> getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
|
|
|
+ return new HashMap<>();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Map<String, Object> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
|
|
|
+ return new HashMap<>();
|
|
|
+ }
|
|
|
+}
|