Springboot+mybatisplus+mysql配置多数据源(注解版)

1、添加依赖,最关键的两个依赖是后面两个"druid依赖"和"配置动态数据源"(已标红),其他"非主要"依赖可按自身实际开发环境进行选择。

        <!--springboot-jdbc-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <!--springboot-web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--Mybatis-Plus-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.1.1</version>
        </dependency>
        <!--Mybatis-Plus逆向工程-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.1.1</version>
        </dependency>

        <!--MySQL-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!--springboot-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!--thymeleaf-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <dependency>
            <groupId>net.sourceforge.nekohtml</groupId>
            <artifactId>nekohtml</artifactId>
        </dependency>
        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.8</version>
        </dependency>

        <!--freemaker-->
        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.28</version>
        </dependency>
        <!--pagehelper-->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.5</version>
            <!--如果有配置mybatisPlus,需要以下配置-->
            <exclusions>
                <exclusion>
                    <groupId>org.mybatis</groupId>
                    <artifactId>mybatis</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--hutool工具集-->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.0.7</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.4</version>
        </dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/druid --> <!-- 引入druid依赖 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.20</version> </dependency> <!--配置动态数据源--> <dependency> <groupId>com.baomidou</groupId> <artifactId>dynamic-datasource-spring-boot-starter</artifactId> <version>2.5.6</version> </dependency>

2、添加数据源,在application.yml添加如下配置,其中primary: demo表示默认数据源为demo,即在不添加注解指定的情况下数据源为demo

spring:
datasource:
dynamic:
primary: demo # 配置默认数据库
datasource:
demo: # 数据源1配置
url: jdbc:mysql://localhost:3306/demo?characterEncoding=utf8&useUnicode=true&useSSL=false&serverTimezone=GMT%2B8
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
test: # 数据源2配置
url: jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useUnicode=true&useSSL=false&serverTimezone=GMT%2B8
username: root
password: 123456
driver-class-name: com.mysql.cj.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配置

3、启动类添加注解

在@SpringBootApplication中添加exclude = DruidDataSourceAutoConfigure.class,可取消系统自动配置数据源,这很关键,不要忘记!否则会报寻找不到"xxx" bean的错误
@MapperScan("com.xlfdy.test.mapper")
@SpringBootApplication(exclude = DruidDataSourceAutoConfigure.class)
@EnableCaching
@EnableScheduling
public class TestApplication {

    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
    }

}

4、功能测试

在需要切换指定数据源的方法或类上添加@DS("xxx")注解,"xxx"表示要切换的数据源名称,当方法和类上都有该注解时,以方法上的注解为准,service和mapper、controller中都可配置该注解。
@Service
public class TblUserServiceImpl extends ServiceImpl<TblUserMapper, TblUser> implements ITblUserService {

    @Override
    @DS("test")
    public TblUser getAllUser(Integer id) {
        QueryWrapper<TblUser> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        TblUser user =  baseMapper.selectOne(wrapper);
        return user;
    }
}

5、测试结果

表结构如下

 

 

 

 

 

 指定数据源调用成功!

posted @ 2020-12-02 15:28  xlfdy  阅读(298)  评论(0)    收藏  举报