If integrating multiple data sources with SQL Server
The driver package is placed in the pom file of the admin module.
<!--sql server drive-- >
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>4.0</version>
</dependency>
Modify data source configuration
application-druid.yml.
# OMS data source
oms:
enabled: true
driverClassName: com.microsoft.sqlserver.jdbc.SQLServerDriver
url: jdbc:sqlserver://xxx;DatabaseName=xxx;
username: xxx
password: xxx
Other configuration modifications.
#configure to check if the connection is valid
validationQuery: SELECT 1
or simply comment it out
#validationQuery: SELECT 1 FROM DUAL
just comment it out and comment it out again DruidProperties in the text
//@Value("${spring.datasource.druid.validationQuery}")
//private String validationQuery;
/**
*used to check if the connection is valid sql , requires a query statement, commonly used select 'x'. if validationQuery by null,testOnBorrow、testOnReturn、testWhileIdle none of them will work.
*/
// datasource.setValidationQuery(validationQuery);
At that time, validationQuery changed to select * and kept reporting "must specify the table to be selected from." I always thought it was a SQL error. .
Add data source enumeration type
DataSourceType.
/**
*data source
*
* @author ruoyi
*/
public enum DataSourceType
{
/**
*main library
*/
MASTER,
/**
*from library
*/
SLAVE,
/**
* OMS database SQL SERVER database
*/
OMS
}
Data source read injection DruidConfig.
@Bean
@ConfigurationProperties("spring.datasource.druid.oms")
@ConditionalOnProperty(prefix = "spring.datasource.druid.oms", name = "enabled", havingValue = "true")
public DataSource omsDataSource(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.OMS.name(), "omsDataSource");
return new DynamicDataSource(masterDataSource, targetDataSources);
}
Add @on methods or classes that require multiple data sources; Datasource annotation, where value is used to represent the data source.
@DataSource(value = DataSourceType.SLAVE)
public List<SysUser> selectUserList(SysUser user)
{
return userMapper.selectUserList(user);
}
@Service
@DataSource(value = DataSourceType.SLAVE)
public class SysUserServiceImpl