背景: 因项目需求,需使用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);
}
}