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);
    }

}
相关推荐
路在脚下@20 分钟前
spring boot的配置文件属性注入到类的静态属性
java·spring boot·sql
啦啦右一22 分钟前
Spring Boot | (一)Spring开发环境构建
spring boot·后端·spring
森屿Serien23 分钟前
Spring Boot常用注解
java·spring boot·后端
苹果醋31 小时前
React源码02 - 基础知识 React API 一览
java·运维·spring boot·mysql·nginx
Hello.Reader2 小时前
深入解析 Apache APISIX
java·apache
菠萝蚊鸭2 小时前
Dhatim FastExcel 读写 Excel 文件
java·excel·fastexcel
旭东怪2 小时前
EasyPoi 使用$fe:模板语法生成Word动态行
java·前端·word
007php0072 小时前
Go语言zero项目部署后启动失败问题分析与解决
java·服务器·网络·python·golang·php·ai编程
∝请叫*我简单先生2 小时前
java如何使用poi-tl在word模板里渲染多张图片
java·后端·poi-tl
别致的影分身3 小时前
使用C语言连接MySQL
数据库·mysql