Ibatis Label detailed

Source: Internet
Author: User
First level label (Root label):
1.<sqlmap></sqlmap>

Property:
namespace: For specifying namespaces, you can use namespaces in Java program segments to invoke the appropriate SQL statements in the form of tag IDs.

Level Two tags:
2.cacheModel

Cachemodel is the cache mechanism inside the Ibatis, the correct application can improve the performance of our system very well.

How to: Add in a sqlmap configuration file

<cachemodel type= "LRU" id= "Article_cache" readonly= "true" Serialize= "false" >
        <flushinterval hours= "24" />
        <flushonexecute statement= "articlelists.insertarticle"/> <property "name="
        value = "/>"        
</cacheModel>

Using caching in query statements

<select id= "Getarticles" resultclass= "articlelist" parameterclass= "Articlelist" Article_cache "" 
</pre> explain some of the usages of Cachemodel first: <strong><span style= "color: #ff0000" >type</span></ Strong&gt: Is the type of cache, there are 4 ways in Ibatis, memory, LRU, FIFO, oscache          memory are memory caches, Similar to the Java Memory Management mechanism [SOFT, WEAK, strong]          LRU is using the least recent usage policy           FIFO is the use of advanced first out strategy           Oscache is implemented via a Third-party cache plugin <strong><span style= "color: #ff0000" > Id</span></strong&gt: is a Cachemodel identification <strong><span style= "color: #ff0000" >readonly</ Span></strong&gt: Refers to whether the cached data object is read-only or read-only, the default is read-only, and here the read-only does not mean that the data object can no longer modify the data once it is put into the cache. Instead, when a data object changes, such as a property of a data object changes, the data object will be abolished from the cache, the next time you need to read data from the database, construct a new data object. Readonly= "false" means that the data objects in the cache can be updated. <strong><span style= "color: #ff0000" >serialize</span></ Strong&gt: Valid only if ReadOnly is false, because when ReadOnly is true, all data objects are the same, and only when read and write, different sessions have different copies of the data objects. <strong><span style= "color: #ff0000" &GT;FLUSHINTERVAL&LT;/SPAN&GT;&LT;/STRONG&GT;: Specifying cacheAutomatic refresh time, can be Hours,minutes,seconds,milliseconds.<strong><span style= "color: #ff0000" >flushonexecute </span></strong&gt: Specifies that the cache is updated when actions occur. <strong><span style= "color: #ff0000" &GT;PROPERTY&LT;/SPAN&GT;&LT;/STRONG&GT: Different types of Cachemodel have different properties. Summary: To update the more frequent data, so that do not use the sense of efficiency is similar to the update is not very frequent data, the use of cache efficiency can be improved. <strong>3.<delete> Delete sql</delete>    <insert> insert SQL </insert>    <update> Update SQL </update></strong> properties: <strong><span style= "color: #ff0000" > ID&LT;/SPAN&GT;&LT;/STRONG&GT: Unique identification <strong><span style= "color: #ff0000" >parameterMap</span> </strong&gt: Parameter list <strong><span style= "color: #ff0000" >parameterClass</span></strong> : Parameter class. <strong><span style= "color: #ff0000" >timeout</span></strong> : Sets the database timeout. Typically, you use Parameterclass to pass in a hashmap and use HashMap to store the parameters on the Java program side. <strong>4.<select> Select SQL </select></strong> properties: <stronG><span style= "color: #ff0000" &GT;ID&LT;/SPAN&GT;&LT;/STRONG&GT: Unique identification in the namespace <strong><span style= " Color: #ff0000 "&GT;PARAMETERCLASS&LT;/SPAN&GT;&LT;/STRONG&GT;: Parameter type <strong><span style=" color: #ff0000 " >parametermap</span></strong&gt: Parameter list <strong><span style= "color: #ff0000" >resultclass </span></strong>: Result type <strong><span style= "color: #ff0000" >resultmap</span></ Strong&gt: Results list <strong><span style= "color: #ff0000" &GT;CACHEMODEL&LT;/SPAN&GT;&LT;/STRONG&GT;: Cache mode < Strong><span style= "color: #ff0000" &GT;TIMEOUT&LT;/SPAN&GT;&LT;/STRONG&GT: Database timeout <strong><span style= "COLOR: #ff0000" &GT;FETCHSIZE&LT;/SPAN&GT;&LT;/STRONG&GT: The amount of data captured at a time <strong><span style= "color:# ff0000 ' >remapresults</span></strong&gt: Has true and false two values. You need to set to TRUE if the column or table of the query changes each time. Use of the words will cause a certain performance loss. <strong><span style= "color: #ff0000" >xmlresultname</span></strong&gt: Converts a result set to an XML form. <strong><span style= "COLOR: #ff0000" &GT;RESULTSETTYPE&LT;/SPAN&GT;&LT;/STRONG&GT;: Result set type. Parameters are passed in an operation similar to delete, insert, update. There are two types of results, and if you explicitly return the type of data, you can use ResultClass. If there are many types of data returned, use Resultmap. <strong>5.<resultMap></resultMap></strong> Properties: <strong><span style= "color:# ff0000 "&GT;ID&LT;/SPAN&GT;&LT;/STRONG&GT: Unique identification <strong><span style=" color: #ff0000 ">class</span ></strong&gt: The type of data to be deposited. You can usually use HashMap. <strong><span style= "color: #ff0000" >extends</span></strong&gt: can inherit other Resultmap<strong ><span style= "color: #ff0000" >groupby</span></strong&gt: can be grouped <strong>6.< Paramatermap></parametermap></strong> property: <strong><span style= "color: #ff0000" >id</ SPAN&GT;&LT;/STRONG&GT: Unique identification <strong><span style= "color: #ff0000" >class</span></strong> : parameter types typically use Parameterclass and then pass in a hashmap. <strong>7.<sql> SOME sql</sql></strong> Properties: <strong><span style= "color: #ff0000" >id&LT;/SPAN&GT;&LT;/STRONG&GT;: Unique identification. <strong><span style= "color: #ff0000" >8.<statement> Dynamic SQL </statement></span></ The Strong> property is the same as the select. <strong>9.<typealias/></strong> can specify an alias attribute for a type: <strong><span style= "color: #ff0000" > Alias</span></strong&gt: Alias <strong><span style= "color: #ff0000" >type</span></ Strong&gt: Type of class to specify alias <strong>10.<procedure> call stored procedure name (parameter 1, parameter 2 ...) </procedure></strong> properties are the same as select. You can call a stored procedure. <span style= "color: #3366ff" ><strong> below is level two label three level label </strong>:</span><strong>11.< Dynamic></dynamic></strong> property: <strong><span style= "color: #ff0000" >prepend</span ></strong&gt: For stitching a prefix. It overrides the prepend of the first element that is true. <strong><span style= "color: #ff0000" >close</span></strong> and <strong><span style= " Color: #ff0000 >open</span></strong> property is used to include a concatenation of SQL. For example: <p></p><pre code_snippet_id= "546471" snippet_file_name= "blog_20141209_3_9526983" name= "code" class= "HTML" >select * FROM table < Dynamic prepend= "where" open= ("close=") "> id= #id # </dynamic>
This is the concatenation of SQL for select * from table where (id=?) (id value is derived from incoming)

