Ibatis configuration file details

Source: Internet
Author: User

The basic ibatis code includes: <br/> 1. ibatis instance configuration <br/> A typical configuration file is as follows (see the following for details about the configuration items): <br/> <? XML version = "1.0" encoding = "UTF-8"?> <Br/> <! Doctype sqlmapconfig <br/> Public "-// ibatis.com//dtd SQL map config 2.0/<br/>" [url] scheme </P> <p> <sqlmapconfig> </P> <p> <Settings <br/> cachemodelsenabled = "true" <br/> enhancementenabled = "true" <br/> lazyloadingenabled = "true" <br/> errortracingenabled = "true ""<br/> maxrequests =" 32 "<br/> maxsessions =" 10 "<br/> maxtransactions =" 5 "<br/> usesta Tementnamespaces = "false" <br/> </P> <p> <transactionmanager type = "JDBC"> <br/> <datasource type = "simple"> <br /> <property name = "JDBC. driver "value =" com. p6spy. engine. spy. p6spydriver "/> <br/> <property name =" JDBC. connectionurl "value =" JDBC: mysql: // localhost/sample "/> <br/> <property name =" JDBC. username "value =" user "/> <br/> <property name =" JDBC. password "value =" mypass "/> <br /> <Property name = "pool. maximumactiveconnections "value =" 10 "/> <br/> <property name =" pool. maximumidleconnections "value =" 5 "/> <br/> <property name =" pool. maximumcheckouttime "value =" 120000 "/> <br/> <property name =" pool. timetowait "value =" 500 "/> <br/> <property name =" pool. pingquery "value =" select 1 from account "/> <br/> <property name =" pool. pingenabled "value =" false "/> <br/> <Property name = "pool. pingconnectionsolderthan "value =" 1 "/> <br/> <property name =" pool. pingconnectionsnotusedfor "value =" 1 "/> <br/> </datasource> <br/> </transactionmanager> </P> <p> <sqlmap resource =" com/ibatis /sample/user. XML "/> </P> <p> </sqlmapconfig> (1) settings node <br/> cachemodelsenabled <br/> whether to enable the cache mechanism on sqlmapclient. We recommend that you set this parameter to "true" <br/> enhancementenabled <br/> whether to enable bytecode for pojo to enhance the call efficiency of getter/setter and avoid performance overhead caused by reflect. This also improves lazy loading. We recommend that you set it to "true" <br/> errortracingenabled <br/> whether to enable error logs, we recommend that you set this parameter to "true" during development to facilitate debugging <br/> lazyloadingenabled <br/> whether to enable the delayed loading mechanism, it is recommended to set it to "true" <br/> maxrequests <br/> maximum number of concurrent requests (statement concurrency) <br/> maxtransactions <br/> maximum number of concurrent transactions <br/> maxsessions maximum number of sessions. That is, the maximum number of concurrent sqlmapclients allowed currently. <Br/> usestatementnamespaces <br/> whether to use the statement namespace. <Br/> The namespace here refers to the namespace attribute of the sqlmap section in the ing file. For example, the sqlmap node of the t_ing file for the t_use <br/> table in the preceding example: <sqlmap namespace = "user"> operations defined under this sqlmap node belong to the "user" namespace. When usestatementnamespaces = "true", the statement call needs to append the namespace, for example, sqlmap. update ("user. updateuser ", user); <br/> otherwise, it can be called directly by the statement name, for example, sqlmap. update ("updateuser", user); note that the statement definition does not have a duplicate name in all ing files. <Br/> transactionmanager node <br/> the transactionmanager node defines the ibatis Transaction Manager. Currently, it provides the following options: <br/> jdbc <br/> using traditional JDBC connection. commit/rollback implement transaction support. <Br/> JTA <br/> use the JTA service provided by the container to implement global transaction management. <Br/> external transaction management. For example, if ibatis is used in ejbs, you can use the EJB deployment configuration to implement a self-initiated transaction management mechanism. <br/>. In this case, ibatis delegates all transactions to external containers for management. <Br/> datasource node <br/> datasource belongs to the transactionmanager node and is used to set the datasource attribute used during ibatis runtime. <Br/> type attribute: <br/> the type attribute of the datasource node specifies the implementation type of datasource. Optional items: <br/> simple is the built-in datasource Implementation of ibatis, which implements a simple <br/> database connection pool mechanism, the corresponding ibatis implementation class is <br/> COM. ibatis. sqlmap. engine. datasource. simpleperformancefactory. <Br/> DBCP: <br/> datasource encapsulation Based on the Apache DBCP connection pool component. This option is recommended when no container is provided <br/> for datasource service, the corresponding ibatis implementation class is <br/> COM. ibatis. sqlmap. engine. datasource. dbcpperformancefactory. <Br/> JNDI: <br/> the datasource implementation provided by the J2EE container is used. datasource is obtained from the container by specifying the <br/> JNDI name. The corresponding ibatis implementation class is <br/> com. ibatis. sqlmap. Engine. datasource. jndidatasourcefactory. <Br/> description of child nodes of datasource (simple & DBCP): <br/> JDBC. Driver JDBC driver. <Br/> for example, org. gjt. Mm. MySQL. Driver. JDBC. connectionurl <br/> Database URL. <Br/> For example: JDBC: mysql: // localhost/sample <br/> If sqlserver JDBC driver is used, you need to append selectmethod = cursor after the URL to obtain multi-statement support for JDBC transactions. <Br/> JDBC. username <br/> database username <br/> JDBC. password <br/> Database User Password <br/> pool. maximumactiveconnections <br/> maximum capacity that can be maintained by the database connection pool. <Br/> pool. maximumidleconnections <br/> Number of suspended (idle) connections allowed in the database connection pool. <Br/> most of the configuration of JNDI is performed on the application server, so the configuration in ibatis is relatively simple <br/> use the jdni configuration of JDBC and JTA transaction management respectively: <br/> Configure the JNDI datasource using JDBC transaction management <br/> <transactionmanager type = "JDBC"> <br/> <datasource type = "JNDI"> <br/> <property name = "datasource" value = "Java: COMP/ENV/jdbc/mydatasource "/> <br/> </datasource> <br/> </transactionmanager> <br/> <transactionmanager type =" JTA "> <br /> <property name = "usertransaction" value = "J Ava:/CTX/CON/usertransaction "/> <br/> <datasource type =" JNDI "> <br/> <property name =" datasource "value =" Java: COMP/ENV/jdbc/mydatasource "/> <br/> </datasource> sqlmap node <br/> the sqlmap node specifies the location of the ing file, multiple sqlmap nodes can appear in the configuration to specify all the ing files contained in the <br/> project. <Br/> basic ibatis semantics <br/> xmlsqlmapclientbuilder is a new component introduced in later versions of ibatis 2.0. It is used to replace xmlsqlmapbuilder in version 1.x< br/>. The function is to create a sqlmapclient instance based on the configuration file. <Br/> sqlmapclient is a core component of ibatis and provides a basic platform for data operations. Sqlmapclient <br/> can be created through xmlsqlmapclientbuilder: <br/> string resource = "com/ibatis/sample/sqlmapconfig. XML "; <br/> reader; </P> <p> reader = resources. getresourceasreader (Resource); </P> <p> xmlsqlmapclientbuilder xmlbuilder = <br/> New xmlsqlmapclientbuilder (); </P> <p> sqlmapclient sqlmap = xmlbuilder. buildsqlmap (Reader); "com/ibatis/sample/sqlmapconfig. XML indicates the relative path of the configuration file in classpath <br/>. Xmlsqlmapclientbuilder creates a sqlmapclient instance based on the configuration parameters by accepting a reader-type configuration file <br/> handle. <Br/> sqlmapclient provides many data operation methods. The following are examples of common methods. For more information, see ibatis Java Doc or ibatis official development manual. <Br/> sqlmapclient basic operation example <br/> The following example is taken from the ibatis official development manual. The author re-typeset it for better reading. <Br/> Example 1: insert, update, delete: <br/> sqlmap. starttransaction (); <br/> product Product = new product (); <br/> product. setid (1); <br/> product. setdescription ("shih tzu"); <br/> int rows = sqlmap. insert ("insertproduct", product); <br/> sqlmap. committransaction (); Example 2: Data Query (select) <br/> sqlmap. starttransaction (); <br/> integer key = new INTEGER (1); <br/> product Product = (product) sqlmap. queryforobject ("getproduct", key); <br/> sqlmap. committransaction (); Example 3: store the query result (select) in the specified object <br/> sqlmap. starttransaction (); <br/> customer = new customer (); <br/> sqlmap. queryforobject ("getcust", parameterobject, customer); <br/> sqlmap. queryforobject ("getaddr", parameterobject, customer); <br/> sqlmap. committransaction (); example 4: execute a batch query (select) <br/> sqlmap. starttransaction (); <br/> List list = sqlmap. queryforlist ("getproductlist", null) <br/> sqlmap. committransaction (); example 5: About autocommit <br/> // when starttransaction is not executed in advance, the default mode is auto_commit <br/> int rows = sqlmap. insert ("insertproduct", product); Example 6: query data within a specified range <br/> sqlmap. starttransaction (); <br/> List list = sqlmap. queryforlist ("getproductlist", null, 0, 40); <br/> sqlmap. committransaction (); Example 7: Query with rowhandler (select) <br/> public class myrowhandler implements rowhandler {<br/> Public void handlerow (Object object, list List) throws <br/> sqlexception {<br/> product Product = (product) object; <br/> product. setquantity (10000); <br/> sqlmap. update ("updateproduct", product); <br/>}< br/> sqlmap. starttransaction (); <br/> rowhandler = new myrowhandler (); <br/> List list = sqlmap. queryforlist ("getproductlist", null, <br/> rowhandler); <br/> sqlmap. committransaction (); // Example 8: Paging query (select) <br/> paginatedlist list = <br/> sqlmap. queryforpaginatedlist ("getproductlist", null, 10); <br/> list. nextpage (); <br/> list. previouspage (); // Example 9: map-based batch query (select) <br/> sqlmap. starttransaction (); <br/> map = sqlmap. queryformap ("getproductlist", null, "productcode"); <br/> sqlmap. committransaction (); <br/> product P = (product) map. get ("EST-93 ");

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.