Get the App
SLTechnology News&Howtos  ›  Servers  › 

How Mybatis uses dynamic SQL-foreach to traverse collections and bulk insert

Shulou Source: shulou.com Published: 2022-05-31 19:45:11 09月22日 Update

This article mainly shows you "Mybatis how to use dynamic SQL-foreach to traverse the collection, batch insert", the content is easy to understand, well-organized, hope to help you solve your doubts, the following let the editor lead you to study and learn "Mybatis how to use dynamic SQL-foreach to traverse the collection, batch insert" this article.

Bulk inserting data into the database is used very frequently during development, and it is impossible for us to insert data one by one in order to save time and improve efficiency; therefore, mybatis provides developers with foreach tags.

The foreach tag explains: collection= "": indicates the collection to traverse

Item= "": assigns the currently traversed element to the specified variable

Separator= "": the separator between each element

Open= "(": all results traversed start with the opening parenthesis

Close= ")"; all results traversed end with a closing parenthesis

Index= ""; index when traversing List, item is value; when traversing map, key,item is value

1. [foreach traversal collection]

Code-mapper class:

/ / Test foreach: traversing

Public List getEmpByConditionForeach (List emp)

Mapper mapping file:

Select * from xjinfo where XH in

# {item}

Test the code:

@ Test

Public void testSelectForeach () throws IOException {

SqlSessionFactory sqlSessionFactory = getSqlSessionFactory ()

SqlSession openSession = sqlSessionFactory.openSession ()

Try {

EmployeemapperDynamicSQL mapper = openSession.getMapper (EmployeemapperDynamicSQL.class)

List emps = mapper.getEmpByConditionForeach (Arrays.asList ("2017000001", "2017000002", "2017000003"))

For (Employee emp: emps) {

System.out.println (emp)

}

} finally {

OpenSession.close ()

}

}

View the console print:

2. [foreach batch insert]

Code-mapper class:

/ / Test foreach: batch insert

Public void addEmpByBatchForeach (@ Param ("emps") List emps)

Mapper mapping file:

Insert into xjinfo (XH,XM,YJSLB)

Values

(# {emp.xh}, # {emp.xm}, # {emp.yjslb})

[explanation]: the annotation @ Param ("emps") in the Mapper class is that emps can be used directly in collection in foreach

Test the code:

@ Test

Public void testBatchInsert () throws IOException {

SqlSessionFactory sqlSessionFactory = getSqlSessionFactory ()

SqlSession openSession = sqlSessionFactory.openSession ()

Try {

EmployeemapperDynamicSQL mapper = openSession.getMapper (EmployeemapperDynamicSQL.class)

List emps = new ArrayList ()

Emps.add (new Employee ("2017000000", "Li Yi", "Shuo")

Emps.add ("2017000001", "Li er", "Bo"))

Mapper.addEmpByBatchForeach (emps)

/ / Don't forget to submit when executing insert update

OpenSession.commit ()

} finally {

OpenSession.close ()

}

}

View the console print:

Finally, sort out the idea: start with the test test class, pass a list emps into the emps of the mapper class, and emps receive it in the mapping file, each time you traverse the emps, the value emp assigned to the item, and then receive the parameters in foreach with # (emp.xh).

The above is all the content of the article "how Mybatis uses dynamic SQL-foreach to traverse collections and insert in batches". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Tests code dynamics content data files articles elements characters parentheses console time tags results learning help development control interpretation use in Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Huawei Apple MySQL Shulou Information