一、添加依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.edu.tju</groupId>
<artifactId>springbootmultipledatasource</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.7</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.35</version>
</dependency>
</dependencies>
</project>
二、在application.properties中配置多个数据源
server.port=9020
spring.datasource.first.url=jdbc:mysql://xxx.xxx.xxx.xxx:3306/test?useSSL=false&serverTimezone=Asia/Shanghai
spring.datasource.first.user=root
spring.datasource.first.password=MyPass
spring.datasource.second.url=jdbc:mysql://xxx.xxx.xxx.xxx:3306/appbyte?useSSL=false&serverTimezone=Asia/Shanghai
spring.datasource.second.user=root
spring.datasource.second.password=MyPass
三、在多个package里创建多个mapper
package cn.edu.tju.mapper.first;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.Map;
public interface FirstUserMapper {
@Select("select * from user limit 1")
Map<String,Object> getUser();
}
package cn.edu.tju.mapper.second;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.Map;
public interface SecondUserMapper {
@Select("select * from user limit 1")
Map<String,Object> getUser();
}
四、配置多个数据源
package cn.edu.tju.config;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Qualifier;
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 org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.jta.JtaTransactionManager;
import javax.sql.DataSource;
import java.util.Properties;
@Configuration
public class MyConfig {
@ConfigurationProperties(prefix = "spring.datasource.first")
@Bean
public Properties firstProperties() {
return new Properties();
}
@Bean(name = "firstDataSource")
@Primary
public DataSource firstDataSource() {
HikariDataSource ds = new HikariDataSource();
ds.setJdbcUrl(firstProperties().getProperty("url"));
ds.setUsername(firstProperties().getProperty("user"));
ds.setPassword(firstProperties().getProperty("password"));
return ds;
}
@ConfigurationProperties(prefix = "spring.datasource.second")
@Bean
public Properties secondProperties() {
return new Properties();
}
@Bean(name = "secondDataSource")
public DataSource secondDataSource() {
HikariDataSource ds = new HikariDataSource();
ds.setJdbcUrl(secondProperties().getProperty("url"));
ds.setUsername(secondProperties().getProperty("user"));
ds.setPassword(secondProperties().getProperty("password"));
return ds;
}
@Bean("firstSqlSessionFactory")
@Primary
public SqlSessionFactory firstSqlSessionFactory(){
try {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(firstDataSource());
return sqlSessionFactoryBean.getObject();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Bean("secondSqlSessionFactory")
public SqlSessionFactory secondSqlSessionFactory(){
try {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(secondDataSource());
return sqlSessionFactoryBean.getObject();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
五、配置多个MapperScan
package cn.edu.tju.config;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@MapperScan(basePackages = "cn.edu.tju.mapper.first", sqlSessionFactoryRef = "firstSqlSessionFactory")
public class FirstMapperConfig {
}
package cn.edu.tju.config;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@MapperScan(basePackages = "cn.edu.tju.mapper.second", sqlSessionFactoryRef = "secondSqlSessionFactory")
public class SecondMapperConfig {
}
六、启动类:
package cn.edu.tju;
import org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, MybatisAutoConfiguration.class})
public class Start {
public static void main(String[] args) {
SpringApplication.run(Start.class,args);
}
}
七、controller中使用Mapper
package cn.edu.tju.controller;
import cn.edu.tju.mapper.first.FirstUserMapper;
import cn.edu.tju.mapper.second.SecondUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
@RestController
public class DemoController {
@Autowired
private FirstUserMapper firstUserMapper;
@Autowired
private SecondUserMapper secondUserMapper;
@RequestMapping("/test1")
public String test1(){
Map<String, Object> user = firstUserMapper.getUser();
return String.valueOf(user.get("username"));
}
@RequestMapping("/test2")
public String test2(){
Map<String, Object> user = secondUserMapper.getUser();
return String.valueOf(user.get("username"));
}
}