First, an actual example of using ibatis to operate mysql

Source: Internet
Author: User

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:
<? Xml version = "1.0" encoding = "UTF-8"?>

<! DOCTYPE sqlMapConfig
PUBLIC "-// ibatis.apache.org//DTD SQL Map Config 2.0 // EN"
Http://ibatis.apache.org/dtd/sql-map-config-2.dtd>

<SqlMapConfig>

<! -- Configure a built-in transaction manager. If youre using
App server, you probably want to use its transaction manager
And a managed datasource -->
<TransactionManager type = "JDBC" commitRequired = "false">
<DataSource type = "SIMPLE">
<Property name = "JDBC. Driver" value = "com. mysql. jdbc. Driver"/>
<Property name = "JDBC. ConnectionURL" value = "jdbc: mysql: // 127.0.0.1: 3306/db"/>
<Property name = "JDBC. Username" value = "root"/>
<Property name = "JDBC. Password" value = ""/>
</DataSource>
</TransactionManager>

<! -- List the SQL Map XML files. They can be loaded from
Classpath, as they are here (com. domain. data...) -->
<SqlMap resource = "test_ibatis/User. xml"/>

</SqlMapConfig>

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

<! -- Use type aliases to avoid typing the full classname every time. -->
<TypeAlias alias = "User" type = "test_ibatis.User"/>

<! -- Select with no parameters using the result map for Account class. -->
<Select id = "selectAllUsers" resultClass = "User">
Select * from user
</Select>

</SqlMap>

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 <User> 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 <User> 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 );
}

Related Article

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.