MyBatis Basics: MyBatis Dynamic SQL (3)

Source: Internet
Author: User

1. Overview

Dynamic SQL includes elements in MyBatis:

Elements function Notes
If Judgment statement Single conditional branch judgment
Choose (when, otherwise) Equivalent to a case-when statement in Java Multi-Conditional branch judgment
Trim (where, set) Auxiliary elements For dealing with SQL stitching issues
Foreach Looping statements For enumeration conditions such as in statements
2. If element

The IF element is the most commonly used judgment statement and is often used in conjunction with the Test property.

2.1 If
<ResultmapID= "Baseresultmap"type= "Com.libing.helloworld.model.Role">    <ID Property= "id"column= "id" />    <result Property= "RoleName"column= "Role_name" /></Resultmap>
<SelectID= "Findbysearchtext"Resultmap= "Baseresultmap">SELECT ID, role_name from role WHERE 1 = 1<ifTest= "SearchText ! = null and SearchText! =" ">and role_name like CONCAT ('% ', #{searchtext,jdbctype=varchar}, '% ')</if>ORDER by ID ASC</Select>
2.2 If + where
<SelectID= "Findbysearchtext"Resultmap= "Baseresultmap">SELECT ID, role_name from role<where>        <ifTest= "ID > 0">ID >= #{id}</if>        <ifTest= "SearchText ! = null and SearchText! =" ">and Role_name like CONCAT (CONCAT ('% ', #{searchtext,jdbctype=varchar}), '% ')</if>    </where>ORDER by ID ASC</Select>

The WHERE tag in the MyBatis will determine if there is a return value in the included tag, insert a ' where '. In addition, if the label returns content that begins with and or or, the beginning of and or or is automatically deleted.

2.3 If + set
<UpdateID= "Update"ParameterType= "Com.libing.helloworld.model.Role">UPDATE Role<Set>        <ifTest= "RoleName ! = null and RoleName! =" ">role_name = #{rolename},</if>        <ifTest= "Remark ! = null and remark! =" ">remark like CONCAT ('% ', #{remark, jdbctype=varchar}, '% ')</if>    </Set>WHERE id = #{id}</Update>

The above form, when ramark=null, the dynamic SQL statement will be due to a "," and error.

3. Choose (when,otherwise) Element
<SelectID= "Findbycondition"Resultmap= "Baseresultmap">SELECT ID, role_name from role<where>        <Choose>            < whenTest= "ID > 0">ID >= #{id}</ when>            <otherwise>and role_name like CONCAT ('% ', #{rolename, jdbctype=varchar}, '% ')</otherwise>        </Choose>    </where>ORDER by ID ASC</Select>
4.trim Element 4.1 Trim:if + where
<SelectID= "Findbycondition"Resultmap= "Baseresultmap">SELECT ID, role_name from role<Trimprefix= "where"Prefixoverrides= "and | OR ">        <ifTest= "ID > 0">ID >= #{id}</if>        <ifTest= "RoleName ! = null and RoleName! =" ">and role_name = like CONCAT ('% ', #{rolename, jdbctype=varchar}, '% ')</if>    </Trim>ORDER by ID ASC</Select>
4.2 Trim:if + Set
<UpdateID= "Update"ParameterType= "Com.libing.helloworld.model.Role">UPDATE Role<Trimprefix= "Set"Suffixoverrides=",">        <ifTest= "RoleName ! = null and RoleName! =" ">role_name = #{rolename},</if>        <ifTest= "Remark ! = null and remark! =" ">remark like CONCAT ('% ', #{remark, jdbctype=varchar}, '% ')</if>    </Trim>WHERE id = #{id}</Update>
5. foreach element

MyBatis Basics: MyBatis Dynamic SQL (3)

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.