在上一章中,我们已经搭建好项目,现在来讲一下如何配置数据源。

由于在有的项目中,用的数据源可能会涉及多个,且是不同类型的,我们接下来就讲解多数据源的配置。

 

情景描述:

现有项目需要访问不同的数据库,有Mysql、Oracle、Sql server数据库

就以该情景为例,讲讲该如何配置。

 

步骤:

1、在resources文件夹下新建persistence-multiple-db.properties资源文件,如下图:

(备注:具体的值自己配置)

%title插图%num

 1 # Mysql数据库
 2 app.datasource.mysql.url=jdbc:mysql://192.168.1.100/XXX数据库名称
 3 app.datasource.mysql.username=username
 4 app.datasource.mysql.password=password
 5 app.datasource.mysql.driver-class-name=com.mysql.jdbc.Driver
 6 app.datasource.mysql.maximum-pool-size=10
 7 
 8 # Oracle数据库
 9 app.datasource.oracle.url=jdbc:oracle:thin:@192.168.1.100:1521/XXX数据库名称
10 app.datasource.oracle.username=username
11 app.datasource.oracle.password=password
12 app.datasource.oracle.driver-class-name=oracle.jdbc.driver.OracleDriver
13 app.datasource.oracle.maximum-pool-size=30
14 
15 # Sql server数据库
16 app.datasource.sqlserver.url=jdbc:sqlserver://192.168.1.100;databaseName=XXX数据库名称
17 app.datasource.sqlserver.username=username
18 app.datasource.sqlserver.password=password
19 app.datasource.sqlserver.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
20 app.datasource.sqlserver.maximum-pool-size=30

 

2、在config文件夹中新建MysqlDatabaseConfig.java、OracleDatabaseConfig.java、SqlserverDatabaseConfig.java

在domain文件夹中新建mysql、oracle、sqlserver文件夹

在repository文件夹中新建mysql、oracle、sqlserver文件夹

(备注:命名可以自定义,我这里是为了便于区分)

%title插图%num

接下来就依次配置这三个文件,以MysqlDatabaseConfig.java为例说明一下,另外两个同理,但是要注意@Primary(优先考虑,优先考虑被注解的对象注入)注解,另外两个不要添加该注解

 1 package com.hyl.springdemo.config;
 2 
 3 import com.ulisesbocchio.jasyptspringboot.annotation.EnableEncryptableProperties;
 4 import com.zaxxer.hikari.HikariDataSource;
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.boot.jdbc.DataSourceBuilder;
 7 import org.springframework.context.annotation.Bean;
 8 import org.springframework.context.annotation.Configuration;
 9 import org.springframework.context.annotation.Primary;
10 import org.springframework.context.annotation.PropertySource;
11 import org.springframework.core.env.Environment;
12 import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
13 import org.springframework.orm.jpa.JpaTransactionManager;
14 import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
15 import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
16 import org.springframework.transaction.PlatformTransactionManager;
17 
18 import javax.sql.DataSource;
19 import java.util.HashMap;
20 
21 @Configuration
22 @EnableEncryptableProperties
23 @PropertySource({ "classpath:persistence-multiple-db.properties" })
24 @EnableJpaRepositories(
25         basePackages = "com.hyl.springdemo.repository.mysql",
26         entityManagerFactoryRef = "mysqlEntityManager",
27         transactionManagerRef = "mysqlTransactionManager"
28 )
29 public class MysqlDatabaseConfig {
30 
31     @Autowired
32     private Environment env;
33 
34     @Bean
35     @Primary
36     public DataSource mysqlDataSource() {
37 
38         HikariDataSource dataSource = DataSourceBuilder.create().type(HikariDataSource.class).build();
39         dataSource.setDriverClassName(env.getProperty("app.datasource.mysql.driver-class-name"));
40         dataSource.setJdbcUrl(env.getProperty("app.datasource.mysql.url"));
41         dataSource.setUsername(env.getProperty("app.datasource.mysql.username"));
42         dataSource.setPassword(env.getProperty("app.datasource.mysql.password"));
43         dataSource.setMaximumPoolSize(Integer.parseInt(env.getProperty("app.datasource.mysql.maximum-pool-size")));
44 
45         return dataSource;
46     }
47 
48     @Bean
49     @Primary
50     public LocalContainerEntityManagerFactoryBean mysqlEntityManager() {
51         LocalContainerEntityManagerFactoryBean em
52                 = new LocalContainerEntityManagerFactoryBean();
53         em.setDataSource(mysqlDataSource());
54         em.setPackagesToScan(
55                 new String[] { "com.hyl.springdemo.domain.mysql" });
56 
57         HibernateJpaVendorAdapter vendorAdapter
58                 = new HibernateJpaVendorAdapter();
59         em.setJpaVendorAdapter(vendorAdapter);
60         em.setPersistenceUnitName("MysqlPersistenceUnit");
61         HashMap<String, Object> properties = new HashMap<>();
62         properties.put("hibernate.hbm2ddl.auto",
63                 env.getProperty("hibernate.hbm2ddl.auto"));
64         properties.put("hibernate.dialect",
65                 "org.hibernate.dialect.MySQL5Dialect");
66         em.setJpaPropertyMap(properties);
67 
68         return em;
69     }
70 
71     @Bean
72     @Primary
73     public PlatformTransactionManager mysqlTransactionManager() {
74 
75         JpaTransactionManager transactionManager
76                 = new JpaTransactionManager();
77         transactionManager.setEntityManagerFactory(
78                 mysqlEntityManager().getObject());
79         return transactionManager;
80     }
81 }

 

3、新建Repository,访问数据库(关于该部分,在后续的文章中再进行介绍)

%title插图%num

 

4、新建Service层,访问Repository

%title插图%num

 

5、在Controller层中进行调用

%title插图%num

 

6、运行项目,在网页中查看

%title插图%num

此篇博文主要是为了介绍配置多数据源,涉及到的其它内容在后续的博文中会逐一介绍