1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/context 8 http://www.springframework.org/schema/context/spring-context.xsd"> 9 10 <!-- 加载mysql数据配置文件 --> 11 <context:property-placeholder location="classpath:database.properties"/> 12 13 <!-- 配置datasource数据源对象 --> 14 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 15 <property name="driverClass" value="${jdbc.driver}" /> 16 <property name="jdbcUrl" value="${jdbc.url}" /> 17 <property name="user" value="${jdbc.username}" /> 18 <property name="password" value="${jdbc.password}" /> 19 <!-- c3p0数据源的私有属性 --> 20 <property name="maxPoolSize" value="30" /> 21 <property name="minPoolSize" value="30" /> 22 <!-- 关闭连接后,不自动提交 --> 23 <property name="autoCommitOnClose" value="false"/> 24 <!-- 获取连接超时时间 --> 25 <property name="checkoutTimeout" value="20000" /> 26 <!-- 获取连接失败,重试次数 --> 27 <property name="acquireIncrement" value="2" /> 28 </bean> 29 30 <!-- 配置sqlSessionFactory对象 --> 31 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 32 <!-- 注入数据库连接池 ,这里为什么是ref?因为dataSource需要的是一个对象--> 33 <property name="dataSource" ref="dataSource" /> 34 <!-- 扫描entity包,使用别名(实体类所在的包) --> 35 <property name="typeAliasesPackage" value="com.imooc.o2o.entity" /> 36 <!-- 配置mybatis全局配置文件:mybatis-config.xml --> 37 <property name="configLocation" value="classpath:mybatis-config.xml" /> 38 <!-- 扫描sql配置文件:mapper需要的xml文件 --> 39 <property name="mapperLocations" value="classpath:mapper/*.xml" /> 40 </bean> 41 42 <!-- 配置扫描DAO接口所在的包,动态实现DAO接口,注入到spring容器中 --> 43 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" > 44 <!-- 注入sqlSessionFactory对象,这里为什么是value?因为sqlSessionFactoryBeanName需要的是一个字符串String --> 45 <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> 46 <property name="basePackage" value="com.imooc.o2o.dao" /> 47 </bean> 48 </beans>