疯狂

STANDING ON THE SHOULDERS OF GIANTS
posts - 481, comments - 486, trackbacks - 0, articles - 1
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

ibatis学习(三)---ibatis与spring的整合

Posted on 2007-12-07 18:26 疯狂 阅读(70573) 评论(25)  编辑  收藏
 

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

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

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

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

    <!-- 配置事务特性 -->

    <tx:advice id="txAdvice" transaction-manager="事务管理器名称">

        <tx:attributes>

           <tx:method name="add*" propagation="REQUIRED"/>

           <tx:method name="del*" propagation="REQUIRED"/>

           <tx:method name="update*" propagation="REQUIRED"/>

           <tx:method name="*" read-only="true"/>

       </tx:attributes>

    </tx:advice>

   

    <!-- 配置哪些类的方法需要进行事务管理 -->

    <aop:config>

       <aop:pointcut id="allManagerMethod" expression="execution(* com.ibatis.manager.*.*(..))"/>

       <aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod"/>

    </aop:config>

这些事务都是声明在业务逻辑层的对象上的。

第二,我们需要一个事务管理器,对事务进行管理。

    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

    <property name="dataSource" ref="dataSource"/>

    </bean>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">

        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>

        <property name="url" value="jdbc:mysql://127.0.0.1/ibatis"/>

        <property name="username" value="root"/>

        <property name="password" value="mysql"/>

    </bean>

此后,我们需要让spring来管理SqlMapClient对象:

    <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">

       <property name="configLocation"><value>classpath:sqlMapConfig.xml</value></property>

    </bean>

我们的sqlMapConfig.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>

    <settings

       lazyLoadingEnabled="true"

        useStatementNamespaces="true" />

    <!-- 使用spring之后,数据源的配置移植到了spring上,所以iBATIS本身的配置可以取消 -->

  <sqlMap resource="com/ibatis/dao/impl/ibatis/User.xml"/>

</sqlMapConfig>

User.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 namespace="User">

 <!-- Use type aliases to avoid typing the full classname every time. -->

 <typeAlias alias="User" type="com.ibatis.User"/>

 <!-- Select with no parameters using the result map for Account class. -->

 <select id="selectAllUsers" resultClass="User">

    select * from t_user

 </select>

 

 <select id="selectUser" resultClass="User" parameterClass="int">

  select * from t_user where id=#id#

 </select>

 

 <insert id="insertUser" parameterClass="User">

  insert into t_user values (

       null,#username#,#password#

  )

 </insert>

 

 <update id="updateUser" parameterClass="User">

  update t_user set username = #username#,password=#password#

  where id=#id#

  </update>

 

 <delete id="deleteUser" parameterClass="int">

  delete from t_user where id=#id#

 </delete>

</sqlMap>

我们的DAO的编写:

package com.iabtis.dao.impl.ibatis;

import java.util.List;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.ibatis.dao.UserDAO;

import com.ibatis.crm.model.User;

public class UserDAOImpl extends SqlMapClientDaoSupport implements UserDAO {

    public void select(User user) {

              getSqlMapClientTemplate().delete("selectUser ",user.getId());

       }

   public List findAll() {

              return getSqlMapClientTemplate().queryForList("selectAllUsers ");

       }

       public void delete(User user) {

              getSqlMapClientTemplate().delete("deleteUser ",user.getId());

       }

       public void save(User user) {

              getSqlMapClientTemplate().insert("insertUser ",user);

       }

       public void update(User user) {

              getSqlMapClientTemplate().update("updateUser ",user);

       }

}

继承SqlMapClientDaoSupport,要求我们注入SqlMapClient对象,因此,需要有如下的DAO配置:

<bean id="userDAO" class="com.ibatils.dao.impl.ibatis.UserDAOImpl">

     <property name=”sqlMapClient” ref=”sqlMapClient”/>

</bean>

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


评论

# re: ibatis学习(三)---ibatis与spring的整合  回复  更多评论   

2007-12-08 09:42 by laocat
豁然开朗 !!

# re: ibatis学习(三)---ibatis与spring的整合  回复  更多评论   

2008-03-24 16:08 by 屹砾
整合的问题现在终于清楚了,
对于advice和aop还有点不清楚。

# re: ibatis学习(三)---ibatis与spring的整合[未登录]  回复  更多评论   

2008-06-13 17:26 by 冷漠大神
真是不错的文章啊 如果在在可以把源码提供下载 那就更完美了 :-) 呵呵 是不是有点贪心

