前置条件
确保mysql的版本是5.7+
一、新建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
查看更多关于Mybatis-Plus读写Mysql的Json字段的操作代码的详细内容...