2.maven依赖CREATE DATAbase `data_source_one`;
USE `data_source_one`;
CREATE TABLE `user` (
`id` INT (11),
`name` VARCHAr (33)
);INSERT INTO `user` VALUE(1,'许仙');
CREATE DATAbase `data_source_two`;
USE `data_source_two`;
CREATE TABLE `user` (
`id` INT (11),
`name` VARCHAr (33)
);INSERT INTO `user` VALUE(1,'白素贞');
3.application.ymlspring-boot-starter-parent org.springframework.boot 2.3.8.RELEASE 1.8 5.1.47 1.1.20 2.5.6 org.springframework.boot spring-boot-starter-webio.easybest spring-data-mybatis-boot-starter2.0.1.RELEASE com.baomidou dynamic-datasource-spring-boot-starter${dynamic.datasource.version} mysql mysql-connector-java${mysql.version} com.alibaba druid-spring-boot-starter${druid.version} org.projectlombok lomboktrue
server:
port: 8088
spring:
datasource:
dynamic:
primary: sourceOne # 配置默认数据库
datasource:
sourceOne: # 数据源1配置
url: jdbc:mysql://127.0.0.1:3306/data_source_one?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true&useSSL=false
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
sourceTwo: # 数据源2配置
url: jdbc:mysql://127.0.0.1:3306/data_source_two?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true&useSSL=false
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
durid:
initial-size: 1
max-active: 20
min-idle: 1
max-wait: 60000
autoconfigure:
exclude: com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure # 去除druid配置,非常重要
4.启动类
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.qq.mapper")
public class MultiSourceApplication {
public static void main(String[] args) {
SpringApplication.run(MultiSourceApplication.class, args);
}
}
5.数据库映射对象
import lombok.Data;
@Data
public class User {
private Integer id;
private String name;
}
6.mapper
7.service# mapper1
import com.qq.entity.User; import org.apache.ibatis.annotations.Select; import org.springframework.stereotype.Repository; import java.util.List; //默认使用sourceOne @Repository public interface SourceoneMapper { @Select("select * from user") ListfindAll(); } #mapper2
import com.baomidou.dynamic.datasource.annotation.DS; import com.qq.entity.User; import org.apache.ibatis.annotations.Select; import org.springframework.stereotype.Repository; import java.util.List; //@DS指定数据源 @DS("sourceTwo") @Repository public interface SourceTwoMapper { @Select("select * from user") ListfindAll(); }
#自己写个SourceService用于实现
import com.qq.entity.User;
import com.qq.mapper.SourceOneMapper;
import com.qq.mapper.SourceTwoMapper;
import com.qq.service.SourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class SourceServiceImpl implements SourceService {
@Autowired
private SourceoneMapper sourceOneMapper;
@Autowired
private SourceTwoMapper sourceTwoMapper;
@Override
public List findAll() {
List list = new ArrayList<>();
//数据合并
List sourceoneList = sourceOneMapper.findAll();
list.addAll(sourceOneList);
List sourceTwoList = sourceTwoMapper.findAll();
list.addAll(sourceTwoList);
return list;
}
@Override
public List findSourceOne() {
return sourceOneMapper.findAll();
}
@Override
public List findSourceTwo() {
return sourceTwoMapper.findAll();
}
}
8.controller
import com.qq.entity.User;
import com.qq.service.SourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class SourceController {
@Autowired
private SourceService sourceService;
@GetMapping("/sourceAll")
public List findAll(){
return sourceService.findAll();
}
@GetMapping("/sourceOne")
public List findSourceOne(){
return sourceService.findSourceOne();
}
@GetMapping("/sourceTwo")
public List findSourceTwo(){
return sourceService.findSourceTwo();
}
}
9.执行启动类,访问接口



