springboot hikaricp 多数据源_Springboot框架

(1) 2024-09-16 15:23

Hi,大家好,我是编程小6,很荣幸遇见你,我把这些年在开发过程中遇到的问题或想法写出来,今天说一说
springboot hikaricp 多数据源_Springboot框架,希望能够帮助你!!!。

一)spring boot + hibernate 多数据源(XML)

  1. import org.springframework.boot.SpringApplication;
  2. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
  5. import org.springframework.boot.builder.SpringApplicationBuilder;
  6. import org.springframework.boot.web.support.SpringBootServletInitializer;
  7. import org.springframework.context.annotation.ComponentScan;
  8. import org.springframework.context.annotation.ImportResource;
  9. @SpringBootApplication
  10. @EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class })
  11. @ComponentScan(basePackages = "com.opentrans.kit")
  12. @ImportResource({ "classpath:META-INF/spring/applicationContext.xml" })
  13. public class KitApplication extends SpringBootServletInitializer {
  14. public static void main(String[] args) {
  15. SpringApplication.run(KitApplication.class, args);
  16. }
  17. @Override
  18. protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
  19. return builder.sources(KitApplication.class);
  20. }
  21. }
  22. 二) properties 配置
  23. ## DB properties:
  24. spring.datasource.kit.url=jdbc:postgresql://localhost:5432/kit
  25. spring.datasource.kit.username=jetty
  26. spring.datasource.kit.password=jetty
  27. spring.datasource.kit.driver-class-name=org.postgresql.Driver
  28. spring.datasource.kit.initial-size=10
  29. spring.datasource.kit.min-idle=10
  30. spring.datasource.kit.max-active=100
  31. ## DB otmsxtt
  32. spring.datasource.xtt.url=jdbc:postgresql://192.168.3.74:5432/otmsxtt2k
  33. spring.datasource.xtt.username=jetty
  34. spring.datasource.xtt.password=jetty
  35. spring.datasource.xtt.driver-class-name=org.postgresql.Driver
  36. spring.datasource.xtt.initial-size=5
  37. spring.datasource.xtt.min-idle=10
  38. spring.datasource.xtt.max-active=100
  39. ## JPA Configuration:
  40. spring.jpa.show-sql=true
  41. ## Spring.jpa.generate-ddl=true
  42. spring.jpa.hibernate.ddl-auto=none
  43. spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
  44. ## log4j
  45. logging.config=classpath:log4j2.xml

三) 注解式数据源配置

