從ibatis過度到mybatis,spring整合i/mybatis配置檔案的差別
阿新 • • 發佈:2019-01-09
這樣所有的配置就結束了,剩下的是在Dao中繼承Spring對ibatis提供的Dao操作的模板類SqlMapClientDaoSupport。通常開發中的做法是寫成一個BaseDao來繼承這個類。然後讓每一個需要的Dao再繼承BaseDao就可以了。原因是因為在BaseDao中我們需要給SqlMapClientDaoSupport注入SqlMapClientFactoryBean的例項SqlMapClient,這樣在父類BaseDao中注入一次就可以了。不然每個Dao都需要注入顯得很繁瑣。直接上程式碼:<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <context:property-placeholder location="classpath:db.properties"/> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driver}" /> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="user" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"></property> </bean> <bean id="sqlMapClientFactoryBean" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> <property name="configLocation"> <value>classpath:ibatis/sqlMapConfig.xml</value> </property> <property name="dataSource" ref="dataSource"/> </bean> <bean id="accountDao" class="fly.sun.demo1.AccountDaoImpl"> <!-- 此處不需要再配置資料來源了,在"sqlMapClientFactoryBean"已經配置過了,如果accountDao中用的不是上面的dataSource,再單獨配置 --> <!-- <property name="dataSource" ref=""/> --> </bean> <bean id="accountService" class="fly.sun.demo1.AccountServiceImpl"> <property name="accountDao" ref="accountDao"/> </bean> </beans>