Spring+iBatis整合(业务层聚合SqlMapClientTemplate)
2012-10-18 23:12
405 查看
PS:
用的是业务层聚合SqlMapClientTemplate的方法,
步骤八中持有一个spring<bean>出来的SqlMapClientTemplate对象
Spring和Ibatis框架整合的思路与spring和hibernate框架的整合思路基本一致。
步骤一:新建立一个项目。
步骤二:为该项目添加spring的应用环境。
步骤三:导入Ibatis的必须JAR包以及数据库JAR包。
步骤四:新建实体Bean。如下:
步骤五:新建相应的Bean的配置文件。如下:Student.xml
步骤六:新建Ibatis的应用配置文件。sql-map-config.xml
步骤七:修改spring的applicationContext.xml文件。添加如下代码:
步骤八:书写数据持久层次以及业务层代码。数据持久层代码如下:
以及修改修改spring的applicationContext.xml文件
步骤九:建立测试类及测试方法
通过以上的配置,相信对SSI的配置也应该易如反掌了吧!
用的是业务层聚合SqlMapClientTemplate的方法,
步骤八中持有一个spring<bean>出来的SqlMapClientTemplate对象
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()); } } }
通过以上的配置,相信对SSI的配置也应该易如反掌了吧!
相关文章推荐
- Ibatis中SqlMapClientTemplate和SqlMapClient的区别
- 整合iBatis和Spring之SqlMapClientDaoSupport
- spring技术内幕13-SqlMapClientTemplate对Ibatis的封装
- ibatis+spring+struts spring 外部注入 SqlMapClientTemplate
- Spring整合Ibatis之SqlMapClientDaoSupport
- ibatis.SqlMapClientTemplate的使用集锦
- SqlMapClientTemplate对Ibatis的封装
- Spring整合Ibatis之SqlMapClientDaoSupport
- 《Spring技术内幕》学习笔记13——SqlMapClientTemplate对Ibatis的封装
- 《Spring技术内幕》学习笔记13——SqlMapClientTemplate对Ibatis的封装
- Ibatis中SqlMapClientTemplate和SqlMapClient的区别
- Spring整合Ibatis之SqlMapClientDaoSupport
- ibatis中SqlMapClientTemplate与SqlMapClient的区别
- 《Spring技术内幕》学习笔记13——SqlMapClientTemplate对Ibatis的封装
- Ibatis中SqlMapClientTemplate和SqlMapClient的区别
- Ibatis相关基础之一-为什么要用sqlMapClientTemplate
- ibatis之SqlMapClientTemplate和SqlMapClient
- 《Spring技术内幕》学习笔记13——SqlMapClientTemplate对Ibatis的封装
- Spring整合Ibatis之SqlMapClientDaoSupport
- 初始化iBatis获得一个SqlMapClient对象