Step 1:
Package com. ibatis;
Public class author {
Private int ID;
Private string name;
Public int GETID (){
Return ID;
}
Public void setid (int id ){
This. ID = ID;
}
Public String getname (){
Return name;
}
Public void setname (string name ){
This. Name = Name;
}
}
Step 2: Author. xml
<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype sqlmap
Public "-// ibatis.com//dtd SQL map 2.0 // en"
Http://www.ibatis.com/dtd/sql-map-2.dtd>
<Sqlmap namespace = "author">
<! -- Module configuration -->
<! -- Set the alias in the ing file -->
<Typealias alias = "author" type = "com. ibatis. Author"/>
<! --
<Cachemodel type = "LRU" id = "authorcache">
Sets the cache validity period. If this time is exceeded, the cache is cleared.
<Flushinterval hours = "24"> </flushinterval>
Indicates that the cache is cleared when a specific statement is executed.
<Flushonexecute Statement = "updateauthor"/>
Size: Maximum number of data objects in this cachemodel
<Property value = "1000" name = "size"/>
</Cachemodel>
The module configuration is required, for example, <select id = "getallauthor" resultclass = "author" cachemodel = "authorcache">
Use cachemodel "authorcache" to cache records. Program Use statement to query data.
Retrieve data from the cache instead of from the database
-->
<! -- Statement configuration -->
<! -- Modify -->
<Update id = "updateauthor" parameterclass = "author">
<! [CDATA [update author set name = # name # Where id = # ID #]>
</Update>
<! -- Delete -->
<Delete id = "deleteauthor" parameterclass = "author">
Delete from author where id = # ID #
</Delete>
<! -- Query all records -->
<Select id = "getallauthor" resultclass = "author">
<! [CDATA [select * from author]>
</SELECT>
<! -- Add -->
<Insert id = "insertauthor" parameterclass = "author">
<! [CDATA [insert into author (ID, name) values (# ID #, # name #)]>
</Insert>
</Sqlmap>
Step 3: sqlmapconfig. Properties
Driver = com. Microsoft. JDBC. sqlserver. sqlserverdriver
Url = JDBC: Microsoft: sqlserver: // 127.0.0.1: 1433; databasename = ibatis
Username = sa
Password =
Step 4: sqlmapconfig. xml
<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype sqlmapconfig
Public "-// ibatis.com//dtd SQL map config 2.0 // en"
Http://www.ibatis.com/dtd/sql-map-config-2.dtd>
<! -- Ibatis configuration file -->
<Sqlmapconfig>
<! -- Load the database connection property file -->
<Properties resource = "com/ibatis/sqlmapconfig. properties"/>
<! --
Cachemodelsenabled: whether to enable the cache mechanism of sqlmapclient.
Enhancementenabled: whether to enable the bytecode increase mechanism for pojo to improve the call utility of geter/seter.
Load brings about and major performance improvements.
Lazyloadingenabled: whether to enable the delayed loading mechanism.
Maxrequests: Maximum and large number of requests.
Maxsessions: the maximum number of sessions allowed for development.
Maxtransactions: Maximum number of concurrent transactions.
-->
<Settings
Cachemodelsenabled = "true"
Enhancementenabled = "true"
Lazyloadingenabled = "true"
Maxrequests = "32"
Maxsessions = "10"
Maxtransactions = "5"
Usestatementnamespaces = "false"
/>
<! -- Datasource -->
<Transactionmanager type = "JDBC">
<Datasource type = "simple">
<! -- JDBC driver -->
<Property name = "JDBC. Driver" value = "$ {driver}"/>
<! -- Database URL -->
<Property name = "JDBC. connectionurl" value = "$ {URL}"/>
<! -- Database username -->
<Property name = "JDBC. username" value = "$ {username}"/>
<! -- Database Password -->
<Property name = "JDBC. Password" value = "$ {password}"/>
<! -- I don't know. I can't find it on the website. I have time to study it. -->
<Property name = "JDBC. defaultautocommit" value = "true"/>
<! -- Maximum capacity that can be maintained by the database connection pool -->
<Property name = "pool. maximumactiveconnections" value = "10"/>
<! -- Number of pending connections allowed in the database connection pool -->
<Property name = "pool. maximumidleconnections" value = "5"/>
<! -- Maximum time occupied by a task in the database connection pool -->
<Property name = "pool. maximumcheckouttime" value = "120000"/>
<! -- When a thread wants to obtain a connection from the connection pool, there is no available connection in the connection pool. This parameter sets the maximum waiting time allowed by the thread. -->
<Property name = "pool. timetowait" value = "500"/>
<! -- Database connection status check statement -->
<Property name = "pool. pingquery" value = "select 1 from author"/>
<! -- Check whether connection status is allowed -->
<Property name = "pool. pingenabled" value = "false"/>
<! -- Checks connections with persistent connections exceeding the set value -->
<Property name = "pool. pingconnectionsolderthan" value = "1"/>
<! -- Check connections with idle space exceeding the set value -->
<Property name = "pool. pingconnectionsnotusedfor" value = "1"/>
</Datasource>
</Transactionmanager>
<! -- Load the sqlmap file -->
<Sqlmap resource = "com/ibatis/author. xml"/>
</Sqlmapconfig>
Step 5:
Package com. ibatis;
Import java. Io. ioexception;
Import java. Io. reader;
Import com. ibatis. Common. Resources. Resources;
Import com. ibatis. sqlmap. Client. sqlmapclient;
Import com. ibatis. sqlmap. Client. sqlmapclientbuilder;
Public class sqlmapconf {
// Initialize sqlmapclient
Private Static sqlmapclient;
Static {
// Define the path of the ibatis configuration file
String resource = "com/ibatis/sqlmapconfig. xml ";
Try {
// Read the ibatis configuration file
Reader reader = resources. getresourceasreader (Resource );
// Create sqlmapclient through sqlmapclientbuilder
Sqlmapclient = sqlmapclientbuilder. buildsqlmapclient (Reader );
} Catch (ioexception e ){
// Todo auto-generated Catch Block
System. Out. println ("sqlmapconfig. xml file not found ~~ ");
}
}
Public static sqlmapclient getinstance (){
// Return to sqlmapclient. sqlmapclient is the core master of ibatis and provides a basic platform for data operations.
Return sqlmapclient;
}
/**
* Another method for creating sqlmapclient
* Xmlsqlmapclientbuilder xmlbuilder = new xmlsqlmapclientbuilder ();
* Sqlmapclient = xmlbuilder. buildersqlmap (Reader );
* Xmlsqlmapclientbuilder is a new component introduced after ibatis2.0 to replace
* Xmlsqlmapbuilder is used to create sqlmapclient.
*/
}
Step 6:
Package com. ibatis;
Import java. SQL. sqlexception;
Import java. util. List;
Import java. util .*;
Import com. ibatis. sqlmap. Client. sqlmapclient;
/**
* Ibatis Transaction Manager. Currently, only three types of transaction managers are supported: JDBC, JTA, and external.
* JDBC: supports transactions through the traditional JDBC connection. comit/rollback.
* JTA: Use the JTA service provided by the container to implement global transaction management.
* External: external transaction management. For example, if ibatis is used in ejbs, the automatic transaction management mechanism can be achieved through EJB deployment and configuration.
*. In this case, ibatis delegates all transactions to external containers for management.
*/
Public class ibatisclient {
Private Static sqlmapclient = sqlmapconf. getinstance ();
// Modify the name based on the primary key ID
Public static void updateauthor (int id, string name ){
Author author = new author ();
Author. setid (ID );
Author. setname (name );
Try {
// Start the transaction and use JDBC Transaction Management
Sqlmapclient. starttransaction ();
Sqlmapclient. Update ("updateauthor", author );
Sqlmapclient. committransaction ();
} Catch (sqlexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
System. Out. println ("modification error ~~ ");
}
Finally {
Try {
Sqlmapclient. endtransaction ();
} Catch (sqlexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
}
}
// Query all records and return a set
Public static list findall (){
List list = NULL;
Try {
Sqlmapclient. starttransaction ();
// 0: set the number of records starting from
// 1: Set the display record
// List = sqlmapclient. queryforlist ("getallauthor", null, 0, 1 );
List = sqlmapclient. queryforlist ("getallauthor", null );
Sqlmapclient. committransaction ();
} Catch (sqlexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
System. Out. println ("query error ~~ ");
}
Finally {
Try {
Sqlmapclient. endtransaction ();
} Catch (sqlexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
}
Return list;
}
// Add operation
Public static Boolean insert (int id, string name ){
Boolean bool = false;
Author author = new author ();
Author. setid (ID );
Author. setname (name );
Try {
Sqlmapclient. starttransaction ();
Sqlmapclient. insert ("insertauthor", author );
Bool = true;
Sqlmapclient. committransaction ();
} Catch (sqlexception e ){
// Todo auto-generated Catch Block
Bool = false;
E. printstacktrace ();
System. Out. println ("add error ~~ ");
}
Finally {
Try {
Sqlmapclient. endtransaction ();
} Catch (sqlexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
}
Return bool;
}
// Delete operation
Public static Boolean Delete (int id ){
Boolean bool = false;
Author author = new author ();
Author. setid (ID );
Try {
Sqlmapclient. committransaction ();
Sqlmapclient. Delete ("deleteauthor", author );
Bool = true;
Sqlmapclient. starttransaction ();
} Catch (sqlexception e ){
// Todo auto-generated Catch Block
Bool = false;
E. printstacktrace ();
System. Out. println ("deletion error ~~ ");
}
Finally {
Try {
Sqlmapclient. endtransaction ();
} Catch (sqlexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
}
Return bool;
}
Public static void main (string STR []) {
// Delete
// Boolean bool = ibatisclient. Delete (3 );
// Add
// Boolean bool = ibatisclient. insert (3, "wanwu ");
// Modify
// Ibatisclient. updateauthor (3, "JJ ");
// Query all records
List list = ibatisclient. findall ();
Iterator = List. iterator ();
While (iterator. hasnext ()){
Author author = (author) iterator. Next ();
System. Out. println ("ID =" + author. GETID ());
System. Out. println ("name =" + author. getname ());
}
}
}
The above is a simple example of adding, deleting, modifying, and querying ibatis.