Spring+iBatis整合(业务层聚合SqlMapClientTemplate)
来源:互联网 发布:sql 最大值 最小值 编辑:程序博客网 时间:2024/06/05 00:54
PS:
用的是业务层聚合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 sqlMapPUBLIC "-//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 sqlMapConfigPUBLIC "-//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的配置也应该易如反掌了吧!
- Spring+iBatis整合(业务层聚合SqlMapClientTemplate)
- SqlMapClientTemplate 的使用,ibatis+spring
- ibatis+spring+struts spring 外部注入 SqlMapClientTemplate
- Spring+IBatis整合各层设计(I)
- Spring框架学习【SqlMapClientTemplate对Ibatis的封装】
- spring技术内幕13-SqlMapClientTemplate对Ibatis的封装
- ibatis--SqlMapClientTemplate用法
- spring与ibatis整合
- Spring 与 ibatis 整合
- spring整合Hibernate+iBATIS
- Spring与Ibatis整合
- ibatis-Spring 整合
- Struts2+Ibatis+Spring整合
- spring和ibatis整合
- ibatis与spring整合
- spring整合ibatis事务管理
- Spring+iBatis整合详解
- SpringMVC+Spring+Ibatis整合
- iphone开发证书 纠结许久
- Android FrameWork——PackageManager框架
- 1005错误解决办法
- Android常用方法总结
- ARM体系结构与编程作业
- Spring+iBatis整合(业务层聚合SqlMapClientTemplate)
- Win32控件使用——通用函数
- 协方差的意义
- 浅谈C++多态性
- Java笔记之字符串
- hdu 1978 How many ways
- 2012华东架构师大会将于2012年11月18日在上海举办
- Android自动测试工具MonkeyRunner之一:基础知识
- Explorer.exe创建与结束"特性"