mysql druid 多数据源_Mybatis+Druid多数据源配置

在日常开发中我们可能会用到多数据源开发,什么是多数据源?

简单来讲的话,就是一个项目连接多个数据库。当然只是可能会用到,我暂时没见过应用场景,但是还是了解学习一下

此项目可以基于上一个简单集成项目进行简单的修改,就能实现多数据源了。

application.yml配置

我们在上一个项目的基础上进行修改,实现多数据源配置

spring:

datasource:

type: com.alibaba.druid.pool.DruidDataSource

url: jdbc:mysql://localhost:3306/demo?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true

username: root

password: 123456

driver-class-name: com.mysql.cj.jdbc.Driver

druid:

one:

username: root

password: 123456

driver-class-name: com.mysql.cj.jdbc.Driver

url: jdbc:mysql://localhost:3306/demo?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true

initialSize: 5

minIdle: 5

maxActive: 20

initial-size: 3

min-idle: 3

max-active: 10

max-wait: 60000

two:

username: root

password: 123456

driver-class-name: com.mysql.cj.jdbc.Driver

url: jdbc:mysql://localhost:3306/layui?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true

initialSize: 5

minIdle: 5

maxActive: 20

initial-size: 6

min-idle: 6

max-active: 20

max-wait: 12000

stat-view-servlet:

login-username: admin

login-password: admin

filter:

stat:

log-slow-sql: true

slow-sql-millis: 2000

mybatis:

mapper-locations: classpath:mappers///Mapper.xml

type-aliases-package: com.ccsert.spdruid..model

configuration:

map-underscore-to-camel-case: true

logging:

file: logs/mass.log

level:

org.springframework: info

com.ccsert: DEBUG

着是完整的配置

主要在druid数据源和mybatis的mapper.xml进行了细微修改

这里我建立一个layui数据库,里面有个和demo里一样的表,数据和结构都一样,方便等会测试

包结构调整,代码修改

包结构调整

我们先把mapper接口修改一下

在原来的mapper包下建立两个包,一个one,一个two

然后把之前的mapper接口分别复制到one和two下

然后改一下名字

改完以后大概就是这个样子

f072efa6cb6cea75ace1a33f7ef40fcc.png

代码修改

把之前的mapper注解都去掉

后面会用别的方法去映射

这是oneMapper

package com.ccsert.spdruid.test.mapper.one;

import com.ccsert.spdruid.test.model.TestUser;

import java.util.List;

public interface TestUserOneMapper {

List getall();

TestUser getById(Integer id);

int save(TestUser testUser);

}

这是twoMapper

package com.ccsert.spdruid.test.mapper.two;

import com.ccsert.spdruid.test.model.TestUser;

public interface TestUserTwoMapper {

TestUser getById(Integer id);

}

这里为了方便我就只写一个接口

然后我们在修改一下service实现类

package com.ccsert.spdruid.test.service.impl;

import com.ccsert.spdruid.test.mapper.one.TestUserOneMapper;

import com.ccsert.spdruid.test.mapper.two.TestUserTwoMapper;

import com.ccsert.spdruid.test.model.TestUser;

import com.ccsert.spdruid.test.service.TestUserService;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.List;

@Service

public class TestUserServiceImpl implements TestUserService {

@Resource

private TestUserOneMapper testUserOneMapper;

@Resource

private TestUserTwoMapper testUserTwoMapper;

@Override

public List getall() {

return testUserOneMapper.getall();

}

@Override

public TestUser getById(Integer id) {

return testUserTwoMapper.getById(id);

}

@Override

public int save(TestUser testUser) {

return testUserOneMapper.save(testUser);

}

}

getById方法让他去调用twoMapper

其余的还是让它去调用原来的接口

配置文件修改

然后我们把xml的位置移动一下

在resources下的mappers下在建立两个文件夹

一个one

一个two

然后在把之前的TestUser目录复制两份到one和two下

把原来的TestUser删除掉

在把之前的xml名字修改一下

改完以后大概就是这个样子了

a971a5c0405efd259cef4bae8154b895.png

onemapper.xml的内容保持不变

主要写一下twomapper.xml的save保存方法

SELECT

id,user_name,password

FROM

test_user

WHERE id=#{id}

因为这里的mapper接口路径是修改过的,onemapper.xml要注意一下

多数据源配置

准备工作做好了接着就是配置多数据源了

在test包下建立一个config包,用于存放配置

然后在config包下建立一个MultiDataSourceConfig类

package com.ccsert.spdruid.test.config;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;

import org.springframework.boot.context.properties.ConfigurationProperties;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;

