栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot配置多数据源

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot配置多数据源

1.多数据源

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,'白素贞');

2.maven依赖

    spring-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-web
    
    
        io.easybest
        spring-data-mybatis-boot-starter
        2.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
        lombok
        true
    
3.application.yml
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

# 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")
    List findAll();
}

#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")
    List findAll();
}
7.service
#自己写个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.执行启动类,访问接口

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/459471.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号