Spring和Ibatis框架整合的思路与spring和hibernate框架的整合思路基本一致。
步骤一:新建立一个项目。
步骤二:为该项目添加spring的应用环境。
步骤三:导入Ibatis的必须JAR包以及数据库JAR包。
步骤四:新建实体Bean。如下:
package cn.test.entity;
import java.io.Serializable;
/**
* @author Administrator
*
*学生实体Bean
*
*/
public class Student implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private Integer id;
private String studentname;
private Integer studentage;
private String studentaddress;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getStudentname() {
return studentname;
}
public void setStudentname(String studentname) {
this.studentname = studentname;
}
public Integer getStudentage() {
return studentage;
}
public void setStudentage(Integer studentage) {
this.studentage = studentage;
}
public String getStudentaddress() {
return studentaddress;
}
public void setStudentaddress(String studentaddress) {
this.studentaddress = studentaddress;
}
}
步骤五:新建相应的Bean的配置文件。如下:Student.xml
<?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>
<!– 查询操作 –>
<select id=”getAllStudent” resultClass=”cn.test.entity.Student”>
select * from student where 1=1
</select>
<!– 通过编号获取信息 –>
<select id=”getStudentById” resultClass=”cn.test.entity.Student” parameterClass=”Integer”>
select * from student where id=#ids#
</select>
</sqlMap>
步骤六:新建Ibatis的应用配置文件。sql-map-config.xml
<?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>
<!– 通知spring到哪里去寻找配置文件 –>
<sqlMap resource=”cn/test/configfile/Student.xml”/>
</sqlMapConfig>
步骤七:修改spring的applicationContext.xml文件。添加如下代码:
<!– 配置数据源 –>
<bean id=”dataSource”>
<property name=”driverClassName”>
<value>com.mysql.jdbc.Driver</value>
</property>
<property name=”url”>
<value>jdbc:mysql://localhost:3306/test</value>
</property>
<property name=”username”>
<value>root</value>
</property>
<property name=”password”>
<value>root</value>
</property>
</bean>
<!– spring的ibatis 配制 –>
<bean id=”sqlMapClient”>
<property name=”dataSource”>
<ref bean=”dataSource”/>
</property>
<property name=”configLocation”>
<value>cn/test/configfile/sql-map-config.xml</value>
</property>
</bean>
<!– 配置模板 –>
<bean id=”sqlMapClientTemplate”>
<property name=”sqlMapClient”>
<ref bean=”sqlMapClient”/>
</property>
</bean>
步骤八:书写数据持久层次以及业务层代码。数据持久层代码如下:
public class StudentDAOImple implements IStudentDAO{
private SqlMapClientTemplate sqlMapClientTemplate;
public void setSqlMapClientTemplate(SqlMapClientTemplate sqlMapClientTemplate) {
this.sqlMapClientTemplate = sqlMapClientTemplate;
}
public List<Student> getAllStudent() {
List<Student> list = sqlMapClientTemplate.queryForList(“getAllStudent”);
return list;
}
}
以及修改修改spring的applicationContext.xml文件
<!– DAO –>
<bean id=”studentDAO”>
<property name=”sqlMapClientTemplate”>
<ref bean=”sqlMapClientTemplate”/>
</property>
</bean>
步骤九:建立测试类以及测试方法。
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
ApplicationContext factory = new ClassPathXmlApplicationContext(“applicationContext.xml”);
IStudentDAO dao = (IStudentDAO) factory.getBean(“studentDAO”);
List<Student> list = dao.getAllStudent();
for (int i = 0; i < list.size(); i++) {
Student stu = list.get(i);
System.out.println(stu.getId()+”:”+stu.getStudentname()+”:”+stu.getStudentage()+”:”+stu.getStudentaddress());
}
}
}