Get the App
SLTechnology News&Howtos  ›  Development  › 

How to read and write the Json field of Mysql by Mybatis-Plus

Shulou Source: shulou.com Published: 2022-05-31 19:31:32 10月03日 Update

This article mainly explains "how to read and write the Json field of Mysql by Mybatis-Plus". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how Mybatis-Plus reads and writes the Json field of Mysql.

Precondition

Make sure that the version of mysql is 5.7 +

1. Add json field to the new mysql table

2. 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 / / some fields are omitted-private String title; private String author; private String publisher; / /-@ TableField (typeHandler = FastjsonTypeHandler.class) private JSONObject aggJson;} 3. Test class 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 (computer Security Technology and methods); data.setPublisher (editorial Publishing); JSONObject jb = new JSONObject (); jb.put ("searchKey", "Eng"); jb.put ("curPage", "1"); JSONArray js = new JSONArray (); js.add ("Northwestern University of political Science and Law") Js.add (Xi'an University of Technology); 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 lqw = new LambdaQueryWrapper () Lqw.select (Data::getAggJson); lqw.groupBy (Data::getAggJson); List dataList = dataMapper.selectList (lqw); System.out.println (dataList) / /--}} at this point, I believe you have a deeper understanding of "how Mybatis-Plus reads and writes the Json field of Mysql". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Fields content university method learning practical deeper security interest practicality practicality technology easy to operate more friends conditions version science and technology university of science and technology editorial depa Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Docker Shulou Technology Shulou Tech Info MySQL