MyBatis Dynamic SQL

Source: Internet
Author: User

SQL statements in MyBatis can be dynamically generated and spelled with conditional judgments

1. Use if+where to spell SQL statements

  

, use if to determine the spelling of SQL according to the parameters passed in

2. Using Trim+if to implement the spelling of SQL

  

There are some properties in Trim:

"1" prefix: prefix

"2" prefixoverrides= "and | Or "Override the prefix to avoid occurrences of the and OR or

"3" suffix= "" suffix, and at the end of Trim plus

"4" suffixoverrides= "" suffix rewrite, in each if in the SQL statement after adding, to avoid unnecessary

3. Update with If+set (set will automatically ignore end,)

4. When a range operation is required, a foreach is usually used

Such as:

  

Use the Foreach loop to facilitate the variable passed into the parameter list. Properties of foreach:

Collection= "": If the passed parameter is a number, write the Array, if the passed parameter is list, then write the list, if the incoming parameter is map, write the corresponding key name in the map

Item: Refers to the current object

Open: Start

Close: End

Separator: Delimiter

5. Use Choose (when,otherwise) similar to switch in Java

    

Otherwise refers to other conditions

MyBatis Dynamic SQL

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.