SpringBoot配置Mysql+达梦双数据源

背景: 因项目需求,需使用Mysql+达梦双数据源

涉及技术栈: SpringBoot、Druid、Mybatis、Mysql、达梦

一、引入pom依赖:

bash 复制代码
<?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.0 https://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>2.7.18</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo-mysql</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo-mysql</name>
    <description>demo-mysql</description>
    <properties>
        <java.version>8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.dameng</groupId>
            <artifactId>DmJdbcDriver18</artifactId>
            <version>8.1.2.192</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
            <version>4.3.0</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.5</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.20</version>
        </dependency>
        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>RELEASE</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

二、application.yml配置文件

此配置是达梦做主数据库,mysql为第二数据库

bash 复制代码
spring:
  application:
    name: demo-mysql
  datasource:
    dynamic:
      primary: first
      datasource:
        first:
          driver-class-name: dm.jdbc.driver.DmDriver
          type: com.alibaba.druid.pool.DruidDataSource
          url: jdbc:dm://127.0.0.1:5236?schema=DEMO
          username: username
          password: password
        second:
          driver-class-name: com.mysql.cj.jdbc.Driver
          type: com.alibaba.druid.pool.DruidDataSource
          url: jdbc:mysql://127.0.0.1:3306/demo?useUnicode=true&characterEncoding=utf-8&useSSL=false
          username: username
          password: password

三、数据库建表

达梦建表

bash 复制代码
CREATE TABLE "DEMO"."USER"
(
"id" CHAR(10) NOT NULL,
"username" CHAR(10),
"password" CHAR(10),
NOT CLUSTER PRIMARY KEY("id")) STORAGE(ON "MAIN", CLUSTERBTR) ;

mysql建表

bash 复制代码
create table goods
(
    id   varchar(255) not null
        primary key,
    name varchar(255) null,
    type varchar(255) null
);

四、创建实体类

bash 复制代码
@Data
@ToString
@TableName("USER")
public class User implements Serializable {
    private String id;
    private String username;
    private String password;
}
bash 复制代码
@Data
@ToString
@TableName("goods")
public class Goods implements Serializable {
    private String id;
    private String name;
    private String type;
}

五、创建mapper

bash 复制代码
@Mapper
public interface UserMapper extends BaseMapper<User> {
    @Select("select * from \"DEMO1\".\"USER\"")
    List<User> selectAll();
}
bash 复制代码
@DS("second")
@Mapper
public interface GoodsMapper extends BaseMapper<Goods> {

    @Select("select * from goods")
    List<Goods> selectAll();
}

注意:mapper类上添加注解@DS("second")表示使用第二数据库

六、创建service

bash 复制代码
@Service
public class UserService {
    @Autowired
    UserMapper userMapper;
    public List<User> find(){
        List<User>  list = userMapper.selectAll();
        return list;
    }
}
bash 复制代码
@Service
public class GoodsService {
    @Autowired
    GoodsMapper goodsMapper;


    public List<Goods> find(){
        List<Goods> list = goodsMapper.selectAll();
        return list;
    }
}

七、启动类添加注解

bash 复制代码
@MapperScan("com.example.demomysql.mapper")

八、测试

bash 复制代码
@SpringBootTest
class DemoMysqlApplicationTests {

    @Autowired
    GoodsService goodsService;
    @Autowired
    UserService userService;
    @Test
    void test() {
        List<Goods> goods = goodsService.find();
        List<User> users = userService.find();
        System.out.println(goods);
        System.out.println(users);
    }

}
相关推荐
慧都小妮子1 分钟前
Spire.PDF for .NET【页面设置】演示:打开 PDF 时自动显示书签或缩略图
java·pdf·.net
m51275 分钟前
LinuxC语言
java·服务器·前端
IU宝9 分钟前
C/C++内存管理
java·c语言·c++
瓜牛_gn10 分钟前
依赖注入注解
java·后端·spring
hakesashou11 分钟前
Python中常用的函数介绍
java·网络·python
打鱼又晒网16 分钟前
【MySQL】数据库精细化讲解:内置函数知识穿透与深度学习解析
数据库·mysql
佚先森20 分钟前
2024ARM网络验证 支持一键云注入引流弹窗注册机 一键脱壳APP加固搭建程序源码及教程
java·html
古月居GYH34 分钟前
在C++上实现反射用法
java·开发语言·c++