Mybatis-Plus怎么读写Mysql的Json字段


本篇内容主要讲解“Mybatis-Plus怎么读写Mysql的Json字段”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Mybatis-Plus怎么读写Mysql的Json字段”吧!

前置条件

确保mysql的版本是5.7+

一、新建mysql表增加json字段

二、pojo类

package&nbsp.cxstar.domain;import&nbsp.alibaba.fastjson.JSONObject;import&nbsp.baomidou.mybatisplus.annotation.IdType;import&nbsp.baomidou.mybatisplus.annotation.TableField;import&nbsp.baomidou.mybatisplus.annotation.TableId;import&nbsp.baomidou.mybatisplus.annotation.TableName;import&nbsp.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler;importjava.io.Serializable;importjava.util.Date;@lombok.Data@TableName(autoResultMap=true)publicclassDataimplementsSerializable{@TableId(value="id",type=IdType.AUTO)privateIntegerid;//部分字段省略-------------privateStringtitle;privateStringauthor;privateStringpublisher;//-----------------------@TableField(typeHandler=FastjsonTypeHandler.class)privateJSONObjectaggJson;}

三、测试类

package&nbsp.cxstar;import&nbsp.alibaba.fastjson.JSONArray;import&nbsp.alibaba.fastjson.JSONObject;import&nbsp.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;import&nbsp.cxstar.domain.Data;import&nbsp.cxstar.domain.SearchMsg;import&nbsp.cxstar.mapper.DataMapper;import&nbsp.cxstar.service.OrderService;import&nbsp.cxstar.service.spider.impl.*;import&nbsp.cxstar.service.utils.ExecutorThread;import&nbsp.cxstar.service.utils.SpiderThread;import&nbsp.cxstar.service.utils.SynContainer;importorg.junit.jupiter.api.Test;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.boot.test.context.SpringBootTest;importjava.util.ArrayList;importjava.util.Date;importjava.util.UUID;@SpringBootTestclassOrderApplicationTests{@AutowiredDataMapperdataMapper;@TestvoidtestJson(){//insert-----------------------------------Datadata=newData();data.setTitle("计算机安全技术与方法");data.setPublisher("<<计算机技术>>编辑部出版");JSONObjectjb=newJSONObject();jb.put("searchKey","英格");jb.put("curPage","1");JSONArrayjs=newJSONArray();js.add("西北政法大学");js.add("西安理工大学");jb.put("source",js);data.setAggJson(jb);dataMapper.insert(data);//------------------------------------------//select--------------------------------------Datadata1=dataMapper.selectById(5837);JSONObjectjb2=data1.getAggJson();System.out.println(jb2.getJSONArray("source"));//---------------------------------------------//groupby-----------------------------------------------LambdaQueryWrapper<Data>lqw=newLambdaQueryWrapper<>();lqw.select(Data::getAggJson);lqw.groupBy(Data::getAggJson);List<Data>dataList=dataMapper.selectList(lqw);System.out.println(dataList);//--------------------------------------------------------}}

到此,相信大家对“Mybatis-Plus怎么读写Mysql的Json字段”有了更深的了解,不妨来实际操作一番吧!这里是主机评测网网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!


上一篇:在css3中可实现缩放效果的属性是哪个

下一篇:php如何实现几秒后执行一个函数


Copyright © 2002-2019 测速网 www.inhv.cn 皖ICP备2023010105号
测速城市 测速地区 测速街道 网速测试城市 网速测试地区 网速测试街道
温馨提示:部分文章图片数据来源与网络,仅供参考!版权归原作者所有,如有侵权请联系删除!

热门搜索 城市网站建设 地区网站制作 街道网页设计 大写数字 热点城市 热点地区 热点街道 热点时间 房贷计算器