ruoyi多数据源sqlserver
时间: 2023-11-24 19:46:46 浏览: 160
对于ruoyi多数据源配置SQL Server,你可以按照以下步骤进行操作:
1. 在ruoyi-generator模块的resources目录下的application.yml文件中,针对每个数据源配置相关信息,如下所示:
```
spring:
datasource:
# 主数据源配置
primary:
url: jdbc:mysql://localhost:3306/primary?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
# 第二个数据源配置
secondary:
url: jdbc:sqlserver://localhost:1433;databaseName=secondary
username: sa
password: password
driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
```
2. 在ruoyi-admin模块的pom.xml文件中,添加SQL Server数据库驱动依赖,如下所示:
```xml
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>8.4.1.jre8</version>
</dependency>
```
3. 在ruoyi-admin模块的src/main/java/com/ruoyi/framework/config目录下,创建一个新的数据源配置类,如下所示:
```java
@Configuration
public class SecondaryDataSourceConfig {
@Bean(name = "secondaryDataSource")
@ConfigurationProperties(prefix = "spring.datasource.secondary")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "secondarySqlSessionFactory")
public SqlSessionFactory secondarySqlSessionFactory(@Qualifier("secondaryDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
// 设置其他配置,例如mapper文件所在路径等
return bean.getObject();
}
@Bean(name = "secondarySqlSessionTemplate")
public SqlSessionTemplate secondarySqlSessionTemplate(@Qualifier("secondarySqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
// 可以根据需要配置其他的数据源
}
```
4. 在ruoyi-admin模块的src/main/java/com/ruoyi/framework/config目录下,创建一个新的数据源配置类,如下所示:
```java
@Configuration
public class DataSourceConfig {
@Primary
@Bean(name = "primaryDataSource")
@ConfigurationProperties(prefix = "spring.datasource.primary")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Bean(name = "primarySqlSessionFactory")
public SqlSessionFactory primarySqlSessionFactory(@Qualifier("primaryDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
// 设置其他配置,例如mapper文件所在路径等
return bean.getObject();
}
@Primary
@Bean(name = "primarySqlSessionTemplate")
public SqlSessionTemplate primarySqlSessionTemplate(@Qualifier("primarySqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
// 可以根据需要配置其他的数据源
}
```
5. 在ruoyi-admin模块的src/main/java/com/ruoyi/framework/config目录下,创建一个新的数据源配置类,如下所示:
```java
@Configuration
public class DynamicDataSourceConfig {
@Bean
public DynamicDataSource dataSource(@Qualifier("primaryDataSource") DataSource primaryDataSource,
@Qualifier("secondaryDataSource") DataSource secondaryDataSource) {
Map<Object, Object> targetDataSources = new HashMap<>();
targetDataSources.put(DatabaseType.PRIMARY, primaryDataSource);
targetDataSources.put(DatabaseType.SECONDARY, secondaryDataSource);
DynamicDataSource dataSource = new DynamicDataSource();
dataSource.setTargetDataSources(targetDataSources);
dataSource.setDefaultTargetDataSource(primaryDataSource);
return dataSource;
}
@Bean
public SqlSessionFactory sqlSessionFactory(DynamicDataSource dataSource) throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
// 设置其他配置,例如mapper文件所在路径等
return sessionFactory.getObject();
}
@Bean
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
// 可以根据需要配置其他的数据源
}
```
6. 在ruoyi-admin模块的src/main/java/com/ruoyi/common/enums目录下,创建一个新的枚举类DatabaseType.java,如下所示:
```java
public enum DatabaseType {
PRIMARY, SECONDARY;
}
```
7. 在ruoyi-admin模块的src/main/java/com/ruoyi/datasource目录下,创建一个新的动态数据源类DynamicDataSource.java,如下所示:
```java
public class DynamicDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
return DatabaseContextHolder.getDatabaseType();
}
}
```
8. 在ruoyi-admin模块的src/main/java/com/ruoyi/datasource目录下,创建一个新的数据源上下文类DatabaseContextHolder.java,如下所示:
```java
public class DatabaseContextHolder {
private static final ThreadLocal<DatabaseType> contextHolder = new ThreadLocal<>();
public static void setDatabaseType(DatabaseType databaseType) {
contextHolder.set(databaseType);
}
public static DatabaseType getDatabaseType() {
return contextHolder.get();
}
public static void clearDatabaseType() {
contextHolder.remove();
}
}
```
9. 在ruoyi-admin模块的src/main/java/com/ruoyi/datasource目录下,创建一个新的数据源切换类DataSourceAspect.java,如下所示:
```java
@Aspect
@Component
public class DataSourceAspect {
@Before("@annotation(com.ruoyi.common.annotation.DataSource)")
public void switchDataSource(JoinPoint point) {
MethodSignature signature = (MethodSignature) point.getSignature();
Method method = signature.getMethod();
DataSource dataSource = method.getAnnotation(DataSource.class);
if (dataSource != null) {
DatabaseContextHolder.setDatabaseType(dataSource.value());
}
}
@After("@annotation(com.ruoyi.common.annotation.DataSource)")
public void restoreDataSource(JoinPoint point) {
DatabaseContextHolder.clearDatabaseType();
}
}
```
10. 在ruoyi-admin模块的src/main/java/com/ruoyi/common/annotation目录下,创建一个新的注解类DataSource.java,如下所示:
```java
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DataSource {
DatabaseType value() default DatabaseType.PRIMARY;
}
```
11. 配置完成后,可以在需要切换数据源的service或mapper方法上,使用@DataSource注解来指定数据源,例如:
```java
@DataSource(DatabaseType.SECONDARY)
public List<User> listUsers() {
return userMapper.listUsers();
}
```
请注意,在进行多数据源配置时,请确保你的ruoyi版本支持多数据源功能。
阅读全文