1. 数据源配置类 DataSourceConfigration.java ,多数据源时必须指定一个数据源首选项注解@Primary(prefix对应properties数据库配置的前缀)

  1. import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
  2. import org.springframework.boot.context.properties.ConfigurationProperties;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.context.annotation.Primary;
  6. import javax.sql.DataSource;
  7. @Configuration
  8. public class DataSourceConfigration {
  9. @Primary
  10. @Bean(name = "dataSourceKit")
  11. @ConfigurationProperties(prefix = "spring.datasource.kit")
  12. public DataSource dataSourceKit() {
  13. return DataSourceBuilder.create().build();
  14. }
  15. @Bean(name = "dataSourceOtms")
  16. @ConfigurationProperties(prefix = "spring.datasource.otms")
  17. public DataSource dataSourceOtms() {
  18. return DataSourceBuilder.create().build();
  19. }
  20. @Bean(name = "dataSourceXtt")
  21. @ConfigurationProperties(prefix = "spring.datasource.xtt")
  22. public DataSource dataSourceXtt() {
  23. return DataSourceBuilder.create().build();
  24. }
  25. }
  26. 2. 数据库A的配置类 KitDefine.java
  27. import org.springframework.beans.factory.annotation.Autowired;
  28. import org.springframework.beans.factory.annotation.Qualifier;
  29. import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
  30. import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
  31. import org.springframework.context.annotation.Bean;
  32. import org.springframework.context.annotation.Configuration;
  33. import org.springframework.context.annotation.Primary;
  34. import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
  35. import org.springframework.orm.jpa.JpaTransactionManager;
  36. import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
  37. import org.springframework.transaction.PlatformTransactionManager;
  38. import org.springframework.transaction.annotation.EnableTransactionManagement;
  39. import javax.sql.DataSource;
  40. import java.util.Map;
  41. @Configuration
  42. @EnableTransactionManagement
  43. @EnableJpaRepositories(
  44. entityManagerFactoryRef = "entityManagerFactoryKit",
  45. transactionManagerRef = "transactionManagerKit",
  46. basePackages = {"com.opentrans.kit.jpa.kit.repository" })
  47. public class KitDefine {
  48. @Autowired
  49. private JpaProperties jpaProperties;
  50. @Autowired
  51. @Qualifier("dataSourceKit")
  52. private DataSource dataSourceKit;
  53. @Primary
  54. @Bean(name = "entityManagerFactoryKit")
  55. public LocalContainerEntityManagerFactoryBean entityManagerFactoryKit(EntityManagerFactoryBuilder builder) {
  56. LocalContainerEntityManagerFactoryBean em = builder.dataSource(dataSourceKit)
  57. .properties(getVendorProperties(dataSourceKit))
  58. .packages("com.opentrans.kit.jpa.kit.entity")
  59. .persistenceUnit("kitPersistenceUnit").build();
  60. return em;
  61. }
  62. @Primary
  63. @Bean(name = "transactionManagerKit")
  64. PlatformTransactionManager transactionManagerKit(EntityManagerFactoryBuilder builder) {
  65. return new JpaTransactionManager(entityManagerFactoryKit(builder).getObject());
  66. }
  67. private Map<String, String> getVendorProperties(DataSource dataSource) {
  68. return jpaProperties.getHibernateProperties(dataSource);
  69. }
  70. }
  71. 3. 数据库B配置类 OtmsXttDefine.java
  72. import java.util.Map;
  73. import javax.sql.DataSource;
  74. import org.springframework.beans.factory.annotation.Autowired;
  75. import org.springframework.beans.factory.annotation.Qualifier;
  76. import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
  77. import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
  78. import org.springframework.context.annotation.Bean;
  79. import org.springframework.context.annotation.Configuration;
  80. import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
  81. import org.springframework.orm.jpa.JpaTransactionManager;
  82. import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
  83. import org.springframework.transaction.PlatformTransactionManager;
  84. import org.springframework.transaction.annotation.EnableTransactionManagement;
  85. @Configuration
  86. @EnableTransactionManagement
  87. @EnableJpaRepositories(
  88. entityManagerFactoryRef = "entityManagerFactoryXtt",
  89. transactionManagerRef = "transactionManagerXtt",
  90. basePackages = {"com.opentrans.kit.jpa.xtt.repository" })
  91. public class OtmsXttDefine {
  92. @Autowired
  93. private JpaProperties jpaProperties;
  94. @Autowired
  95. @Qualifier("dataSourceXtt")
  96. private DataSource dataSourceXtt;
  97. @Bean(name = "entityManagerFactoryXtt")
  98. public LocalContainerEntityManagerFactoryBean entityManagerFactoryXtt(EntityManagerFactoryBuilder builder) {
  99. LocalContainerEntityManagerFactoryBean em = builder.dataSource(dataSourceXtt)
  100. .properties(getVendorProperties(dataSourceXtt))
  101. .packages("com.opentrans.kit.jpa.xtt.entity")
  102. .persistenceUnit("XttPersistenceUnit").build();
  103. return em;
  104. }
  105. @Bean(name = "transactionManagerXtt")
  106. PlatformTransactionManager transactionManagerXtt(EntityManagerFactoryBuilder builder) {
  107. return new JpaTransactionManager(entityManagerFactoryXtt(builder).getObject());
  108. }
  109. private Map<String, String> getVendorProperties(DataSource dataSource) {
  110. return jpaProperties.getHibernateProperties(dataSource);
  111. }
  112. }
  1. 四)pom.xml - maven
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <project xmlns="http://maven.apache.org/POM/4.0.0"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  6. <modelVersion>4.0.0</modelVersion>
  7. <groupId>com.otms.field</groupId>
  8. <artifactId>toolkit</artifactId>
  9. <version>1.0-SNAPSHOT</version>
  10. <name>toolkit</name>
  11. <description>Team field engineering toolkit</description>
  12. <parent>
  13. <groupId>org.springframework.boot</groupId>
  14. <artifactId>spring-boot-starter-parent</artifactId>
  15. <version>1.5.1.RELEASE</version>
  16. <relativePath/>
  17. </parent>
  18. <properties>
  19. <postgres.version>8.4-702.jdbc4</postgres.version>
  20. <fastjson.version>1.2.16</fastjson.version>
  21. </properties>
  22. <dependencies>
  23. <dependency>
  24. <groupId>org.springframework.boot</groupId>
  25. <artifactId>spring-boot-starter-actuator</artifactId>
  26. <exclusions>
  27. <exclusion>
  28. <groupId>org.springframework.boot</groupId>
  29. <artifactId>spring-boot-starter-logging</artifactId>
  30. </exclusion>
  31. </exclusions>
  32. </dependency>
  33. <dependency>
  34. <groupId>org.springframework.boot</groupId>
  35. <artifactId>spring-boot-starter-web</artifactId>
  36. </dependency>
  37. <dependency>
  38. <groupId>org.springframework.boot</groupId>
  39. <artifactId>spring-boot-starter-websocket</artifactId>
  40. </dependency>
  41. <dependency>
  42. <groupId>org.springframework.boot</groupId>
  43. <artifactId>spring-boot-starter-log4j2</artifactId>
  44. </dependency>
  45. <dependency>
  46. <groupId>org.springframework.boot</groupId>
  47. <artifactId>spring-boot-starter-amqp</artifactId>
  48. </dependency>
  49. <dependency>
  50. <groupId>org.springframework.boot</groupId>
  51. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  52. </dependency>
  53. <dependency>
  54. <groupId>org.springframework.boot</groupId>
  55. <artifactId>spring-boot-starter-security</artifactId>
  56. </dependency>
  57. <dependency>
  58. <groupId>org.springframework.boot</groupId>
  59. <artifactId>spring-boot-starter-data-jpa</artifactId>
  60. </dependency>
  61. <dependency>
  62. <groupId>org.springframework.boot</groupId>
  63. <artifactId>spring-boot-starter-test</artifactId>
  64. </dependency>
  65. <!-- <dependency>
  66. <groupId>org.springframework</groupId>
  67. <artifactId>spring-context-support</artifactId>
  68. </dependency>-->
  69. <dependency>
  70. <groupId>org.springframework.boot</groupId>
  71. <artifactId>spring-boot-starter-websocket</artifactId>
  72. </dependency>
  73. <dependency>
  74. <groupId>org.apache.httpcomponents</groupId>
  75. <artifactId>httpclient</artifactId>
  76. <version>4.5.3</version>
  77. </dependency>
  78. <dependency>
  79. <groupId>org.apache.httpcomponents</groupId>
  80. <artifactId>httpcore</artifactId>
  81. <version>4.4.6</version>
  82. </dependency>
  83. <dependency>
  84. <groupId>com.google.code.gson</groupId>
  85. <artifactId>gson</artifactId>
  86. <version>2.8.0</version>
  87. </dependency>
  88. <dependency>
  89. <groupId>joda-time</groupId>
  90. <artifactId>joda-time</artifactId>
  91. </dependency>
  92. <dependency>
  93. <groupId>postgresql</groupId>
  94. <artifactId>postgresql</artifactId>
  95. <version>${postgres.version}</version>
  96. </dependency>
  97. <dependency>
  98. <groupId>com.alibaba</groupId>
  99. <artifactId>fastjson</artifactId>
  100. <version>${fastjson.version}</version>
  101. </dependency>
  102. </dependencies>
  103. <build>
  104. <finalName>${project.artifactId}</finalName>
  105. <plugins>
  106. <plugin>
  107. <groupId>org.springframework.boot</groupId>
  108. <artifactId>spring-boot-maven-plugin</artifactId>
  109. <configuration>
  110. <fork>true</fork>
  111. </configuration>
  112. </plugin>
  113. </plugins>
  114. </build>
  115. </project>
springboot hikaricp 多数据源_Springboot框架_https://bianchenghao6.com/blog__第1张

今天的分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。

上一篇

已是最后文章

下一篇

已是最新文章

发表回复