├── pom.xml
└── src
└── main
├── java
└── com
│ └── jorden
│ ├── ApplicationStart.java
│ ├── aop
│ └── DynamicDataSourceAspect.java
│ ├── base
│ └── BaseDao.java
│ ├── config
│ └── DataSourceConfig.java
│ ├── core
│ └── DynamicDataSource.java
│ ├── dao
│ └── UserDao.java
│ ├── entity
│ └── User.java
│ ├── enums
│ └── DynDataSourceEnum.java
│ ├── service
│ ├── UserService.java
│ └── impl
│ │ └── UserServiceImpl.java
│ └── web
│ └── UserContorller.java
└── resources
├── application.properties
└── logback-spring.xml
/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 | springboot-read-write
5 | springboot-read-write
6 | 0.0.1-SNAPSHOT
7 |
8 |
9 |
10 | org.springframework.boot
11 | spring-boot-starter-parent
12 | 1.4.7.RELEASE
13 |
14 |
15 |
16 | org.springframework.boot
17 | spring-boot-starter-web
18 |
19 |
20 | org.springframework.boot
21 | spring-boot-starter-test
22 | test
23 |
24 |
25 | org.springframework.boot
26 | spring-boot-starter-jdbc
27 |
28 |
29 | org.springframework.boot
30 | spring-boot-starter-aop
31 |
32 |
33 | mysql
34 | mysql-connector-java
35 |
36 |
37 | com.alibaba
38 | druid
39 | 1.1.5
40 |
41 |
42 |
43 | org.scala-lang
44 | scala-library
45 | 2.11.0
46 |
47 |
48 | org.javassist
49 | javassist
50 |
51 |
52 | org.apache.hadoop
53 | hadoop-core
54 | 1.0.0
55 |
56 |
57 |
--------------------------------------------------------------------------------
/src/main/java/com/jorden/ApplicationStart.java:
--------------------------------------------------------------------------------
1 | package com.jorden;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 | import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
6 | /**
7 | * 项目启动主类
8 | *
9 | * @ClassName: ApplicationStart
10 | * @Description:TODO(这里用一句话描述这个类的作用)
11 | * @author: jorden.li
12 | * @date: 2018年4月2日 上午11:23:49
13 | *
14 | * @Copyright: 2018 www.tydic.com Inc. All rights reserved.
15 | * Success is never final. Failure is never fatal. Courage is what counts. -Sir Winston Churchill
16 | */
17 | @SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
18 | public class ApplicationStart {
19 |
20 | public static void main(String[] args) {
21 | SpringApplication.run(ApplicationStart.class, args);
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/src/main/java/com/jorden/aop/DynamicDataSourceAspect.java:
--------------------------------------------------------------------------------
1 | package com.jorden.aop;
2 |
3 | import java.lang.reflect.Method;
4 |
5 | import org.aspectj.lang.JoinPoint;
6 | import org.aspectj.lang.annotation.After;
7 | import org.aspectj.lang.annotation.Aspect;
8 | import org.aspectj.lang.annotation.Before;
9 | import org.aspectj.lang.annotation.Pointcut;
10 | import org.aspectj.lang.reflect.MethodSignature;
11 | import org.slf4j.Logger;
12 | import org.slf4j.LoggerFactory;
13 | import org.springframework.core.annotation.Order;
14 | import org.springframework.stereotype.Component;
15 |
16 | import com.jorden.core.DynamicDataSource;
17 | import com.jorden.enums.DynDataSourceEnum;
18 |
19 | /**
20 | *
21 | * @ClassName: DynamicDataSourceAspect
22 | * @Description:TODO(这里用一句话描述这个类的作用)
23 | * @author: jorden.li
24 | * @date: 2018年4月2日 下午1:21:36
25 | *
26 | * @Copyright: 2018 www.tydic.com Inc. All rights reserved. Success is never
27 | * final. Failure is never fatal. Courage is what counts. -Sir
28 | * Winston Churchill
29 | */
30 | @Order(-1) // 在 @Transactional 执行
31 | @Aspect
32 | @Component
33 | public class DynamicDataSourceAspect {
34 |
35 | public Logger logger=LoggerFactory.getLogger(DynamicDataSourceAspect.class);
36 |
37 | @Before(value = "execution(* com.jorden.service..*.*(..))")
38 | public void changeDataSource(JoinPoint point) throws Throwable {
39 |
40 | /* 获取当前的方法 */
41 | Class currentClass = point.getTarget().getClass();
42 | logger.info("访问当前的类: " + currentClass.getName());
43 | //slaveDataSource masterDataSource
44 | //定义的接口方法
45 | Method abstractMethod = ((MethodSignature) point.getSignature()).getMethod();
46 | String currentMethodName=abstractMethod.getName().toUpperCase();
47 | if(currentMethodName.contains(DynDataSourceEnum.SELECT.toString())||currentMethodName.contains(DynDataSourceEnum.FIND.toString())){
48 | logger.info("切换数据源,现在是读请求");
49 | DynamicDataSource.setDataSource("master");
50 |
51 | }else{
52 | logger.info("切换数据源,现在是写请求");
53 | DynamicDataSource.setDataSource("slave");
54 | }
55 | }
56 |
57 | @Pointcut("execution(* com.jorden.service..*.*(..))")
58 | public void pointCut() {
59 | }
60 |
61 | @After("pointCut()")
62 | public void after(JoinPoint point) {
63 | logger.info("after");
64 | // DynamicDataSource.clearDataSource();
65 | }
66 |
67 | }
68 |
--------------------------------------------------------------------------------
/src/main/java/com/jorden/base/BaseDao.java:
--------------------------------------------------------------------------------
1 | package com.jorden.base;
2 |
3 | import java.util.List;
4 | /**
5 | *
6 | * @ClassName: BaseDao
7 | * @Description:TODO(这里用一句话描述这个类的作用)
8 | * @author: jorden.li
9 | * @date: 2018年4月2日 上午11:25:41
10 | *
11 | * @param
12 | * @Copyright: 2018 www.tydic.com Inc. All rights reserved.
13 | * Success is never final. Failure is never fatal. Courage is what counts. -Sir Winston Churchill
14 | */
15 | public interface BaseDao {
16 |
17 |
18 | /**
19 | *
20 | * @Title: selectAll
21 | * @Description: TODO(这里用一句话描述这个方法的作用)
22 | * @param: @return
23 | * @return: List
24 | * @throws
25 | */
26 | List selectAll();
27 |
28 | /**
29 | *
30 | * @Title: selectOne
31 | * @Description: TODO(这里用一句话描述这个方法的作用)
32 | * @param: @param t
33 | * @param: @return
34 | * @return: T
35 | * @throws
36 | */
37 | T selectByPirmayKey(T t);
38 | /**
39 | *
40 | * @Title: deleteByPrimayKey
41 | * @Description: TODO(这里用一句话描述这个方法的作用)
42 | * @param: @param t
43 | * @return: void
44 | * @throws
45 | */
46 | void deleteByPrimayKey(T t );
47 |
48 |
49 |
50 | }
51 |
--------------------------------------------------------------------------------
/src/main/java/com/jorden/config/DataSourceConfig.java:
--------------------------------------------------------------------------------
1 | package com.jorden.config;
2 |
3 | import java.util.HashMap;
4 | import java.util.Map;
5 |
6 | import javax.sql.DataSource;
7 |
8 | import org.springframework.beans.factory.annotation.Value;
9 | import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
10 | import org.springframework.boot.context.properties.ConfigurationProperties;
11 | import org.springframework.context.annotation.Bean;
12 | import org.springframework.context.annotation.Configuration;
13 | import org.springframework.jdbc.core.JdbcTemplate;
14 |
15 | import com.jorden.core.DynamicDataSource;
16 |
17 | /**
18 | * DataSourceConfig 配置
19 | * @ClassName: DataSourceConfig
20 | * @Description:TODO(这里用一句话描述这个类的作用)
21 | * @author: jorden.li
22 | * @date: 2018年4月2日 上午10:56:36
23 | *
24 | * @Copyright: 2018 www.tydic.com Inc. All rights reserved.
25 | * Success is never final. Failure is never fatal. Courage is what counts. -Sir Winston Churchill
26 | */
27 | @Configuration
28 | public class DataSourceConfig {
29 |
30 | @Value("${spring.datasource.type}")
31 | private Class < ? extends DataSource> dataSourceType;
32 | /**
33 | * master 配置
34 | * @Title: masterDataSource
35 | * @Description: TODO(这里用一句话描述这个方法的作用)
36 | * @param: @return
37 | * @return: DataSource
38 | * @throws
39 | */
40 | @Bean(name="masterDataSource",destroyMethod="close",initMethod="init")
41 | @ConfigurationProperties(prefix="spring.datasource.master")
42 | public DataSource masterDataSource(){
43 | return DataSourceBuilder.create().type(dataSourceType).build();
44 | }
45 | /**
46 | * slave 配置
47 | * @Title: slaveDataSource
48 | * @Description: TODO(这里用一句话描述这个方法的作用)
49 | * @param: @return
50 | * @return: DataSource
51 | * @throws
52 | */
53 | @Bean(name="slaveDataSource",destroyMethod="close",initMethod="init")
54 | @ConfigurationProperties(prefix="spring.datasource.slave")
55 | public DataSource slaveDataSource(){
56 | return DataSourceBuilder.create().type(dataSourceType).build();
57 |
58 | }
59 | @Bean(name="dataSource")
60 | public DataSource dataSource(){
61 | DynamicDataSource dataSource=new DynamicDataSource();
62 | // 配置多数据源
63 | Map