Spring+JPA+druid+hibernate配置

来源:互联网 发布:打字练习软件 编辑:程序博客网 时间:2024/06/12 23:56
  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" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"  
  5.     xmlns:p="http://www.springframework.org/schema/p" xmlns:cache="http://www.springframework.org/schema/cache"  
  6.     xmlns:jpa="http://www.springframework.org/schema/data/jpa"  
  7.   
  8.     xsi:schemaLocation="http://www.springframework.org/schema/beans     
  9.           http://www.springframework.org/schema/beans/spring-beans-3.1.xsd     
  10.           http://www.springframework.org/schema/context     
  11.           http://www.springframework.org/schema/context/spring-context-3.1.xsd     
  12.           http://www.springframework.org/schema/aop     
  13.           http://www.springframework.org/schema/aop/spring-aop-3.1.xsd     
  14.           http://www.springframework.org/schema/tx      
  15.           http://www.springframework.org/schema/tx/spring-tx-3.1.xsd  
  16.           http://www.springframework.org/schema/cache   
  17.           http://www.springframework.org/schema/cache/spring-cache-3.1.xsd  
  18.           http://www.springframework.org/schema/data/jpa  
  19.           http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd">  
  20.   
  21.   
  22.     <!-- 数据源配置 -->  
  23.     <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"  
  24.         init-method="init" destroy-method="close">  
  25.         <!-- 驱动名称 -->  
  26.         <property name="DriverClassName" value="com.mysql.jdbc.Driver" />  
  27.         <!-- JDBC连接串 -->  
  28.         <property name="url"  
  29.             value="jdbc:mysql://192.168.7.7:3306/tmc_db?useUnicode=true&amp;characterEncoding=UTF-8&amp;zeroDateTimeBehavior=convertToNull&amp;transformedBitIsBoolean=true" />  
  30.         <!-- 数据库用户名称 -->  
  31.         <property name="username" value="root" />  
  32.         <!-- 数据库密码 -->  
  33.         <property name="password" value="root" />  
  34.         <!-- 连接池最大使用连接数量 -->  
  35.         <property name="maxActive" value="20" />  
  36.         <!-- 初始化大小 -->  
  37.         <property name="initialSize" value="5" />  
  38.         <!-- 获取连接最大等待时间 -->  
  39.         <property name="maxWait" value="60000" />  
  40.         <!-- 连接池最小空闲 -->  
  41.         <property name="minIdle" value="2" />  
  42.         <!-- 逐出连接的检测时间间隔 -->  
  43.         <property name="timeBetweenEvictionRunsMillis" value="3000" />  
  44.         <!-- 最小逐出时间 -->  
  45.         <property name="minEvictableIdleTimeMillis" value="300000" />  
  46.         <!-- 测试有效用的SQL Query -->  
  47.         <property name="validationQuery" value="SELECT 'x'" />  
  48.         <!-- 连接空闲时测试是否有效 -->  
  49.         <property name="testWhileIdle" value="true" />  
  50.         <!-- 获取连接时测试是否有效 -->  
  51.         <property name="testOnBorrow" value="false" />  
  52.         <!-- 归还连接时是否测试有效 -->  
  53.         <property name="testOnReturn" value="false" />  
  54.     </bean>  
  55.   
  56.   
  57.     <!-- JPA实体工厂配置 -->  
  58.     <bean id="entityManagerFactory"  
  59.         class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">  
  60.         <property name="dataSource" ref="dataSource" />  
  61.         <!-- 扫描实体路径 -->  
  62.         <property name="packagesToScan" value="com.spinfosec.dao.entity" />  
  63.         <property name="jpaVendorAdapter">  
  64.             <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">  
  65.                 <property name="showSql" value="true" />  
  66.                 <property name="generateDdl" value="false" />  
  67.             </bean>  
  68.         </property>  
  69.         <property name="jpaProperties">  
  70.             <props>  
  71.                 <!--设置外连接抓取树的最大深度 -->  
  72.                 <prop key="hibernate.max_fetch_depth">3</prop>  
  73.                 <prop key="hibernate.jdbc.fetch_size">18</prop>  
  74.                 <prop key="hibernate.jdbc.batch_size">10</prop>  
  75.                 <!-- 自动建表类型 validate|create|create-drop|update -->  
  76.                 <prop key="hibernate.hbm2ddl.auto">validate</prop>  
  77.                 <!-- 是否显示SQL -->  
  78.                 <prop key="hibernate.show_sql">false</prop>  
  79.                 <!-- 显示SQL是否格式化 -->  
  80.                 <prop key="hibernate.format_sql">false</prop>  
  81.                 <!-- 关闭二级缓存 -->  
  82.                 <prop key="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</prop>  
  83.                 <!-- 关闭实体字段映射校验 -->  
  84.                 <prop key="javax.persistence.validation.mode">none</prop>  
  85.             </props>  
  86.         </property>  
  87.     </bean>  
  88.   
  89.     <!-- 配置事务管理器 -->  
  90.     <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">  
  91.         <property name="dataSource" ref="dataSource" />  
  92.         <property name="entityManagerFactory" ref="entityManagerFactory" />  
  93.     </bean>  
  94.   
  95.     <!-- 配置事务传播特性 -->  
  96.     <tx:advice id="txAdvice">  
  97.         <tx:attributes>  
  98.             <tx:method name="find*" read-only="true" />  
  99.             <tx:method name="*" propagation="REQUIRED" />  
  100.         </tx:attributes>  
  101.     </tx:advice>  
  102.   
  103.     <!-- 配置参与事务的类 -->  
  104.     <aop:config>  
  105.         <aop:pointcut id="dao-tx" expression="execution(* *..dao.impl..*(..))" />  
  106.         <aop:pointcut id="service-tx" expression="execution(* *..service.impl..*(..))" />  
  107.         <aop:advisor pointcut-ref="dao-tx" advice-ref="txAdvice" />  
  108.         <aop:advisor pointcut-ref="service-tx" advice-ref="txAdvice" />  
  109.     </aop:config>  
  110.   
  111.     <!-- 事务注解 -->  
  112.     <tx:annotation-driven />  
  113.   
  114. </beans>  
原创粉丝点击