Http://www.cnblogs.com/13590/archive/2013/03/01/2938126.html
<?xml version="1.0"encoding="UTF-8"? ><sqlmapnamespace="Product"Xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:nonamespaceschemalocation="sqlmap.xsd"> <alias> <typealias alias="Product"Type="NPetshop.Domain.Catalog.Product, Npetshop.domain"/> </alias> <cacheModels> <cachemodel id="Productlist-cache"implementation="MEMORY"> <flushinterval hours=" -"/> <property name="Type"Value="Weak"/> </cacheModel> </cacheModels> <resultMaps> <resultmap id="Productresult" class="Product"> <result property="Id"column="product_id"/> <result property="Name"column="Product_Name"/> <result property="Description"column="product_description"/> </resultMap> <resultmap id="ProductList" class="Product"extends="Productresult"> <result property="Category"resultmapping="Category.categoryresult"/> </resultMap> </resultMaps> <!--=============================================MAPPED Statements=============================================-<statements> <SelectId="getproductlistbycategory"Cachemodel="Productlist-cache"resultmap="ProductList"parameterclass="string">Selectproduct_id, Product_Name, Product_description, P.categ ory_id, Category_name, Category_description fromProducts asP, Categories asCwhereC.category_id=p.category_id and p.category_id=#value #</Select> <SelectId="getproduct"resultmap="Productresult"parameterclass="string">Selectproduct_id, product_name, Product_description from Productswhereproduct_id =#value #</Select> <SelectId="searchproductlist"resultmap="Productresult">Selectproduct_id, product_name, Product_description from Products<dynamic prepend="WHERE"> <iterate property="keywordlist"open=""Close=""conjunction="OR">Lower (product_name) like #KeywordList []# or Lower (category_id) like #KeywordList []# or Lower (Prod uct_description) Like #KeywordList []#</iterate> </dynamic> </Select> </statements></sqlMap>
Type: The types of caches, there are 4 types in ibatis.net, respectively, memory, LRU, FIFO, Oscache.
Where memory is the cache, the LRU is using the least recently used policy, FIFO is the use of first-out policy, Oscache is implemented by a third-party cache plug-in.
ID: is an identity of the Cachemodel that identifies the name of the cache for use in subsequent settings.
readOnly: Refers to the cached data object is read-only or read-write, the default is "true", that is, read-only, where the read-only does not mean that the data object once placed in the cache will no longer be able to modify the data. Instead, when the data object changes, such as a property of the data object changes, the data object will be abolished from the cache, the next time you need to re-read the data from the database to construct a new data object. The readonly= "false" means that the data objects in the cache can be updated.
Serialize: whether to read the same object from the cache or a copy of the object. This parameter is valid only if the ReadOnly is false, because the cache is read-only, and the objects returned for different sessions are definitely one, and only a copy of the object needs to be returned for each session when the cache is read-write.
flushinterval: Specifies the time that the cache is automatically refreshed, which can be hours, minutes, seconds, and milliseconds. Note that this interval is not the time, the information in the cache will be automatically refreshed, but after the interval, the next time the query will not be taken from the cache, and SQL to query, and the results of the query to update the cached value.
Flushonexecute: Specifies what happens when the cache is updated.
Property: Additional attribute configurations for Cachemodel, different types of Cachemodel will have their own proprietary properties, such as:
FIFO: <property name= "CacheSize" value= "/>"
LRU: <property name= "CacheSize" value= "/>"
MEMORY: <property name= "Type" value= "WEAK"/>
3 Statement Configuration
Statement configuration (Mapped statements): As the name implies, is the statement declaration of the map. It is the core of the XML data mapping file, and the executed SQL statements (or stored procedures) that are actually working with the database in the Ibatis.net framework must be explicitly declared here. The statement configuration can contain 6 different types of statements: statement, select, INSERT, UPDATE, Delete, and procedure. Where statement can contain all types of SQL statements (stored procedures), it is a general statement configuration, no specific responsibilities, instead, the other 5 types of statement configuration is specifically responsible for a variety of different SQL statements. The following table lists the different responsibilities and invocation methods for various types of statements.
Statement type |
Property |
Child elements |
Method |
<statement> |
Id Parameterclass ResultClass Parametermap Resultmap Cachemodel |
All dynamic elements |
Insert Update Delete All the query methods |
<insert> |
Id Parameterclass Parametermap |
All the dynamic elements <selectKey> |
Insert Update Delete |
<update> |
Id Parameterclass Parametermap |
All the dynamic elements |
Insert Update Delete |
<delete> |
Id Parameterclass Parametermap |
All the dynamic elements |
Insert Update Delete |
<select> |
Id Parameterclass ResultClass Parametermap Resultmap Cachemodel |
All the dynamic elements |
All the query methods |
<procedure> |
Id Parameterclass ResultClass Parametermap Resultmap |
All the dynamic elements |
Insert Update Delete All the query methods |
4 Special Configurations
4.1 XML escape character
Many times in SQL statements will use greater than or below the symbol (that is,:><), this time with the XML specification conflict, affecting the legitimacy of the XML mapping file. Avoid this situation by adding CDATA nodes, such as:
<statement id= "selectpersonsbyage" parameterclass= "int" resultclass= "person" >
<! [cdata[
SELECT * FROM person WHERE age > #value #
]]>
</statement>
4.2 Auto-Generate primary key
Many databases currently support the automatic generation of primary keys for newly inserted records, and Oracle provides this functionality, which is addressed by sequence plus triggers. This is certainly convenient, but when you want to get the primary key value of the record immediately after inserting a record, the problem arises because it is likely that you will have to write a new statement to get the primary key value. Ibatis.net provides a better way to solve this problem. These automatically generated primary key values are obtained and saved in the object by the <selectKey> element in the XML data map file.
As above, the "Add" action configuration information is modified to:
<insert id= "Insertsysuser" parameterclass= "Sysuser" >
<selectkey resultclass= "Int32" Property= "Userid" type= "Pre" >
SELECT DEAN. Seq_sysuser_userid. Nextval as Userid from DUAL
</selectKey>
INSERT into DEAN. Sysuser (Userid,password,loginname,sex,birthday,idcard,officephone,familyphone,mobilephone,email,address, Zipcode,remark,status)
VALUES (#Userid #, #Password #, #Loginname #, #Sex #, #Birthday #, #Idcard #, #Officephone #, #Familyphone #, #Mobilephone #,# email#, #Address #, #Zipcode #, #Remark #, #Status #)
</insert>
The Bold section is the newly added Selectkey node. The #Userid # parameter is populated with the values selected from the sequence in the node. To modify the code of the call:
Isqlmapper mapper = Mapper.instance (); Get Isqlmapper instances
Int32 result = (Int32) mapper. Insert ("Sysusermap.insertsysuser", model);//Call the Insert method
Label1.Text = "Add user succeeds, result returns userid" + convert.tostring (Result) + ", sysuser instance model with UserID" + convert.tostring "(model . Userid);
Run the program so that result gets the key value that you want to get, and the UserID property of Sysuser's instantiated object model returns the key value of the new record.
4.3 #与 $ choice
In the XML data map file, the parameters are represented by the # number. As mentioned earlier, you can also use $ to identify the #userid#. What is the difference between the two, and how to choose?
(1) #是把传入的数据当作参数, such as ORDER by #field #, if the value passed in by #field# is a character type, the incoming value is an ID, then the SQL statement generates an ORDER by "id", and it is obvious that the generated SQL statement is submitted to the database for error execution. #参数往往用在需传入实际变量值的情况, such as where Id= #id #, the variable #id# is passed in 10, the SQL statement generates where ID=10.
(2) $ The SQL statement is generated by stitching, that is, the incoming data is generated directly in the SQL statement, such as an order by $field $, if $field$ is passed in an ID, the SQL statement generates an order by ID. The $ parameter method is typically used to pass in a database object. For example, incoming table names. It is important to note, however, that the use of this parameter is directly composed of SQL statements, so you should be careful to prevent SQL injection.
4.4 Like statement processing
Fuzzy queries are often used during query processing. Ibatis How to deal with this fuzzy query? There are two ways to handle a like fuzzy query Ibatis:
(1) Use # parameter: Configure such as where UserName like #param #| | ' % ', in the way that parameters are passed, such as #param# passed in the character d, the resulting SQL statement is where UserName like ' d% ', other fuzzy situations can be configured accordingly.
(2) using the $ parameter: configuration such as where UserName like ' $param $% ', with string substitution, is to replace param with the value of the parameter, such as $param$ passing in the character D, can also generate the same statement.
The $ method causes SQL injection risk, and in practice, it is recommended that you use the first configuration method to process a like fuzzy query.
5 Conclusion
The above procedure is tested on WINDOWS7 (64-bit) +vs2012+oracle 11g (64-bit).
"Ibatis" Cachemodel, properties and special configurations