1.配置application.yml文件
# 数据库配置
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driverClassName: com.mysql.cj.jdbc.Driver
druid:
# 主库数据源
master:
url: jdbc:mysql://xxx/xxx?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
username: xxx
password: xxx
# 从库数据源
slave:
# 从数据源开关/默认关闭
enabled: true
url: jdbc:mysql://xxx/xxx?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8&allowMultiQueries=true
username: xxx
password: xxx
# 这里就是新增加的数据源,比如是chatgpt库名
chatgpt:
# 从数据源开关/默认关闭
enabled: true
url: jdbc:mysql://xxx/chatgpt?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8&allowMultiQueries=true
username: chatgpt
password: chatgpt
2.增加枚举DataSourceType
package com. ruoyi. common. enums ;
public enum DataSourceType
{
MASTER ,
SLAVE ,
CHATGPT
}
3.增加数据源加载DruidConfig
package com. ruoyi. framework. config ;
import java. io. IOException ;
import java. util. HashMap ;
import java. util. Map ;
import javax. servlet. Filter ;
import javax. servlet. FilterChain ;
import javax. servlet. ServletException ;
import javax. servlet. ServletRequest ;
import javax. servlet. ServletResponse ;
import javax. sql. DataSource ;
import org. springframework. boot. autoconfigure. condition. ConditionalOnProperty ;
import org. springframework. boot. context. properties. ConfigurationProperties ;
import org. springframework. boot. web. servlet. FilterRegistrationBean ;
import org. springframework. context. annotation. Bean ;
import org. springframework. context. annotation. Configuration ;
import org. springframework. context. annotation. Primary ;
import com. alibaba. druid. pool. DruidDataSource ;
import com. alibaba. druid. spring. boot. autoconfigure. DruidDataSourceBuilder ;
import com. alibaba. druid. spring. boot. autoconfigure. properties. DruidStatProperties ;
import com. alibaba. druid. util. Utils ;
import com. ruoyi. common. enums. DataSourceType ;
import com. ruoyi. common. utils. spring. SpringUtils ;
import com. ruoyi. framework. config. properties. DruidProperties ;
import com. ruoyi. framework. datasource. DynamicDataSource ;
@Configuration
public class DruidConfig
{
@Bean
@ConfigurationProperties ( "spring.datasource.druid.master" )
public DataSource masterDataSource ( DruidProperties druidProperties)
{
DruidDataSource dataSource = DruidDataSourceBuilder . create ( ) . build ( ) ;
return druidProperties. dataSource ( dataSource) ;
}
@Bean
@ConfigurationProperties ( "spring.datasource.druid.slave" )
@ConditionalOnProperty ( prefix = "spring.datasource.druid.slave" , name = "enabled" , havingValue = "true" )
public DataSource slaveDataSource ( DruidProperties druidProperties)
{
DruidDataSource dataSource = DruidDataSourceBuilder . create ( ) . build ( ) ;
return druidProperties. dataSource ( dataSource) ;
}
@Bean
@ConfigurationProperties ( "spring.datasource.druid.chatgpt" )
@ConditionalOnProperty ( prefix = "spring.datasource.druid.chatgpt" , name = "enabled" , havingValue = "true" )
public DataSource chatgptDataSource ( DruidProperties druidProperties)
{
DruidDataSource dataSource = DruidDataSourceBuilder . create ( ) . build ( ) ;
return druidProperties. dataSource ( dataSource) ;
}
@Bean ( name = "dynamicDataSource" )
@Primary
public DynamicDataSource dataSource ( DataSource masterDataSource)
{
Map < Object , Object > targetDataSources = new HashMap < > ( ) ;
targetDataSources. put ( DataSourceType . MASTER . name ( ) , masterDataSource) ;
setDataSource ( targetDataSources, DataSourceType . SLAVE . name ( ) , "slaveDataSource" ) ;
setDataSource ( targetDataSources, DataSourceType . CHATGPT . name ( ) , "chatgptDataSource" ) ;
return new DynamicDataSource ( masterDataSource, targetDataSources) ;
}
}
4.编写测试Mapper
package com. ruoyi. xxx. mapper ;
import com. ruoyi. common. annotation. DataSource ;
import com. ruoyi. common. enums. DataSourceType ;
import org. apache. ibatis. annotations. Select ;
import org. springframework. stereotype. Component ;
import java. util. List ;
import java. util. Map ;
@Component
public interface TestChatgptMapper {
@DataSource ( value= DataSourceType . CHATGPT )
@Select ( "select * from user" )
List < Map > selectAll ( ) ;
}
5.编写测试类使用
@Autowired
private TestChatgptMapper testChatgptMapper;
@Test
void test7 ( ) {
List < Map > res = testChatgptMapper. selectAll ( ) ;
System . out. println ( res. toString ( ) ) ;
}