SpringBoot+MYSQL 配置支持json数据格式

自定义handler处理器

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.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import com.alibaba.fastjson.JSONObject;

/**
 * mysql中json类型转换扩展
 * 
 * @author: xiaowei.go
 * @date: 2019年11月20日
 * 
 */
@MappedTypes(JSONObject.class)
@MappedJdbcTypes(JdbcType.VARCHAR)
public class MySqlJsonHandler extends BaseTypeHandler<JSONObject> {

	/**
	 * 设置非空参数
	 * 
	 * @author: xiaowei.go
	 * @date: 2019年11月20日
	 * @see org.apache.ibatis.type.BaseTypeHandler#setNonNullParameter(java.sql.PreparedStatement,
	 *      int, java.lang.Object, org.apache.ibatis.type.JdbcType)
	 */
	@Override
	public void setNonNullParameter(PreparedStatement ps, int i, JSONObject parameter,
			JdbcType jdbcType) throws SQLException {
		ps.setString(i, String.valueOf(parameter.toJSONString()));
	}

	/**
	 * 根据列名,获取可以为空的结果
	 * 
	 * @author: xiaowei.go
	 * @date: 2019年11月20日
	 * @see org.apache.ibatis.type.BaseTypeHandler#getNullableResult(java.sql.ResultSet,
	 *      java.lang.String)
	 */
	@Override
	public JSONObject getNullableResult(ResultSet rs, String columnName) throws SQLException {
		String sqlJson = rs.getString(columnName);
		if (null != sqlJson) {
			return JSONObject.parseObject(sqlJson);
		}
		return null;
	}

	/**
	 * 根据列索引,获取可以为空的结果
	 * 
	 * @author: xiaowei.go
	 * @date: 2019年11月20日
	 * @see org.apache.ibatis.type.BaseTypeHandler#getNullableResult(java.sql.ResultSet, int)
	 */
	@Override
	public JSONObject getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
		String sqlJson = rs.getString(columnIndex);
		if (null != sqlJson) {
			return JSONObject.parseObject(sqlJson);
		}
		return null;
	}

	/**
	 * 根据列索引,获取可以为空的结果
	 * 
	 * @author: xiaowei.go
	 * @date: 2019年11月20日
	 * @see org.apache.ibatis.type.BaseTypeHandler#getNullableResult(java.sql.CallableStatement, int)
	 */
	@Override
	public JSONObject getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
		String sqlJson = cs.getString(columnIndex);
		if (null != sqlJson) {
			return JSONObject.parseObject(sqlJson);
		}
		return null;
	}

}

实体类

代码段

import com.alibaba.fastjson.JSONObject;

@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class BizExportJob implements Serializable {
    private JSONObject dataRule;
}

map.xml配置

<!-- resultMap中配置实体类json属性字段 -->
<resultMap id="ResultMap" type="com.magicpose.entity.domain.BizExportJob">
    <result column="data_rule" property="dataRule" typeHandler="com.magicpose.handler.MySqlJsonHandler"/>
</resultMap>
<!-- 插入时指定rule的typeHandler -->
<insert id="save">
    insert into table (data_rule) values (#{dataRule,jdbcType=OTHER,typeHandler=com.magicpose.handler.MySqlJsonHandler})
</insert>

注册Handler

2种方式

在application.yml声明handler包

mybatis:
  config-location: classpath:mybatis-config.xml
  mapperLocations: classpath:mappers/*.xml
      #配置mybaits自定义类型转换类所在的包
  type-handlers-package: com.magicpose.handler

在mybatis-config.xml中注册该Handler

<typeHandlers>
  <typeHandler handler="com.tudou.gbf.handler.MySqlJsonHandler" javaType="" />
</typeHandlers>

附:优化版

既然自定义handler处理字段值,当然可以直接转化为java bean.

// 注册器如下定义,其他方法参考返回为JSONObject类型的,转化为Class
public class MySqlJsonHandler<T extends Object> extends BaseTypeHandler<T>{
    private Class<T> clazz;
}

使用mybatis-config.xml中注册该Handler,增加 javaType属性,指定该处理器转换的java bean

<typeHandlers>
  <typeHandler handler="com.tudou.gbf.handler.MySqlJsonHandler" javaType="com.tudou.gbf.entity.DataRule" />
</typeHandlers>

记得实体类的属性类型修改为对应的java bean

posted @ 2019-11-20 10:26  xiaowei丶go  阅读(4439)  评论(0编辑  收藏  举报