将SpringBoot项目的数据库从MySQL迁移到达梦
将SpringBoot项目的数据库从MySQL迁移到达梦数据库
·
1.安装达梦数据库
1.加载达梦镜像
docker load -i dm8_20240715_x86_rh6_rq_single.tar
#2.创建挂载目录
mkdir E:\development_sources\docker\dm\data
#3.创建dm8容器
docker run -d -p 5236:5236 --restart=always --name dm8 --privileged=true -e CASE_SENSITIVE=0 -e UNICODE_FLAG=1 -e LENGTH_IN_CHAR=1 -e PAGE_SIZE=16 -e LD_LIBRARY_PATH=/opt/dmdbms/bin -e INSTANCE_NAME=dm8 -v E:\development_sources\docker\dm\data:/opt/dmdbms/data dm8_single:dm8_20240715_rev232765_x86_rh6_64
2.迁移数据表到达梦
(1).打开官方的数据迁移工具,创建迁移任务,达梦数据库的账号为SYSDBA,密码为SYSDBA001,操作界面如下图:
(2).设置数据源MySQL数据库的信息,如下图:
(3).设置目的达梦数据库的信息,如下图:
(4).配置获取对象的方式和迁移策略,如下图:
(5).选择模式,如下图:
(6).选择模式对象,如下图:
(7).迁移信息预览,如下图:
(8).完成迁移任务,查看迁移的结果,如下图:
(9).在达梦数据管理工具中查看表结构,如下图:
3.创建SpringBoot项目连接达梦实现简单CRUD
(1).在pom.xml文件中加上依赖,完整的pom文件如下,仅供参考
<?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.15</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.luozheng</groupId>
<artifactId>dm</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>dm</name>
<description>这是一个展示达梦数据库使用方式的简单demo,技术栈为达梦+SpringBoot+Mybatis-Plus+简单的CRUD</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 达梦驱动 -->
<dependency>
<groupId>com.dameng</groupId>
<artifactId>DmJdbcDriver18</artifactId>
<version>8.1.2.192</version>
</dependency>
<!--lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>
<!--mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.2</version>
</dependency>
<!--mybatis-plus-generator 生成器-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.31</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
(2).在项目的application.yml中配置达梦数据库和mybatis-plus,完成的yml文件如下
#配置达梦数据库
spring:
datasource:
driver-class-name: dm.jdbc.driver.DmDriver
url: jdbc:dm://localhost:5236
username: SYSDBA
password: SYSDBA001
mybatis-plus:
mapper-locations: classpath*:mappers/**/*Mapper.xml
global-config:
db-config:
update-strategy: NOT_NULL
server:
port: 8080
(3).使用MybatisPlus的代码生成器来生成entity,mapper,mappers,service,serviceImpl,controller,在包com.luozheng.dm.util下新建一个代码生成器启动类CodeGenerator.java,具体代码如下如下:
package com.luozheng.dm.util;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import java.nio.file.Paths;
import java.util.Collections;
public class CodeGenerator {
public static void main(String[] args) {
FastAutoGenerator.create("jdbc:dm://localhost:5236", "SYSDBA", "SYSDBA001")
.globalConfig(builder -> builder
.author("luozheng")
.outputDir(Paths.get(System.getProperty("user.dir")) + "/src/main/java")
.dateType(DateType.ONLY_DATE) // 设置时间类型策略
.commentDate("yyyy-MM-dd") // 设置注释日期格式
.disableOpenDir()
)
.packageConfig(builder -> builder
.parent("com.luozheng.dm")
.entity("entity")
.mapper("mapper")
.service("service")
.serviceImpl("service.impl")
// .xml("mapper.mappers")
.controller("controller")
.pathInfo(Collections.singletonMap(OutputFile.xml, "src/main/resources/mappers"))
)
.strategyConfig(builder -> builder
.addInclude("user")
.entityBuilder()
.columnNaming(NamingStrategy.underline_to_camel)
.enableLombok()
.mapperBuilder()
.enableBaseResultMap()
.enableBaseColumnList()
.serviceBuilder()
.formatServiceFileName("%sService")
.controllerBuilder()
.enableRestStyle()
)
.templateEngine(new FreemarkerTemplateEngine())
.execute();
}
}
(4).在service层,serviceImpl层和controller层添加代码实现简单的CRUD功能
package com.luozheng.dm.service;
import com.luozheng.dm.entity.User;
import com.baomidou.mybatisplus.extension.service.IService;
import java.util.List;
public interface UserService extends IService<User> {
public List<User> getAllUser();
public User getUserById(int id);
public void insertUser(User user);
public void updateUserById(User user);
public void deleteById(int id);
}
package com.luozheng.dm.service.impl;
import com.luozheng.dm.entity.User;
import com.luozheng.dm.mapper.UserMapper;
import com.luozheng.dm.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
@Resource
private UserMapper userMapper;
@Override
public List<User> getAllUser() {
return userMapper.selectList(null);
}
@Override
public void deleteById(int id) {
userMapper.deleteById(id);
}
@Override
public User getUserById(int id) {
return userMapper.selectById(id);
}
@Override
public void insertUser(User user) {
userMapper.insert(user);
}
@Override
public void updateUserById(User user) {
userMapper.updateById(user);
}
}
package com.luozheng.dm.controller;
import com.luozheng.dm.entity.User;
import com.luozheng.dm.service.UserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
@RestController
@RequestMapping("/user")
public class UserController {
@Resource
private UserService userService;
@GetMapping("/getAllUser")
public List<User> getAllUser() {
return userService.getAllUser();
}
@GetMapping("/getUserById")
public User getUserById(int id) {
return userService.getUserById(id);
}
@PostMapping("/insertUser")
public void insertUser(User user) {
userService.insertUser(user);
}
@PostMapping("/updateUserById")
public void updateUserById(User user) {
userService.updateUserById(user);
}
@PostMapping("/deleteById")
public void deleteById(int id) {
userService.deleteById(id);
}
}
(5).在启动类上面加上注解@MapperScan(“com.luozheng.dm.mapper”),并启动SpringBoot项目
4.使用apifox测试接口
(1).测试getAllUser接口
(2).测试getUserById接口
(3).测试insertUser 接口
(4).测试updateUserById接口
(5).测试deleteById接口
更多推荐
已为社区贡献2条内容
所有评论(0)