一.导包:
<properties><spring-version>4.3.10.RELEASE</spring-version>
<hibernate-version>5.2.11.Final</hibernate-version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring-version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate-version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.42</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.4</version>
</dependency>
二.配置spring-context.xml文件和需要引入的数据库连接配置
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:P="http://www.springframework.org/schema/p" 6 xmlns:util="http://www.springframework.org/schema/util" 7 xmlns:tx="http://www.springframework.org/schema/tx" 8 xmlns:aop="http://www.springframework.org/schema/aop" 9 xsi:schemaLocation="http://www.springframework.org/schema/beans 10 http:// 11 http:// 12 http:// 13 http:// 14 http:// 15 http:// 16 http:// 17 http:// 18 http://"> 19 <context:component-scan base-package="com.yztc.app"/> 20 <bean 21 class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" 22 P:dataSource-ref="dataSource" 23 P:packagesToScan="com.yztc.app" 24 P:jpaProperties-ref="jpaProperties" 25 P:persistenceProvider-ref="persistenceProvider" 26 P:jpaVendorAdapter-ref="jpaVendorAdapter" 27 /> 28 <!--数据库连接配置--> 29 <context:property-placeholder location="classpath:config/db.properties"/> 30 <!--适配--> 31 <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> 32 <property value="MYSQL"/> 33 <property value="true"/> 34 <property value="true"/> 35 <property value="org.hibernate.dialect.MySQL55Dialect"/> 36 </bean> 37 <bean class="org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider"> 38 39 </bean> 40 <!--jpa 属性配置--> 41 <util:properties> 42 <!--设置外连接抓取树的最大深度--> 43 <prop key="hibernate.max_fetch_depth">3</prop> 44 <!--查询返回的行数--> 45 <prop key="hibernate.jdbc.fetch_size">50</prop> 46 <!--批量执行sql语句的满25条提交一次--> 47 <prop key="hibernate.jdbc.batch_size">25</prop> 48 <!-- 自动建表类型 validate|create|create-drop|update --> 49 <prop key="hibernate.hbm2ddl.auto">create</prop> 50 <!-- 是否显示SQL --> 51 <prop key="hibernate.show_sql">true</prop> 52 <!-- 显示SQL是否格式化 --> 53 <prop key="hibernate.format_sql">false</prop> 54 <!-- 关闭二级缓存 --> 55 <prop key="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</prop> 56 <!-- 关闭实体字段映射校验 --> 57 <prop key="javax.persistence.validation.mode">none</prop> 58 </util:properties> 59 <!--第三方的连接池的配置 c3p0 druid--> 60 <bean class="com.alibaba.druid.pool.DruidDataSource"> 61 <!--JDBC连接串--> 62 <property value="${jdbc.url}" /> 63 <!--数据库用户名称--> 64 <property value="${jdbc.username}" /> 65 <!--数据库密码--> 66 <property value="${jdbc.password}" /> 67 <!-- 连接池最大使用连接数量 --> 68 <property value="${jdbc.maxActive}"/> 69 <!-- 初始化大小 --> 70 <property value="${jdbc.initialSize}"/> 71 <!-- 获取连接最大等待时间 --> 72 <property value="${jdbc.maxWait}"/> 73 <!-- 连接池最小空闲 --> 74 <property value="${jdbc.minIdle}"/> 75 <!-- 逐出连接的检测时间间隔 --> 76 <property value="${jdbc.timeBetweenEvictionRunsMillis}"/> 77 <!-- 最小逐出时间 --> 78 <property value="${jdbc.minEvictableIdleTimeMillis}"/> 79 <!-- 测试有效用的SQL Query --> 80 <property value="SELECT \'x\'"/> 81 <!-- 连接空闲时测试是否有效 --> 82 <property value="true"/> 83 <!-- 获取连接时测试是否有效 --> 84 <property value="false"/> 85 <!-- 归还连接时是否测试有效 --> 86 <property value="false"/> 87 </bean> 88 <!--事务--> 89 <bean class="org.springframework.orm.jpa.JpaTransactionManager"> 90 <property ref="entityManagerFactory"/> 91 </bean> 92 <!--<tx:advice transaction-manager="tx">--> 93 <!--<!–crud–>--> 94 <!--<tx:attributes>--> 95 <!--<tx:method rollback-for="java.lang.Exception"/>--> 96 <!--<tx:method rollback-for="java.lang.Exception"/>--> 97 <!--<tx:method rollback-for="java.lang.Exception"/>--> 98 <!--<tx:method rollback-for="java.lang.Exception"/>--> 99 <!--<tx:method read-only="true" propagation="SUPPORTS"/>--> 100 <!--<tx:method read-only="true" propagation="SUPPORTS"/>--> 101 <!--</tx:attributes>--> 102 <!--</tx:advice>--> 103 <!--<aop:config>--> 104 <!--<aop:pointcut expression="execution(* com.yztc.app.dao.*.*(..))"/>--> 105 <!--<aop:advisor advice-ref="advice" pointcut-ref="pointcut"/>--> 106 <!--</aop:config>--> 107 <tx:annotation-driven transaction-manager="tx" proxy-target-class="true"/> 108 </beans>