Maven builds Springboot+MyBatisPlus+Renda Jincang database

Foreword

I recently worked on a project, because of the need for localization, Party A requested to use the National People’s Congress Jincang database. I quickly checked that MyBatisPlus can be adapted, and I plan to build a simple Springboot + MyBatisPlus + Renmin University Jincang database environment for some simple verification.

Environment

Eclipse Oxygen

JAVA1.8

Springboot2.6.2

MyBatis3.5.2

National People’s Congress Jincang database kingbase8.6.0

Build environment

The download, installation and configuration are skipped here, except that the Jincang database is relatively unfamiliar, and several other java developers should be very familiar with it. It is not difficult to install the Jincang database. Download the installation package from the official website. If you are an individual developer, download the development version authorization file and select the authorization file during installation. There are still many restrictions on the personal development version. It seems that the maximum number of connections is limited to 1 (although the official website says 10). I use the database management tool to connect, and when I use the database migration tool to connect, the number of connections is exceeded.

Database preparation

We want to create a database in the Jincang database, create a table, and insert test data.

First create the database directly

Then create a table and execute the sql statement to create the users table. Because the user keyword conflicts, the table name uses users:

CREATE TABLE "public".users (
    id bigint AUTO_INCREMENT NOT NULL,
    name varchar(30) NOT NULL,
    age integer NOT NULL,
    email varchar(50) NOT NULL
);

ALTER TABLE "public".users SET
TABLESPACE "sys_default";

COMMENT ON
COLUMN "public".users.id IS 'primary key';

COMMENT ON
COLUMN "public".users.name IS 'Name';

COMMENT ON
COLUMN "public".users.age IS 'age';

COMMENT ON
COLUMN "public".users.email IS 'Email';

ALTER TABLE "public".users ADD CONSTRAINT con_public_users_constraint_1 PRIMARY KEY (id) ENABLE VALIDATE;

Insert data:

INSERT INTO users (id, name, age, email) VALUES
(1, 'Jone', 18, '[email protected]'),
(2, 'Jack', 20, '[email protected]'),
(3, 'Tom', 28, '[email protected]'),
(4, 'Sandy', 21, '[email protected]'),
(5, 'Billie', 24, '[email protected]');

Create a new maven project

Use Eclipse to create a new maven project.

The pom file is configured as follows:

 <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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.sadoshi</groupId>
    <artifactId>kingbase</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.2</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.2</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.20</version>
        </dependency>
        <dependency>
            <groupId>com.kingbase8</groupId>
            <artifactId>kingbase8</artifactId>
            <version>8.6.0</version>
        </dependency>

    </dependencies>

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

An error will be reported above, because the maven central warehouse does not have version 8.6.0 of kingbase8, and we need to make one in the local maven warehouse later.

Do some configuration on the project in application.ym, including database connection, etc.:

server:
  port: 9999

spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driverClassName: com.kingbase8.Driver
    url: jdbc:kingbase8://127.0.0.1:54321/mybatisplus_test
    username: system
    password: 12345678
    druid:
      stat-view-servlet:
        enabled: false

Note that driverClassName is replaced by the driver of Jincang database.

Install kingbase8 jar to local maven

Find kingbase8-8.6.0.jar in the Jincang database installation directory. We want to install it in the local maven, so that there will be no error in the pom file

Execute the command on the command line:

mvn install:install-file -DgroupId=com.kingbase8 -DartifactId=kingbase8 -Dversion=8.6.0 -Dpackaging=jar -Dfile=(jar package path of kingbase)\kingbase8-8.6.0.jar</pre >
 <p></p>
 <h3 style="">Write code</h3>
 <p>The previous preparations are done, and the next step is to write the business code.</p>
 <p>Create the main class App, add the @MapperScan annotation to scan the mapper in the specified directory:</p>
 <pre>package kingdb;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("kingdb.mapper")
public class App {

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
        
    }
}

Create a test control class HelloController, and visit the path “/getStudent” during the test to see if the console can print out the student list:

package kingdb. controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import kingdb.entity.User;
import kingdb.mapper.UserMapper;

@RestController
public class HelloController {

    @Autowired
    private UserMapper userMapper;
    
    @RequestMapping("/test")
    public String test() {
        return "test";
    }
    
    @RequestMapping("/getStudent")
    public String getStudent() {
        List<User> userList = userMapper. selectList(null);
        userList.forEach(System.out::println);
        return "getStudent";
    }
}

Create the entity class User, where the @TableName annotation is used to specify the database table name corresponding to the entity class:

package kingdb.entity;

import com.baomidou.mybatisplus.annotation.TableName;

import lombok.Data;

@Data
@TableName("users")
public class User {
    private Long id;
    private String name;
    private Integer age;
    private String email;
}

Create a Mapper class:

package kingdb.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import kingdb.entity.User;

public interface UserMapper extends BaseMapper<User>{

}

Test

Start the App class, and access http://localhost:9999/getStudent in the browser. Because I set the server port to 9999 in the previous application.yml, so the access port is this.

Then check the console:

Successfully printed student information. It proves that the verification of MyBatisPlus docking with the Jincang database is completed.

Summary

Some of the syntax of Jincang database is different from mysql. For example, the table creation statement of users, I generated ddl after creating the table through the database tool, and it is still somewhat different from the table creation statement of mysql in the MyBatisPlus official website example. If you want to use some mature frameworks as the base and are required to use the Jincang database, you can first build the database with mysql, and then use the database migration tool of the Jincang database to migrate there. For example, when I use the framework of jeecg, I first create a table in mysql, and then migrate it through tools. It is too troublesome to directly modify the table sql, and there are many differences in syntax with mysql.