Spring Boot Multiple Datasource
来源:互联网 发布:雷霆vs快船球员数据 编辑:程序博客网 时间:2024/06/07 02:47
- application.yml
spring: datasource: user: url: jdbc:postgresql://192.168.1.254:5432/trunk-226-mems-auth driver-class-name: org.postgresql.Driver username: postgres password: password09! basic: url: jdbc:postgresql://192.168.1.254:5432/trunk-226-mems-basic driver-class-name: org.postgresql.Driver username: postgres password: password09! work: url: jdbc:postgresql://192.168.1.254:5432/trunk-226-mems-work-order driver-class-name: org.postgresql.Driver username: postgres password: password09!
- Application类
@Primary @Qualifier("dataSourceUser") @Bean(name = "dataSourceUser") @ConfigurationProperties(prefix="spring.datasource.user") public DataSource userDataSource(){ return DataSourceBuilder.create().build(); } @Bean(name = "dataSourceBasic") @Qualifier("dataSourceBasic") @ConfigurationProperties(prefix="spring.datasource.basic") public DataSource basicDataSource(){ return DataSourceBuilder.create().build(); } @Bean(name = "dataSourceWork") @Qualifier("dataSourceWork") @ConfigurationProperties(prefix="spring.datasource.work") public DataSource workOrderDataSource(){ return DataSourceBuilder.create().build(); }
- 创建一个config文件
package com.avp.mems.backstage.conf;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Primary;import org.springframework.data.jpa.repository.config.EnableJpaRepositories;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.orm.jpa.JpaTransactionManager;import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;import org.springframework.transaction.PlatformTransactionManager;import org.springframework.transaction.annotation.EnableTransactionManagement;import org.springframework.transaction.jta.JtaTransactionManager;import javax.persistence.EntityManager;import javax.sql.DataSource;import java.util.Map;import java.util.Properties;/** * Created by Amber Wang on 2017/5/22. */@Configuration@EnableTransactionManagement@EnableJpaRepositories( entityManagerFactoryRef = "entityManagerFactoryBasic", transactionManagerRef = "transactionManagerBasic", basePackages = {"com.avp.mems.backstage.repositories.basic"})public class RepositoryBasicConfig { @Autowired private JpaProperties jpaProperties; @Autowired @Qualifier("dataSourceBasic") private DataSource dataSourceBasic; Properties additionalJpaProperties(){ Properties properties = new Properties(); properties.setProperty("hibernate.show_sql", "true"); return properties; } @Primary @Bean(name = "entityManagerFactoryBasic") public LocalContainerEntityManagerFactoryBean userEntityManager() { LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); em.setDataSource(dataSourceBasic); em.setPackagesToScan(new String[]{"com.avp.mems.backstage.entity.basic"}); em.setJpaVendorAdapter(new HibernateJpaVendorAdapter()); em.setJpaPropertyMap(getVendorProperties(dataSourceBasic)); em.setJpaProperties(additionalJpaProperties()); return em; } private Map<String, String> getVendorProperties(DataSource dataSource) { return jpaProperties.getHibernateProperties(dataSource); } @Primary @Bean(name = "transactionManagerBasic") public PlatformTransactionManager userTransactionManager() { return new JpaTransactionManager(userEntityManager().getObject()); //return new JtaTransactionManager(); }}
阅读全文
0 0
- Spring Boot Multiple Datasource
- Spring Boot 配置DataSource
- Spring boot 配置Druid DataSource
- 6. datasource - mysql【从零开始学Spring Boot】
- 6. datasource - mysql【从零开始学Spring Boot】
- spring-boot配置文件-DataSource(中文翻译)
- Spring Boot 学习6--DataSource-MySql
- Spring Boot配置多个DataSource
- Spring Boot配置多个DataSource
- spring boot创建datasource的问题
- Spring Boot配置多个DataSource
- (6)Spring Boot datasource - mysql【从零开始学Spring Boot】
- spring-boot-multi-datasource(spring-boot多路数据源)
- spring-boot-dyna-datasource(spring-boot动态数据源)
- 91.Spring Boot属性spring.datasource.type特别篇
- Spring-boot JDBC with multiple DataSources sample
- Spring boot ssh dataSource,通过SSH协议链接mysql
- Spring Boot 装配DataSource缺少依赖导致失败
- 信号与槽简易实现方式
- MyBatis 配置文件以及SqlSessionFactory对象的创建
- codeforces——584A——Olesya and Rodion
- java使用阻塞队列(BlockingQueue)来控制线程通信
- android 7.0 因为.so文件而崩溃事件解决
- Spring Boot Multiple Datasource
- Spring security使用/不使用MD5加密匹配数据库
- 嵌入式Linux系统设计--第三章:嵌入式系统软件基础
- 邮箱地址有效性验证功能中验证结果不准确原因分析
- 修改MySQL数据库root用户密码
- Material Design学习之 TabLayout(ViewPager实现页卡最简单方法)
- Android5.0新特性:Toolbar标题栏
- TabLayout的使用
- unity3d优化总结篇