Again for example:
SELECT * FROM account  
<dynamic prepend= "where" >  
    <isgreaterthan property= "id" comparevalue= "0" >  
        acc_id = #id #  
    </isGreaterThan>  
    <isnotnull prepend= "and" property= "LastName" >  
        acc_last _name = #lastName #  
    </isNotNull>  
If you write this, you will spell SQL for SELECT * from account where acc_id= #id # acc_last_name= #lastName #,
Because it overwrites the first prepend that is true. Attribute prepend should be added to <isGreaterThan>. It is recommended that all prepend be assigned to avoid errors when stitching.

12.<include/>
Used to reference a section of SQL that has been defined well
Property:
refID: ID of the referenced SQL

13.<isempty></isempty>
Determines whether the passed in parameter is null or NULL. If it is empty, then the incoming parameter is spelled into the SQL statement.
Property:
prepend: The prefix of the concatenation SQL.
Property: Corresponding to Java properties
CloseAnd Open: includes concatenation of SQL.

For example:
Insert into table (
<dynamic prepend= ">
    <isempty prepend=", preperty= "name" >name</isempty >
    <isempty prepend= "," preperty= "age" >age</isEmpty>
</dynamic>
) VALUES (
<dynamic prepend= "" >
    <isempty prepend= "," preperty= "name" > #name #</isempty>
    < IsEmpty prepend= "," preperty= "age" >1</isEmpty>
</dynamic>
)
If the passed in age is null and incoming name is John, then the concatenation of SQL is

