MyBatis Dynamic SQL (ii)

Source: Internet
Author: User

1. <if> (single condition)

2, <choose> <when> (multiple branch conditions)

3, <where> for intelligent processing where conditions, can be intelligent to add and remove conditions and

<select>

SELECT * FROM user

<where>

<if test= "UserName! = null" >

and userName like #{username}

</if>

<if test= "id!= null" >

and Id= #{id}

</if>

</where>

</select>

4, <set> processing update operation, you can intelligently remove or add the comma inside the set

<update>

Update user

<set>

<if test= "UserName! = null" > username=#{username},</if>

<if test= "password!= null" > Password=#{password},</if>

</set>

</update>

5. <trim> can automatically add prefixes and suffixes to SQL to reduce prefix characters or suffix characters

<update>

Update user

<trim prefix= "Set" suffixoverride= "," suffix= "where Id=#{id}" >

<if test= "Username!=null and username!=" >

Username=#{username},

</if>

<if test= "Password!=null and password!=" >

Password=#{password},

</if>

</trim>

</update>

6, Foreach Loop flag, mainly used for cyclic query conditions, circular update

Circular query Condition:

<select>

SELECT * FROM user

<where>

ID in

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

#{item}

</foreach>

</where>

</select>

Batch assignment:

<insert>

Insert into User (Username,password)

Values

<foreach item= "Item" index= "index" collection= "list" open= "" close= "" separator= "," >

(#{item.username},#{item.password})

<foreach>

</insert>

Explain:

An element that the item loops to

Index of an index loop

Collection of collection Loops

Start symbol for Open loop

Closing symbol for Close loop

MyBatis Dynamic SQL (ii)

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.