文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

mybatis-plus读取JSON类型并处理JSON中数据

2023-09-04 07:27

关注

mybatis-plus读取JSON类型

本文总共三个步骤:
1、在数据库表定义JSON字段;
2、在实体类加上@TableName(value = “extra_info”, autoResultMap = true)、在JSON字段映射的属性加上
@TableField(typeHandler = JacksonTypeHandler.class);

@TableField(typeHandler = ObjectAndJsonHandler.class);
3、建一些业务代码进行测试;

在数据库表定义JSON字段

DROP TABLE IF EXISTS `extra_info`;CREATE TABLE `extra_info`  (  `id` int(0) NOT NULL AUTO_INCREMENT,  `extra_json` json NULL,  `extra_object` json NULL,  `extra_list` json NULL,  `extra_array` json NULL,  PRIMARY KEY (`id`) USING BTREE) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;INSERT INTO `extra_info` VALUES (1, '{\"id\": 6, \"name\": \"7\"}', '{\"id\": 1, \"name\": \"2\"}', '[{\"id\": 1, \"name\": \"2\"}, {\"id\": 2, \"name\": \"3\"}]', '[{\"id\": 1, \"name\": \"2\"}, {\"id\": 4, \"name\": \"5\"}]');

实体类处理

在实体类加上@TableName(value = "extra_info", autoResultMap = true)、在JSON字段映射的属性加上@TableField(typeHandler = JacksonTypeHandler.class),@TableField(typeHandler = ObjectAndJsonHandler.class) JacksonTypeHandler为mybatisplus自带,ObjectAndJsonHandler自定义;

ExtraInfo,ExtraNode

import com.baomidou.mybatisplus.annotation.IdType;import com.baomidou.mybatisplus.annotation.TableField;import com.baomidou.mybatisplus.annotation.TableId;import com.baomidou.mybatisplus.annotation.TableName;import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;import com.util.ObjectAndJsonHandler;import lombok.Data;import java.io.Serializable;import java.util.List;@Data@TableName(value = "extra_info", autoResultMap = true)public class ExtraInfo implements Serializable {    @TableId(type = IdType.AUTO)    private Integer id;    @TableField(typeHandler = JacksonTypeHandler.class)    private ExtraNode extraObject;    @TableField(typeHandler = FastjsonTypeHandler.class)    private JSONObject extraJson;    @TableField(typeHandler = ObjectAndJsonHandler.class)    private List<ExtraNode> extraList;    @TableField(typeHandler = JacksonTypeHandler.class)    private ExtraNode[] extraArray;}
import lombok.Data;@Datapublic class ExtraNode {private Integer id;private String name;}

业务代码

dao

@Mapperpublic interface ExtraInfoDao extends BaseMapper<ExtraInfo> {}

service

public interface ExtraInfoService extends IService<ExtraInfo> {List<ExtraInfo> selectAll();}

serviceImpl

@Service("extraInfoService")public class ExtraInfoServiceImpl extends ServiceImpl<ExtraInfoDao, ExtraInfo> implements ExtraInfoService {@AutowiredExtraInfoDao extraInfoDao;@Overridepublic List<ExtraInfo> selectAll() {return extraInfoDao.selectList(null);}}

工具类

import com.fasterxml.jackson.core.type.TypeReference;import org.apache.ibatis.type.BaseTypeHandler;import org.apache.ibatis.type.JdbcType;import org.apache.ibatis.type.MappedJdbcTypes;import java.sql.CallableStatement;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;@MappedJdbcTypes({JdbcType.VARCHAR})public class ObjectAndJsonHandler extends BaseTypeHandler<Object> {    @Override    public void setNonNullParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType) throws SQLException {        String json = GenericAndJson.objectToJson(parameter);        ps.setString(i, json);    }    @Override    public Object getNullableResult(ResultSet rs, String columnName) throws SQLException {        String string = rs.getString(columnName);        return GenericAndJson.jsonToObject(string, new TypeReference<Object>() {        });    }    @Override    public Object getNullableResult(ResultSet rs, int columnIndex) throws SQLException {        String string = rs.getString(columnIndex);        return GenericAndJson.jsonToObject(string, new TypeReference<Object>() {        });    }    @Override    public Object getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {        String string = cs.getString(columnIndex);        return GenericAndJson.jsonToObject(string, new TypeReference<Object>() {        });    }}
import com.fasterxml.jackson.core.JsonProcessingException;import com.fasterxml.jackson.core.type.TypeReference;import com.fasterxml.jackson.databind.ObjectMapper;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;@Componentpublic class GenericAndJson {    private static ObjectMapper mapper;    @Autowired    public void setMapper(ObjectMapper mapper) {        GenericAndJson.mapper = mapper;    }    public static <T> String objectToJson(T o) {        try {            return GenericAndJson.mapper.writeValueAsString(o);        } catch (Exception e) {            e.printStackTrace();            throw new RuntimeException();        }    }    public static <T> T jsonToObject(String s, TypeReference<T> typeReference) {        if (s == null) {            return null;        }        try {            return GenericAndJson.mapper.readValue(s, typeReference);        } catch (JsonProcessingException e) {            e.printStackTrace();            throw new RuntimeException();        }    }}

mapper.xml

DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.dao.ExtraInfoDao">    <resultMap type="com.entity.ExtraInfo" id="extraInfoMap">        <result property="id" column="id"/>        <result property="extraObject" column="extra_object" typeHandler="com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler"/>        <result property="extraJson" column="extra_json" typeHandler="com.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler"/>        <result property="extraList" column="extra_list" typeHandler="com.hftp.mq.util.ObjectAndJsonHandler"/>        <result property="extraArray" column="extra_array" typeHandler="com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler"/>    resultMap>mapper>

测试

import org.codehaus.jackson.map.ObjectMapper;import org.codehaus.jackson.type.TypeReference;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.util.ArrayList;import java.util.Arrays;import java.util.List;import java.util.stream.Collectors;@RestController@RequestMapping("/test")public class TestExtraInfoController {@Autowiredprivate ExtraInfoService extraInfoMapper;@GetMappingpublic List<ExtraNode> listAll() {List<ExtraNode> listExtraNode = new ArrayList<>();ExtraNode[] strArr = new ExtraNode[0];String str = null;String str2 = null;List<ExtraInfo> listExtraInfo = extraInfoMapper.selectAll();for (ExtraInfo extraInfo : listExtraInfo) {listExtraNode = extraInfo.getExtraList();strArr = extraInfo.getExtraArray();extraNode = extraInfo.getExtraObject();str2 = extraInfo.getExtraJson().get("name").toString();}System.out.println(extraNode.getName());System.out.println(str2);//类型转换异常:java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.所以要用下面方法解决ObjectMapper mapper = new ObjectMapper();List<ExtraNode> list = mapper.convertValue(listExtraNode, new TypeReference<List<ExtraNode>>() {});str = list.stream().filter(f -> f.getId() == 2).map(ExtraNode::getName).collect(Collectors.joining(""));System.out.println(str);List<ExtraNode> listExtraNodeArr = new ArrayList<>(Arrays.asList(strArr));System.out.println(listExtraNodeArr.stream().map(ExtraNode::getName).collect(Collectors.joining(",")));return listExtraNode;}}

来源地址:https://blog.csdn.net/qnxyj/article/details/123095384

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     801人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     348人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     311人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     432人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     220人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-数据库
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