<?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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"> <!-- 1. 数据源配置 --> <context:property-placeholder ignore-unresolvable="true" location="classpath:jdbc.properties" /> <!-- Druid方式配置数据源 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <!-- 基本属性 url、user、password --> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <!-- 配置初始化大小、最小、最大 --> <property name="initialSize" value="10" /> <property name="minIdle" value="20" /> <property name="maxActive" value="100" /> <!-- 配置获取连接等待超时的时间 --> <property name="maxWait" value="60000" /> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="6000" /> <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="300000" /> <!-- 验证是否回收 --> <property name="validationQuery" value="SELECT 'x' FROM DUAL" /> <property name="testWhileIdle" value="true" /> <property name="testOnBorrow" value="false" /> <property name="testOnReturn" value="false" /> <!-- 开启Druid的监控统计功能 --> <property name="filters" value="stat" /> </bean> <!-- 2. 创建SqlSession的工厂 --> <!-- dataSource:引用数据源,统一加载配置--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" ></property> <!-- 自动配置别名-作用类似mybatis-config.xml的别名 --> <property name="typeAliasesPackage" value="com.demo.model" /> <!-- 设置别名的类加上父类限定 --> <property name="typeAliasesSuperType" value="com.demo.common.base.BaseEntity"/> <!-- 当mybatis的xml文件和mapper接口不在相同包下时,需要用mapperLocations属性指定xml文件的路径 --> <!-- *是个通配符,代表所有的文件,**代表所有目录下 --> <property name="mapperLocations" value="classpath*:mappings/**/*.xml"/> <!-- 指定mybatis核心配置文件 --> <property name="configLocation" value="classpath:mybatis-config.xml"></property> </bean> <!-- 3. 自动扫描加载Sql映射文件/接口 --> <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- sqlSessionFactoryBeanName:代表延迟加载--> <!-- 这个配置的前提条件是:映射接口类文件(.java)和映射XML文件(.xml)需要放在相同的包下(com.demo.mapper)--> <!-- <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> --> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> <!-- basePackage:指定sql映射文件/接口所在的包(自动扫描)--> <property name="basePackage" value="com.demo.mapper"></property> <!-- 扫描basePackage下所有以@MyBatisDao注解的接口 --> <property name="annotationClass" value="com.demo.common.persistence.annotation.MyBatisDao"/> </bean> <!-- 4. 事务管理 --> <!-- dataSource:引用上面定义的数据源 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 5. 使用声明式事务 --> <!-- transaction-manager:引用上面定义的事务管理器 --> <!-- 配置 Annotation 驱动,扫描@Transactional注解的类定义事务 --> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/> <!-- 定义JdbcTemplate的Bean --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" p:dataSource-ref="dataSource"></bean> </beans>
这篇文档对您是否有帮助?