Spring和Ibatis框架整合

 

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());

}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章