# re: ibatis学习(三)---ibatis与spring的整合[未登录]  回复  更多评论   

2008-06-13 17:34 by 冷漠大神
有没有代码下载啊?

# re: ibatis学习(三)---ibatis与spring的整合  回复  更多评论   

2008-08-25 11:35 by 379548695qq
UserDAO类里面的内容是什么?

# re: ibatis学习(三)---ibatis与spring的整合[未登录]  回复  更多评论   

2009-03-19 10:17 by 虫子
正在烦恼中,google到了你的方案!呵呵

# re: ibatis学习(三)---ibatis与spring的整合  回复  更多评论   

2009-08-13 16:32 by jadmin
很好,学习了

# re: ibatis学习(三)---ibatis与spring的整合  回复  更多评论   

2009-08-15 23:15 by 匹马单枪
好帖, 学习了!

# re: ibatis学习(三)---ibatis与spring的整合  回复  更多评论   

2009-09-01 13:07 by 2
为什么我的出错了

# re: ibatis学习(三)---ibatis与spring的整合[未登录]  回复  更多评论   

2010-04-28 11:10 by test
autocommit 如果不设置为false, 事务有用么

# re: ibatis学习(三)---ibatis与spring的整合  回复  更多评论   

2010-11-01 13:42 by rr
sqlMapClient里应该注入dataSource

# re: ibatis学习(三)---ibatis与spring的整合[未登录]  回复  更多评论   

2011-09-19 16:27 by ddd
不用封装实体类吗?

# re: ibatis学习(三)---ibatis与spring的整合  回复  更多评论   

2011-09-27 19:33 by LL
public void select(User user) {

getSqlMapClientTemplate().delete("selectUser ",user.getId());

}


出错了

# re: ibatis学习(三)---ibatis与spring的整合  回复  更多评论   

2012-04-28 14:42 by 张毅
文章很好

# re: ibatis学习(三)---ibatis与spring的整合  回复  更多评论   

2012-09-04 14:02 by 兰伟
能帮帮我吗 我在sqlserver上建了个user表 id为自增的主键 要增加个user 在配置文件中的selectkey 怎么写啊??

# re: ibatis学习(三)---ibatis与spring的整合[未登录]  回复  更多评论   

2012-10-09 17:22 by 1
1

# re: ibatis学习(三)---ibatis与spring的整合呃呃呃  回复  更多评论   

2013-04-11 15:11 by 恩恩
地对地导弹

# re: ibatis学习(三)---ibatis与spring的整合[未登录]  回复  更多评论   

2013-04-28 10:39 by tbw
有没有实例啊

# re: ibatis学习(三)---ibatis与spring的整合[未登录]  回复  更多评论   

2013-04-28 15:09 by 123
很垃圾ibatis

# re: ibatis学习(三)---ibatis与spring的整合  回复  更多评论   

2013-10-08 14:08 by 可耕地
在在苛下人手仍有雨

# re: ibatis学习(三)---ibatis与spring的整合  回复  更多评论   

2014-01-23 13:17 by 哈林木
iBatis 是apache 的一个开源项目,一个O/R Mapping 解决方案,iBatis 最大的特点就是小巧,上手很快。
如果不需要太多复杂的功能,iBatis 是能够满足你的要求又足够灵活的最简单的解决方案,现在的iBatis 已经改名为Mybatis 了。

近期项目用到 iBatis,所以需要学习iBatis,下面是总结几个不错学习网站给大家学习参考:

1、官网(英文资料):http://www.mybatis.org/

2、iBATIS(中文教程):http://www.yiibai.com/ibatis/

3、iBATIS - iBATIS Apache软件基金会的官方网站。
http://ibatis.apache.org/index.html

# re: ibatis学习(三)---ibatis与spring的整合  回复  更多评论   

2014-04-15 00:01 by 最代码
最代码的转载地址:http://www.zuidaima.com/share/1780211932679168.htm

# re: ibatis学习(三)---ibatis与spring的整合  回复  更多评论   

2014-05-13 11:33 by rh
very good

# re: ibatis学习(三)---ibatis与spring的整合  回复  更多评论   

2014-06-27 17:24 by 。。。
和Hibernate很相似。。

# re: ibatis学习(三)---ibatis与spring的整合[未登录]  回复  更多评论   

2014-11-11 14:48 by 小白
applicationContext-common.xml文件在哪加载的,怎么没讲明白,没加载等于没用啊!

只有注册用户登录后才能发表评论。


网站导航: