在SpringBoot Mybatis 项目中,需要连接 多个数据源,连接多个数据库,需要连接一个MySQL数据库和一个Oracle数据库和一个Redis
- <dependencies>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-webartifactId>
- dependency>
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-testartifactId>
- <scope>testscope>
- dependency>
-
- <dependency>
- <groupId>mysqlgroupId>
- <artifactId>mysql-connector-javaartifactId>
- <version>8.0.26version>
- dependency>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-jdbcartifactId>
- dependency>
-
- <dependency>
- <groupId>org.mybatis.spring.bootgroupId>
- <artifactId>mybatis-spring-boot-starterartifactId>
- <version>1.3.2version>
- dependency>
-
- <dependency>
- <groupId>com.oracle.database.jdbcgroupId>
- <artifactId>ojdbc8artifactId>
- <version>19.8.0.0version>
- dependency>
-
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-data-redisartifactId>
- <version>2.4.4version>
- dependency>
-
- <dependency>
- <groupId>org.projectlombokgroupId>
- <artifactId>lombokartifactId>
- <version>1.18.16version>
- <scope>providedscope>
- dependency>
- <dependency>
- <groupId>javax.persistencegroupId>
- <artifactId>javax.persistence-apiartifactId>
- <version>2.2version>
- dependency>
-
- <dependency>
- <groupId>io.springfoxgroupId>
- <artifactId>springfox-swagger2artifactId>
- <version>2.9.2version>
- dependency>
-
- <dependency>
- <groupId>io.springfoxgroupId>
- <artifactId>springfox-swagger-uiartifactId>
- <version>2.9.2version>
- dependency>
-
-
-
- <dependency>
- <groupId>cn.easyprojectgroupId>
- <artifactId>orai18nartifactId>
- <version>12.1.0.2.0version>
- dependency>
-
- dependencies>

spring.datasource.url数据库的JDBC URL
spring.datasource.jdbc-url用来重写自定义连接池
Hikari没有url属性,但是有jdbcUrl属性,在这中情况下必须使用jdbc_url
- server:
- port: 8080
-
- spring:
- datasource:
- primary:
- jdbc-url: jdbc:mysql://localhost:3306/database_name
- username: root
- password: 123456
- driver-class-name: com.mysql.cj.jdbc.Driver
-
- secondary:
- jdbc-url: jdbc:oracle:thin:@localhost:1521/ORCL
- username: root
- password: 123456
- driver-class-name: oracle.jdbc.driver.OracleDriver
MysqlDataSourceConfig
使用注解@Primary配置默认数据源
- package com.example.multipledata.config.mysqlconfig;
-
- import org.apache.ibatis.session.SqlSessionFactory;
- import org.mybatis.spring.SqlSessionFactoryBean;
- import org.mybatis.spring.annotation.MapperScan;
- import org.springframework.beans.factory.annotation.Qualifier;
- import org.springframework.boot.context.properties.ConfigurationProperties;
- import org.springframework.boot.jdbc.DataSourceBuilder;
- 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 org.springframework.jdbc.datasource.DataSourceTransactionManager;
-
- import javax.sql.DataSource;
-
-
- @Configuration
- @MapperScan(basePackages = MysqlDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "mysqlSqlSessionFactory")
- public class MysqlDataSourceConfig {
-
- static final String PACKAGE = "com.example.multipledata.mapper.mysqlmapper";
-
- static final String MAPPER_LOCATION = "classpath*:mapper/mysqlmapper/*.xml";
-
- @Primary
- @Bean(name = "mysqlDataSource")
- @ConfigurationProperties(prefix = "spring.datasource.primary")
- public DataSource mysqlDataSource() {
- return DataSourceBuilder.create().build();
- }
-
- @Primary
- @Bean(name = "mysqlTransactionManager")
- public DataSourceTransactionManager mysqlTransactionManager() {
- return new DataSourceTransactionManager((mysqlDataSource()));
- }
-
- @Primary
- @Bean(name = "mysqlSqlSessionFactory")
- public SqlSessionFactory mysqlSqlSessionFactory(@Qualifier("mysqlDataSource") DataSource mysqlDatasource) throws Exception {
- final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
- sessionFactory.setDataSource(mysqlDatasource);
- sessionFactory.setMapperLocations(
- new PathMatchingResourcePatternResolver().getResources(MysqlDataSourceConfig.MAPPER_LOCATION)
- );
- return sessionFactory.getObject();
- }
- }
OracleDataSourceConfig
- package com.example.multipledata.config.oracleconfig;
-
- import org.apache.ibatis.session.SqlSessionFactory;
- import org.mybatis.spring.SqlSessionFactoryBean;
- import org.mybatis.spring.annotation.MapperScan;
- import org.springframework.beans.factory.annotation.Qualifier;
- import org.springframework.boot.context.properties.ConfigurationProperties;
- import org.springframework.boot.jdbc.DataSourceBuilder;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
- import org.springframework.jdbc.datasource.DataSourceTransactionManager;
-
- import javax.sql.DataSource;
-
- @Configuration
- @MapperScan(basePackages = OracleDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "oracleSqlSessionFactory")
- public class OracleDataSourceConfig {
-
- static final String PACKAGE = "com.example.multipledata.mapper.oraclemapper";
-
- static final String MAPPER_LOCATION = "classpath*:mapper/oraclemapper/*.xml";
-
- @Bean(name = "oracleDataSource")
- @ConfigurationProperties(prefix = "spring.datasource.secondary")
- public DataSource oracleDataSource() {
- return DataSourceBuilder.create().build();
- }
-
- @Bean(name = "oracleTransactionManager")
- public DataSourceTransactionManager oracleTransactionManager() {
- return new DataSourceTransactionManager(oracleDataSource());
- }
-
- @Bean(name = "oracleSqlSessionFactory")
- public SqlSessionFactory oracleSqlSessionFactory(@Qualifier("oracleDataSource") DataSource oracleDataSource) throws Exception {
- final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
- sessionFactory.setDataSource(oracleDataSource);
- sessionFactory.setMapperLocations(
- new PathMatchingResourcePatternResolver().getResources(OracleDataSourceConfig.MAPPER_LOCATION)
- );
- return sessionFactory.getObject();
- }
- }
原文地址:
https://www.cnblogs.com/windy-xmwh/p/14748567.html
注意,我的Redis连接,使用了密码
pom.xml
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-data-redisartifactId>
- <version>2.4.4version>
- dependency>
- <dependency>
- <groupId>io.lettucegroupId>
- <artifactId>lettuce-coreartifactId>
- dependency>
- spring:
- redis:
- host: host
- port: 6379
- password: 1
在config目录下,新增RedisConfig文件
- package com.example.kyjjserver.config;
-
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.data.redis.connection.RedisConnectionFactory;
- import org.springframework.data.redis.core.RedisTemplate;
- import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
- import org.springframework.data.redis.serializer.StringRedisSerializer;
- import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
-
- @Configuration
- public class RedisConfig {
-
- @Value("${spring.redis.host}")
- private String redisHost;
-
- @Value("${spring.redis.port}")
- private int redisPort;
-
- @Value("${spring.redis.password}")
- private String redisPassword;
-
- @Bean
- public RedisConnectionFactory redisConnectionFactory() {
- LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory(redisHost, redisPort);
- lettuceConnectionFactory.setPassword(redisPassword);
- return lettuceConnectionFactory;
- }
-
- @Bean
- public RedisTemplate
redisTemplate(RedisConnectionFactory redisConnectionFactory) { - RedisTemplate
redisTemplate = new RedisTemplate<>(); - redisTemplate.setConnectionFactory(redisConnectionFactory);
- redisTemplate.setKeySerializer(new StringRedisSerializer());
- redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
- redisTemplate.setHashKeySerializer(new StringRedisSerializer());
- redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
- redisTemplate.afterPropertiesSet();
- return redisTemplate;
- }
- }
在service目录下,新增RedisService文件
- package com.example.kyjjserver.service;
-
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.data.redis.core.RedisTemplate;
- import org.springframework.stereotype.Service;
-
- import java.util.Set;
-
- @Service
- public class RedisService {
-
- private final RedisTemplate
redisTemplate; -
- @Autowired
- public RedisService(RedisTemplate
redisTemplate) { - this.redisTemplate = redisTemplate;
- }
-
- public void deleteData(String key) {
- redisTemplate.delete(key);
- }
-
- public void deleteDataAll(String pattern) {
- Set
keys = redisTemplate.keys("*" + pattern + "*"); - if (keys != null) {
- redisTemplate.delete(keys);
- }
- }
- }
1、注入
2、调用
- @Component
- public class DeleteRedisInfo {
- @Autowired
- private RedisService redisService;
-
- @Transactional
- public AAA deleteUser(xxx xxx){
- // 删除手机号
- redisService.deleteDataAll(xxx);
-
-
-
- return xxx;
- }
- }