Insert into table (age) VALUES (1)


14.<isnotempty></isnotempty>
In contrast to <isEmpty>, it is not NULL to determine whether an incoming parameter is null or empty. If it is not null and empty, then the parameters are spliced into SQL.
Property:
prepend: The prefix of the concatenation SQL.
Property: properties corresponding to Java
Close and open: includes concatenation of SQL.

For example:

Insert into table (
<dynamic prepend= ">
    <isnotempty prepend=", preperty= "name" >name</ isnotempty>
    <isnotempty prepend= "," preperty= "age" >age</isNotEmpty>
</dynamic>
VALUES (
<dynamic prepend= "" >
    <isnotempty prepend= "," preperty= "name" > #name #</isnotempty >
    <isnotempty prepend= "," preperty= "age" > #age #</isnotempty>
</dynamic>
)
If the passed in name is John, and the incoming age is null, then the concatenation of SQL is

Insert into table (name) VALUES (' John ')


15.<isnull></isnull>
Determines whether the incoming argument is null. If NULL, then the corresponding stitching is performed.
Property:
prepend: The prefix of the concatenation SQL.
Property: properties corresponding to Java
Close and open: includes concatenation of SQL.

For example:

Insert into table (
<dynamic prepend= ">
    <isnull prepend=", "preperty=" name >name</isNull>
    <isnull prepend= "," preperty= "age" >age</isNull>
</dynamic>
) VALUES (
< Dynamic prepend= "" >
    <isnull prepend= "," preperty= "name" > ' John ' </isNull>
    <isnull prepend= "," preperty= "age" > #age #</isnull>
</dynamic>
)
If incoming name is null and incoming age is 22, the concatenation of SQL is
Insert into table (name) VALUES (' John ')

16.<isnotnull></isnotnull>
Determines whether the incoming parameter is not NULL. If it is not NULL, then the corresponding stitching is performed.
Property:
prepend: The prefix of the concatenation SQL.
Property: Corresponding to Java properties
CloseAnd Open: includes concatenation of SQL.

For example:
Insert into table (
<dynamic prepend= ">
    <isnotnull prepend=", preperty= "name" >name</ isnotnull>
    <isnotnull prepend= "," preperty= "age" >age</isNotNull>
</dynamic>
) VALUES (
<dynamic prepend= "" >
    <isnotnull prepend= "," preperty= "name" > #name #</isnotnull>
    <isnotnull prepend= "," preperty= "age" > #age #</isnotnull>
</dynamic>
)
If the passed in name is John, and the incoming age is null, then the concatenation of SQL is
Insert into table (name) VALUES (' John ')

17.<isequal></isequal>
Determines whether the incoming parameter is equal to the specified value.
Property:
prepend: The prefix of the concatenation SQL.
Property: Corresponding to Java properties
CloseAnd Open: includes concatenation of SQL.
CompareValue: Used to specify the value to compare with the parameter.
Compareproperty: Used to specify properties that are compared to parameters.

For example:
SELECT * FROM table 
<dynamic prepend= "where" >
  <isequal prepend= "" property= "id" comparevalue= "001" >id= #id #</isequal>
</dynamic>
If the ID is passed in to 001, then the concatenation of SQL is
SELECT * FROM table where id= ' 001 '
If the incoming is not 001, then the concatenation of SQL is select * FROM table

18.<isgreaterequal>,<isgreaterthan>,<islessequal>,<islessthan>
corresponding to and greater than or equal to, greater than, less than or equal to, less than.

19.<isparameterpresent>
If the parameter is not NULL, execute the SQL
Property:
prepend: The prefix of the concatenation SQL.
CloseAnd Open: includes concatenation of SQL.

20.<isnotparameterpresent>
If the parameter is NULL, execute the SQL
Property:
prepend: The prefix of the concatenation SQL.
Close and open: includes concatenation of SQL.

21.<ispropertyavalible>
The query condition is valid if the parameter is in use.

22.<isnotpropertyavalible>
The query condition is valid if the parameter is not used.

23.<result>
Configured under Resultmap to specify each item in the result set.
Property:
Property: Corresponds to the name of the field to be output, which can be obtained by the value of this property in the Java program segment
COLUNM: A field corresponding to a datasheet
Columindex: The index corresponding to the Data table field
Jdbctype: You can set jdbctype only when adding or deleting changes to a field that may have null values. When the result is a date type, the specified Jdbctype can be more explicit about SQL. Because date is a type in Java, there are different date types in different databases.
Javatype: Corresponds to the type of java. If you use an aggregate function in SQL, you need to configure the attribute in the corresponding result, otherwise the object of the aggregate function is taken out and the hash value of the Java object is displayed.
Nullvalue: null value.

24.<iterator></iterator>
Used to loop the output to generate SQL for looping.
Property:
Property: For the collection to be exported
Open and close: Used to start and end, including a piece of concatenation of SQL.
prepend: Concatenation of SQL prefix
conjunction: The connection string that is required to loop out the stitching to define and or

For example:
<select id= "Selectuseiterator" parameterclass= "Java.util.List" resultmap= "Student" >
select * from student
<dynamic prepend= "WHERE ID in" >
    <iterate open= "(" close= ")" conjunction= "," >#[]#</iterate " >
</dynamic>
</select>
The parameter list passed 3 IDs, respectively, 1,3,4. You must write [] to prevent the parser from simply outputting the list as a string. The concatenation of SQL is
SELECT * from STUDENT WHERE ID in (1,3,4)

25.<selectkey></selectkey>
Used to get the sequence from the growth.
Property:
ResultClass: Returns the type of the result.
Keyproperty: The properties of the corresponding key.
For inserting operations, for example:
<insert id= "addstudent" parameterclass= "earl.vo.Student" >
    insert INTO Student (name,age,sex,major) VALUES ( #name #, #age #, #sex #, #major #)
    <selectkey resultclass= "int" keyproperty= "id" >
        SELECT last_insert_id () As ID
    </selectKey>
</insert>
The ID is the primary key of the student table, through which IDs can be automatically generated and inserted into the student table.



Here are some of the label usage differences and tips: the difference between <isNotEmpty> and <isNotNull>:
If you do not pass in the parameter,<isnotempty> the string is not spelled into SQL. The <isNotNull> will spell the strings in SQL, but not assign values.
For example:

<insert id= "Insertfeeitem" parameterclass= "Java.util.HashMap" >
insert INTO Paymentrecords (
< Dynamic prepend= "" >
    <isnotempty prepend= "," property= "payfee_record_id" >payfee_record_id</ isnotempty>
    <isnotnull prepend= "," property= "Order_no" >order_no</isNotNull>
</dynamic >
) VALUES (
<dynamic prepend= ">
    <isnotempty prepend=", "property=" payfee_record_id ">#" payfee_record_id#</isnotempty>
    <isnotnull prepend= "," property= "Order_no" > #order_no #</ isnotnull>
</dynamic>
)
</insert>
If all two parameters are passed in as null, then the concatenation of SQL is
INSERT into Paymentrecords (order_no) VALUES ("")
There is no stitching payfee_record_id, and order_no is just the form of strings.

when dynamically stitching SQL, you will often encounter # and $, and their differences are:
1. #是将参数按照字符串传入, $ is to pass parameters directly into
2. #相当于是参数变量替换, $ is equivalent to a concatenation of strings
3. #可以防止sql注入, $ not allowed
So the place that can use # must not use $

If you do not specify a parameterclass, then any javabean with the Get/set method can be used as an input parameter.

When you configure SQL, you can write a select * from table directly, but be sure to configure a resultmap that contains all the columns of the datasheet


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.