@Configuration

public class MultiDataSourceConfig {

@Primary

@Bean(name = "oneDataSource")

@ConfigurationProperties("spring.datasource.druid.one")

public DataSource dataSourceOne(){

return DruidDataSourceBuilder.create().build();

}

@Bean(name = "twoDataSource")

@ConfigurationProperties("spring.datasource.druid.two")

public DataSource dataSourceTwo(){

return DruidDataSourceBuilder.create().build();

}

}

这里的ConfigurationProperties是获取的yml或者properties里的值

spring.datasource.druid.one和spring.datasource.druid.two就是我们配置的数据源

Primary只能指定一个为默认数据源,这里指定了one数据库

在config下建立DataSource1Config类,用于配置数据源one

package com.ccsert.spdruid.test.config;

import org.apache.ibatis.session.SqlSessionFactory;

import org.mybatis.spring.SqlSessionFactoryBean;

import org.mybatis.spring.SqlSessionTemplate;

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 = "com.ccsert.spdruid.test.mapper.one", sqlSessionTemplateRef = "test1SqlSessionTemplate")

public class DataSource1Config {

@Bean(name = "test1SqlSessionFactory")

@Primary

public SqlSessionFactory testSqlSessionFactory(@Qualifier("oneDataSource") DataSource dataSource) throws Exception {

SqlSessionFactoryBean bean = new SqlSessionFactoryBean();

bean.setDataSource(dataSource);

bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mappers/one/**/*Mapper.xml"));

return bean.getObject();

}

@Bean(name = "test1TransactionManager")

@Primary

public DataSourceTransactionManager testTransactionManager(@Qualifier("oneDataSource") DataSource dataSource) {

return new DataSourceTransactionManager(dataSource);

}

@Bean(name = "test1SqlSessionTemplate")

@Primary

public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {

return new SqlSessionTemplate(sqlSessionFactory);

}

}

@MapperScan(basePackages = "com.ccsert.spdruid.test.mapper.one", sqlSessionTemplateRef = "test1SqlSessionTemplate")指定了实体类的路径,这里就完成了映射,所以不需要在mapper接口上写@Mapper注解

然后在建立一个DataSource2Config类

内容和上面的差不多

package com.ccsert.spdruid.test.config;

import org.apache.ibatis.session.SqlSessionFactory;

import org.mybatis.spring.SqlSessionFactoryBean;

import org.mybatis.spring.SqlSessionTemplate;

import org.mybatis.spring.annotation.MapperScan;

import org.springframework.beans.factory.annotation.Qualifier;

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 = "com.ccsert.spdruid.test.mapper.two", sqlSessionTemplateRef = "test2SqlSessionTemplate")

public class DataSource2Config {

@Bean(name = "test2SqlSessionFactory")

public SqlSessionFactory testSqlSessionFactory(@Qualifier("twoDataSource") DataSource dataSource) throws Exception {

SqlSessionFactoryBean bean = new SqlSessionFactoryBean();

bean.setDataSource(dataSource);

bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mappers/two/**/*Mapper.xml"));

return bean.getObject();

}

@Bean(name = "test2TransactionManager")

public DataSourceTransactionManager testTransactionManager(@Qualifier("twoDataSource") DataSource dataSource) {

return new DataSourceTransactionManager(dataSource);

}

@Bean(name = "test2SqlSessionTemplate")

public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {

return new SqlSessionTemplate(sqlSessionFactory);

}

}

注意这两个路径写成自己的不要搞错了

到这里就配置完成了

测试使用

我们先访问一下接口能否调通

启动项目然后使用谷歌插件访问接口

c5e1fc5e3215d53590f20e19bf96afdb.png

9aa36cee92babe1d94dcd6af02d9baac.png

可以看到我们两个接口都调用成功

我们去druid的监控界面查看一下执行的sql和数据源信息

8ff0b6410d8936b1c0ae06a325464837.png

55a3a3afe1356122ceeebe3dcac9c320.png

e54485f2210c2a163ea596d47675e1e2.png

可以看到druid监控了我们执行的两条sql,以及两个数据源信息

在Spring Boot项目中使用MyBatis Plus和Druid多数据源的步骤如下: 1. 添加依赖 在`pom.xml`文件中添加以下依赖: ```xml <!-- MyBatis Plus --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.3.1</version> </dependency> <!-- Druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.2.6</version> </dependency> ``` 2. 配置Druid数据源 在`application.yml`中添加Druid数据源的配置: ```yaml spring: datasource: # 主数据源 druid: url: jdbc:mysql://localhost:3306/main_db?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8 username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver # Druid配置 initialSize: 5 minIdle: 5 maxActive: 20 testOnBorrow: false testOnReturn: false testWhileIdle: true timeBetweenEvictionRunsMillis: 60000 validationQuery: SELECT 1 FROM DUAL # 从数据源 druid2: url: jdbc:mysql://localhost:3306/sub_db?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8 username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver # Druid配置 initialSize: 5 minIdle: 5 maxActive: 20 testOnBorrow: false testOnReturn: false testWhileIdle: true timeBetweenEvictionRunsMillis: 60000 validationQuery: SELECT 1 FROM DUAL ``` 3. 配置MyBatis Plus 在`application.yml`中添加MyBatis Plus的配置: ```yaml mybatis-plus: # 主数据源配置 mapper-locations: classpath:mapper/main/*.xml type-aliases-package: com.example.main.entity global-config: db-config: id-type: auto field-strategy: not_empty logic-delete-value: 1 logic-not-delete-value: 0 configuration: map-underscore-to-camel-case: true log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 从数据源配置 multi-datasource: main: mapper-locations: classpath:mapper/main/*.xml type-aliases-package: com.example.main.entity sub: mapper-locations: classpath:mapper/sub/*.xml type-aliases-package: com.example.sub.entity ``` 4. 配置数据源路由 在`com.example.config`包下创建`DynamicDataSourceConfig`类,用于配置数据源路由: ```java @Configuration public class DynamicDataSourceConfig { @Bean @ConfigurationProperties("spring.datasource.druid") public DataSource mainDataSource() { return DruidDataSourceBuilder.create().build(); } @Bean @ConfigurationProperties("spring.datasource.druid2") public DataSource subDataSource() { return DruidDataSourceBuilder.create().build(); } @Bean public DataSource dynamicDataSource() { DynamicDataSource dynamicDataSource = new DynamicDataSource(); Map<Object, Object> dataSourceMap = new HashMap<>(2); dataSourceMap.put("main", mainDataSource()); dataSourceMap.put("sub", subDataSource()); // 将主数据源作为默认数据源 dynamicDataSource.setDefaultTargetDataSource(mainDataSource()); dynamicDataSource.setTargetDataSources(dataSourceMap); return dynamicDataSource; } @Bean public SqlSessionFactory sqlSessionFactory() throws Exception { MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(dynamicDataSource()); sqlSessionFactoryBean.setTypeAliasesPackage("com.example.main.entity"); sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/main/*.xml")); return sqlSessionFactoryBean.getObject(); } @Bean public SqlSessionTemplate sqlSessionTemplate() throws Exception { return new SqlSessionTemplate(sqlSessionFactory()); } } ``` 5. 配置数据源切换 在`com.example.config`包下创建`DynamicDataSource`类,用于实现数据源切换: ```java public class DynamicDataSource extends AbstractRoutingDataSource { @Override protected Object determineCurrentLookupKey() { return DataSourceContextHolder.getDataSource(); } } ``` 在`com.example.config`包下创建`DataSourceContextHolder`类,用于存储当前数据源: ```java public class DataSourceContextHolder { private static final ThreadLocal<String> DATASOURCE_CONTEXT_HOLDER = new ThreadLocal<>(); public static void setDataSource(String dataSource) { DATASOURCE_CONTEXT_HOLDER.set(dataSource); } public static String getDataSource() { return DATASOURCE_CONTEXT_HOLDER.get(); } public static void clearDataSource() { DATASOURCE_CONTEXT_HOLDER.remove(); } } ``` 在`com.example.aop`包下创建`DataSourceAspect`类,用于切换数据源: ```java @Aspect @Component public class DataSourceAspect { @Pointcut("@annotation(com.example.annotation.DataSource)") public void dataSourcePointCut() { } @Before("dataSourcePointCut()") public void before(JoinPoint joinPoint) { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); DataSource dataSource = signature.getMethod().getAnnotation(DataSource.class); if (dataSource != null) { String value = dataSource.value(); DataSourceContextHolder.setDataSource(value); } } @After("dataSourcePointCut()") public void after(JoinPoint joinPoint) { DataSourceContextHolder.clearDataSource(); } } ``` 6. 使用多数据源 在需要使用从数据源的方法上加上`@DataSource("sub")`注解,如: ```java @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public List<User> listUsers() { DataSourceContextHolder.setDataSource("sub"); List<User> users = userMapper.selectList(null); DataSourceContextHolder.clearDataSource(); return users; } } ``` 这样就完成了Spring Boot项目中使用MyBatis Plus和Druid多数据源配置
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值