服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Mybatis-Plus读写Mysql的Json字段的操作代码

Mybatis-Plus读写Mysql的Json字段的操作代码

2022-11-22 11:29什么都干的派森 Java教程

这篇文章主要介绍了Mybatis-Plus读写Mysql的Json字段的操作代码,文中通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

前置条件

确保mysql的版本是5.7+

一、新建mysql表增加json字段

Mybatis-Plus读写Mysql的Json字段的操作代码

二、pojo类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.cxstar.domain;
 
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 java.io.Serializable;
import java.util.Date;
@lombok.Data
@TableName(autoResultMap = true)
public class Data implements Serializable {
    @TableId(value = "id",type = IdType.AUTO)
    private Integer id;      
    
    // 部分字段省略-------------
    private String title;      
    private String author;        
    private String publisher;
    // -----------------------  
    @TableField(typeHandler = FastjsonTypeHandler.class)
    private JSONObject aggJson;
}

三、测试类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.cxstar;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cxstar.domain.Data;
import com.cxstar.domain.SearchMsg;
import com.cxstar.mapper.DataMapper;
import com.cxstar.service.OrderService;
import com.cxstar.service.spider.impl.*;
import com.cxstar.service.utils.ExecutorThread;
import com.cxstar.service.utils.SpiderThread;
import com.cxstar.service.utils.SynContainer;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.ArrayList;
import java.util.Date;
import java.util.UUID;
@SpringBootTest
class OrderApplicationTests {
    @Autowired
    DataMapper dataMapper;
    @Test
    void testJson() {
        // insert -----------------------------------
        Data data = new Data();
        data.setTitle("计算机安全技术与方法");
        data.setPublisher("<<计算机技术>>编辑部出版");
        JSONObject jb = new JSONObject();
        jb.put("searchKey", "英格");
        jb.put("curPage", "1");
        JSONArray js = new JSONArray();
        js.add("西北政法大学");
        js.add("西安理工大学");
        jb.put("source", js);
        data.setAggJson(jb);
        dataMapper.insert(data);
        // ------------------------------------------
        // select --------------------------------------
        Data data1 = dataMapper.selectById(5837);
        JSONObject jb2 = data1.getAggJson();
        System.out.println(jb2.getJSONArray("source"));
        // ---------------------------------------------
        
        // group by -----------------------------------------------
        LambdaQueryWrapper<Data> lqw = new LambdaQueryWrapper<>();
        lqw.select(Data::getAggJson);
        lqw.groupBy(Data::getAggJson);
        List<Data> dataList = dataMapper.selectList(lqw);
        System.out.println(dataList);
        // --------------------------------------------------------
    }
}

到此这篇关于Mybatis-Plus读写Mysql的Json字段的文章就介绍到这了,更多相关Mybatis-Plus Json字段内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/weixin_43721000/article/details/124399097

延伸 · 阅读

精彩推荐