Spring和iBatis整合的例子

来源:互联网 发布:淘宝著名的女童模特 编辑:程序博客网 时间:2024/06/05 04:30
OR Mapping的思想相信不用多说大家都明白了,在这里我选择的是ibatis由于手动的控制事务会带来很多额外的工作,同时也没有很好的体现面向对象的思想,因而利用ibatis整合spring ;由于要注意的细节非常多现在整理核心步骤如下:

配置前需把ibatis的jar导入到工程,这里从略

一 web.xml的配置
Java代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <web-app version="2.4"  
  3.     xmlns="http://java.sun.com/xml/ns/j2ee"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee  
  6.     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
  7.   <servlet>   
  8.     <description>This is the description of my J2EE component</description>   
  9.     <display-name>This is the display name of my J2EE component</display-name>   
  10.     <servlet-name>InserttoDBServlet</servlet-name>   
  11.     <servlet-class>service.InserttoDBServlet</servlet-class>   
  12.   </servlet>   
  13.   <servlet-mapping>   
  14.     <servlet-name>InserttoDBServlet</servlet-name>   
  15.     <url-pattern>/InserttoDBServlet</url-pattern>   
  16.   </servlet-mapping>   
  17.     
  18.   <context-param>   
  19.     <param-name>contextConfigLocation</param-name>   
  20.     <param-value>   
  21.         classpath:applicationContext.xml   
  22.     </param-value>   
  23. </context-param>   
  24. <listener>   
  25.     <listener-class>   
  26.         org.springframework.web.context.ContextLoaderListener   
  27.     </listener-class>   
  28. </listener>   
  29.     
  30. <servlet>   
  31.         <servlet-name>action</servlet-name>   
  32.         <servlet-class>   
  33.             org.apache.struts.action.ActionServlet   
  34.         </servlet-class>   
  35.         <init-param>   
  36.             <param-name>config</param-name>   
  37.             <param-value>/WEB-INF/struts-config.xml</param-value>   
  38.         </init-param>   
  39.         <load-on-startup>1</load-on-startup>   
  40.     </servlet>   
  41.   <servlet>   
  42.     <description>This is the description of my J2EE component</description>   
  43.     <display-name>This is the display name of my J2EE component</display-name>   
  44.     <servlet-name>TestServlet</servlet-name>   
  45.     <servlet-class>test.TestServlet</servlet-class>     
  46.   </servlet>   
  47.   
  48.     <servlet-mapping>   
  49.         <servlet-name>action</servlet-name>   
  50.         <url-pattern>*.do</url-pattern>   
  51.     </servlet-mapping>   
  52.   <servlet-mapping>   
  53.     <servlet-name>TestServlet</servlet-name>   
  54.     <url-pattern>/servlet/TestServlet</url-pattern>   
  55.   </servlet-mapping>   
  56.     
  57.   <filter>   
  58.       <filter-name>ExtFilter</filter-name>   
  59.      <filter-class>filter.ExtFilter</filter-class>   
  60.       
  61.   </filter>   
  62.     
  63.   <filter-mapping>   
  64.      <filter-name>ExtFilter</filter-name>   
  65.     <url-pattern>/*</url-pattern>   
  66.   </filter-mapping>   
  67.     
  68. </web-app>  

二 applicationContext.xml文件的配置
Java代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">   
  3. <beans>   
  4. <bean id="transactionManager"  
  5.         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">   
  6.         <property name="dataSource" ref="dataSource_oracle" />   
  7.     </bean>   
  8.   
  9.     <bean id="dataSource"  
  10.         class="org.apache.commons.dbcp.BasicDataSource"  
  11.         destroy-method="close">   
  12.         <property name="driverClassName"  
  13.             value="com.mysql.jdbc.Driver" />   
  14.         <property name="url" value="jdbc:mysql://localhost:3306/test" />   
  15.         <property name="username" value="root" />   
  16.         <property name="password" value="root" />   
  17.         <property name="initialSize" value="1" />   
  18.         <property name="maxActive" value="4" />   
  19.     </bean>   
  20.   
  21.     <bean id="dataSource_oracle"  
  22.         class="org.apache.commons.dbcp.BasicDataSource"  
  23.         destroy-method="close">   
  24.         <property name="driverClassName"  
  25.             value="oracle.jdbc.OracleDriver" />   
  26.         <property name="url" value="jdbc:oracle:thin:@192.168.100.235:1521:mpptest" />   
  27.         <property name="username" value="gmcc" />   
  28.         <property name="password" value="skywin" />   
  29.         <property name="initialSize" value="1" />   
  30.         <property name="maxActive" value="4" />   
  31.     </bean>   
  32.        
  33.     <bean id="sqlMapClient"  
  34.         class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">   
  35.         <property name="configLocation"  
  36.             value="classpath:sqlmap-config.xml" />   
  37.         <property name="dataSource" ref="dataSource_oracle" />   
  38.     </bean>   
  39.   
  40.     <bean id="baseTxService"  
  41.         class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"  
  42.         abstract="true">   
  43.         <property name="transactionManager" ref="transactionManager" />   
  44.         <property name="proxyTargetClass" value="true" />   
  45.         <property name="transactionAttributes">   
  46.             <props>   
  47.                 <prop key="insert*">PROPAGATION_REQUIRED</prop>   
  48.                 <prop key="query*">readOnly</prop>   
  49.                 <prop key="get*">readOnly</prop>   
  50.                 <prop key="del*">PROPAGATION_REQUIRED</prop>   
  51.                 <prop key="update*">PROPAGATION_REQUIRED</prop>   
  52.             </props>   
  53.         </property>   
  54.     </bean>   
  55.       
  56.     <bean id="studentDao" class="dao.StudentDaoImpl">   
  57.        <property name="sqlMapClient" ref="sqlMapClient"></property>   
  58.     </bean>   
  59.       
  60.     <bean id="studentService" class="service.StudentServiceImpl">   
  61.         <property name="studentDao" ref="studentDao"></property>   
  62.     </bean>   
  63.       
  64.     <bean id="studentServiceProxy" parent="baseTxService">   
  65.        <property name="target" ref="studentService"></property>   
  66.     </bean>   
  67.       
  68.     <bean id="treeService" class="service.TreeServiceImpl_map">   
  69.          <property name="treeDao" ref="treeDao"></property>   
  70.     </bean>   
  71.       
  72.     <bean id="treeServiceProxy" parent="baseTxService">   
  73.        <property name="target" ref="treeService"></property>   
  74.     </bean>   
  75.       
  76.     <bean id="treeDao" class="dao.TreeDaoImpl">   
  77.        <property name="sqlMapClient" ref="sqlMapClient"></property>   
  78.     </bean>   
  79.       
  80.     <bean id="xmlTreeService" class="service.XmlTreeServiceImpl"></bean>   
  81. </beans>  

三 sqlmap-config.xml 文件配置
Java代码 复制代码 收藏代码
  1. <!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">   
  2. <sqlMapConfig>   
  3.     <sqlMap resource="student_oracle.xml" />   
  4.     <sqlMap resource="treeNode_oracle.xml" />   
  5. </sqlMapConfig>  

四 ormpping文件的配置(student_oracle.xml等 )
Java代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="UTF-8" ?>   
  2. <!DOCTYPE sqlMap        
  3.     PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"        
  4.     "http://ibatis.apache.org/dtd/sql-map-2.dtd">   
  5. <sqlMap namespace="Student_oracle">   
  6.     <resultMap id="result"  
  7.         class="entity.Student">   
  8.         <result property="stuId" column="id"  
  9.             columnIndex="1" />   
  10.         <result property="name" column="stuname"  
  11.             columnIndex="2" />   
  12.         <result property="password" column="stupassword" columnIndex="3" />   
  13.     </resultMap>   
  14.        
  15.     <insert id="insertToDb">   
  16.         insert into test_qjk_stu(id,stuname,stupassword)values(#stuId#,#name#,#password#)   
  17.     </insert>   
  18.       
  19.     <insert id="insertTest">   
  20.         <selectKey resultClass="Integer" keyProperty="stuId">      
  21.               SELECT test_qjk_stu_seq.nextval FROM DUAL       
  22.         </selectKey>   
  23.         insert into test_qjk_stu(id,stuname,stupassword)values(#stuId#,#name#,#password#)       
  24.     </insert>   
  25. </sqlMap>  

五 实体bean代码
Java代码 复制代码 收藏代码
  1. package entity;   
  2.   
  3. import java.util.Date;   
  4.   
  5. public class Student {   
  6.       
  7.     private Integer stuId;   
  8.     private String name;   
  9.     private Date birthday;   
  10.     private String sex;   
  11.     private String passport;   
  12.     private byte[] password;   
  13.       
  14.     public byte[] getPassword() {   
  15.         return password;   
  16.     }   
  17.     public void setPassword(byte[] password) {   
  18.         this.password = password;   
  19.     }   
  20.     public Date getBirthday() {   
  21.         return birthday;   
  22.     }   
  23.     public void setBirthday(Date birthday) {   
  24.         this.birthday = birthday;   
  25.     }   
  26.     public String getName() {   
  27.         return name;   
  28.     }   
  29.     public void setName(String name) {   
  30.         this.name = name;   
  31.     }   
  32.     public String getPassport() {   
  33.         return passport;   
  34.     }   
  35.     public void setPassport(String passport) {   
  36.         this.passport = passport;   
  37.     }   
  38.     public String getSex() {   
  39.         return sex;   
  40.     }   
  41.     public void setSex(String sex) {   
  42.         this.sex = sex;   
  43.     }   
  44.   
  45.       
  46.     public Integer getStuId() {   
  47.         return stuId;   
  48.     }   
  49.     public void setStuId(Integer stuId) {   
  50.         this.stuId = stuId;   
  51.     }   
  52.     public String toString(){   
  53.         return "stuid:"+stuId+"---name:"+name+"---passport:"+passport+"-----sex:"+sex+"--birthday:"+birthday;   
  54.     }   
  55.   
  56. }  

六 dao实现类
Java代码 复制代码 收藏代码
  1. package dao;   
  2.   
  3. import java.sql.SQLException;   
  4. import java.util.List;   
  5.   
  6. import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;   
  7.   
  8. import util.Print;   
  9.   
  10. import entity.Student;   
  11.   
  12. public class StudentDaoImpl extends SqlMapClientDaoSupport implements StudentDao {   
  13.   
  14.     public void insertToDb(Student stu) {   
  15.           
  16.           getSqlMapClientTemplate().insert("insertToDb", stu);   
  17.     }   
  18.   
  19.     public Student queryStuById(Integer id) {   
  20.         Student stu=(Student) getSqlMapClientTemplate().queryForObject("queryStuById", id);   
  21.         Print.contrlPrint("dao查到的对象是"+stu);   
  22.         return stu;   
  23.     }   
  24.   
  25.     public List getSomeStu(int start, int pageSize) {   
  26.         System.out.println("dao ---start"+start);   
  27.         System.out.println("dao ---pageSize"+pageSize);   
  28.         List list=getSqlMapClientTemplate().queryForList("queryStudent"null, start, pageSize);   
  29.         System.out.println("dao---list---size"+list.size());   
  30.         return list;   
  31.     }   
  32.   
  33.     public Long getStudentCount() {   
  34.         return (Long) getSqlMapClientTemplate().queryForObject("getStudentCount"null);   
  35.     }   
  36.   
  37.     public void delStuById(Long stuId) {   
  38.           getSqlMapClientTemplate().delete("delStuById", stuId);   
  39.           
  40.     }   
  41.   
  42.     public void updateStudent(Student stu) {   
  43.         getSqlMapClientTemplate().update("updateStudent", stu);   
  44.           
  45.     }   
  46.       
  47.       
  48.     public List test(){   
  49.         return getSqlMapClientTemplate().queryForList("test",null);   
  50.          
  51.     }   
  52.   
  53.     public Integer insertTest(Student stu) {   
  54.         Print.contrlPrint("调用了insertTest");   
  55.         return (Integer) getSqlMapClientTemplate().insert("insertTest", stu);   
  56.     }   
  57.   
  58. }  

七 业务层实现类
Java代码 复制代码 收藏代码
  1. package service;   
  2.   
  3. import java.util.List;   
  4.   
  5. import util.Tools;   
  6.   
  7. import dao.StudentDao;   
  8. import entity.Student;   
  9.   
  10. public class StudentServiceImpl implements StudentService {   
  11.   
  12.     private StudentDao studentDao;   
  13.       
  14.     public void insertToDb(Student stu) {   
  15.           
  16.         studentDao.insertToDb(stu);   
  17. //        Integer  id=studentDao.insertTest(stu);  
  18. //        System.out.println("插入记录的id是"+id);  
  19.     }   
  20.   
  21.     public StudentDao getStudentDao() {   
  22.         return studentDao;   
  23.     }   
  24.   
  25.     public void setStudentDao(StudentDao studentDao) {   
  26.         this.studentDao = studentDao;   
  27.     }   
  28.   
  29.     public Student queryById(Integer id) {   
  30.           
  31.         return studentDao.queryStuById(id);   
  32.     }   
  33.   
  34.     public List getSomeStu(int start, int pageSize) {   
  35.           
  36.         return studentDao.getSomeStu(start, pageSize);   
  37.     }   
  38.   
  39.     public String getResponseJson(int start, int pageSize) {   
  40.         List list=getSomeStu(start, pageSize);   
  41.         Long count=getStudentCount();   
  42.         return Tools.getResponseJson(list,count);   
  43.     }   
  44.   
  45.     public Long getStudentCount() {   
  46.           
  47.         return studentDao.getStudentCount();   
  48.     }   
  49.   
  50.     public void delStuById(Long stuId) {   
  51.         studentDao.delStuById(stuId);   
  52.           
  53.     }   
  54.   
  55.     public void updateStudent(Student stu) {   
  56.         studentDao.updateStudent(stu);   
  57.     }   
  58.   
  59. }  
原创粉丝点击