spring-mybatis.xml 筆記
整合Spring和Mybatis的主配置文件
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
< context:property-placeholder >
我們在基於spring開發應用的時候,一般都會將數據庫的配置放置在properties文件中.
<!--1 引入屬性文件,在配置中佔位使用 -->
<context:property-placeholder location="classpath:db.properties" />
這樣/src/main/resources/db.properties文件就會被spring加載
如果想使用多個配置文件,可以添加order字段來進行排序
< bean id=“datasource” >
配置數據源
<!--2 配置C3P0數據源 -->
<bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<!--驅動類名 -->
<property name="driverClass" value="${jdbc.driver}" />
<!-- url -->
<property name="jdbcUrl" value="${jdbc.url}" />
<!-- 用戶名 -->
<property name="user" value="${jdbc.uid}" />
<!-- 密碼 -->
<property name="password" value="${jdbc.pwd}" />
<!-- 當連接池中的連接耗盡的時候c3p0一次同時獲取的連接數 -->
<property name="acquireIncrement" value="5"></property>
<!-- 初始連接池大小 -->
<property name="initialPoolSize" value="10"></property>
<!-- 連接池中連接最小個數 -->
<property name="minPoolSize" value="5"></property>
<!-- 連接池中連接最大個數 -->
<property name="maxPoolSize" value="20"></property>
</bean>
c3p0 與 dpdp的區別:
dpcp 沒有自動回收空閒連接的功能,c3p0有
兩者主要是對數據連接的處理方式不同!C3P0提供最大空閒時間,DBCP提供最大連接數。
前者當連接超過最大空閒連接時間時,當前連接就會被斷掉。DBCP當連接數超過最大連接數時,所有連接都會被斷開。
dbcp它的原理是維護多個連接對象Connection,在web項目要連接數據庫時直接使用它維護的對象進行連接,省去每次都要創建連接對象的麻煩。提高效率和減少內存使用。
< bean id=“sqlSessionFactory” >
Spring和Mybatis整合,不用配置Mybatis-config.xml文件,直接將sqlSessionFactory的創建工作交給Spring
<!--3 會話工廠bean sqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 數據源 -->
<property name="dataSource" ref="datasource"></property>
<!-- 別名,掃描實體包 -->
<property name="typeAliasesPackage" value="com.zhangguo.bookstore.entities"></property>
<!-- 配置mapper文件掃描路徑 -->
<property name="mapperLocations" value="classpath:com/zhangguo/bookstore/mapper/*Mapper.xml"></property>
</bean>
Mybatis動態代理
MapperScannerConfigurer類:spring-mybatis-1.x.x.jar包 中生產出mapper接口的實現類
<!--4 自動掃描對象關係映射 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--指定會話工廠,如果當前上下文中只定義了一個則該屬性可省去 -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
<!-- 指定要自動掃描接口的基礎包,實現接口 -->
<property name="basePackage" value="com.zhangguo.bookstore.mapper"></property>
</bean>
< bean id=“transactionManager” >
<!--5 聲明式事務管理 -->
<!--定義事物管理器,由spring管理事務 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="datasource"></property>
</bean>
<!--支持註解驅動的事務管理,指定事務管理器 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
< context:component-scan >
配置完這個標籤後,spring就會去自動掃描base-package對應的路徑或者該路徑的子包下面的java文件,如果掃描到文件中帶有@Service,@Component,@Repository,@Controller等這些註解的類,則把這些類註冊爲bean
<!--6 容器自動掃描IOC組件 -->
<context:component-scan base-package="com.zhangguo.bookstore"></context:component-scan>
註解:
- @Service告訴spring容器,這是一個Service類,標識持久層Bean組件,默認情況會自動加載它到spring容器中。
- @Autowried註解告訴spring,這個字段需要自動注入
- @Scope指定此spring bean的scope是單例
- @Repository註解指定此類是一個容器類,是DA層類的實現。標識持久層Bean組件
- @Componet:基本註解,標識一個受Spring管理的Bean組件
- @Controller:標識表現層Bean組件
<!--7 aspectj支持自動代理實現AOP功能 -->
<aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
</beans>