广告
返回顶部
首页 > 资讯 > 数据库 >mybatis plus实体类中字段映射mysql中的json格式方式
  • 358
分享到

mybatis plus实体类中字段映射mysql中的json格式方式

2024-04-02 19:04:59 358人浏览 安东尼
摘要

目录mybatis plus实体类中字段映射Mysql中的JSON格式1.实体类中有个属性是其他对象2.那么取出时怎么进行映射呢,有分为两种情况mybatis-plus 实体 jso

mybatis plus实体类中字段映射mysql中的json格式

1.实体类中有个属性是其他对象

或者是List;在数据库中存储时使用的是mysql的json格式,此时可以用mybatis plus的一个注解


@TableField(typeHandler = JacksonTypeHandler.class) 

在这里插入图片描述


@TableField(typeHandler = JacksonTypeHandler.class)

这样在存入是就可以把对象自动转换为json格式,

2.那么取出时怎么进行映射呢,有分为两种情况

  • a:当没有使用到xml时:

在这里插入图片描述


@Data
@Accessors(chain = true)
@TableName(value = "wx_user",autoResultMap = true)
  • b:当使用了xml文件时:

mybatis-plus 实体 json 处理

本文总共三个步骤

  • 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" }
    ]
  }
]

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

您可能感兴趣的文档:

--结束END--

本文标题: mybatis plus实体类中字段映射mysql中的json格式方式

本文链接: https://www.lsjlt.com/news/134089.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作