SpringBoot整合Mybatis多数据源 (AOP+注解)
Posted Architect剑
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot整合Mybatis多数据源 (AOP+注解)相关的知识,希望对你有一定的参考价值。
SpringBoot整合Mybatis多数据源 (AOP+注解)
1、pom.xml文件(开发用的JDK 10)
<?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>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>demo</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>10</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- alibaba的druid数据库连接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.10</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.3</version> </dependency> <!-- 热部署 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</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-configuration-processor</artifactId> <optional>true</optional> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2、多数据源配置类
@Configuration public class DataSourceConfig { @Bean(name = "readTestDb") @ConfigurationProperties(prefix = "spring.datasource.read-test-db") public DataSource readTestDb() { return DataSourceBuilder.create().build(); } @Bean(name = "writeTestDb") @ConfigurationProperties(prefix = "spring.datasource.write-test-db") public DataSource writeTestDb() { return DataSourceBuilder.create().build(); } /** * 动态数据源: 通过AOP在不同数据源之间动态切换 * @return */ @Primary @Bean(name = "dynamicDataSource") public DataSource dynamicDataSource() { DynamicDataSource dynamicDataSource = new DynamicDataSource(); // 默认数据源 dynamicDataSource.setDefaultTargetDataSource(readTestDb()); // 配置多数据源 Map<Object, Object> dsMap = new HashMap<Object, Object>(); dsMap.put("readTestDb", readTestDb()); dsMap.put("writeTestDb", writeTestDb()); dynamicDataSource.setTargetDataSources(dsMap); return dynamicDataSource; } /** * 配置@Transactional注解事物 * @return */ @Bean public PlatformTransactionManager transactionManager() { return new DataSourceTransactionManager(dynamicDataSource()); }
3、数据源切换读取类
public class DataSourceContextHolder { /** * 默认数据源 */ public static final String DEFAULT_DS = "readTestDb"; private static final ThreadLocal<String> contextHolder = new ThreadLocal<>(); // 设置数据源名 public static void setDB(String dbType) { System.out.println("切换到{"+dbType+"}数据源"); contextHolder.set(dbType); } // 获取数据源名 public static String getDB() { return (contextHolder.get()); } // 清除数据源名 public static void clearDB() { contextHolder.remove(); } }
4、注解类(注意:要和配置类放在同一个包下,否则会抛出找不到注解异常)
@Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD}) @Documented public @interface DS { String value() default "readTestDb"; }
5、动态数据源(设置数据源自动调用该类方法)
public class DynamicDataSource extends AbstractRoutingDataSource { @Override protected Object determineCurrentLookupKey() { System.out.println("数据源为"+DataSourceContextHolder.getDB()); return DataSourceContextHolder.getDB(); } }
6、AOP
@Aspect @Component public class DynamicDataSourceAspect { @Before("@annotation(DS)") @SuppressWarnings("rawtypes") public void beforeSwitchDS(JoinPoint point){ //获得当前访问的class Class<?> className = point.getTarget().getClass(); //获得访问的方法名 String methodName = point.getSignature().getName(); //得到方法的参数的类型 Class[] argClass = ((MethodSignature)point.getSignature()).getParameterTypes(); String dataSource = DataSourceContextHolder.DEFAULT_DS; try { // 得到访问的方法对象 Method method = className.getMethod(methodName, argClass); // 判断是否存在@DS注解 if (method.isAnnotationPresent(DS.class)) { DS annotation = method.getAnnotation(DS.class); // 取出注解中的数据源名 dataSource = annotation.value(); } } catch (Exception e) { e.printStackTrace(); } // 切换数据源 DataSourceContextHolder.setDB(dataSource); } @After("@annotation(DS)") public void afterSwitchDS(JoinPoint point){ DataSourceContextHolder.clearDB(); } }
7、application.yml 配置文件 (和application.properties区别 可以百度一下 很详细)
spring: datasource: read-test-db: jdbc-url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8 username: root password: root # 使用druid数据源 type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver write-test-db: jdbc-url: jdbc:mysql://127.0.0.1:3306/writetest?useUnicode=true&characterEncoding=utf-8 username: root password: root # 使用druid数据源 type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver jpa: show-sql: true mybatis: #mapper-locations: classpath:mapping/mapper/*.xml type-aliases-package: com.example.demo.model
8、启动类( 注解:@MapperScan 扫描mapper接口类路径 、多数据源 @SpringBootApplication这个注解是必须写的 )
@MapperScan("com.example.demo.mapper") @SpringBootApplication(exclude = { DataSourceAutoConfiguration.class }) public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
9、Controller( 注解:@RestController( 包含@controller、@responseBody等其他注解 所以用该注解就不用每个方法上@responseBody ) )
@RestController public class DemoController { @Autowired private UserService userService; @RequestMapping("/getRequestDBObj") public User getRequestDBObj(HttpServletRequest request,HttpServletResponse response) { return userService.selectByPrimaryKey(1); } @RequestMapping("/getRequestDbList") public User getRequestDbList(HttpServletRequest request,HttpServletResponse response) { return userService.selectAllUser(1); } }
10、Service
public interface UserService { public User selectByPrimaryKey(Integer userId); public User selectAllUser(Integer userId); }
11、ServiceImpl
@Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override @DS("writeTestDb") public User selectByPrimaryKey(Integer userId) { return userMapper.selectByPrimaryKey(userId); } @Override @DS("readTestDb") public User selectAllUser(Integer userId) { return userMapper.selectByPrimaryKey(userId); } }
12、Mapper
public interface UserMapper { @Select("SELECT user_id as userId, user_name as userName, password, phone FROM t_user WHERE user_id = #{userId}") User selectByPrimaryKey(Integer userId); }
13、本人 QQ 1036943655 本人萌新 有代码不对的地方 大家指教一下 也可以加好友 大家一起互相学习、互相进步
以上是关于SpringBoot整合Mybatis多数据源 (AOP+注解)的主要内容,如果未能解决你的问题,请参考以下文章
SpringBoot-技术专区-Mybatis plus 实现多数据源整合
SpringBoot:4.SpringBoot整合Mybatis实现数据库访问