当前位置: 首页 > news >正文

Springboot3多数据源案例

pom.xml配置

<?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.0http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.5.4</version><relativePath/></parent><groupId>com.zjw</groupId><artifactId>springboot3-undertow-demo</artifactId><version>1.0.0</version><name>springboot3-undertow-demo</name><properties><java.version>21</java.version><maven.compiler.source>21</maven.compiler.source><maven.compiler.target>21</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><exclusions><exclusion><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId></exclusion></exclusions></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-undertow</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>3.0.5</version></dependency><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-spring-boot3-starter</artifactId><version>3.5.12</version></dependency><dependency><groupId>com.baomidou</groupId><artifactId>dynamic-datasource-spring-boot3-starter</artifactId><version>4.3.1</version></dependency><dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId></dependency><dependency><groupId>org.postgresql</groupId><artifactId>postgresql</artifactId><version>42.7.7</version></dependency><dependency><groupId>com.oracle.database.jdbc</groupId><artifactId>ojdbc11</artifactId><version>23.9.0.25.07</version></dependency><dependency><groupId>org.xerial</groupId><artifactId>sqlite-jdbc</artifactId><version>3.50.3.0</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.2.27</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><excludes><exclude><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></exclude></excludes></configuration></plugin></plugins></build>
</project>

application.yaml配置

spring:application:name: springboot3-undertow-demodatasource:dynamic:primary: mysqlstrict: falsedatasource:mysql:type: com.zaxxer.hikari.HikariDataSourceurl: jdbc:mysql://127.0.0.1:3306/testusername: **********password: **********driver-class-name: com.mysql.cj.jdbc.Driverpostgres:type: com.alibaba.druid.pool.DruidDataSourceurl: jdbc:postgresql://127.0.0.1:5432/testusername: **********password: **********driver-class-name: org.postgresql.Driveroracle:type: com.alibaba.druid.pool.DruidDataSourceurl: jdbc:oracle:thin:@127.0.0.1:1521:ORCLusername: **********password: **********driver-class-name: oracle.jdbc.OracleDriversqlite:type: com.zaxxer.hikari.HikariDataSourcedruid:connect-timeout: 500min-idle: 20query-timeout: 100max-active: 50driver-class-name: org.sqlite.JDBCurl: jdbc:sqlite:my-sqlite.dbmain:banner-mode: offmybatis-plus:configuration:log-impl: org.apache.ibatis.logging.nologging.NoLoggingImplglobal-config:banner: falsemapper-locations: classpath:mapper/*.xml,classpath:mapper/*/*.xml,classpath:mapper/*/*/*.xml

Entity Java类

package com.zjw.entity.mysql;import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("tb_user")
public class UserEntity {private Integer id;private String name;private Integer age;private String gender;private String mate;private String profession;private String description;
}package com.zjw.entity.oracle;import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName(schema = "TEST", value = "DEPARTMENT")
public class Department {private String id;private String name;private String pid;
}package com.zjw.entity.postgresql;import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;import java.util.Date;@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName(schema = "public", value = "person_info")
public class PersonInfo {private String id;private String name;private int age;private String gender;private Date createTime;private Date updateTime;private int isDelete;
}package com.zjw.entity.sqlite;import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("user_info")
public class UserRecord {private Long id;private String name;private Integer age;private String gender;
}

Mapper

package com.zjw.mapper.mysql;import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zjw.entity.mysql.UserEntity;
import org.apache.ibatis.annotations.Mapper;@Mapper
public interface UserEntityMapper extends BaseMapper<UserEntity> {
}package com.zjw.mapper.oracle;import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zjw.entity.oracle.Department;
import org.apache.ibatis.annotations.Mapper;@Mapper
public interface DepartmentMapper extends BaseMapper<Department> {
}package com.zjw.mapper.postgresql;import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zjw.entity.postgresql.PersonInfo;
import org.apache.ibatis.annotations.Mapper;@Mapper
public interface PersonInfoMapper extends BaseMapper<PersonInfo> {
}
package com.zjw.mapper.sqlite;import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zjw.entity.sqlite.UserRecord;
import org.apache.ibatis.annotations.Mapper;@Mapper
@DS("sqlite")
public interface UserRecordMapper extends BaseMapper<UserRecord> {
}

Service

