一个使用ibatis对数据库增删改查的例子:
这里用的开发环境是:Eclipse3.2+mysql5.0.20,ibatis包是ibatis-common-2.jar,
ibatis-dao-2.jar,ibatis-sqlmap-2.jar,mysql包是mysql-connector-java-5.0.3-bin.jar.
步骤:
1.创建数据库:
create database itcast;
use itcast;
创建表:
create table student
(
id int primary key auto_increment,
firstname varchar(20) not null,
lastname varchar(20) not null
)
2.创建POJO类,Student.java,此程序中用到的所有类都放在cn.itcast包下面,
其他的配置文件都放在txd.configfile包下面。
package cn.itcast;
public class Student {
private Integer id;
private String firstname;
private String lastname;
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
}
3.跟Student类对应的xml配置文件Student.xml,
insert into student(firstname,lastname) values
(#firstname#,#lastname#)
select id, firstname, lastname from student
delete from student where id=#value#
update student set firstname=#firstname#,lastname=#lastname#
where id=#id#
4.jdbc.properties文件,存储数据库连接的driver,url,username,password等信息,
driver=com.mysql.jdbc.Driver
url=jdbc:mysql:///itcast
username=root
password=
5. SqlMap的配置文件SqlMapConfigExample.xml,
/p>
PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-config-2.dtd">
lazyLoadingEnabled="true" maxRequests="32" maxSessions="10"
maxTransactions="5" useStatementNamespaces="false" />
6.MySqlMapClient.java类,用于产生一个SqlMapClient
package cn.itcast;
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 MySqlMapClient {
private static SqlMapClient sqlMapClient;
static {
String resource = "txd/configfile/SqlMapConfigExample.xml";
Reader reader = null;
try {
reader = Resources.getResourceAsReader(resource);
sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlMapClient getSqlMapInstance() {
return sqlMapClient;
}
}
7.StudentManager 类测试一下上面的代码,
package cn.itcast;
import java.sql.SQLException;
import java.util.List;
import com.ibatis.sqlmap.client.SqlMapClient;
public class StudentManager {
public static void main(String[] args) {
SqlMapClient sqlMap = MySqlMapClient.getSqlMapInstance();
// insert一条记录
// Student student = new Student();
// student.setFirstname("zhang");
// student.setLastname("san");
// try {
// sqlMap.insert("insert_student", student);
// System.out.println("插入成功!");
// } catch (SQLException e) {
// e.printStackTrace();
// }
// delete一条记录
// try {
// int result = sqlMap.delete("delStudent", 5);
// System.out.println(result);
// } catch (SQLException e) {
// e.printStackTrace();
// }
// update一条已存在的记录
Student student = new Student();
student.setId(1);
student.setFirstname("jiang");
student.setLastname("zemin");
try {
int result = sqlMap.update("updateStudent", student);
System.out.println(result);
} catch (SQLException e) {
e.printStackTrace();
}
// query获得多条记录
// try {
// List list = sqlMap.queryForList("getStudent", null);
// for (Student student : list) {
// System.out
// .println(student.getId() + "\t"
// + student.getFirstname() + "\t"
// + student.getLastname());
// }
// } catch (SQLException e) {
// e.printStackTrace();
// }
// query单条记录
// try {
// Student student = (Student) sqlMap.queryForObject("getStudent", 1);
// System.out.println(student.getId());
// System.out.println(student.getFirstname());
// System.out.println(student.getLastname());
// } catch (SQLException e) {
// e.printStackTrace();
// }
}
}