日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring中的Ibatis之SqlMapClientDaoSupport

發(fā)布時間:2024/1/17 javascript 79 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring中的Ibatis之SqlMapClientDaoSupport 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Spring通過DAO模式,提供了對iBATIS的良好支持。SqlMapClient對象是iBATIS中的主要對象,我們可以通過配置讓spring來管理SqlMapClient對象的創(chuàng)建,繼而整合iBatis和Spring。

與hibernate類似,Spring 提供了SqlMapClientDaoSupport對象,我們的DAO可以繼承這個類,通過它所提供的SqlMapClientTemplate對象來操縱數(shù)據(jù)庫??雌饋磉@些概念都與hibernate類似。 通過SqlMapClientTemplate來操縱數(shù)據(jù)庫的CRUD是沒有問題的,這里面關(guān)鍵的問題是事務(wù)處理。Spring提供了強大的聲明式事務(wù)處理的功能,我們已經(jīng)清楚hibernate中如何配置聲明式的事務(wù),那么在iBATIS中如何獲得聲明式事務(wù)的能力呢?我們又怎樣整合iBatis和Spring呢? 第一,我們需要了解的是spring通過AOP來攔截方法的調(diào)用,從而在這些方法上面添加聲明式事務(wù)處理的能力。典型配置如下:applicationContext-common.xml

?

  • <!--?配置事務(wù)特性?-->?
  • ?
  • ????<tx:advice?id="txAdvice"?transaction-manager="事務(wù)管理器名稱">?
  • ?
  • ????????<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>?
  • ?
  • ???? ?
  • ?
  • ????<!--?配置哪些類的方法需要進行事務(wù)管理?-->?
  • ?
  • ????<aop:config>?
  • ?
  • ???????<aop:pointcut?id="allManagerMethod"?expression="execution(*?com.ibatis.manager.*.*(..))"/>?
  • ?
  • ???????<aop:advisor?advice-ref="txAdvice"?pointcut-ref="allManagerMethod"/>?
  • ?
  • ????</aop:config>?
  • 這些事務(wù)都是聲明在業(yè)務(wù)邏輯層的對象上的。 第二,我們需要一個事務(wù)管理器,對事務(wù)進行管理,實現(xiàn)整合iBatis和Spring的第二步。

  • <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對象,實現(xiàn)整合iBatis和Spring的第三步

  • <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之后,數(shù)據(jù)源的配置移植到了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配置,這是整合iBatis和Spring的最后一步了

  • <bean?id="userDAO"?class="com.ibatils.dao.impl.ibatis.UserDAOImpl">?
  • ?
  • ?????<property?name=”sqlMapClient”?ref=”sqlMapClient”/>?
  • ?
  • </bean>?
  • 這就是所有需要注意的問題了,此后就可以在業(yè)務(wù)邏輯層調(diào)用DAO對象了!

    轉(zhuǎn)載于:https://www.cnblogs.com/sysmfh5200/archive/2013/05/07/sym5200.html

    總結(jié)

    以上是生活随笔為你收集整理的Spring中的Ibatis之SqlMapClientDaoSupport的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。