业务数据在存储的时候,并不因此mysql中的 varchar 丶 int 等格式来存储的,而是利用 json 格式.
这样做的好处是当数据构造有变革或者需求变革时,我们不用再添加字段,方便扩展.
那么mysql中 json 格式的数据我们利用mybatis或者mybatisplus读取往后,肯定是希望转换成工具或者工具凑集的,本篇博文就记录一下利用 mybatis 读取 json 格式的数据转换为工具的方法

首先我们如果要利用sql语句来在表中添加一个 json 格式的字段,可以利用以下脚本:
ALTER TABLE a ADD COLUMN alert_up_config json DEFAULT NULL COMMENT '注释' AFTER id
以上sql实行往后,会在 a 表的 id 字段之后,添加一个字段 alert_up_config ,它的格式便是 json
添加完成往后,我们可以在这个字段中保存json工具或者json数组格式的字符串.
当前的业务场景是: alert_up_config 字段中,是个jsonArr,保存的是告警的升级规则,比如持续韶光,次数等.
仿照数据如下:
[{"alertNum": 100, "alertLevel": "P1", "durationTime": 100, "resourceInfoId": "123456"}]
把稳,利用json类型有2点须要把稳:
存储数据必须是json格式,否则会报错json数据类型没有默认值三. 数据处理1. 利用sql语句获取json数据首先我们可以利用sql语句来提取我们须要的json字段,语法如下:
#json工具类型处理JSON_EXTRACT(json列, '$.键')#jsonArray类型处理, index从0开始打算JSON_EXTRACT(json列, '$[index].键')
比如对付我们上面的示例数据,我们要 查询出alert_up_config 字段中的 alertNum 的值,可以利用如下语句:
select JSON_EXTRACT(alert_up_config, '$[0].alertNum') from a;
查询结果如下:
2. 利用mybatis&mybatisPlus来读取数据库中的json数据
首先我们须要知道的是,mybatis供应了一个接口 org.apache.ibatis.type.TypeHandler 来对数据库中各种类型数据的处理,它紧张有四个方法:
public interface TypeHandler<T> { //保存数据到数据库之前的处理 void setParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException; //下面三个时候拿到数据往后的处理 T getResult(ResultSet rs, String columnName) throws SQLException; T getResult(ResultSet rs, int columnIndex) throws SQLException; T getResult(CallableStatement cs, int columnIndex) throws SQLException;}
也便是说,如果我们要处理从 mysql 中取出的数据,就可以实现这个接口,来自定义我们的数据处理逻辑,得到我们想要的数据.
现在对付我们来说,我们的源数据是这样的:
[{"alertNum": 100, "alertLevel": "P1", "durationTime": 100, "resourceInfoId": "123456"}]
也有可能是这样的:
{"alertNum": 100, "alertLevel": "P1", "durationTime": 100, "resourceInfoId": "123456"}
前者是 JSONArray ,后者是 JSONObject
对付前面的数据,我们可能会用 List<AlertUpConfig> 这种格式来吸收,如果是后者,我们可以利用 AlertUpConfig 这种工具来直接吸收数据.
现在,我们的须要mybatis或者mybatisplus帮助我们将数据库中的 json 数据自动转换为相应的格式,一起来看下怎么实现吧!
3. 利用mybatisplus实现json转java工具/凑集在mybatisplus中,已经为我们供应了一系列的 TypeHandler
有一个抽象类为: com.baomidou.mybatisplus.extension.handlers.AbstractJsonTypeHandler
它抽象了json处理的方法:
public void setNonNullParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException { ps.setString(i, this.toJson(parameter)); } public T getNullableResult(ResultSet rs, String columnName) throws SQLException { String json = rs.getString(columnName); return StringUtils.isBlank(json) ? null : this.parse(json); } public T getNullableResult(ResultSet rs, int columnIndex) throws SQLException { String json = rs.getString(columnIndex); return StringUtils.isBlank(json) ? null : this.parse(json); } public T getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { String json = cs.getString(columnIndex); return StringUtils.isBlank(json) ? null : this.parse(json); }
同时,它供应两个抽象方法,用于供应给不同的json包:
protected abstract T parse(String json); protected abstract String toJson(T obj);
我们看一下 AbstractJsonTypeHandler 的实现类,可以创造有我们想要的东西:
很明显,mybatisplus已经供应了 FastJson 、 Gson 和 JackSon 的typehandler给我们利用.
下面是利用方法:
json转java工具如果不用 xml 文件(xml的办法在mybatis部分解释),在实体工具相应的字段上,用 typeHandler 参数指定我们要利用的 typeHandler同时,在实体类上利用表明: @TableName(value = "a", autoResultMap = true)autoResultMap 参数表明字段在查询处理的时候自动转换为工具@TableField(value = "alert_up_config", typeHandler = FastjsonTypeHandler.class) private AlertUpConfig alertUpConfig;配置好往后,利用mapper查询,可以创造 alert_up_config 字段直接被转换成了 java工具json转List4. 利用mybatis在xml中实现json转java工具/凑集在xml中实现json和工具的转换也比较大略
<result column="alert_up_config" property="alertUpConfig" jdbcType="VARCHAR" javaType="com.xxxx.AlertUpConfig" typeHandler="com.xxxx.AlertUpConfigHandler"/>
同样的handler,只是配置办法从表明改成xml中的属性了.
须要把稳的是, FastjsonTypeHandler 是mybatisplus供应的处理器,如果你的项目中没有引入mp,那么可以自己参照它写一个.