In the SQL statement in the MyBatis query, be sure to determine the condition of NULL

Source: Internet
Author: User

Not rigorous writing, may be error: in (), this situation does not conform to the MySQL syntax.

Select from Loanwhere loan_id in

<foreach item= "item" index= "id" collection= "list" open= "(" separator= "," close= ")" >

#{item}


Either in a MyBatis SQL file or in a Java program.

the syntax in SQL :

<select id= "Findbyloanids" parametertype= "List" >

SELECT *

From p2p_loan_variation where 1 = 1

<if test= "List! = null and list.size>0" >

and loan_id in

<foreach item= "item" index= "id" collection= "list" open= "("

Separator= "," close= ")" >

#{item}

</foreach>

</if>

<if test= "List==null or list.size==0" >and 1=0</if>

</select>

What to do in Java:

list<loanvariation> list = null;

if (Listutils.isnotempty (loadids) {

List =loanvariationdao.findbyloanids (loanids);

}

return list;

If there are multiple Java methods calling DAO Mapper, it is easier to write in SQL, after all, only one "parameter check" is required.

The above wording is for a query condition, if there are multiple query conditions, remember to deal with the relationship between the conditions,

such as a=1 and B =2 or c=3.

If there is an in condition, it is best to add parentheses, (a=1) and (b =2) or (c in ...).

In the SQL statement in the MyBatis query, be sure to determine the condition of NULL

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.