实战解析:如何整合iBatis和Spring

Spring通过DAO模式,提供了对iBATIS的良好支持。SqlMapClient对象是iBATIS中的主要对象,我们可以通过配置让spring来管理SqlMapClient对象的创建,继而整合iBatis和Spring。

创新互联建站主要从事网站制作、做网站、网页设计、企业做网站、公司建网站等业务。立足成都服务博乐,10多年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18980820575

与hibernate类似,Spring 提供了SqlMapClientDaoSupport对象,我们的DAO可以继承这个类,通过它所提供的SqlMapClientTemplate对象来操纵数据库。看起来这些概念都与hibernate类似。

通过SqlMapClientTemplate来操纵数据库的CRUD是没有问题的,这里面关键的问题是事务处理。Spring提供了强大的声明式事务处理的功能,我们已经清楚hibernate中如何配置声明式的事务,那么在iBATIS中如何获得声明式事务的能力呢?我们又怎样整合iBatis和Spring呢?

***,我们需要了解的是spring通过AOP来拦截方法的调用,从而在这些方法上面添加声明式事务处理的能力。典型配置如下:applicationContext-common.xml

 
 
 
  1.  
  2.  
  3.      
  4.  
  5.          
  6.  
  7.             
  8.  
  9.             
  10.  
  11.             
  12.  
  13.             
  14.  
  15.         
  16.  
  17.      
  18.  
  19.       
  20.  
  21.      
  22.  
  23.      
  24.  
  25.         
  26.  
  27.         
  28.  
  29.      

这些事务都是声明在业务逻辑层的对象上的。 第二,我们需要一个事务管理器,对事务进行管理,实现整合iBatis和Spring的第二步。

 
 
 
  1.  
  2.  
  3.     
  4.  
  5.     
  6.  
  7.     
  8.  
  9.         
  10.  
  11.         
  12.  
  13.         
  14.  
  15.         
  16.  
  17.     

此后,我们需要让spring来管理SqlMapClient对象,实现整合iBatis和Spring的第三步

 
 
 
  1.  
  2.  
  3.       classpath:sqlMapConfig.xml 
  4.  
  5.     

我们的sqlMapConfig.xml就可以简写为:

 
 
 
  1.  
  2.  
  3.  
  4.     PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"        
  5.  
  6.     "http://ibatis.apache.org/dtd/sql-map-config-2.dtd"> 
  7.  
  8.  
  9.  
  10.     
  11.  
  12.        lazyLoadingEnabled="true" 
  13.  
  14.         useStatementNamespaces="true" /> 
  15.  
  16.      
  17.  
  18.    
  19.  
  20.  
  21.  
  22. User.xml:如下  
  23.  
  24.  
  25.  
  26.  
  27.     PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"        
  28.  
  29.     "http://ibatis.apache.org/dtd/sql-map-2.dtd"> 
  30.  
  31.  
  32.  
  33.   
  34.  
  35.   
  36.  
  37.   
  38.  
  39.   
  40.  
  41.     select * from t_user  
  42.  
  43.   
  44.  
  45.    
  46.  
  47.   
  48.  
  49.   select * from t_user where id=#id#  
  50.  
  51.   
  52.  
  53.    
  54.  
  55.   
  56.  
  57.   insert into t_user values (  
  58.  
  59.        null,#username#,#password#  
  60.  
  61.   )  
  62.  
  63.   
  64.  
  65.    
  66.  
  67.   
  68.  
  69.   update t_user set username = #username#,password=#password#  
  70.  
  71.   where id=#id#  
  72.  
  73.    
  74.  
  75.    
  76.  
  77.   
  78.  
  79.   delete from t_user where id=#id#  
  80.  
  81.   
  82.  
  83.  
  84.  

我们的DAO的编写:

 
 
 
  1. package com.iabtis.dao.impl.ibatis;  
  2.  
  3. import java.util.List;  
  4.  
  5. import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;  
  6.  
  7. import com.ibatis.dao.UserDAO;  
  8.  
  9. import com.ibatis.crm.model.User;  
  10.  
  11. public class UserDAOImpl extends SqlMapClientDaoSupport implements UserDAO {  
  12.  
  13.     public void select(User user) {  
  14.  
  15.               getSqlMapClientTemplate().delete("selectUser ",user.getId());  
  16.  
  17.        }  
  18.  
  19.    public List findAll() {  
  20.  
  21.               return getSqlMapClientTemplate().queryForList("selectAllUsers ");  
  22.  
  23.        }  
  24.  
  25.        public void delete(User user) {  
  26.  
  27.               getSqlMapClientTemplate().delete("deleteUser ",user.getId());  
  28.  
  29.        }  
  30.  
  31.        public void save(User user) {  
  32.  
  33.               getSqlMapClientTemplate().insert("insertUser ",user);  
  34.  
  35.        }  
  36.  
  37.        public void update(User user) {  
  38.  
  39.               getSqlMapClientTemplate().update("updateUser ",user);  
  40.  
  41.        }  
  42.  
  43. }  

继承SqlMapClientDaoSupport,要求我们注入SqlMapClient对象,因此,需要有如下的DAO配置,这是整合iBatis和Spring的***一步了

 
 
 
  1.  
  2.  
  3.       
  4.  
  5.  

这就是所有需要注意的问题了,此后就可以在业务逻辑层调用DAO对象了!

本文名称:实战解析:如何整合iBatis和Spring
链接分享:http://www.shufengxianlan.com/qtweb/news38/49888.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联