Spring通过DAO模式,提供了对iBATIS的良好支持。SqlMapClient对象是iBATIS中的主要对象,我们可以通过配置让spring来管理SqlMapClient对象的创建。
与hibernate类似,Spring提供了SqlMapClientDaoSupport对象,我们的DAO可以继承这个类,通过它所提供的SqlMapClientTemplate对象来操纵数据库。看起来这些概念都与hibernate类似。
通过SqlMapClientTemplate来操纵数据库的CRUD是没有问题的,这里面关键的问题是事务处理。Spring提供了强大的声明式事务处理的功能,我们已经清楚hibernate中如何配置声明式的事务,那么在iBATIS中如何获得声明式事务的能力呢?
第一,我们需要了解的是spring通过AOP来拦截方法的调用,从而在这些方法上面添加声明式事务处理的能力。典型配置如下:applicationContext-common.xml
- <!--配置事务特性-->
- <tx:adviceid="txAdvice"transaction-manager="事务管理器名称">
- <tx:attributes>
- <tx:methodname="add*"propagation="REQUIRED"/>
- <tx:methodname="del*"propagation="REQUIRED"/>
- <tx:methodname="update*"propagation="REQUIRED"/>
- <tx:methodname="*"read-only="true"/>
- </tx:attributes>
- </tx:advice>
- <!--配置哪些类的方法需要进行事务管理-->
- <aop:config>
- <aop:pointcutid="allManagerMethod"expression="execution(*com.ibatis.manager.*.*(..))"/>
- <aop:advisoradvice-ref="txAdvice"pointcut-ref="allManagerMethod"/>
- </aop:config>
这些事务都是声明在业务逻辑层的对象上的。
第二,我们需要一个事务管理器,对事务进行管理。
- <beanid="txManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <propertyname="dataSource"ref="dataSource"/>
- </bean>
- <beanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource">
- <propertyname="driverClassName"value="com.mysql.jdbc.Driver"/>
- <propertyname="url"value="jdbc:mysql://127.0.0.1/ibatis"/>
- <propertyname="username"value="root"/>
- <propertyname="password"value="mysql"/>
- </bean>
此后,我们需要让spring来管理SqlMapClient对象:
- <beanid="sqlMapClient"class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
- <propertyname="configLocation"><value>classpath:sqlMapConfig.xml</value></property>
- </bean>
我们的sqlMapConfig.xml就可以简写为:
- <?xmlversion="1.0"encoding="UTF-8"?>
- <!DOCTYPEsqlMapConfig
- PUBLIC"-//ibatis.apache.org//DTDSQLMapConfig2.0//EN"
- "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
- <sqlMapConfig>
- <settings
- lazyLoadingEnabled="true"
- useStatementNamespaces="true"/>
- <!--使用spring之后,数据源的配置移植到了spring上,所以iBATIS本身的配置可以取消-->
- <sqlMapresource="com/ibatis/dao/impl/ibatis/User.xml"/>
- </sqlMapConfig>
User.xml:如下
- <?xmlversion="1.0"encoding="UTF-8"?>
- <!DOCTYPEsqlMap
- PUBLIC"-//ibatis.apache.org//DTDSQLMap2.0//EN"
- "http://ibatis.apache.org/dtd/sql-map-2.dtd">
- <sqlMapnamespace="User">
- <!--Usetypealiasestoavoidtypingthefullclassnameeverytime.-->
- <typeAliasalias="User"type="com.ibatis.User"/>
- <!--SelectwithnoparametersusingtheresultmapforAccountclass.-->
- <selectid="selectAllUsers"resultClass="User">
- select*fromt_user
- </select>
- <selectid="selectUser"resultClass="User"parameterClass="int">
- select*fromt_userwhereid=#id#
- </select>
- <insertid="insertUser"parameterClass="User">
- insertintot_uservalues(
- null,#username#,#password#
- )
- </insert>
- <updateid="updateUser"parameterClass="User">
- updatet_usersetusername=#username#,password=#password#
- whereid=#id#
- </update>
- <deleteid="deleteUser"parameterClass="int">
- deletefromt_userwhereid=#id#
- </delete>
- </sqlMap>
我们的DAO的编写:
- packagecom.iabtis.dao.impl.ibatis;
- importjava.util.List;
- importorg.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
- importcom.ibatis.dao.UserDAO;
- importcom.ibatis.crm.model.User;
- publicclassUserDAOImplextendsSqlMapClientDaoSupportimplementsUserDAO{
- publicvoidselect(Useruser){
- getSqlMapClientTemplate().delete("selectUser",user.getId());
- }
- publicListfindAll(){
- returngetSqlMapClientTemplate().queryForList("selectAllUsers");
- }
- publicvoiddelete(Useruser){
- getSqlMapClientTemplate().delete("deleteUser",user.getId());
- }
- publicvoidsave(Useruser){
- getSqlMapClientTemplate().insert("insertUser",user);
- }
- publicvoidupdate(Useruser){
- getSqlMapClientTemplate().update("updateUser",user);
- }
- }
继承SqlMapClientDaoSupport,要求我们注入SqlMapClient对象,因此,需要有如下的DAO配置:
- <beanid="userDAO"class="com.ibatils.dao.impl.ibatis.UserDAOImpl">
- <propertyname=”sqlMapClient”ref=”sqlMapClient”/>
- </bean>
这就是所有需要注意的问题了,此后就可以在业务逻辑层调用DAO对象了!
<?xml version="1.0" encoding="UTF-8"?>
<beans<wbr><wbr><br style="margin:0px; padding:0px"> xmlns="http://www.springframework.org/schema/beans"<wbr><wbr><br style="margin:0px; padding:0px"> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"<wbr><wbr><br style="margin:0px; padding:0px"> xmlns:aop="http://www.springframework.org/schema/aop"<wbr><wbr><br style="margin:0px; padding:0px"> xmlns:tx="http://www.springframework.org/schema/tx"<wbr><wbr><br style="margin:0px; padding:0px"> xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd<wbr><wbr><br style="margin:0px; padding:0px"> http://www.springframework.org/schema/aop<br style="margin:0px; padding:0px"> http://www.springframework.org/schema/aop/spring-aop-2.5.xsd<wbr><wbr><br style="margin:0px; padding:0px"> http://www.springframework.org/schema/tx<wbr><br style="margin:0px; padding:0px"> http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"><wbr><wbr><br style="margin:0px; padding:0px"><wbr><wbr><wbr><wbr><br style="margin:0px; padding:0px"><wbr><wbr><wbr><!-- 数据源配置 --><br style="margin:0px; padding:0px"><wbr><wbr><wbr><bean id="dataSource"<br style="margin:0px; padding:0px"><wbr><wbr><wbr><wbr><wbr><wbr>class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"><br style="margin:0px; padding:0px"><wbr><wbr><wbr><wbr><wbr><wbr><property name="driverClassName"<br style="margin:0px; padding:0px"><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>value="net.sourceforge.jtds.jdbc.Driver"><br style="margin:0px; padding:0px"><wbr><wbr><wbr><wbr><wbr><wbr></property><br style="margin:0px; padding:0px"><wbr><wbr><wbr><wbr><wbr><wbr><property name="url"<br style="margin:0px; padding:0px"><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>value="jdbc:jtds:sqlserver://localhost:1433;DatabaseName=dbName;SelectMethod=cursor"><br style="margin:0px; padding:0px"><wbr><wbr><wbr><wbr><wbr><wbr></property><br style="margin:0px; padding:0px"><wbr><wbr><wbr><wbr><wbr><wbr><property name="username" value="sa"></property><br style="margin:0px; padding:0px"><wbr><wbr><wbr><wbr><wbr><wbr><property name="password" value="sa"></property><br style="margin:0px; padding:0px"><wbr><wbr><wbr></bean><br style="margin:0px; padding:0px"><wbr><wbr><wbr><wbr><br style="margin:0px; padding:0px"><wbr><wbr><wbr><!-- Spring iBatis Template --><br style="margin:0px; padding:0px"><wbr><wbr><wbr><bean id="sqlMapClient"<br style="margin:0px; padding:0px"><wbr><wbr><wbr><wbr><wbr><wbr>class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"><br style="margin:0px; padding:0px"><wbr><wbr><wbr><wbr><wbr><wbr><property name="configLocation" value="classpath:config/SqlMapConfig.xml" /><br style="margin:0px; padding:0px"><wbr><wbr><wbr><wbr><wbr><wbr><property name="dataSource" ref="dataSource" /><br style="margin:0px; padding:0px"><wbr><wbr><wbr></bean><br style="margin:0px; padding:0px"><wbr><wbr><wbr><wbr><br style="margin:0px; padding:0px"><wbr><wbr><wbr><!-- 事务管理 --><br style="margin:0px; padding:0px"><wbr><wbr><wbr><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionMan<wbr>ager"><br style="margin:0px; padding:0px"><wbr><wbr><wbr><property name="dataSource" ref="dataSource"></property><br style="margin:0px; padding:0px"><wbr><wbr><wbr></bean><br style="margin:0px; padding:0px"><wbr><wbr><wbr><wbr><br style="margin:0px; padding:0px"><wbr><wbr><wbr><!-- 声明式事务管理 --><br style="margin:0px; padding:0px"><wbr><wbr><wbr><bean id="baseTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryB<wbr>ean" abstract="true"><br style="margin:0px; padding:0px"><wbr><wbr><wbr><wbr><wbr><wbr><property name="transactionManager" ref="transactionManager"></property><br style="margin:0px; padding:0px"><wbr><wbr><wbr><wbr><wbr><wbr><property name="transactionAttributes"><br style="margin:0px; padding:0px"><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><props><br style="margin:0px; padding:0px"><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><prop key="add*">PROPAGATION_REQUIRED</prop><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><br style="margin:0px; padding:0px"><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><prop key="edit*">PROPAGATION_REQUIRED</prop><br style="margin:0px; padding:0px"><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><prop key="*">PROPAGATION_REQUIRED,readOnly</prop><br style="margin:0px; padding:0px"><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></props><br style="margin:0px; padding:0px"><wbr><wbr><wbr><wbr><wbr><wbr></property><br style="margin:0px; padding:0px"><wbr><wbr><wbr></bean><br style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><wbr><span style="word-wrap:normal; word-break:normal; line-height:17.600000381469727px; margin:0px; padding:0px; color:rgb(255,0,0)"><wbr><wbr><!-- 注意该DAO 一定要继承SqlMapClientDaoSupport 使用getSqlMapClientTemplate()方法,并且要抛出 throws DataAccessException 异常 spring才能捕获并回滚 --></wbr></wbr></span><br style="margin:0px; padding:0px"><wbr><wbr><wbr><bean id="UserDAO" class="com.gong.struts.test.UserDAO"><br style="margin:0px; padding:0px"><wbr><wbr><wbr><wbr><wbr><wbr><property name="sqlMapClient" ref="sqlMapClient"></property><br style="margin:0px; padding:0px"><wbr><wbr><wbr style="margin:0px; padding:0px"></bean><br></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>