The first practical example of using ibatis to operate mysql is iBATIS.
BitsCN.com
The procedure is as follows:
1. create a new project ibatisnew in eclipse, and import the jar packages of mysql and ibatis. These two packages (ibatis-2.3.4.726.jar + mysql-connector-java-5.0.8-bin.jar) can be downloaded from the Internet and copied directly to the WEB-INF/lib directory.
2. create the SqlMapConfig. xml file.
This file contains the configuration of the database and the reference section of the xml corresponding to each data table.
File name: SqlMapConfig. xml
File content:
PUBLIC "-// ibatis.apache.org//DTD SQL Map Config 2.0 // EN"
Http://ibatis.apache.org/dtd/sql-map-config-2.dtd>
3. create the User. xml file referenced in SqlMapConfig. xml,
This file corresponds to the user table in the database. in this file, you can define aliases and write SQL statements.
File name: User. xml
File content:
PUBLIC "-// ibatis.apache.org//DTD SQL Map 2.0 // EN"
Http://ibatis.apache.org/dtd/sql-map-2.dtd>
Select * from user
4. create a class file specified in the user. xml file.
This file is a javabean, which corresponds to the fields of the database table and has the set and get methods.
File name; User. java
File content:
Package test_ibatis;
Import java. SQL. Date;
Public class User {
@ Override
Public String toString (){
// TODO Auto-generated method stub
String str = "id =" + this. id;
Str + = "name =" + this. name;
Str + = "birthday =" + this. birthday;
Str + = "money =" + this. money;
Return str;
}
Private int id;
Private String name;
Private Date birthday;
Private float money;
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;
}
Public Date getBirthday (){
Return birthday;
}
Public void setBirthday (Date birthday ){
This. birthday = birthday;
}
Public float getMoney (){
Return money;
}
Public void setMoney (float money ){
This. money = money;
}
}
5. establish DAO program and implementation program
Interface name: IUserDAO. java
Content:
Package test_ibatis;
Import java. util. List;
Public interface IUserDAO {
Public List GetAllUser ();
}
Implementation class: IUserDAOImpl. java
Content:
Package test_ibatis;
Import java. io. IOException;
Import java. io. Reader;
Import java. SQL. SQLException;
Import java. util. List;
Import com. ibatis. common. resources. Resources;
Import com. ibatis. sqlmap. client. SqlMapClient;
Import com. ibatis. sqlmap. client. SqlMapClientBuilder;
Public class IUserDAOImpl implements IUserDAO {
Private static SqlMapClient client = null;
Static {
Try {
Reader reader = Resources. getResourceAsReader ("test_ibatis/SqlMapConfig. xml ");
Client = SqlMapClientBuilder. buildSqlMapClient (reader );
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
Public List GetAllUser (){
// TODO Auto-generated method stub
Try {
Return client. queryForList ("selectAllUsers ");
} Catch (SQLException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
Return null;
}
}
1. the implementation class must use the functions provided by ibatis. First, read the configuration from the xml file to construct the SqlMapClient object.
2. the specific implementation method is to call the method provided by SqlMapClient and specify the id in the xml to execute the corresponding SQL statement and return the result.
6. test the class
File name: UserDAO. java
File content:
Package test_ibatis;
Public class UserDAO {
/**
* @ Param args
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
IUserDAO dao = new IUserDAOImpl ();
For (User user: dao. getAllUser ()){
System. out. println (user );
}
& NbspbitsCN.com