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 Mybatis uses dynamic SQL-foreach to traverse collections and bulk insert

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

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!

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

Servers

Wechat

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

12
Report