Ibatis multiplexing SQL fragment, introducing fragment dynamic condition increase

Source: Internet
Author: User
Tags cdata

1:ibatis reusing SQL fragments, introducing fragments using the "SQL" and "include" Tags:

Typically, you would write:
XML code

<id= "Selectitemcount"  resultclass= "int">   <ID= "Selectitems"  resultclass= "Item"  >

To eliminate duplicate fragments, we use the "SQL" and "include" tags. The "SQL" tab is used to contain duplicate fragments, and the "include" tag is used to introduce fragments:

<SQLID= "Selectitem_fragment">From items WHERE parentid = 6 sql><SelectID= "Selectitemcount"ResultClass= "int">SELECT COUNT (*) as Total<includerefID= "Selectitem_fragment"/>select><SelectID= "Selectitems"ResultClass= "Item">SELECT ID, name<includerefID= "Selectitem_fragment"/>select>

The "inclued" tag is known as a namespace, so you can introduce fragments from other maps. (However, since Ibatis introduces the order of Sqlmap, the fragment being introduced takes precedence over the SQL part to be imported)

Repeating fragments are introduced and executed when the query is executed, so parameters can still be used:

<SQLID= "Selectitem_fragment">From items WHERE parentid = #value # sql><SelectID= "Selectitemcount"Parameterclass= "int"ResultClass= "int">SELECT COUNT (*) as Total<includerefID= "Selectitem_fragment"/>select><SelectID= "Selectitems"Parameterclass= "int"ResultClass= "Item">SELECT ID, name<includerefID= "Selectitem_fragment"/>select>

2: Use of dynamic condition increase <dynamic>

<SelectID= "Getcitylistbyprovinceid"Parameterclass= "Simplemap"ResultClass= "Dictmodel">  <! [Cdata[Select Xid dictcode, XName dictname from the city]]>    <Dynamicprepend= "where">   <Isnotemptyprepend= "and" Property= "Xflag">      <! [Cdata[xflag= $xflag $]]>      </Isnotempty>             <Isnotemptyprepend= "and" Property= "Xprovinceid">      <! [Cdata[xprovince_id = #xprovinceId #]]>      </Isnotempty>  </Dynamic> </Select>

With the dynamic node, we define a dynamically where clause. This WHERE clause will

May contain two criteria for the name and address fields. Whether or not these two fields join is retrieved depends on the query criteria provided by the user.

<dynamic> inside is the statement of the dynamic condition, which is filled with where related conditions for this dynamic depends on what the verb is prepend= "" which can be filled in where also can write group by and order by

The following label is used for the existence of the decision condition to be dynamic: <isNotEmpty> means to execute the statement prepend= "" dependency constraint when the secondary condition is not NULL, the value can be and can also be OR

Property= "" is for this condition to determine the value of the field such as "Xflag" so that the above-mentioned SELECT statement can implement a decision <=4 conditions of the SQL implementation statement, in the application, the page here can design 4 input box, The user only enters the conditions in which the query is dynamically implemented.

<isgreaterthan prepend= "and" property= "comparevalue=" "> field is greater than a value CompareValue compare value

<isGreateEqual> greater than equals comparison ditto usage

<isEqual> whether equal ibid usage

<isNotEqual> is not equal ibid usage

<isLessThan> less than comparison ibid usage

<isLessEqual> less than equals comparison ditto usage

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.