0
点赞
收藏
分享

微信扫一扫

springboot+mybatis Plus纯注解实现多数据源连接

写心之所想 2023-07-26 阅读 61

mybatisplus官网

看了七八篇文章,也走了误区,好多用的配置文件配置,然而就因为没公布文件导致我这个菜鸟的失败。。。。

在此进行纯注解实现

我的springboot版本2.7.4

1、导入坐标

检查好这几个必须的,我就是少了mysql。。。

<!--  mybatisPlus-->
         <dependency>
             <groupId>com.baomidou</groupId>
             <artifactId>mybatis-plus-boot-starter</artifactId>
             <version>3.4.2</version>
         </dependency>
         <!--mybatisPlus官方推荐的多数据源 -->
         <dependency>
             <groupId>com.baomidou</groupId>
             <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
             <version>2.5.6</version>
         </dependency>
         <!--阿里的数据库连接池-->
         <dependency>
             <groupId>com.alibaba</groupId>
             <artifactId>druid-spring-boot-starter</artifactId>
             <version>1.1.20</version>
         </dependency>
         <!--mysql-->
         <dependency>
             <groupId>mysql</groupId>
             <artifactId>mysql-connector-java</artifactId>
             <scope>runtime</scope>
         </dependency>

2、配置文件

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

3、在非默认的数据库引用时加上注解

据说mapper和serviceImpl上不能同时加注解

package com.example.selfinspectiontools.service.impl;
  
 import com.baomidou.dynamic.datasource.annotation.DS;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.example.selfinspectiontools.mapper.SysRoleMapper;
 import com.example.selfinspectiontools.pojo.SysRole;
 import com.example.selfinspectiontools.service.SysRoleService;
 import org.springframework.stereotype.Service;
  
 /**
  * @author zd
  * @create 2023-01-11 10:47
  */
 @Service
 @DS("db2")
 public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {
 }

package com.example.selfinspectiontools.mapper;
  
 import com.baomidou.dynamic.datasource.annotation.DS;
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import com.example.selfinspectiontools.pojo.SysRole;
 import org.apache.ibatis.annotations.Mapper;
  
 /**
  * @author zd
  * @create 2023-01-11 10:48
  */
 @Mapper
 @DS("db2")
 public interface SysRoleMapper extends BaseMapper<SysRole> {
 }

实体类和表就不用发了,普普通通  目录结构也不用变

springboot+mybatis Plus纯注解实现多数据源连接_springboot

package com.example.selfinspectiontools;
  
 import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
  
 @SpringBootApplication(exclude = DruidDataSourceAutoConfigure.class)
 public class SelfInspectionToolsApplication {
  
     public static void main(String[] args) {
         SpringApplication.run(SelfInspectionToolsApplication.class, args);
     }
  
 }

5、controller查询

我的是springboot+thymeleaf,不要在意controller注解

package com.example.selfinspectiontools.controller;
  
  
 import com.example.selfinspectiontools.pojo.Student;
 import com.example.selfinspectiontools.pojo.SysRole;
 import com.example.selfinspectiontools.service.StudentService;
 import com.example.selfinspectiontools.service.SysRoleService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.GetMapping;
  
  
 import java.util.List;
  
 /**
  * @author zd
  * @create 2023-01-06 16:43
  */
 //@RestController //返回的是json数据
 @Controller
 public class TestController {
     @Autowired
     private SysRoleService sysRoleService;
     @Autowired
     private StudentService studentService;
  
     @GetMapping("/hello")
     public String myTest() {
         List<Student> list = studentService.list();
         System.out.println(list.size());
         List<SysRole> list1 = sysRoleService.list();
         System.out.println(list1.size());
         return "hello";
     }
  
  
 }

6、访问查询结果

这里就简单打印一下查到的长度,测试成功 

springboot+mybatis Plus纯注解实现多数据源连接_java_02

举报

相关推荐

0 条评论