Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report