1.安装人大金仓数据库

1.加载金仓镜像
docker load -i kdb_x86_64_V008R006C008B0014.tar

2.创建挂载目录
mkdir E:\development_sources\docker\kingbase\data

3.创建kingbase容器
docker run -d --privileged -p 54321:54321 -v E:\development_sources\docker\kingbase\data:/home/kingbase/userdata -e NEED_START=yes -e ENABLE_CI=yes -e DB_USER=kingbase -e DB_PASSWORD=123456 -e DB_MODE=mysql -e ENCODING=utf8 --name kingbase kingbase_v008r006c008b0014_single_x86:v1 /usr/sbin/init

2.迁移数据表到人大金仓

(1).打开人大金仓的官方数据迁移工具,在浏览器的地址栏输入http://localhost:54523,打开数据迁移的web操作界面。

(2).在数据源管理中创建源数据库和目标数据库,如下图所示:
在这里插入图片描述
在这里插入图片描述
(3).在迁移任务管理中创建迁移任务,并依次选择数据源,选择模式,选择迁移对象,配置参数,然后完成迁移任务创建,如下图所示:
在这里插入图片描述
在这里插入图片描述
(4).在迁移之前一定要将MySQL的主键自增取消掉,然后启动迁移任务,迁移成功之后再手动在金仓中给主键增加默认的自增序列,如下图所示:
在这里插入图片描述
在这里插入图片描述

3.创建SpringBoot项目连接金仓实现简单CRUD

(1).在pom.xml文件中加上依赖,pom文件仅供参考,完整的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>kingbase</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>kingbase</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>cn.com.kingbase</groupId>
           <artifactId>kingbase8</artifactId>
           <version>8.6.0</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: com.kingbase8.Driver
  url: jdbc:kingbase8://localhost:54321/kingbase
  username: kingbase
  password: 123456

mybatis-plus:
mapper-locations: classpath*:mappers/**/*Mapper.xml
global-config:
  db-config:
    update-strategy: NOT_NULL
    schema: public
server:
  port: 8080

(3).使用MybatisPlus的代码生成器来生成entity,mapper,mappers,service,serviceImpl,controller,在包com.luozheng.kingbase.util下新建一个代码生成器启动类CodeGenerator.java,具体代码如下如下:

package com.luozheng.kingbase.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:kingbase8://localhost:54321/kingbase", "kingbase", "123456")
              .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.kingbase")
                      .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.kingbase.service;

import com.luozheng.kingbase.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.kingbase.service.impl;

import com.luozheng.kingbase.entity.User;
import com.luozheng.kingbase.mapper.UserMapper;
import com.luozheng.kingbase.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.kingbase.controller;

import com.luozheng.kingbase.entity.User;
import com.luozheng.kingbase.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.kingbase.mapper”),并启动SpringBoot项目

4.使用apifox测试接口

(1).测试getAllUser接口

在这里插入图片描述

(2).测试getUserById接口

在这里插入图片描述

(3).测试insertUser 接口

在这里插入图片描述

(4).测试updateUserById接口

在这里插入图片描述

(5).测试deleteById接口

在这里插入图片描述

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