mysql 5.7后新增了一個json類型字段,以往json入庫都是轉字符串,取到前端造成了不少困擾。今天就做了個小例子把這個整合到ssm例子中。
這里插句題外話,因為最近開始改用idea,配置項目的時候出了一大堆問題,這邊也順便說下如果idea在啟動tomcat客戶端控制台出現亂碼處理辦法
打開idea安裝目錄-bin
用記事本打開idea.exe.vmoptions和idea64.exe.vmoptions文件
在文件后面添加一行:-Dfile.encoding=UTF-8
好了進入整體
第一步先配置一個typehandler,代碼如下
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig.Feature;
import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;
/**
* mapper里json型字段到類的映射。
* 用法一:
* 入庫:#{jsonDataField, typeHandler=com.adu.spring_test.mybatis.typehandler.JsonTypeHandler}
* 出庫:
* <resultMap>
* <result property="jsonDataField" column="json_data_field" javaType="com.xxx.MyClass" typeHandler="com.adu.spring_test.mybatis.typehandler.JsonTypeHandler"/>
* </resultMap>
*
* 用法二:
* 1)在mybatis-config.xml中指定handler:
* <typeHandlers>
* <typeHandler handler="com.adu.spring_test.mybatis.typehandler.JsonTypeHandler" javaType="com.xxx.MyClass"/>
* </typeHandlers>
* 2)在MyClassMapper.xml里直接select/update/insert。
*
*
* @author yunjie.du
* @date 2016/5/31 19:33
*/
public class JsonTypeHandler<T extends Object> extends BaseTypeHandler<T> {
private static final ObjectMapper mapper = new ObjectMapper();
private Class<T> clazz;
public JsonTypeHandler(Class<T> clazz) {
if (clazz == null) throw new IllegalArgumentException("Type argument cannot be null");
this.clazz = clazz;
}
@Override
public void setNonNullParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException {
ps.setString(i, this.toJson(parameter));
}
@Override
public T getNullableResult(ResultSet rs, String columnName) throws SQLException {
return this.toObject(rs.getString(columnName), clazz);
}
@Override
public T getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
return this.toObject(rs.getString(columnIndex), clazz);
}
@Override
public T getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
return this.toObject(cs.getString(columnIndex), clazz);
}
private String toJson(T object) {
try {
return mapper.writeValueAsString(object);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private T toObject(String content, Class<?> clazz) {
if (content != null && !content.isEmpty()) {
try {
return (T) mapper.readValue(content, clazz);
} catch (Exception e) {
throw new RuntimeException(e);
}
} else {
return null;
}
}
static {
mapper.configure(Feature.WRITE_NULL_MAP_VALUES, false);
mapper.setSerializationInclusion(Inclusion.NON_NULL);
}
mapper代碼
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cm.dao.UserDao">
<resultMap id="user" type="com.cm.model.UserModel">
<id column="id" jdbcType="NUMERIC" property="id"/>
<result column="name" jdbcType="VARCHAR" property="name"/>
<result column="age" jdbcType="NUMERIC" property="age"/>
<result column="hobby" jdbcType="NUMERIC" property="hobby" typeHandler="com.cm.mybaits.JsonTypeHandler"/>
</resultMap>
<select id="getAllUsers" resultMap="user">
select * from user
</select>
<insert id="addUser">
<!--ignore忽略自動增長的主鍵id-->
insert ignore into user (name, age, hobby) values (#{id}, #{name} ,#{hobby, typeHandler=com.cm.mybaits.JsonTypeHandler})
</insert>
<update id="updateUser">
update user set name=#{name} where id=#{id}
</update>
<delete id="deleteUser" parameterType="String">
delete from user where id=#{id}
</delete>
<select id="getUser" resultType="UserModel">
select * from user where id = #{id}
</select>
</mapper>
mysql表結構

插入的測試代碼

效果預覽
入庫

取數據

這邊有個坑是mysql 驅動一定要5.1.40,不然取出來的json中文是亂碼。雖然說是低於5.1.36會亂碼,但是我試了5.1.6還是亂碼。
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
</dependency>
最后附上我的項目是一個SSM框架的小demo,有興趣的小伙伴可以下載參考
鏈接:https://pan.baidu.com/s/14yaYRg-fXeyaPN7oWU7-Ow
提取碼:dggg
