参考:
https://blog.csdn.net/qq_34382367/article/details/88890260 https://www.cnblogs.com/ceshi2016/p/7381478.html
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>2.0.11version>
dependency>
继承BaseTypeHandler,泛型为fastJson中的JSONObject
参考如下,可直接复制粘贴
该类放到com.xxx.xxx.handler下即可,handler包与controller包同级。
import com.alibaba.fastjson.JSONObject;
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;
/**
* @Project
* @Autor
* @Create 2019/7/16 1:32 PM
* @Description
*/
@MappedTypes(JSONObject.class)
@MappedJdbcTypes(JdbcType.VARCHAR)
public class MySqlJsonHandler extends BaseTypeHandler<JSONObject>{
/**
* 设置非空参数
* @param ps
* @param i
* @param parameter
* @param jdbcType
* @throws SQLException
*/
@Override
public void setNonNullParameter(PreparedStatement ps, int i, JSONObject parameter, JdbcType jdbcType) throws SQLException {
ps.setString(i,String.valueOf(parameter.toJSONString()));
}
/**
* 根据列名,获取可以为空的结果
* @param rs
* @param columnName
* @return
* @throws SQLException
*/
@Override
public JSONObject getNullableResult(ResultSet rs, String columnName) throws SQLException {
String sqlJson = rs.getString(columnName);
if (null != sqlJson) {
return JSONObject.parseObject(sqlJson);
}
return null;
}
/**
* 根据列索引,获取可以为内控的接口
* @param rs
* @param columnIndex
* @return
* @throws SQLException
*/
@Override
public JSONObject getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
String sqlJson = rs.getString(columnIndex);
if (null != sqlJson) {
return JSONObject.parseObject(sqlJson);
}
return null;
}
/**
*
* @param cs
* @param columnIndex
* @return
* @throws SQLException
*/
@Override
public JSONObject getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
String sqlJson = cs.getNString(columnIndex);
if (null != sqlJson) {
return JSONObject.parseObject(sqlJson);
}
return null;
}
}
mybatis:
# 指定实体类
type-aliases-package: com.emergency.templatemanager.entity
# 指定mapper映射配置包
mapper-locations: classpath:mybatis/mapper/*.xml
# 指定handler的位置
type-handlers-package: com.emergency.templatemanager.handler
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class TemplateContent {
Integer tid;
JSONObject directoryStructure;
JSONObject specialHeadquartersData;
JSONObject fieldHeadquartersData;
JSONObject earlyWarningResponseData;
JSONObject emergencyResponseData;
}
关注两个点
<resultMap type="com.emergency.templatemanager.entity.TemplateContent" id="TemplateContentResultMap">
<result property="tid" column="tid"/>
<result property="directoryStructure" column="directory_structure"/>
<result property="specialHeadquartersData" column="special_headquarters_data" typeHandler="com.emergency.templatemanager.handler.MySqlJsonHandler"/>
<result property="fieldHeadquartersData" column="field_headquarters_data" typeHandler="com.emergency.templatemanager.handler.MySqlJsonHandler"/>
<result property="earlyWarningResponseData" column="early_warning_response_data" typeHandler="com.emergency.templatemanager.handler.MySqlJsonHandler"/>
<result property="emergencyResponseData" column="emergency_response_data" typeHandler="com.emergency.templatemanager.handler.MySqlJsonHandler"/>
resultMap>
<insert id="newTemplateContent" parameterType="com.emergency.templatemanager.entity.TemplateContent">
insert into templateContent
(tid,directory_structure,special_headquarters_data,field_headquarters_data,early_warning_response_data,emergency_response_data)
values(#{templateContent.tid},
#{templateContent.directoryStructure, typeHandler=com.emergency.templatemanager.handler.MySqlJsonHandler},
#{templateContent.specialHeadquartersData, typeHandler=com.emergency.templatemanager.handler.MySqlJsonHandler},
#{templateContent.fieldHeadquartersData, typeHandler=com.emergency.templatemanager.handler.MySqlJsonHandler},
#{templateContent.earlyWarningResponseData, typeHandler=com.emergency.templatemanager.handler.MySqlJsonHandler},
#{templateContent.emergencyResponseData, typeHandler=com.emergency.templatemanager.handler.MySqlJsonHandler})
insert>
Map map = new HashMap();
map.put("1", 123);
map.put("2", "222");
map.put("3", new HashMap());
JSONObject object = JSONObject.parseObject(JSON.toJSONString(map));
templateDao.newTemplateContent(new TemplateContent(1, object,object,object,object,object));
mapper:ResultMap上面有写到,注意每个json类型的字段都要指定typeHandler
<select id="getTemplatesContent" resultMap="TemplateContentResultMap">
select tid,directory_structure,special_headquarters_data,field_headquarters_data,early_warning_response_data,emergency_response_data
from templateContent
select>
获取json对象,可直接操作键值对
List<TemplateContent> list = templateDao.getTemplatesContent();
list.stream().forEach((e) -> {
JSONObject json = e.getDirectoryStructure();
System.out.println(json.get("1"));
});