Ibatis framework file configuration

Source: Internet
Author: User

After learning ibatis in the last two days, I wrote it down.

First, configure the ibatis environment, add ibatis2.x. jar, mysql-connection-bin.5.1.8.jar, create a web project:

0. Write a class user. jsva with attributes:... (self-written), create a table: User. SQL

1. First create a database file datasource. properties. The content is as follows:

JDBC. Driver = com. MySQL. JDBC. Driver
JDBC. url = JDBC: mysql: // localhost: 3306/test
JDBC. Username = root
JDBC. Password = root

2, this is ibatis Core File: sqlmap-config.xml, the content is as follows:

<? 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>

<Sqlmapconfig>
<Properties resource = "datasource. properties"> </Properties>
<Transactionmanager type = "JDBC">
<Datasource type = "simple">
<Property name = "JDBC. Driver" value = "$ {JDBC. Driver}"/>
<Property name = "JDBC. connectionurl" value = "$ {JDBC. url}"/>
<Property name = "JDBC. username" value = "$ {JDBC. Username}"/>
<Property name = "JDBC. Password" value = "$ {JDBC. Password}"> </property>
</Datasource>
</Transactionmanager>
<Sqlmap resource = "com/resource/user. xml"/>
</Sqlmapconfig>

3. Create a user. xml file containing SQL statements. This is the advantage of ibatis that it is more flexible than hibernate to query data, simple crud:

<? 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>
<! -- Define category name -->
<Typealias alias = "user" type = "com. Bean. User"/>
 
<! -- Query all -->
<Select id = "getallusers" resultclass = "user">
Select *
From User
</SELECT>
 
<! -- Add -->
<Insert id = "saveuser" parameterclass = "user">
Insert into user (name, password, sex, address, phone, email)
Values
(# Name #, # password #, # Sex #, # address #, # Phone #, # email #)
<Selectkey resultclass = "int" keyproperty = "ID">
Select last_insert_id ()
</Selectkey>
</Insert>
 
<! -- Query by id -->
<Select id = "getbyid" parameterclass = "int" resultclass = "user">
Select * from user u where u. ID = # ID #
</SELECT>
 
<! -- Delete -->
<Delete id = "deleteuser" parameterclass = "int">
Delete from user where id = # ID #
</Delete>
 
<Update id = "updatebyid" parameterclass = "user">
Update user U set U. name = # name #, U. password = # password #, U. sex = # Sex #, U. address = # address #, U. phone = # Phone #, U. email = # email #
Where U. ID = # ID #
</Update>
</Sqlmap>

4. Test. Write a main () method and test whether the building is successful.

Public class ibatisdemo {


Public static void main (string [] ARGs) throws ioexception, exception {
// List ();
// User = new user ();
// User. setname ("ibatis ");
// User. setpassword ("123 ");
// User. setsex ("male ");
// User. setaddress ("Guangzhou Tianhe ");
// User. setphone ("1324567 ");
// User. setemail ("[email protected]");
// Add (User );

// Get (24 );

// User = new user ();
// User. setname ("ibatis2 ");
// User. setpassword ("1232 ");
// User. setsex ("male ");
// User. setaddress ("Guangzhou Tianhe 2 ");
// User. setphone ("13245672 ");
// User. setemail ("[email protected]");
// User. setid (23 );
// Update (User );

// Delete1 (23 );

}
 
Public static void list () throws ioexception, sqlexception {
Reader reader = resources. getresourceasreader ("sqlmap-config-user.xml ");
Sqlmapclient = sqlmapclientbuilder. buildsqlmapclient (Reader );
List <user> List = sqlmapclient. queryforlist ("getallusers ");
For (User: List ){
System. Out. println (User );
}
}

Public static void add (User user) throws ioexception, sqlexception {
Reader reader = resources. getresourceasreader ("sqlmap-config-user.xml ");
Sqlmapclient = sqlmapclientbuilder. buildsqlmapclient (Reader );
Sqlmapclient. insert ("saveuser", user );
}
Public static void get (integer ID) throws ioexception, sqlexception {
Reader reader = resources. getresourceasreader ("sqlmap-config-user.xml ");
Sqlmapclient = sqlmapclientbuilder. buildsqlmapclient (Reader );
// Method 1:
// List <user> List = sqlmapclient. queryforlist ("getbyid", ID );
// For (User: List ){
// System. Out. println ("----------------" + list. Size ());
//}
// Method 2:
User user0 = (User) sqlmapclient. queryforobject ("getbyid", ID );
System. Out. println ("----------------" + user0.getname ());
}
 
Public static void delete1 (int id) throws ioexception, sqlexception {
Reader reader = resources. getresourceasreader ("sqlmap-config-user.xml ");
Sqlmapclient = sqlmapclientbuilder. buildsqlmapclient (Reader );
Sqlmapclient. Delete ("deleteuser", ID );
}
 
Public static void Update (User user) throws ioexception, sqlexception {
Reader reader = resources. getresourceasreader ("sqlmap-config-user.xml ");
Sqlmapclient = sqlmapclientbuilder. buildsqlmapclient (Reader );
Sqlmapclient. Update ("updatebyid", user );
}
}

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.