Mybatis uses @ Insert annotation to Insert multiple records. Several methods of JSONObject to List

Keywords: Java MySQL Spring Boot leetcode Dynamic Programming

Mybatis inserts multiple records using the @ Insert annotation

Several methods of converting JSONObject to List

In a large software engineering job, if an administrator wants to send messages to multiple users, the front end needs to send an array with user numbers to the back end.
Here is a summary of several methods for the back-end to receive the list sent by the front-end
from to front end hair give to after end of number according to grid type by J s o n , after end meet collect of grid type by J s o n Since the format of data sent by the front end to the back end is Json, the format received by the back end is Json Since the format of data sent by the front end to the back end is Json, the format received by the back end is Json
I Guys need want take J s o n grid type turn turn by l i s t We need to convert Json format to list We need to convert Json format to list

  • 1. 1. 1. Use Alibaba cloud's JSONObject

I haven't fully understood the source code of JSONObject. If a big man has a deep understanding of the source code, he can guide me. JSOBObject inherits the JSON class. Ali seems to have a fastjason

First, we need to know the format of the Json array, for example

[1,2,3,4,5] / / this is the simplest Json array
["a", "b", "c", "d"] / / Json array. The contents are stored in strings
The contents of the Json array can also be objects

See this blog for details
My simple understanding is to use the data installed in [],
At the same time, the type of each element in JSONArray is JSONObject
Method 1:

//JSONObject.getJSONArray
List<Integer> list = p.getJSONArray("list").toJavaList(Integer.class);
//When the passed data is an object.
List<Student> stus = p.getJSONArray("stus").toJavaList(Student.class);
// We can also get the value of an element from JSONArray
int adminId = p.getJSONArray("admins").getJSONObject(1).getInteger("adminId");
/*JSONArray There is a getJSONObject method, which is similar to the value of the subscript of the array,
But the object type is JSONObject, and we are using the get basic type method of JSONObject
 Get the element with key "adminId"*/

I feel that it will be clearer to read the source code when I have time. Now I just can use it for the time being.

Method 2:
The following method is very awesome. It can be said that it can be converted into any form or Map, although I haven't used it yet.
Just one line of code

List<Integer> list1 = JSONObject.parseObject(p.getJSONArray("list").toJSONString(),new TypeReference<ArrayList<Integer>>(){});

The writing method is very simple. Let's see the effect.

We pass a Json data from the front end, which contains a Json array called list. According to the code, p is the JSONObject object passed from the front end to the back end, and new typereference < > () {} fill in the data type to be converted in square brackets
Reference blog
TypeReference also seems to be a very good thing. Many people don't know. I'm a good dish

Insert multiple records using @ insert annotation

Paste the code directly

@Insert("<script> " +
                "insert into news values\n"+
                "  <foreach collection= 'list' item= 'item'  separator=','>\n" +
                    "(#{item.sander_Id},#{item.receiver_Id},#{item.content},#{item.createTime},#{item.state})\n"+
                "</foreach> \n"+
            "</script>")
    void sendNews(@Param(value = "list") List<News> list);

Introduce the @ Param annotation. When the annotation is used to simplify XML configuration (for example, the sql parameter in Mapper.xml of Mybatis), the @ Param annotation is used to name the parameter. The parameter passed in to the above code is called list. Item can be understood as a element in the passed in list and named item.

After mybatis is parsed, the logic of the whole mysql statement is about

insert into values (1,1,'a',NOW(),0), (1,2,'a',NOW(),0) 

Equivalent to a string concatenation, this comma , , , actually < f o r e a c h > <foreach> Provided by the separator in the < foreach > statement, which means it is used when splicing , , , separate.

Posted by mzfp2 on Mon, 29 Nov 2021 07:27:19 -0800