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 xmlns:tx="http://www.springframework.org/schema/tx" 6 xmlns:jpa="http://www.springframework.org/schema/data/jpa" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd 9 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd 10 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> 11 12 13 <!-- 配置自动扫描的包 --> 14 <context:component-scan base-package="com"> 15 </context:component-scan> 16 17 <!-- 配置数据源(数据库连接池) --> 18 <context:property-placeholder location="classpath:db.properties"/> 19 <bean id="dataSource" 20 class="com.mchange.v2.c3p0.ComboPooledDataSource"> 21 <property name="user" value="${jdbc.user}"></property> 22 <property name="password" value="${jdbc.password}"></property> 23 <property name="driverClass" value="${jdbc.driverClass}"></property> 24 <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property> 25 </bean> 26 27 <!-- 配置 JPA 的 EntityManagerFactory --> 28 <bean id="entityManagerFactory" 29 class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 30 <property name="dataSource" ref="dataSource"></property><!-- 添加数据源 --> 31 <property name="jpaVendorAdapter"> 32 <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"></bean> 33 </property> 34 <property name="packagesToScan" value="com.entity"></property> 35 <!-- hibernate 配置 --> 36 <property name="jpaProperties"> 37 <props> 38 <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop> 39 <prop key="hibernate.hbm2ddl.auto">none</prop> 40 <prop key="hibernate.show_sql">true</prop> 41 <prop key="hibernate.format_sql">true</prop> 42 <!-- 方言 hibernate 并不知道自己要使用哪种数据库 --> 43 <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop> 44 <!-- 45 <prop key="hibernate.cache.use_second_level_cache">true</prop> 46 <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop> 47 <prop key="hibernate.cache.use_query_cache">true</prop> 48 --> 49 </props> 50 </property> 51 <!-- <property name="sharedCacheMode" value="ENABLE_SELECTIVE"></property>--> 52 </bean> 53 54 <!-- 配置事务 纯事物 jpa事物 --> 55 <bean id="transactionManager" 56 class="org.springframework.orm.jpa.JpaTransactionManager"> 57 <property name="entityManagerFactory" ref="entityManagerFactory"></property> 58 </bean> 59 60 <!-- 配置支持基于注解的事务 --> 61 <tx:annotation-driven transaction-manager="transactionManager"/> 62 63 <!-- 配置 SpringData --> 64 <jpa:repositories base-package="com.dao" 65 entity-manager-factory-ref="entityManagerFactory"></jpa:repositories> 66 67 68 </beans>