Skip to content

Commit

Permalink
add springboot-dynamicDataSource
Browse files Browse the repository at this point in the history
  • Loading branch information
527515025 committed Mar 27, 2019
1 parent 13be5bc commit bbe8e37
Show file tree
Hide file tree
Showing 20 changed files with 893 additions and 0 deletions.
57 changes: 57 additions & 0 deletions springboot-dynamicDataSource/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>cn.abel</groupId>
<artifactId>springboot-dynamicDataSource</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package cn.abel;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
* @author yyb
* @time 2019/3/26
*/
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

}
50 changes: 50 additions & 0 deletions springboot-dynamicDataSource/src/main/java/cn/abel/bean/News.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package cn.abel.bean;


public class News {
private Integer id;
private String title;
private String content;
private String imagePath;
private Integer readSum;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}

public String getImagePath() {
return imagePath;
}

public void setImagePath(String imagePath) {
this.imagePath = imagePath;
}

public Integer getReadSum() {
return readSum;
}

public void setReadSum(Integer readSum) {
this.readSum = readSum;
}
}
70 changes: 70 additions & 0 deletions springboot-dynamicDataSource/src/main/java/cn/abel/bean/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package cn.abel.bean;

import java.util.Date;


public class User {
private Integer id;
private String name;
private String address;
private String mobile;
private String email;
private Date createTime;
private Integer role;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public String getMobile() {
return mobile;
}

public void setMobile(String mobile) {
this.mobile = mobile;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public Date getCreateTime() {
return createTime;
}

public void setCreateTime(Date createTime) {
this.createTime = createTime;
}

public Integer getRole() {
return role;
}

public void setRole(Integer role) {
this.role = role;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package cn.abel.config;

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

/**
* @author yyb
* @time 2019/3/27
*/
public class DynamicDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
return DynamicDataSourceContextHolder.getDatabaseType();
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package cn.abel.config;

import cn.abel.enums.DatabaseTypeEnum;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
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 org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
* @author yyb
* @time 2019/3/27
*/
@Configuration
@MapperScan(basePackages = "cn.abel.dao")
public class DynamicDataSourceConfig {
@Value("${spring.datasource.primary.url}")
private String primaryUrl;
@Value("${spring.datasource.user.url}")
private String userUrl;
@Value("${mybatis.mapper-locations}")
private String resources;
//当两个数据库连接账号密码不一样时
// @Value("${spring.datasource.user.username}")
// private String userName;
// @Value("${spring.datasource.user.password}")
// private String password;

@Autowired
private HikariConfig hikariConfig;

@Primary
@Bean(name = "primaryDataSource")
public DataSource getPrimaryDataSource() {
return hikariConfig.getHikariDataSource(primaryUrl);
}

@Bean(name = "userDataSource")
public DataSource getUserDataSource() {
return hikariConfig.getHikariDataSource(userUrl);
}


//当两个数据库连接账号密码不一样时使用
// @Bean(name = "userDataSource")
// public DataSource getUserDataSource() {
// return hikariConfig.getHikariDataSource(userUrl, userName, password);
// }


@Bean("dynamicDataSource")
public DynamicDataSource dynamicDataSource(@Qualifier("primaryDataSource") DataSource primaryDataSource,
@Qualifier("userDataSource") DataSource miaoMoreDataSource) {
Map<Object, Object> targetDataSources = new HashMap<>();
targetDataSources.put(DatabaseTypeEnum.PRIMARY, primaryDataSource);
targetDataSources.put(DatabaseTypeEnum.USER, miaoMoreDataSource);

DynamicDataSource dataSource = new DynamicDataSource();
dataSource.setTargetDataSources(targetDataSources);// 该方法是AbstractRoutingDataSource的方法
dataSource.setDefaultTargetDataSource(primaryDataSource);// 默认的datasource设置为myTestDbDataSource
return dataSource;
}

/**
* 根据数据源创建SqlSessionFactory
*/
@Bean
public SqlSessionFactory sqlSessionFactory(@Qualifier("dynamicDataSource") DynamicDataSource dynamicDataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dynamicDataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(resources));
return bean.getObject();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package cn.abel.config;

import cn.abel.enums.DatabaseTypeEnum;

/**
* @author yyb
* @time 2019/3/27
*/
public class DynamicDataSourceContextHolder {
private static final ThreadLocal<DatabaseTypeEnum> contextHolder = new ThreadLocal<>();

public static void setDatabaseType(DatabaseTypeEnum type){
contextHolder.set(type);
}

public static DatabaseTypeEnum getDatabaseType(){
return contextHolder.get();
}

public static void resetDatabaseType() {
contextHolder.set(DatabaseTypeEnum.PRIMARY);
}
}
Loading

0 comments on commit bbe8e37

Please sign in to comment.