• Mybatisplus读取JSON类型


    Mybatis-plus读取JSON类型

    版本:

    <dependency>
          <groupId>com.baomidou</groupId>
          <artifactId>mybatis-plus-boot-starter</artifactId>
          <version>3.3.1.tmp</version>
        </dependency>

    一、Mybatis基本查询

    共三个步骤:

    1、在数据库表定义JSON字段;
    2、在实体类加上@TableName(autoResultMap = true)、在JSON字段映射的属性加上@TableField(typeHandler = FastjsonTypeHandler.class);
    3、建一些业务代码进行测试;

    • 在数据库表定义JSON字段

    CREATE TABLE `extra_info`  (
       `id` int(10) NOT NULL AUTO_INCREMENT PRIMARY KEY,
       `extra_object` json NULL,
       `extra_list` json NULL,
       `extra_array` json NULL
     );
    
    INSERT INTO `extra_info` VALUES (1, '{\"id\": 1, \"name\": \"2\"}', '[{\"id\": 1, \"name\": \"2\"}]', '[{\"id\": 1, \"name\": \"2\"}]');
    • 在实体类加上@TableName(autoResultMap = true)、在JSON字段映射的属性加上@TableField(typeHandler = FastjsonTypeHandler.class)

    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.FastjsonTypeHandler;
    
    import java.io.Serializable;
    import java.util.List;
    
    @TableName(autoResultMap = true)
    public class ExtraInfo implements Serializable {
    
        @TableId(type = IdType.AUTO)
        private Integer id;
    
        @TableField(typeHandler = FastjsonTypeHandler.class)
        private ExtraNode extraObject;
    
        @TableField(typeHandler = FastjsonTypeHandler.class)
        private List<ExtraNode> extraList;
    
        @TableField(typeHandler = FastjsonTypeHandler.class)
        private ExtraNode[] extraArray;
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public ExtraNode getExtraObject() {
            return extraObject;
        }
    
        public void setExtraObject(ExtraNode extraObject) {
            this.extraObject = extraObject;
        }
    
        public List<ExtraNode> getExtraList() {
            return extraList;
        }
    
        public void setExtraList(List<ExtraNode> extraList) {
            this.extraList = extraList;
        }
    
        public ExtraNode[] getExtraArray() {
            return extraArray;
        }
    
        public void setExtraArray(ExtraNode[] extraArray) {
            this.extraArray = extraArray;
        }
    }
    • 建一些业务代码进行测试

    import java.io.Serializable;
    
    public class ExtraNode implements Serializable {
    
        private Integer id;
        private String name;
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    }
    import com.baomidou.mybatisplus.core.mapper.BaseMapper;
    import org.springframework.stereotype.Repository;
    
    @Repository
    public interface ExtraInfoMapper extends BaseMapper<ExtraInfo> {
    }
    import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
    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.List;
    
    @RestController
    @RequestMapping("/test")
    public class TestController {
    
        @Autowired
        private ExtraInfoMapper extraInfoMapper;
    
        @GetMapping
        public List<ExtraInfo> listAll() {
            return this.extraInfoMapper.selectList(new LambdaQueryWrapper<>());
        }
    }
    运行结果:
    
    [
      {
        "id": 1,
        "extraObject": { "id": 1, "name": "2" },
        "extraList": [
          { "name": "2", "id": 1 }
        ],
        "extraArray": [
          { "id": 1, "name": "2" }
        ]
      }
    ]

    二、nataive自义SQL查询

    1、在实体类加上@TableName(autoResultMap = true)、在JSON字段映射的属性加上@TableField(typeHandler = FastjsonTypeHandler.class);

    2、XXMapper.xml里json型字段到类的映射

    <resultMap>
         <result property="jsonDataField" column="json_data_field" jdbcType="OTHER" javaType="com.alibaba.fastjson.JSONObject" typeHandler="com.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler"/>
    </resultMap>

    实体类

    package cc.oyz.bean;
    
    import com.alibaba.fastjson.JSONObject;
    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.FastjsonTypeHandler;
    import io.swagger.annotations.ApiModelProperty;
    import lombok.Data;
    
    /**
     * @description: 辖区管理
     * @author: libin.hao
     * @time: 2021/4/8 15:35
     */
    @Data
    @TableName(autoResultMap = true)
    public class UrbanArea {
    
        @TableId(value = "id",type = IdType.AUTO)
        @ApiModelProperty(value = "主键ID",example="主键ID")
        private Integer id;
    
        private String name;
    
        private Double height;
    
        @ApiModelProperty(value = "地理数据")
        private String geog;
    
        @ApiModelProperty(value = "几何数据")
        private String geometry;
    
        @TableField(typeHandler = FastjsonTypeHandler.class)
        private JSONObject destination;
    
        @TableField(typeHandler = FastjsonTypeHandler.class)
        private JSONObject orientation;
    
    }

    Mapper.xml文件

    <?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="cc.oyz.mapper.UrbanAreaMapper" >
    
        <resultMap id="BaseResultMap" type="cc.oyz.bean.UrbanArea" >
            <id column="id" property="id" jdbcType="INTEGER" />
            <result column="name" property="name" jdbcType="VARCHAR" />
            <result column="height" property="height" jdbcType="DOUBLE" />
            <result column="geog" property="geog" jdbcType="VARCHAR" />
            <result column="geometry" property="geometry" jdbcType="VARCHAR" />
            <result column="destination" property="destination" jdbcType="OTHER" javaType="com.alibaba.fastjson.JSONObject" typeHandler="com.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler" />
            <result column="orientation" property="orientation" jdbcType="OTHER" javaType="com.alibaba.fastjson.JSONObject" typeHandler="com.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler" />
        </resultMap>
    
        <select id="selectOneByName" resultType="cc.oyz.bean.UrbanArea" resultMap="BaseResultMap">
            SELECT
                *,
                ST_AsGeoJson ( geog ) AS geometry
            FROM
                urban_area
            WHERE 1 = 1
            <if test="name != null and name != ''">
                AND name = #{name}
            </if>
        </select>
    
    </mapper>
  • 相关阅读:
    软件包管理器(bzoj 4196)
    Aragorn's Story(hdu3966)
    染色(bzoj 2243)
    文艺平衡树(bzoj 3223)
    区间翻转(codevs 3243)
    Python 中 configparser 配置文件的读写及封装,配置文件存放数据,方便修改
    Python 中 openpyxl 模块封装,读写 Excel 文件中自动化测试用例数据
    Python 中 unittest 框架加载测试用例的常用方法
    Python 中 unittest 单元测试框架中概念、作用、运行、用例执行顺序、断言方法、装饰器
    接口自动化的应用场景、测试用例、自动化流程
  • 原文地址:https://www.cnblogs.com/haolb123/p/16553076.html
Copyright © 2020-2023  润新知