您好,欢迎访问一九零五行业门户网

MybatisPlus怎么处理Mysql的json类型

mybatisplus处理mysql的json类型1、在数据库表定义json字段;
2、在实体类加上@tablename(autoresultmap = true)、在json字段映射的属性加上@tablefield(typehandler = jacksontypehandler.class);
1.实体类中有个属性是其他对象,或者是list;在数据库中存储时使用的是mysql的json格式,此时可以用mybatis plus的一个注解@tablefield(typehandler = jacksontypehandler.class)
@tablefield(typehandler = jacksontypehandler.class)
这样在存入是就可以把对象自动转换为json格式
2.那么取出时怎么进行映射呢,有分为两种情况
a:当没有使用到xml时:
@data@tablename(value = "person",autoresultmap = true)
b:当使用了xml文件时:
<result property="advance" column="advance" typehandler="com.baomidou.mybatisplus.extension.handlers.jacksontypehandler"/>
mybatis plus - xml中如何使用autoresultmap构造的resultmapmybatis plus有一个很大的缺陷,就是insert和select的时候使用的resultmap是不同的,修复的办法就是在实体类上增加注解@tablename(autoresultmap = true)。但是这个autoresultmap并不能使用在自定义的方法上,只在mybatis plus内置方法上生效。
展示autoresultmap存在的问题
实体类person
该实体类中有自定义的typehandler: integerlisttypehandler, stringlisttypehandler
@tablename(autoresultmap = true)public class person { private integer id; private string name; private integer age; @tablefield(typehandler = integerlisttypehandler.class) private list<integer> orgids; @tablefield(typehandler = stringlisttypehandler.class) private list<string> hobbies;}
@mapperpublic interface personmapper extends basemapper<person> { /** * 自定义的根据id获取person的方法,与mybatis-plus中的selectbyid相同的功能(但是不能使用autoresultmap生成的resultmap). */ @select("select * from person where id=#{id}") person selectonebyid(int id);}
自定义方法拿不到一些字段
因为person中的orgids和hobbies需要自定义的typehandler,自定义的方法使用的是resulttype=person,而不是生成的resultmap,所以都是null
person person = new person();person.setage(1);person.setname("tim");person.setorgids(lists.newarraylist(1,2,3));person.sethobbies(lists.newarraylist("basketball", "pingpong"));personmapper.insert(person);# 可以得到正确的字段值person personindb = personmapper.selectbyid(person.getid());# orgids和hobbies都为nullpersonindb = personmapper.selectonebyid(person.getid());preconditions.checkargument(personindb.gethobbies().equals(person.gethobbies()));preconditions.checkargument(personindb.getname().equals(person.getname()));preconditions.checkargument(personindb.getage().equals(person.getage()));preconditions.checkargument(personindb.getorgids().equals(person.getorgids()));
改进
设置@resultmap(“mybatis-plus_person”)
/** * 设置了resultmap为`mybatis-plus_person`后就可以拿到正确的值. */@resultmap("mybatis-plus_person")@select("select * from person where id=#{id}")person selectonebyid(int id);
命名规则就是:mybatis-plus_{实体类名}
个人理解
mybatis plus本身并不是一个动态的orm,而只是在mybatis初始化的时候,为mybatis提供常用的sql语句,resultmap设置,并不会改变mybatis本身的行为
常见问题
@tablefield(typehandler = integerlisttypehandler.class)没有生效:自定义的方法上没有配置resulttype
mybatis-plus - jacksontypehandler vs fastjsontypehandlerjacksontypehandler
支持 mvc json 解析
支持 mysql json 解析
传统的方法是通过 xml sql 的 resultmap 来做 typehandler 映射处理,但是这样会影响 mp 的功能,所以 jacksontypehandler 正好可以兼容 mp 的功能和满足 支持 mysql json 解析。
fastjsontypehandler
支持 mvc json 解析
不支持 mysql json 解析
可以通过 xml 支持,只是会失去 mp 特性。
<resultmap id="rxapivo" type="rxapivo" > <result column="api_dataway" property="apidataway" typehandler="com.baomidou.mybatisplus.extension.handlers.fastjsontypehandler" /></resultmap>
注意事项:
mvc json 解析时,可以不用加 @tablename(value = “t_test”, autoresultmap = true) 【高亮部分】,但是 mysql json 解析查询的时候,如果不加,查出来为 null
mysql json 解析查询时,只支持json格式:{“name”:“tom”,“age”:12},不支持:{“name”:“tom”,“age”:12} 和 “{“name”:“tom”,“age”:12}”
mybatisplus读写mysql的json字段前置条件确保mysql的版本是5.7+
一、新建mysql表增加json字段
二、pojo类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;}
三、测试类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;@springboottestclass 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); // -------------------------------------------------------- }}
以上就是mybatisplus怎么处理mysql的json类型的详细内容。
其它类似信息

推荐信息