spring+hibernate+jpa+Druid的配置文件,spring整合Druid

来源:互联网 发布:无网络也可以自动报时 编辑:程序博客网 时间:2024/06/05 11:15
  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:aop="http://www.springframework.org/schema/aop"  
  5.     xmlns:context="http://www.springframework.org/schema/context"  
  6.     xmlns:task="http://www.springframework.org/schema/task"  
  7.     xmlns:tx="http://www.springframework.org/schema/tx"  
  8.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  9.         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd  
  10.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd  
  11.         http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd  
  12.         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">  
  13.   
  14.     <context:property-placeholder location="classpath:jdbc.properties"/>  
  15.       
  16.     <context:component-scan base-package="com.lqy.spring.iwx.**">  
  17.         <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>  
  18.         <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>  
  19.     </context:component-scan>  
  20.       
  21.     <!-- mysql数据源配置 -->  
  22.     <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">  
  23.         <!-- 数据库用户名称 -->  
  24.         <property name="username" value="${jdbc.username}"/>  
  25.         <!-- 数据库密码 -->  
  26.         <property name="password" value="${jdbc.password}"/>  
  27.         <!-- 驱动名称 -->  
  28.         <property name="driverClassName" value="${jdbc.driverClassName}" />  
  29.         <!-- JDBC连接串 -->  
  30.         <property name="url" value="${jdbc.url}" />  
  31.         <!-- 连接池最大使用连接数量 -->  
  32.         <property name="maxActive" value="${jdbc.maxActive}" />  
  33.         <!-- 初始化大小 -->  
  34.         <property name="initialSize" value="${jdbc.initialSize}" />  
  35.         <!-- 获取连接最大等待时间 -->  
  36.         <property name="maxWait" value="${jdbc.maxWait}" />  
  37.         <!-- 连接池最小空闲 -->  
  38.         <property name="minIdle" value="${jdbc.minIdle}" />  
  39.         <!-- 逐出连接的检测时间间隔 -->  
  40.         <property name="timeBetweenEvictionRunsMillis" value="${jdbc.timeBetweenEvictionRunsMillis}" />  
  41.         <!-- 最小逐出时间 -->  
  42.         <property name="minEvictableIdleTimeMillis" value="${jdbc.minEvictableIdleTimeMillis}" />  
  43.         <!-- 测试有效用的SQL Query -->  
  44.         <property name="validationQuery" value="SELECT 'x'" />  
  45.         <!-- 连接空闲时测试是否有效 -->  
  46.         <property name="testWhileIdle" value="true" />  
  47.         <!-- 获取连接时测试是否有效 -->  
  48.         <property name="testOnBorrow" value="false" />  
  49.         <!-- 归还连接时是否测试有效 -->  
  50.         <property name="testOnReturn" value="false" />  
  51.     </bean>  
  52.       
  53.     <bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">  
  54.         <property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect" />  
  55.     </bean>  
  56.               
  57.     <!-- 配置Spring管理Jpa的工厂Bean,需要加入spring-orm-4.1.7.RELEASE.jar(LocalEntityManagerFactoryBean类在里面) -->  
  58.     <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">  
  59.         <property name="dataSource" ref="dataSource"></property>  
  60.         <!--待扫描的实体类包,不再需要persistence.xml了-->  
  61.         <property name="packagesToScan" value="com.lqy.spring.iwx.bean.**"></property>  
  62.         <property name="jpaVendorAdapter" ref="jpaVendorAdapter"></property>  
  63.         <property name="jpaProperties">  
  64.             <props>  
  65.                 <!--设置外连接抓取树的最大深度 -->  
  66.                 <prop key="hibernate.max_fetch_depth">3</prop>  
  67.                 <prop key="hibernate.jdbc.fetch_size">18</prop>  
  68.                 <prop key="hibernate.jdbc.batch_size">10</prop>  
  69.                   
  70.                 <!-- 自动建表类型 validate|create|create-drop|update -->  
  71.                 <prop key="hibernate.hbm2ddl.auto">update</prop>  
  72.                 <!-- 是否显示SQL -->  
  73.                 <prop key="hibernate.show_sql">false</prop>  
  74.                 <!-- 显示SQL是否格式化 -->  
  75.                 <prop key="hibernate.format_sql">false</prop>  
  76.                 <!-- 关闭二级缓存 -->  
  77.                 <prop key="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</prop>  
  78.                 <!-- 关闭实体字段映射校验 -->  
  79.                 <prop key="javax.persistence.validation.mode">none</prop>  
  80.             </props>  
  81.         </property>  
  82.     </bean>  
  83.       
  84.     <!-- 配置事务管理 -->  
  85.     <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">  
  86.         <property name="entityManagerFactory" ref="entityManagerFactory"></property>  
  87.     </bean>  
  88.       
  89.       
  90.     <!-- 启用事务注解 -->  
  91.     <!--   
  92.         Spring事务默认只能对运行时异常(RuntimeException)进行回滚,  
  93.         不会对Exception进行回滚。  
  94.         如果需要指定其他异常,则需要配置:rollbackFor=Exception.class  
  95.      -->  
  96.     <tx:annotation-driven transaction-manager="transactionManager"/>  
  97.       
  98.     <task:annotation-driven scheduler="taskScheduler" mode="proxy"/>      
  99.     <task:scheduler id="taskScheduler" pool-size="10"/>  
  100.   
  101. </beans>  
原创粉丝点击