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

  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>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Druid数据源和Spring Boot的多数据源支持来实现动态切换MySQL数据源。具体步骤如下: 1. 添加DruidSpring Boot多数据源依赖 在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.21</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> ``` 2. 配置Druid数据源 在application.properties文件中添加以下配置: ```properties # 数据源1 spring.datasource.druid.datasource-1.url=jdbc:mysql://localhost:3306/db1?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false spring.datasource.druid.datasource-1.username=root spring.datasource.druid.datasource-1.password=123456 spring.datasource.druid.datasource-1.driver-class-name=com.mysql.cj.jdbc.Driver # 数据源2 spring.datasource.druid.datasource-2.url=jdbc:mysql://localhost:3306/db2?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false spring.datasource.druid.datasource-2.username=root spring.datasource.druid.datasource-2.password=123456 spring.datasource.druid.datasource-2.driver-class-name=com.mysql.cj.jdbc.Driver ``` 3. 配置多数据源 在application.properties文件中添加以下配置: ```properties # 数据源1对应的JPA配置 spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect spring.datasource.druid.datasource-1.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.druid.datasource-1.initial-size=5 spring.datasource.druid.datasource-1.min-idle=5 spring.datasource.druid.datasource-1.max-active=20 spring.datasource.druid.datasource-1.filters=stat,log4j # 数据源2对应的JPA配置 datasource2.jpa.hibernate.ddl-auto=update datasource2.jpa.show-sql=true datasource2.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect spring.datasource.druid.datasource-2.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.druid.datasource-2.initial-size=5 spring.datasource.druid.datasource-2.min-idle=5 spring.datasource.druid.datasource-2.max-active=20 spring.datasource.druid.datasource-2.filters=stat,log4j # 多数据源配置 spring.datasource.dynamic.primary=datasource-1 spring.datasource.dynamic.datasource-1=druid.datasource-1 spring.datasource.dynamic.datasource-2=druid.datasource-2 ``` 4. 编写动态数据源切换代码 在需要使用多数据源的地方,通过以下代码来动态切换数据源: ```java // 获取数据源 DynamicDataSourceContextHolder.setDataSourceKey("datasource-1"); DataSource dataSource = DynamicDataSource.getInstance().getDataSource(); // 使用数据源 JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); List<Map<String, Object>> result = jdbcTemplate.queryForList("select * from user"); // 切换数据源 DynamicDataSourceContextHolder.setDataSourceKey("datasource-2"); dataSource = DynamicDataSource.getInstance().getDataSource(); jdbcTemplate = new JdbcTemplate(dataSource); result = jdbcTemplate.queryForList("select * from user"); ``` 以上就是使用DruidSpring Boot多数据源支持来实现动态切换MySQL数据源的步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值