Ibatis use 3

Source: Internet
Author: User

3.6 use a namespace to identify the same ID

Public class ibatissamesqlmapid {// ibatis core object. You can perform database operations Private Static sqlmapclient sqlmapper; static {try {// read the ibatis database connection configuration file reader = resources. getresourceasreader ("sqlmapconfig. XML "); // create the ibatis core object sqlmapper = sqlmapclientbuilder. buildsqlmapclient (Reader); // close the file input stream reader. close ();} catch (ioexception e) {// fail fast. throw new runtimeexception ("something bad happened while Bui LDing the sqlmapclient instance. "+ E, E) ;}/ *** @ Param ARGs */public static void main (string [] ARGs) throws exception {queryforlist ();} public static void queryforlist () throws exception {// The ibatis Technical Framework binds sqlmapid and SQL together. If the same name of sqlmapid exists in multiple ing files, the problem may occur. // Solution: // 1) modify the ing ID name to ensure that it is different. // 2) Add a namespace to solve the same name problem. Similar to the concept of Java package. // If you specify a namespace, you must run the namespace when accessing the sqlid. // if you want to use the namespace, you must enable the namespace for ibatis and modify the connection configuration file. String sqlmapid = "user. selectlist"; sqlmapper. queryforlist (sqlmapid );}}

3.6 SQL File

<? XML version = "1.0" encoding = "UTF-8"?> <! Doctype sqlmap public "-// ibatis.apache.org//dtd SQL map 2.0/EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd"> <sqlmap namespace = "user"> <! The -- select tag declares that the query SQL statement ID attribute indicates the SQL statement Association ID. When the program accesses this ID, it is equivalent to accessing this SQL statement. The resultclass attribute indicates that when the query result is available, the result is directly converted to an object of the specified type --> <! -- # In SQL indicates that the SQL statement requires parameters, which can be replaced? No. --> <! -- The query result uses reflection to place the values of all query fields in the attributes of the object. If the query field does not exist, if the attribute value is null usercode> setusercode usercode setuser_code user_code> setuser_code, if the table field name is inconsistent with the object attribute name, you can use aliases to encapsulate data. --> <Select id = "selectuserbycode" resultclass = "com. CJF. ibatis. bean. user "> select usercode as user_code, username, userpswd, orgtype from t_user where usercode = # usercode # </SELECT> <! -- The resultclass attribute indicates that each piece of data to be queried is converted to a specified type of object. If multiple pieces of data are returned, the converted object is automatically encapsulated as a collection object --> <select id = "selectusers" resultclass = "com. CJF. ibatis. bean. user "> select * From t_user </SELECT> <select id =" selectlist "resultclass =" com. CJF. ibatis. bean. user "> select * From t_user </SELECT> </sqlmap>

---------------------

3.7 Use Cache

Public class ibatisquerycache {// ibatis core object. You can perform database operations Private Static sqlmapclient sqlmapper; static {try {// read the ibatis database connection configuration file reader = resources. getresourceasreader ("sqlmapconfig. XML "); // create the ibatis core object sqlmapper = sqlmapclientbuilder. buildsqlmapclient (Reader); // close the file input stream reader. close ();} catch (ioexception e) {// fail fast. throw new runtimeexception ("something bad happened while build Ing the sqlmapclient instance. "+ E, E) ;}/ *** @ Param ARGs */public static void main (string [] ARGs) throws exception {queryforlist ();} public static void queryforlist () throws exception {// ibatis technical framework query cache // ibatis associates the query result with the ing ID and places it in the cache, when I access the same ing ID again, the database is no longer accessed, but the query efficiency is obtained from the cache. // Steps for using the cache: // 1) enable the cache, modify the connection configuration file, and add cache operations. // The query cache may have problems with modification. The modification operation may result in inconsistent data. String sqlmapid = "usercache. selectusers "; List <user> userlist = sqlmapper. queryforlist (sqlmapid); For (User U: userlist) {system. out. println (U. getUserName ();} system. out. println ("**************************"); sqlmapid = "usercud. updateuser "; user = new user (); User. setusercode ("1"); User. setusername ("zhangsan"); sqlmapper. update (sqlmapid, user); system. out. println ("**************************"); userlist = sqlmapper. queryforlist (sqlmapid); For (User U: userlist) {system. out. println (U. getUserName ());}}}

3.7 SQL File

<? XML version = "1.0" encoding = "UTF-8"?> <! Doctype sqlmap public "-// ibatis.apache.org//dtd SQL map 2.0/EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd"> <sqlmap namespace = "usercache"> <! -- Cache model, where the query results can be placed in the cache. The ID attribute indicates the reference ID of the model. The ID type attribute indicates the cache processing method. LRU: The most recently used object memoery: the referenced Cache Policy is related to the garbage collector. FIFO: first in first out Oscache: Cache Policy Based on Oscache components. --> <Cachemodel type = "LRU" id = "usercache"> <property name = "size" value = "100"/> <! -- When performing an operation, you can clear the query results. The next query will still access the database to obtain the latest data --> <flushonexecute Statement = "usercud. updateuser "/> <! -- When the specified time is reached, the cached data is automatically cleared. --> <Flushinterval hours = "2" minutes = "30" seconds = "30"/> </cachemodel> <select id = "selectusers" resultclass = "com. CJF. ibatis. bean. user "cachemodel =" usercache "> select * From t_user </SELECT> </sqlmap>

 

 

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.