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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM