mybatis solves the problem that < foreach > tags cannot exceed 1000
Wrong way to write:
<select id="getProductInfoList" resultType="vo"> select a.name from A a where a.idin <foreach collection="ids" item="item" index="index" open="(" close=")" separator=","> #{item} </foreach> </select>
Error cause:
When the number of <foreach> tags exceeds 1000, an error message is displayed:
java.sql.SQLSyntaxErrorException: ORA-01795: maximum number of expressions in a list is 1000
Correct writing:
Option 1(nesting the parameter into an SQL statement in SQL) :
<select id="getProductInfoList" resultType="vo"> select a.name from A a where a.id in (select b.id from B b where b.id = #{billNo} and DELETED = 0) </select>
Option 2 (Add one or per 1000 with or)
SELECT * FROM ${tabNameMx} M WHERE M.CODE_ID IN <foreach collection="idList" index="index" open="(" close=")" item="id" separator=","> <if test="(index % 999) == 998"> NULL) OR M.CODE_ID IN(</if>#{id} </foreach>
This SQL we may feel a little confused, now write you a section of this sql will eventually become what, so that you instantly understand.
What the final execution of SQL looks like:
CODE_ID IN('...... ','998',NULL ) OR M.CODE_ID IN('999',..... NULL) OR M.CODE_ID IN('..... ')
Option 3 (splice OR ID IN ()) :
<select id="queryEnoByCapita"> select t.custid,to_char(t.eno) as eno from T_E_ACCOUNT t where t.status ! ='2' and t.custid in <trim suffixOverrides=" OR t.custid in()"> <foreach collection="capita" item="custId" index="index" open="(" close=")"> <if test="index ! = 0"> <choose> <when test="index % 1000 == 999">) OR t.custid in (</when> <otherwise>,</otherwise> </choose> </if> #{custId,jdbcType=VARCHAR} </foreach> </trim> </select>
Analysis:
<trim> Tag suffixOverrides: in this example, if the suffix OR t.tid in() matches the attribute value of suffixOverrides, then the iterated position of the OR t.tid in() index set starts from 0. Why do I need <if test="index! = 0">? If not, sql is t.cust in (,1,2...) The concatenation SQL without 999 data is: t.ust in (1,2... 998) Data concatenation SQL for more than 999 items is: t.ust in (1,2... 998) or T.tid in (999,1000... 1998)...
Expanded:
Extension 1: The main properties of the foreach element are item, index, collection, open, separator, and close
Each element in the item collection of the collection foreach loop, or the object of the collection, supports the subscript of the attribute #{obj.filed} or #{value} index loop as an object point attribute, starting with 0 separator What is used as the separator between each iteration? close indicates what ends
Extension 2: MyBatis trim four attributes of the tag and their effects
prefix Add prefix prefixOverrides Delete prefix suffix Add suffix suffixOverrides Delete suffix
This article about mybatis to solve the < foreach > tag can not exceed 1000 is introduced to this article, more related mybatis foreach can not exceed 1000 content, please search the previous articles of script home or continue to browse the following related articles hope that you will support script home in the future!
- MyBatis resolves the problem by using the <foreach> tag like to query the error
- MyBatis reported an error using the <foreach> tag and resolved it
- mybatis,foreach, parameter error not found and resolved
- Mybatis uses the foreach tag to implement batch insertion
- Usage of MyBatis' foreach tag and various loop problems
Related article
-
Java array, remove duplicate values, add, delete array elements methods
The following Xiaobian will bring you a Java array, remove duplicate values, increase, delete array elements of the method. Xiaobian feel very good, now to share with you, but also to give you a reference. Let's take a look2016-10-10 -
Sample code for configuring multiple Kafkas for the springboot project
This article mainly introduces the springboot project configuration of multiple kafka, this article through the example code to give you a very detailed introduction, for everyone's study or work has a certain reference value, the need of friends can refer to the next2023-04-04 -
How to disable instruction rearrangement in Java volatile
This article mainly introduces the Java volatile how to achieve the prohibition of instruction rearrangement, the article through the example code to introduce very detailed, for everyone's study or work has a certain reference learning value, need friends can refer to the next2020-11-11 -
Detail the xml configuration of Spring Aop instances
This article mainly introduces the detailed xml configuration of Spring Aop examples, using xml can centrally configure aop, has certain reference value, interested partners can refer to it2017-04-04 -
springboot implementation rabbitmq message validation example code
There are two kinds of RabbitMQ message confirmation, one is message sending confirmation, the second is consumption receive confirmation, this article mainly introduces springboot to achieve rabbitmq message confirmation example code, has a certain reference value, interested can understand2023-09-09 -
spring Boot is packaged and deployed to tomcat on a remote server
This article mainly introduces the relevant information about spring Boot packaging and deployment of tomcat to remote servers. The article introduces it in great detail through example code, which has certain reference value for your study or work. The friends who need it will learn together with Xiaobian below.2017-12-12 -
Java to determine the number of digits of the method summary
This article gives you a collation of Java to judge the number of two common methods, interested in this can follow the small series together to learn.2018-02-02 -
How does SpringBoot get objects from a container
This article mainly introduces how SpringBoot obtains objects from containers, has a good reference value, I hope to help you. If there are mistakes or incomplete areas, please feel free to comment2022-08-08 -
java EasyExcel for Excel document read and write logic example detailed
This article mainly introduces the detailed explanation of java EasyExcel for Excel document reading and writing logic examples. If you need it, you can use it for reference. I hope it can be helpful2022-07-07
Latest comments