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 use Mybatis annotations to complete the splicing of SQL statements with an input parameter of list

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

Share

Shulou(Shulou.com)06/02 Report--

This article is about how to use Mybatis annotations to complete the splicing of SQL statements with an input parameter of list. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Mybatis annotations complete the splicing of SQL statements with input parameters of list

First, concatenate the list collection into a string in the format of "1pm, 2pm, 3pm 4".

Then encapsulate the string into a class: TyreInfoIdStr

The @ SelectProvider here calls the return value of a method of an external class as a sql statement.

In this method, I concatenate the strings of the SQL statement and the list collection, which I have concatenated in advance.

The sql function whose stitching query condition is the list set

The sql statement does not change when deptId is 1

The id entry in list is used as a sql query condition when deptId is another number

At the beginning of writing, the idea was to use the or statement to loop and repeat and add the sql statement of or to query.

Finally, the in function of sql is adopted to realize it.

Public String getDeptIdSql (List deptIdList) {Iterator it = deptIdList.iterator (); / / when department id is 1, filter while (it.hasNext ()) {Long deptId = it.next (); if (deptId = = 1) {return ";}} / / concatenate in conditional statement String s ="; for (int I = 0) I < deptIdList.size ()) {if (iTunes = (deptIdList.size ()-1)) {s + = deptIdList.get (I) + ",";} else {s + = deptIdList.get (I);}} String sql = "and mtMaintenanceStandard.dept_Id in (" + s + ")" Return sql;}

After writing, it is found that the function can improve the reusability of the function, and the mtMaintenanceStandard.dept_Id is set to the passed variable.

Finally get

Public String getDeptIdSql (String condition,List deptIdList) {Iterator it = deptIdList.iterator (); / / when department id is 1, filter while (it.hasNext ()) {Long deptId = it.next (); if (deptId = = 1) {return ";}} / / concatenate in conditional statement String s =" For (int I = 0; I < deptIdList.size ()) {if (I = (deptIdList.size ()-1)) {s + = deptIdList.get (I) + ",";} else {s + = deptIdList.get (I) }} String sql = "and" + condition + "in (" + s + ")"; return sql;}

The more general version is that there is no need to judge whether deptId is 1.

Public String getDeptIdSql (String condition,List deptIdList) {String s = ""; for (int I = 0; I < deptIdList.size ()) {if (I = (deptIdList.size ()-1)) {s + = deptIdList.get (I) + ",";} else {s + = deptIdList.get (I) }} String sql = "and" + condition + "in (" + s + ")"; return sql;} Thank you for reading! This is the end of this article on "how to use Mybatis annotations to complete the splicing of SQL statements with input parameters of list". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see it!

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