一、ibatis的配置和测试
ibatis的配置和测试的项目的结构图如下:
步骤如下:
1. 先创建一个web项目Z_ibatisTest.导入mysql和ibatis的相应jar包,我这边用的是(mysql-connector-java-5.1.15-bin.jar,ibatis-2.3.4.726\lib\ibatis-2.3.4.726.jar).
2. 在mysql数据库中创建相应的表,并创建相应的实体类Student.java
create table student(
id int primary key auto_increment,
name varchar(20),
password varchar(20),
birthday date,
create_time datetime
);
insert into student (name,password,birthday,create_time)
values('aa','aa',now(),now());
insert into student (name,password,birthday,create_time)
values('bb','bb',now(),now());
insert into student (name,password,birthday,create_time)
values('cc','cc',now(),now());
package com.wendellup.entity;
import java.util.Date;
public class Student {
private Integer id;
private String name;
private String password;
private Date birthday;
private Date createTime;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public String toString(){
return "id:"+id+", name:"
+name+", password:"+password
+", birthday:"+birthday
+", creatTime:"+createTime;
}
}
3. 创建与ibatis相关的数据源配置文件db.properties,SqlMapConfig.xml,Student.xml
driver=com.mysql.jdbc.Driver
url=jdbc\:mysql\://localhost\:3306/train_demo2
username=root
password=root
<?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>
<properties resource="ibatis/db.properties" />
<settings cacheModelsEnabled="true" enhancementEnabled="true"
lazyLoadingEnabled="true" errorTracingEnabled="true" maxRequests="32"
maxSessions="10" maxTransactions="5" useStatementNamespaces="true" />
<transactionManager type="JDBC">
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="${driver}" />
<property name="JDBC.ConnectionURL" value="${url}" />
<property name="JDBC.Username" value="${username}" />
<property name="JDBC.Password" value="${password}" />
<property name="Pool.MaximumActiveConnections" value="10" />
<property name="Pool.MaximumIdleConnections" value="5" />
<property name="Pool.MaximumCheckoutTime" value="120000" />
<property name="Pool.TimeToWait" value="500" />
<property name="Pool.PingEnabled" value="false" />
<property name="Pool.PingConnectionsOlderThan" value="1" />
<property name="Pool.PingConnectionsNotUsedFor" value="1" />
</dataSource>
</transactionManager>
<sqlMap resource="ibatis/Student.xml" />
</sqlMapConfig>
<?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>
<typeAlias alias="Student" type="com.wendellup.entity.Student"/>
<resultMap class="Student" id="studentResult">
<result property="id" column="ID"/>
<result property="name" column="NAME"
javaType="String" jdbcType="Varchar"/>
<result property="password" column="PASSWORD"
javaType="String" jdbcType="Varchar"/>
<result property="birthday" column="BIRTHDAY"
javaType="java.util.Date" jdbcType="Date"/>
<result property="createTime" column="CREATE_TIME"
javaType="java.util.Date" jdbcType="datetime"/>
</resultMap>
<select id="selectAllStudent" resultMap="studentResult">
select ID,NAME,PASSWORD,BIRTHDAY,CREATE_TIME from student;
</select>
<insert id="insertStudent" parameterClass="Student">
<![CDATA[
insert into student
(NAME,PASSWORD,BIRTHDAY,CREATE_TIME)
values (#name#,#password#,#birthday#,#createTime#);
]]>
<selectKey resultClass="int" keyProperty="id">
select max(id) from student;
</selectKey>
</insert>
</sqlMap>
4. 创建工具类IBatisUtil.java
package com.wendellup.util;
import java.io.InputStream;
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
public class IbatisUtil {
private static SqlMapClient client;
static{
InputStream inputStream = null;
try{
inputStream = Resources.class.getClassLoader().getResourceAsStream("ibatis/SqlMapConfig.xml");
}catch (Exception e) {
e.printStackTrace();
}
client = SqlMapClientBuilder.buildSqlMapClient(inputStream);
}
public static SqlMapClient getClient(){
return client;
}
public static void main(String[] args) {
System.out.println(IbatisUtil.getClient());
}
}
5. 在项目中创建与表相应的数据访问对象接口类IStudentDAO.java,数据访问对象类StudentDAOImpl.java
package com.wendellup.dao;
import java.util.List;
import com.wendellup.entity.Student;
public interface IStudentDAO {
/** 查询所有的学生信息 */
public List<Student> queryAllStudent() throws Exception ;
/** 插入一条学生信息 */
public int addStudent(Student student) throws Exception;
}
package com.wendellup.dao.impl;
import java.sql.SQLException;
import java.util.List;
import com.wendellup.dao.IStudentDAO;
import com.wendellup.entity.Student;
import com.wendellup.util.IbatisUtil;
public class StudentDAOImpl implements IStudentDAO{
public List<Student> queryAllStudent() throws Exception {
List<Student> studentList = null;
try {
studentList = IbatisUtil.getClient().queryForList("selectAllStudent");
} catch (Exception e) {
e.printStackTrace();
throw e;
}
return studentList;
}
public int addStudent(Student student) throws Exception {
int key;
try {
key = (Integer) IbatisUtil.getClient().insert("insertStudent",student);
} catch (Exception e) {
e.printStackTrace();
throw e;
}
return key;
}
}
6. 创建测试类TestStudentDAO.java
完成查询所有学生和插入一条学生的操作
package test.wendellup;
import java.util.Date;
import java.util.List;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import com.wendellup.dao.IStudentDAO;
import com.wendellup.dao.impl.StudentDAOImpl;
import com.wendellup.entity.Student;
public class TestStudentDAO {
private IStudentDAO iStudentDAO = null;
@Before
public void setUp(){
iStudentDAO = new StudentDAOImpl();
}
@Ignore
@Test
public void testQueryAllStudent() throws Exception{
List<Student> studentList = iStudentDAO.queryAllStudent();
for(Student stu : studentList){
System.out.println(stu);
}
}
@Test
public void testInsertStudent()throws Exception{
Student student = new Student();
student.setName("wendellup");
student.setPassword("xxx");
student.setBirthday(new Date());
student.setCreateTime(new Date());
int key = iStudentDAO.addStudent(student);
System.out.println(key);
}
}
项目资源地址:
http://pan.baidu.com/share/link?shareid=139030&uk=3475027816