1. 程式人生 > >spring整合mybatis配置檔案

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:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	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/aop 
        http://www.springframework.org/schema/aop/spring-aop.xsd">
	<!-- 開啟自動掃描 -->
	<context:component-scan base-package="com.project.spring_mybatis"></context:component-scan>
	<!-- -->
	<aop:aspectj-autoproxy />
	<!-- 引入外部資料庫的配置檔案,location位置填寫的是相對位置 -->
	<context:property-placeholder location="jdbc.properties"
		ignore-resource-not-found="false" local-override="false" />
	<!-- 配置資料庫連線池 -->
	<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
		<property name="driverClassName" value="${jdbc.driver}"></property>
		<property name="url" value="${jdbc.url}"></property>
		<property name="username" value="${jdbc.username}"></property>
		<property name="password" value="${jdbc.password}"></property>
		<!-- 是否預設自動提交 -->
		<property name="defaultAutoCommit" value="false"></property>
		<!-- 是否為只讀 -->
		<property name="defaultReadOnly" value="false"></property>
		<!-- 預設的事務隔離級別 -->
		<property name="defaultTransactionIsolation" value="4" />


		<!-- dbcp在初始化時,新建的連線數量 -->
		<property name="initialSize" value="10" />
		<!-- 在同一時間能夠擁有最大的活躍的連線的數量 -->
		<property name="maxTotal" value="50" />
		<!-- 配置空閒區的最小連線數 -->
		<property name="minIdle" value="0" />
		<!-- 配置空閒區的最大連線數 -->
		<property name="maxIdle" value="10" />


		<!-- 設定連線處理查詢語句的超時時間,單位為秒 -->
		<property name="validationQueryTimeout" value="10"></property>
		<!-- 連線池中是否允許快取預編譯物件 -->
		<property name="poolPreparedStatements" value="true" />
	</bean>


	<!-- ========================================針對myBatis的配置項============================== -->
	<!-- 配置SessionFactory -->
	<bean name="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 例項化sqlSessionFactory時需要使用上述配置好的資料來源以及SQL對映檔案 -->
		<property name="dataSource" ref="dataSource" />
		<property name="typeAliasesPackage" value="com.project.spring_mybatis.beans"></property>
		<property name="mapperLocations" value="com.project.spring_mybatis.stumag.mapper.*" />
	</bean>
	<!-- 掃描mapper配置檔案的地址 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.project.spring_mybatis.stumag.mapper.*" />
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
	</bean>
	<!-- 配置Spring的事務管理器 -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>

</beans>