IBatis learning Summary: dynamic SQL

Source: Internet
Author: User

When compiling SqlMaps, you often need to split an SQL statement into multiple segments. IBatis provides a simple and practical splitting node for SQL splitting. Let's take a simple example. Suppose you need to query some student information.

The original statement is:

 

You can use the SQL label to write the statement as follows:

        

<Include refid = "SelectALL"/> refid: the id of the SQL tag to be called.

The SQL tag is equivalent to defining a string in C #, which contains some values and can be called by other labels. In this way, you can reduce repeated code writing.

IBatis dynamic join SQL

Instance:

Id = resultMap = resultClass => <include refid =/> <include refid => </include> <dynamic prepend => <isParameterPresent> <isNotEmpty prepend = property => </isNotEmpty> <isNotEmpty prepend = property => </isNotEmpty> <isGreaterThan prepend = property = compareValue => <! -- Greater than --> = </isGreaterThan> <isNotEqual prepend = property = compareValue => <! -- Not equal to -- >=</isNotEqual> </isParameterPresent> </dynamic> </>View Code

Dynamic label classification: 1 <dynamic>, 2 binary labels, 3 one dollar labels, 4 <iterate>
These four tags and their sub-tags share the common attributes prepend, open, and close.

Remove the first prepend = "and"

Attributes of a mona1 condition Element:
Prepend-an SQL statement that can be overwritten. (optional)
Property-the property to be compared (required)

<IsPropertyAvailable> check whether this attribute exists (the parameter bean attribute exists ).
<IsNotPropertyAvailable> check whether this attribute does not exist (the parameter bean attribute does not exist ).
<IsNull> check whether the property is null.
<IsNotNull> check whether the attribute is not null.
<IsEmpty> check whether the value of Collection. size (), String or String. valueOf () of the attribute is null or empty ("" or size () <1 ).
<IsNotEmpty> check Collection. size () value, String or String of the attribute. valueOf () value, whether it is not null or not empty ("" or size ()> 0 ).
Example:
<IsNotEmpty prepend = "AND" property = "firstName">
FIRST_NAME = # firstName #
</IsNotEmpty>

Attributes of binary condition Elements: Binary is the comparison of two objects.
Prepend-an SQL statement that can be overwritten. (optional)
Property-the property to be compared (required)
CompareProperty-another property used for comparison with the former (required or selected compareValue)
CompareValue-value used for comparison (required or compareProperty)

<IsEqual> compare whether the property value is equal to the static value or another property value.
<IsNotEqual> compare whether the property value is not equal to the static value or another property value.
<IsGreaterThan> compare whether the property value is greater than the static value or another property value.
<IsGreaterEqual> compares whether the property value is greater than or equal to the static value or another property value.
<IsLessThan> compares whether the property value is smaller than the static value or another property value.
<IsLessEqual> compares whether the property value is less than or equal to the static value or another property value.
Example:
<IsLessEqual prepend = "AND" property = "age" compareValue = "18">
ADOLESCENT = 'true'
</IsLessEqual>

Differences between isPropertyAvailable
These two attributes are very useful.
IsPropertyAvailable
IsNotEmpty
Generally, an input parameter encapsulates data.
Use
If you want an attribute to be used only when it is not null

<Iterate>:This property traverses the entire set and repeats the content of the element body in the List set.
Iterate attributes:
Prepend-an SQL statement that can be overwritten. (optional)
Property-elements used for traversal whose type is java. util. List (required)
Open-string starting from the whole traversal content body, used to define parentheses (optional)
Close-the end string of the entire traversal body, used to define parentheses (optional)
Conjunction-a string between each traversal content, used to define and or (optional)
Example:
<Iterate prepend = "AND" property = "userNameList" open = "(" close = ") "conjunction =" OR "> username = # userNameList [] # </iterate> = and (username = name1 or username = name2 ...)

Note: When Using <iterate>, it is very important to include square brackets [] after the name of the List element. square brackets [] Mark the object as List, in case the parser simply outputs the List as a String.
[Color = darkred] [/color]

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.