package com.zjw.service.mysql;import com.baomidou.mybatisplus.extension.service.IService;
import com.zjw.entity.mysql.UserEntity;public interface UserEntityService extends IService<UserEntity> {}
package com.zjw.service.mysql.impl;import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zjw.entity.mysql.UserEntity;
import com.zjw.mapper.mysql.UserEntityMapper;
import com.zjw.service.mysql.UserEntityService;
import org.springframework.stereotype.Service;@DS("mysql")
@Service("userService")
public class UserEntityServiceImpl extends ServiceImpl<UserEntityMapper, UserEntity> implements UserEntityService {
}
package com.zjw.service.oracle;import com.baomidou.mybatisplus.extension.service.IService;
import com.zjw.entity.oracle.Department;public interface DepartmentService extends IService<Department> {}
package com.zjw.service.oracle.impl;import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zjw.entity.mysql.UserEntity;
import com.zjw.entity.oracle.Department;
import com.zjw.mapper.mysql.UserEntityMapper;
import com.zjw.mapper.oracle.DepartmentMapper;
import com.zjw.service.mysql.UserEntityService;
import com.zjw.service.oracle.DepartmentService;
import org.springframework.stereotype.Service;@DS("oracle")
@Service("departmentService")
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService {
}
package com.zjw.service.postgresql;import com.baomidou.mybatisplus.extension.service.IService;
import com.zjw.entity.postgresql.PersonInfo;public interface PersonInfoService extends IService<PersonInfo> {}
package com.zjw.service.postgresql.impl;import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zjw.entity.postgresql.PersonInfo;
import com.zjw.mapper.postgresql.PersonInfoMapper;
import com.zjw.service.postgresql.PersonInfoService;
import org.springframework.stereotype.Service;@DS("postgres")
@Service("personInfoService")
public class PersonInfoServiceImpl extends ServiceImpl<PersonInfoMapper, PersonInfo> implements PersonInfoService {
}
package com.zjw.service.sqlite;import com.baomidou.mybatisplus.extension.service.IService;
import com.zjw.entity.sqlite.UserRecord;public interface UserRecordService extends IService<UserRecord> {}
package com.zjw.service.sqlite.impl;import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zjw.entity.sqlite.UserRecord;
import com.zjw.mapper.sqlite.UserRecordMapper;
import com.zjw.service.sqlite.UserRecordService;
import org.springframework.stereotype.Service;@Service
public class UserRecordServiceImpl extends ServiceImpl<UserRecordMapper, UserRecord> implements UserRecordService {}

Controller

package com.zjw.controller;import com.zjw.service.mysql.UserEntityService;
import com.zjw.service.oracle.DepartmentService;
import com.zjw.service.postgresql.PersonInfoService;
import com.zjw.service.sqlite.UserRecordService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.HashMap;
import java.util.Map;@RestController
@RequestMapping("/hello")
public class HelloController {@Resourceprivate UserEntityService userService;@Resourceprivate PersonInfoService personInfoService;@Resourceprivate DepartmentService departmentService;@Resourceprivate UserRecordService userRecordService;@GetMapping("/")public String hello(String name) {return "Hello " + name + ".";}@GetMapping("/users")public Map<String, Object> ret() {Map<String, Object> map = new HashMap<>();map.put("mysql_users", userService.list());map.put("postgresql_persons", personInfoService.list());map.put("oracle_departments", departmentService.list());map.put("sqlite_users", userRecordService.list());return map;}
}

启动类

package com.zjw;import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
@MapperScan("com.zjw.mapper")
public class SpringbootUndertowDemoApplication {public static void main(String[] args) {SpringApplication.run(SpringbootUndertowDemoApplication.class, args);}
}

Sqlite SQL

CREATE TABLE user_info(id int  AUTO_INCREMENT PRIMARY KEY,name varchar(64) NOT NULL,age int,gender varchar(6)
);INSERT INTO user_info (`name`, `age`, `gender`) VALUES
(1, '闻人天', 31, '男'),
(2, '公冶平', 23, '男');
http://www.lryc.cn/news/618556.html

相关文章:

  • Springboot注册过滤器的三种方式(Order 排序)
  • 亚马逊后台功能风险解构:“清除并替换库存” 的致命陷阱与全链路防控策略
  • 第五章 特征值与特征向量
  • Wireshark专家模式定位网络故障:14种TCP异常深度解剖
  • 【Altium designer】快速建立原理图工程的步骤
  • 深度学习-卷积神经网络-NIN
  • Nginx反向代理功能
  • 【实时Linux实战系列】复杂实时系统中的需求管理
  • 【无标题】centos 配置阿里云的yum源
  • CS2服务器是何方神圣
  • linux 执行ls命令文件夹显示全白色
  • C++——高性能组件
  • 深度学习与遥感入门(六)|轻量化 MobileNetV2 高光谱分类
  • Python 标准库模块shutil
  • 《算法导论》第 20 章 - van Emde Boas 树
  • 电商双11美妆数据分析(一)
  • 多语言与隐形攻击:LLM安全防线为何被频频突破?
  • 【k8s】pvc 配置的两种方式volumeClaimTemplates 和 PersistentVolumeClaim
  • 腾讯云iOA:全面提升企业办公安全与效率的智能解决方案
  • 可泛化逻辑推理Python编程作为医疗AI发展方向研究
  • OpenBMC中C++单例模式架构与实现全解析
  • 【自动化备份全网服务器数据项目】
  • 虚拟机环境部署Ceph集群的详细指南
  • 雪花算法snowflake分布式id生成原理详解,以及对解决时钟回拨问题几种方案讨论
  • Docker守护进程安全加固在香港VPS环境的操作标准
  • 【读代码】深度解析 Researcher:开源自动化科研助手
  • 【前端实战】如何封装日期格式化工具,满足后端 LocalDate 和 LocalDateTime 格式需求
  • Ubuntu 全盘备份
  • Day 38: Dataset类和DataLoader类
  • Labelme从安装到标注:零基础完整指南