springboot mybatis 同时连接mysql和sqlserver数据库
时间: 2023-11-07 11:14:09 浏览: 208
可以在Spring Boot项目中同时连接MySQL和SQL Server数据库,具体步骤如下:
1. 在pom.xml文件中添加MySQL和SQL Server数据库的驱动依赖,例如:
```xml
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>${sqlserver.version}</version>
</dependency>
```
2. 在application.yml文件中配置两个数据源,例如:
```yaml
spring:
datasource:
primary:
url: jdbc:mysql://localhost:3306/db_mysql
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 123456
secondary:
url: jdbc:sqlserver://localhost:1433;databaseName=db_sqlserver
driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
username: sa
password: 123456
```
3. 在Spring Boot项目中配置两个数据源,例如:
```java
@Configuration
public class DataSourceConfig {
@Bean(name = "primaryDataSource")
@Qualifier("primaryDataSource")
@ConfigurationProperties(prefix = "spring.datasource.primary")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "secondaryDataSource")
@Qualifier("secondaryDataSource")
@ConfigurationProperties(prefix = "spring.datasource.secondary")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}
}
```
4. 在MyBatis中配置两个数据源,并指定使用哪个数据源,例如:
```java
@Configuration
@MapperScan(basePackages = "com.example.mapper", sqlSessionTemplateRef = "primarySqlSessionTemplate")
public class MybatisConfig {
@Bean(name = "primarySqlSessionFactory")
@Primary
public SqlSessionFactory primarySqlSessionFactory(@Qualifier("primaryDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
return bean.getObject();
}
@Bean(name = "secondarySqlSessionFactory")
public SqlSessionFactory secondarySqlSessionFactory(@Qualifier("secondaryDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
return bean.getObject();
}
@Bean(name = "primarySqlSessionTemplate")
@Primary
public SqlSessionTemplate primarySqlSessionTemplate(@Qualifier("primarySqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
@Bean(name = "secondarySqlSessionTemplate")
public SqlSessionTemplate secondarySqlSessionTemplate(@Qualifier("secondarySqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
@Bean
@Primary
public PlatformTransactionManager primaryTransactionManager(@Qualifier("primaryDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean
public PlatformTransactionManager secondaryTransactionManager(@Qualifier("secondaryDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
}
```
在定义Mapper时,可以通过@Qualifier注解指定使用哪个数据源的SqlSessionTemplate,例如:
```java
public interface UserMapper {
@Select("SELECT * FROM user")
@Results({
@Result(property = "id", column = "id"),
@Result(property = "name", column = "name"),
@Result(property = "age", column = "age")
})
List<User> getAllUsers();
@Select("SELECT * FROM user WHERE id = #{id}")
@Results({
@Result(property = "id", column = "id"),
@Result(property = "name", column = "name"),
@Result(property = "age", column = "age")
})
User getUserById(@Param("id") Long id);
}
```
在Service层中,可以通过@Autowired注解注入UserMapper,并指定使用哪个数据源的SqlSessionTemplate,例如:
```java
@Service
public class UserService {
@Autowired
@Qualifier("primarySqlSessionTemplate")
private SqlSessionTemplate primarySqlSessionTemplate;
@Autowired
@Qualifier("secondarySqlSessionTemplate")
private SqlSessionTemplate secondarySqlSessionTemplate;
public List<User> getAllUsersFromPrimaryDataSource() {
UserMapper userMapper = primarySqlSessionTemplate.getMapper(UserMapper.class);
return userMapper.getAllUsers();
}
public User getUserByIdFromSecondaryDataSource(Long id) {
UserMapper userMapper = secondarySqlSessionTemplate.getMapper(UserMapper.class);
return userMapper.getUserById(id);
}
}
```
这样就可以在Spring Boot项目中同时连接MySQL和SQL Server数据库了。
阅读全文