<?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: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.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd"> <!-- <bean id="configer" --> <!-- class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> --> <!-- <property name="location" value="classpath:application.properties"></property> --> <!-- </bean> --> <context:component-scan base-package="com.service"/> <!-- 使用 dbcp2 来管理 数据库连接 --> <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${driver}"></property> <property name="username" value="${username}"></property> <property name="password" value="${password}"></property> <property name="initialSize" value="${initialSize}"></property> <property name="url" value="${url}"></property> <property name="maxTotal" value="${maxTotal}"></property> <property name="maxIdle" value="${maxIdle}"></property> <property name="minIdle" value="${minIdle}"></property> <property name="maxWaitMillis" value="${maxWait}"></property> <property name="defaultAutoCommit" value="false"></property> <!-- 连接空闲后一段时间 被逐出连接池的时间 毫秒 --> <property name="minEvictableIdleTimeMillis" value="120000"></property> </bean> <!-- mybatis 持久化实例 准备 所有的 DAO 对象 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <!-- 读取配置文件 --> <property name="mapperLocations" value="classpath:mapper/*.xml"></property> </bean> <!-- 查找 DAO 并将其 放入 spring 容器中便于管理 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.dao"></property> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> <!-- spring 事务声明 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 事务注解驱动,标注@Transactional的类和方法将具有事务性 --> <tx:annotation-driven transaction-manager="transactionManager" /> </beans>