通常咱們會在datasource.xml中進行以下配置,可是其中每一個配置項原理和用途是什麼,並非那麼清楚,若是不清楚的話,在使用時候就頗有可能會遇到坑,因此下面對這些配置項進行一一解說java
(1)配置數據源 <bean id="dataSourace" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /> <property name="url" value="${db_url}" /> <property name="username" value="$db_user}" /> <property name="password" value="${db_passwd}" /> <property name="maxWait" value="${db_maxWait}" /> <property name="maxActive" value="28" /> <property name="initialSize" value="2" /> <property name="minIdle" value="0" /> <property name="timeBetweenEvictionRunsMillis" value="db_time" /> </bean> (2)建立sqlSessionFactory <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="mapperLocations" value="classpath*:com/**/mapper/*Mapper*.xml" /> <property name="dataSource" ref="dataSource" /> <property name="typeAliasesPackage" value="com.test.***.dal" /> </bean> (3)配置掃描器,掃描指定路徑的mapper生成數據庫操做代理類 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="annotationClass" value="javax.annotation.Resource"></property> <property name="basePackage" value="com.test.***.dal.***.mapper" /> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean> (4)配置事務管理器 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> (5)聲明使用註解式事務 <tx:annotation-driven transaction-manager="transactionManager" /> (6)註冊各類beanfactory處理器 <context:annotation-config /> (7)該配置建立了一個TransactionInterceptor的bean,做爲事務切面的執行方法 <tx:advice id="defaultTxAdvice"> <tx:attributes> <tx:method name="*" rollback-for="Exception" /> </tx:attributes> </tx:advice> (8)該配置建立了一個DefaultBeanFactoryPointcutAdvisor的bean,該bean是一個advisor,裏面包含了pointcut和advice.前者說明切面加在哪裏,後者是執行邏輯。此處能夠配多個advisor <aop:config> <aop:pointcut id="myCut" expression="(execution(* *..*BoImpl.*(..))) "/> <aop:advisor pointcut-ref="myCut" advice-ref="defaultTxAdvice" /> </aop:config>
(1)是數據源配置,這個沒啥好說的。spring
(2) 做用是根據配置建立一個SqlSessionFactory,看下SqlSessionFactoryBean的代碼知道它實現了FactoryBean和InitializingBean類,因爲實現了InitializingBean,因此天然它的afterPropertiesSet方法,因爲實現了FactoryBean類,因此天然會有getObject方法。下面看下時序圖:sql