SpringBoot配置多数据源,是很常见的一个场景,为了便于测试,引入JPA框架查询数据库
pom.xml文件
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.0.RELEASE
com.example
dadasource
0.0.1-SNAPSHOT
dadasource
Demo project for Spring Boot
17
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
io.springfox
springfox-swagger-ui
2.7.0
io.springfox
springfox-swagger2
2.7.0
org.springframework.boot
spring-boot-starter-data-jpa
org.projectlombok
lombok
com.alibaba
druid-spring-boot-starter
1.1.13
mysql
mysql-connector-java
5.1.44
org.springframework.boot
spring-boot-maven-plugin
org.apache.maven.plugins
maven-compiler-plugin
3.6.0
1.8
server:
servlet:
context-path: /api
port: 8380
spring:
profiles:
active: dev
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
druid:
driver-class-name: com.mysql.jdbc.Driver
connectionInitSqls: set names utf8mb4
# url: jdbc:mysql://10.0.173.220:3307/dtbk_rzt?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
db1:
url: jdbc:mysql://xxxx/dtbk_rzt?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
username: xxxx
password: xxxx
db2:
url: jdbc:mysql://xxxx:3307/dtbk_rzt_admin?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
username: xxxx
password: xxxx
initial-size: 10
max-active: 100
min-idle: 10
max-wait: 60000
pool-prepared-statements: true
max-pool-prepared-statement-per-connection-size: 20
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 300000
#Oracle需要打开注释
validation-query: SELECT 1 FROM DUAL
test-while-idle: true
test-on-borrow: false
test-on-return: false
stat-view-servlet:
enabled: true
url-pattern: /druid/*
#login-username: admin
#login-password: admin
filter:
stat:
enabled: true
log-slow-sql: true
slow-sql-millis: 1000
merge-sql: false
wall:
config:
multi-statement-allow: true
jpa:
show-sql: true
hibernate:
ddl-auto: update
database: mysql
package com.example.dadasource.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
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.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import java.util.Map;
/**
* @Author [email protected]
* @Description
* @Date 15:08 2022/8/31
* @param null
* @Return
**/
@Configuration
@EnableJpaRepositories(entityManagerFactoryRef = "db1EntityManagerFactory", // 实体类工厂依赖
transactionManagerRef = "db1TransactionManager", // 事务依赖
basePackages = "com.example.dadasource.repository.db1") // repository类所在的包
@EnableTransactionManagement
@Slf4j
public class DB1Config {
@Autowired
private JpaProperties jpaProperties;
@Autowired
@Qualifier("analysisDataSource")
private DataSource dataSource;
@Autowired
private HibernateProperties hibernateProperties;
/*
* 通过LocalContainerEntityManagerFactoryBean来获取EntityManagerFactory实例
*/
@Bean(name = "db1EntityManagerFactoryBean")
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(
EntityManagerFactoryBuilder builder) {
Map properties = hibernateProperties.determineHibernateProperties(
jpaProperties.getProperties(), new HibernateSettings());
return builder.dataSource(dataSource).properties(properties)
.packages("com.example.dadasource.entity").build();
}
/*
* EntityManagerFactory类似于Hibernate的SessionFactory,mybatis的SqlSessionFactory
* 总之,在执行操作之前,我们总要获取一个EntityManager,这就类似于Hibernate的Session,
* mybatis的sqlSession.
*/
@Bean(name = "db1EntityManagerFactory")
@Primary
public EntityManagerFactory entityManagerFactory(EntityManagerFactoryBuilder builder) {
log.info("创建db1EntityManagerFactory");
return this.entityManagerFactoryBean(builder).getObject();
}
/*
* 配置事务管理器
*/
@Bean(name = "db1TransactionManager")
@Primary
public PlatformTransactionManager transactionManager(EntityManagerFactoryBuilder builder) {
log.info("创建db1TransactionManager");
return new JpaTransactionManager(this.entityManagerFactory(builder));
}
}
package com.example.dadasource.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
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.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import java.util.Map;
/**
* @Author [email protected]
* @Description
* @Date 15:08 2022/8/31
* @param null
* @Return
**/
@Configuration
@EnableJpaRepositories(entityManagerFactoryRef = "db2EntityManagerFactory",
transactionManagerRef = "db2TransactionManager",
basePackages = "com.example.dadasource.repository.db2")
@EnableTransactionManagement
@Slf4j
public class DB2Config {
@Autowired
private JpaProperties jpaProperties;
@Autowired
@Qualifier("userDataSource")
private DataSource dataSource;
@Autowired
private HibernateProperties hibernateProperties;
@Bean(name = "userEntityManagerFactoryBean")
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(
EntityManagerFactoryBuilder builder) {
Map properties = hibernateProperties.determineHibernateProperties(
jpaProperties.getProperties(), new HibernateSettings());
return builder.dataSource(dataSource).properties(properties)
.packages("com.example.dadasource.entity").build();
}
@Bean(name = "db2EntityManagerFactory")
public EntityManagerFactory entityManagerFactory(EntityManagerFactoryBuilder builder) {
log.info("创建db2EntityManagerFactory");
return this.entityManagerFactoryBean(builder).getObject();
}
@Bean(name = "db2TransactionManager")
@Primary
public PlatformTransactionManager transactionManager(EntityManagerFactoryBuilder builder) {
log.info("创建db2TransactionManager");
return new JpaTransactionManager(this.entityManagerFactory(builder));
}
}
package com.example.dadasource.config;
import com.alibaba.druid.pool.DruidDataSource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.List;
/**
* @Author [email protected]
* @Description
* @Date 11:44 2022/8/31
* @param
* @Return
**/
@Configuration
@Slf4j
public class DruidDataSourceConfig {
@Value("${spring.datasource.druid.driver-class-name}")
private String driverClassName;
@Value("${spring.datasource.druid.initial-size}")
private int initialSize;
@Value("${spring.datasource.druid.min-idle}")
private int minIdle;
@Value("${spring.datasource.druid.max-active}")
private int maxActive;
@Value("${spring.datasource.druid.max-wait}")
private int maxWait;
@Value("${spring.datasource.druid.connectionInitSqls}")
private String connectionInitSqls;
//
// @Value("${spring.datasource.druid.timeBetweenEvictionRunsMillis}")
// private int timeBetweenEvictionRunsMillis;
//
// @Value("${spring.datasource.druid.minEvictableIdleTimeMillis}")
// private int minEvictableIdleTimeMillis;
//
// @Value("${spring.datasource.druid.validationQuery}")
// private String validationQuery;
//
// @Value("${spring.datasource.druid.testWhileIdle}")
// private boolean testWhileIdle;
//
// @Value("${spring.datasource.druid.testOnBorrow}")
// private boolean testOnBorrow;
//
// @Value("${spring.datasource.druid.testOnReturn}")
// private boolean testOnReturn;
//
// @Value("${spring.datasource.druid.poolPreparedStatements}")
// private boolean poolPreparedStatements;
//
// @Value("${spring.datasource.druid.maxPoolPreparedStatementPerConnectionSize}")
// private int maxPoolPreparedStatementPerConnectionSize;
//
// @Value("${spring.datasource.druid.filters}")
// private String filters;
//
// @Value("{spring.datasource.druid.connectionProperties}")
// private String connectionProperties;
/* #####################基础公共配置##################### */
/* #####################analysisdb配置##################### */
@Value("${spring.datasource.druid.db1.url}")
private String analysisUrl;
@Value("${spring.datasource.druid.db1.username}")
private String analysisUsername;
@Value("${spring.datasource.druid.db1.password}")
private String analysisPassword;
@Bean(name = "analysisDataSource")
@Primary // 确定主数据源
public DataSource analysisDataSource() {
log.info("创建analysisDataSource数据源");
return createDataSource(analysisUrl, analysisUsername, analysisPassword);
}
/* #####################userdb配置##################### */
@Value("${spring.datasource.druid.db2.url}")
private String userUrl;
@Value("${spring.datasource.druid.db2.username}")
private String userUsername;
@Value("${spring.datasource.druid.db2.password}")
private String userPassword;
@Bean(name = "userDataSource")
public DataSource userDataSource() {
log.info("创建userDataSource数据源");
return createDataSource(userUrl, userUsername, userPassword);
}
/* #####################DataSource配置##################### */
private DataSource createDataSource(String url, String username, String password) {
DruidDataSource datasource = new DruidDataSource();
datasource.setUrl(url);
datasource.setUsername(username);
datasource.setPassword(password);
datasource.setDriverClassName(driverClassName);
// configuration
datasource.setInitialSize(initialSize);
datasource.setMinIdle(minIdle);
datasource.setMaxActive(maxActive);
datasource.setMaxWait(maxWait);
// datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
// datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
// datasource.setValidationQuery(validationQuery);
// datasource.setTestWhileIdle(testWhileIdle);
// datasource.setTestOnBorrow(testOnBorrow);
// datasource.setTestOnReturn(testOnReturn);
// datasource.setPoolPreparedStatements(poolPreparedStatements);
// datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
// try {
// datasource.setFilters(filters);
// } catch (SQLException e) {
// e.printStackTrace();
// }
// datasource.setConnectionProperties(connectionProperties);
List connectInitSqls = new ArrayList(){{
add(connectionInitSqls);
}};
datasource.setConnectionInitSqls(connectInitSqls);
return datasource;
}
}
package com.example.dadasource.repository.db1;
import com.example.dadasource.entity.LocationEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* @Author [email protected]
* @Description
* @Date 11:32 2022/8/31
* @param
* @Return
**/
@Repository
public interface LocationRepository extends JpaRepository {
@Modifying
@Query(value = "SELECT t1.* FROM t_location t1 LEFT JOIN location_authorize t2 ON t1.id=t2.t_location_id WHERE t1.parent_id is null and (t1.create_user = ?1 or t2.user_id=?1) and (?2 is null or ?2='' or t1.`status`=?2) ORDER BY t1.update_date desc LIMIT ?3,?4" ,nativeQuery = true)
List findAllByAuthorize(String userId,Integer status,Integer pageNo, Integer pageSize);
//当前用户不包含分页参数,计算总条数据
@Modifying
@Query(value = "SELECT t1.* FROM t_location t1 LEFT JOIN location_authorize t2 ON t1.id=t2.t_location_id WHERE t1.parent_id is null and (?1 is null or ?1='' or t1.create_user = ?1 or t2.user_id=?1) and (?2 is null or ?2='' or t1.`status`=?2) ORDER BY t1.update_date desc" ,nativeQuery = true)
List findAllByAuthorizeAll(String userId,Integer status);
}
package com.example.dadasource.repository.db2;
import com.example.dadasource.entity.SysUser;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* @Author [email protected]
* @Description
* @Date 11:32 2022/8/31
* @param
* @Return
**/
@Repository
public interface SysUserRepository extends JpaRepository {
@Modifying
@Query(value = "select * from sys_user",nativeQuery=true)
List findAllUser();
@Modifying
@Query(value = "select t from sys_user t where t.userId=?1")
List findByID(Integer userId);
}
package com.example.dadasource.service.impl;
import com.example.dadasource.DadasourceApplication;
import com.example.dadasource.entity.LocationEntity;
import com.example.dadasource.entity.ResponseResult;
import com.example.dadasource.repository.db1.LocationRepository;
import com.example.dadasource.service.LocationService;
import junit.framework.TestCase;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Map;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = DadasourceApplication.class)
public class LocationServiceImplTest extends TestCase {
@Autowired
private LocationService locationService;
@Autowired
private LocationRepository locationRepository;
@Test
public void testFindAllByAuthorize(){
ResponseResult
package com.example.dadasource.service.impl;
import com.example.dadasource.DadasourceApplication;
import com.example.dadasource.entity.ResponseResult;
import com.example.dadasource.repository.db2.SysUserRepository;
import com.example.dadasource.service.SysUserService;
import junit.framework.TestCase;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = DadasourceApplication.class)
@ComponentScan("crc.bi.dtbk")
public class SysUserServiceImplTest extends TestCase {
@Autowired
private SysUserService sysUserService;
@Autowired
private SysUserRepository repository;
@Test
public void testSysUserList() {
ResponseResult result = sysUserService.sysUserList();
System.out.println("result:"+result);
}
@Test
public void testGetSysUserByID(){
ResponseResult responseResult = sysUserService.getSysUserByID(6);
System.out.println("responseResult:"+responseResult);
}
}