• SpringBoot Mybatis 多数据源 MySQL+Oracle+Redis


     一、背景

    在SpringBoot Mybatis 项目中,需要连接 多个数据源,连接多个数据库,需要连接一个MySQL数据库和一个Oracle数据库和一个Redis

    二、依赖 pom.xml

    1. <dependencies>
    2. <dependency>
    3. <groupId>org.springframework.bootgroupId>
    4. <artifactId>spring-boot-starter-webartifactId>
    5. dependency>
    6. <dependency>
    7. <groupId>org.springframework.bootgroupId>
    8. <artifactId>spring-boot-starter-testartifactId>
    9. <scope>testscope>
    10. dependency>
    11. <dependency>
    12. <groupId>mysqlgroupId>
    13. <artifactId>mysql-connector-javaartifactId>
    14. <version>8.0.26version>
    15. dependency>
    16. <dependency>
    17. <groupId>org.springframework.bootgroupId>
    18. <artifactId>spring-boot-starter-jdbcartifactId>
    19. dependency>
    20. <dependency>
    21. <groupId>org.mybatis.spring.bootgroupId>
    22. <artifactId>mybatis-spring-boot-starterartifactId>
    23. <version>1.3.2version>
    24. dependency>
    25. <dependency>
    26. <groupId>com.oracle.database.jdbcgroupId>
    27. <artifactId>ojdbc8artifactId>
    28. <version>19.8.0.0version>
    29. dependency>
    30. <dependency>
    31. <groupId>org.springframework.bootgroupId>
    32. <artifactId>spring-boot-starter-data-redisartifactId>
    33. <version>2.4.4version>
    34. dependency>
    35. <dependency>
    36. <groupId>org.projectlombokgroupId>
    37. <artifactId>lombokartifactId>
    38. <version>1.18.16version>
    39. <scope>providedscope>
    40. dependency>
    41. <dependency>
    42. <groupId>javax.persistencegroupId>
    43. <artifactId>javax.persistence-apiartifactId>
    44. <version>2.2version>
    45. dependency>
    46. <dependency>
    47. <groupId>io.springfoxgroupId>
    48. <artifactId>springfox-swagger2artifactId>
    49. <version>2.9.2version>
    50. dependency>
    51. <dependency>
    52. <groupId>io.springfoxgroupId>
    53. <artifactId>springfox-swagger-uiartifactId>
    54. <version>2.9.2version>
    55. dependency>
    56. <dependency>
    57. <groupId>cn.easyprojectgroupId>
    58. <artifactId>orai18nartifactId>
    59. <version>12.1.0.2.0version>
    60. dependency>
    61. dependencies>

    三、项目结构

    四、application.yml

    spring.datasource.url数据库的JDBC URL

    spring.datasource.jdbc-url用来重写自定义连接池

    Hikari没有url属性,但是有jdbcUrl属性,在这中情况下必须使用jdbc_url

    1. server:
    2. port: 8080
    3. spring:
    4. datasource:
    5. primary:
    6. jdbc-url: jdbc:mysql://localhost:3306/database_name
    7. username: root
    8. password: 123456
    9. driver-class-name: com.mysql.cj.jdbc.Driver
    10. secondary:
    11. jdbc-url: jdbc:oracle:thin:@localhost:1521/ORCL
    12. username: root
    13. password: 123456
    14. driver-class-name: oracle.jdbc.driver.OracleDriver

    五、MySQL配置类

    MysqlDataSourceConfig

    使用注解@Primary配置默认数据源

    1. package com.example.multipledata.config.mysqlconfig;
    2. import org.apache.ibatis.session.SqlSessionFactory;
    3. import org.mybatis.spring.SqlSessionFactoryBean;
    4. import org.mybatis.spring.annotation.MapperScan;
    5. import org.springframework.beans.factory.annotation.Qualifier;
    6. import org.springframework.boot.context.properties.ConfigurationProperties;
    7. import org.springframework.boot.jdbc.DataSourceBuilder;
    8. import org.springframework.context.annotation.Bean;
    9. import org.springframework.context.annotation.Configuration;
    10. import org.springframework.context.annotation.Primary;
    11. import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
    12. import org.springframework.jdbc.datasource.DataSourceTransactionManager;
    13. import javax.sql.DataSource;
    14. @Configuration
    15. @MapperScan(basePackages = MysqlDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "mysqlSqlSessionFactory")
    16. public class MysqlDataSourceConfig {
    17. static final String PACKAGE = "com.example.multipledata.mapper.mysqlmapper";
    18. static final String MAPPER_LOCATION = "classpath*:mapper/mysqlmapper/*.xml";
    19. @Primary
    20. @Bean(name = "mysqlDataSource")
    21. @ConfigurationProperties(prefix = "spring.datasource.primary")
    22. public DataSource mysqlDataSource() {
    23. return DataSourceBuilder.create().build();
    24. }
    25. @Primary
    26. @Bean(name = "mysqlTransactionManager")
    27. public DataSourceTransactionManager mysqlTransactionManager() {
    28. return new DataSourceTransactionManager((mysqlDataSource()));
    29. }
    30. @Primary
    31. @Bean(name = "mysqlSqlSessionFactory")
    32. public SqlSessionFactory mysqlSqlSessionFactory(@Qualifier("mysqlDataSource") DataSource mysqlDatasource) throws Exception {
    33. final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
    34. sessionFactory.setDataSource(mysqlDatasource);
    35. sessionFactory.setMapperLocations(
    36. new PathMatchingResourcePatternResolver().getResources(MysqlDataSourceConfig.MAPPER_LOCATION)
    37. );
    38. return sessionFactory.getObject();
    39. }
    40. }

    六、Oracle配置类

    OracleDataSourceConfig

    1. package com.example.multipledata.config.oracleconfig;
    2. import org.apache.ibatis.session.SqlSessionFactory;
    3. import org.mybatis.spring.SqlSessionFactoryBean;
    4. import org.mybatis.spring.annotation.MapperScan;
    5. import org.springframework.beans.factory.annotation.Qualifier;
    6. import org.springframework.boot.context.properties.ConfigurationProperties;
    7. import org.springframework.boot.jdbc.DataSourceBuilder;
    8. import org.springframework.context.annotation.Bean;
    9. import org.springframework.context.annotation.Configuration;
    10. import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
    11. import org.springframework.jdbc.datasource.DataSourceTransactionManager;
    12. import javax.sql.DataSource;
    13. @Configuration
    14. @MapperScan(basePackages = OracleDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "oracleSqlSessionFactory")
    15. public class OracleDataSourceConfig {
    16. static final String PACKAGE = "com.example.multipledata.mapper.oraclemapper";
    17. static final String MAPPER_LOCATION = "classpath*:mapper/oraclemapper/*.xml";
    18. @Bean(name = "oracleDataSource")
    19. @ConfigurationProperties(prefix = "spring.datasource.secondary")
    20. public DataSource oracleDataSource() {
    21. return DataSourceBuilder.create().build();
    22. }
    23. @Bean(name = "oracleTransactionManager")
    24. public DataSourceTransactionManager oracleTransactionManager() {
    25. return new DataSourceTransactionManager(oracleDataSource());
    26. }
    27. @Bean(name = "oracleSqlSessionFactory")
    28. public SqlSessionFactory oracleSqlSessionFactory(@Qualifier("oracleDataSource") DataSource oracleDataSource) throws Exception {
    29. final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
    30. sessionFactory.setDataSource(oracleDataSource);
    31. sessionFactory.setMapperLocations(
    32. new PathMatchingResourcePatternResolver().getResources(OracleDataSourceConfig.MAPPER_LOCATION)
    33. );
    34. return sessionFactory.getObject();
    35. }
    36. }

    原文地址:

    https://www.cnblogs.com/windy-xmwh/p/14748567.html

    七、增加Redis连接

    注意,我的Redis连接,使用了密码

    7.1 新增依赖 pom.xml

    pom.xml

    1. <dependency>
    2. <groupId>org.springframework.bootgroupId>
    3. <artifactId>spring-boot-starter-data-redisartifactId>
    4. <version>2.4.4version>
    5. dependency>
    6. <dependency>
    7. <groupId>io.lettucegroupId>
    8. <artifactId>lettuce-coreartifactId>
    9. dependency>

    7.2 配置Redis连接  application.yml

    1. spring:
    2. redis:
    3. host: host
    4. port: 6379
    5. password: 1

    7.3 Redis 配置文件 RedisConfig

    在config目录下,新增RedisConfig文件

    1. package com.example.kyjjserver.config;
    2. import org.springframework.beans.factory.annotation.Value;
    3. import org.springframework.context.annotation.Bean;
    4. import org.springframework.context.annotation.Configuration;
    5. import org.springframework.data.redis.connection.RedisConnectionFactory;
    6. import org.springframework.data.redis.core.RedisTemplate;
    7. import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
    8. import org.springframework.data.redis.serializer.StringRedisSerializer;
    9. import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
    10. @Configuration
    11. public class RedisConfig {
    12. @Value("${spring.redis.host}")
    13. private String redisHost;
    14. @Value("${spring.redis.port}")
    15. private int redisPort;
    16. @Value("${spring.redis.password}")
    17. private String redisPassword;
    18. @Bean
    19. public RedisConnectionFactory redisConnectionFactory() {
    20. LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory(redisHost, redisPort);
    21. lettuceConnectionFactory.setPassword(redisPassword);
    22. return lettuceConnectionFactory;
    23. }
    24. @Bean
    25. public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
    26. RedisTemplate redisTemplate = new RedisTemplate<>();
    27. redisTemplate.setConnectionFactory(redisConnectionFactory);
    28. redisTemplate.setKeySerializer(new StringRedisSerializer());
    29. redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
    30. redisTemplate.setHashKeySerializer(new StringRedisSerializer());
    31. redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
    32. redisTemplate.afterPropertiesSet();
    33. return redisTemplate;
    34. }
    35. }

    7.4 操作Redis

    7.4.1 封装操作方法

    在service目录下,新增RedisService文件

    1. package com.example.kyjjserver.service;
    2. import org.springframework.beans.factory.annotation.Autowired;
    3. import org.springframework.data.redis.core.RedisTemplate;
    4. import org.springframework.stereotype.Service;
    5. import java.util.Set;
    6. @Service
    7. public class RedisService {
    8. private final RedisTemplate redisTemplate;
    9. @Autowired
    10. public RedisService(RedisTemplate redisTemplate) {
    11. this.redisTemplate = redisTemplate;
    12. }
    13. public void deleteData(String key) {
    14. redisTemplate.delete(key);
    15. }
    16. public void deleteDataAll(String pattern) {
    17. Set keys = redisTemplate.keys("*" + pattern + "*");
    18. if (keys != null) {
    19. redisTemplate.delete(keys);
    20. }
    21. }
    22. }

    7.4.2 调用方法,操作Redis库

    1、注入

    2、调用

    1. @Component
    2. public class DeleteRedisInfo {
    3. @Autowired
    4. private RedisService redisService;
    5. @Transactional
    6. public AAA deleteUser(xxx xxx){
    7. // 删除手机号
    8. redisService.deleteDataAll(xxx);
    9. return xxx;
    10. }
    11. }

  • 相关阅读:
    数据中心深度制冷联合解决方案登陆VMware云市场及VMware Explore 2022大会
    android获取RAM、CPU频率、系统版本、CPU核数
    1704. 判断字符串的两半是否相似 : 简单模拟题
    vue项目关闭eslint
    Cy5反式环辛烯,TCO-Cy5,Cy5 trans-cyclooctene标记生物分子
    Drools(8):WorkBench使用
    尚硅谷大数据项目《在线教育之实时数仓》笔记006
    Redis数据结构
    断点续传-http中Header参数Range(分段请求基础)
    vscode launch.json
  • 原文地址:https://blog.csdn.net/qq_39208536/article/details/132575001