[SpringBoot Application] SpringBoot+MybatisPlus integrates the domestic DM8 (Dameng) database

Introduction and Installation

DM8 is a new generation of high-performance database product launched by Dameng Database Co., Ltd. It has an open and scalable architecture, an easy-to-use transaction processing system, and low maintenance costs. It is a completely self-developed product of Dameng. DM8 takes RDBMS as the core and SQL as the standard. It is an efficient and stable general-purpose database management system that can span multiple software and hardware platforms and has comprehensive management capabilities for large-scale data.

Connect to Dameng database instance installation document

Connection tool, installed Dameng database already comes with a client visualization tool
insert image description here

insert image description here

Introduction to Basic Concepts

Here is a brief introduction to the following simple but important concepts: database, instance, user, table space, schema, table, role, and data file.

Database : The physical medium on which data is stored. In a broad sense, any physical medium that stores data can be called a database, such as a word document. But in fact, when we say database, we generally refer to software systems like MySQL, ORACLE, HBASE, etc. In addition to providing data storage, these systems also provide a set of related tools or interfaces to manage the stored data. Dameng database is such a system.

Database instance : A warehouse that has been instantiated to store data. There is a big difference between the Dameng database and the MySQL database: after the MySQL service is started, N databases (database instances) are created through the create database statement; while one instance of the Dameng database can only be connected to one database (that is, an instance is a database ), if you want to build multiple databases, you need to configure and start the corresponding number of instances. Each instance of Dameng has a series of background processes and memory structures, which are independent of each other.

User : Dameng database users are built under the instance, because the instances are independent of each other, all different instances of Dameng database can have the same user name. In addition, when a user is created through the create user statement in the Dameng database, a "schema" with the same name will be created at the same time. You can also create a schema separately through the create schema statement (special attention: in Oracle, users and schemas are in one-to-one correspondence, and schemas cannot be created independently. To create a schema, you need to create a user with the same name), and authorize it to a certain There are existing users, so there is a 1:N relationship between users and schemas in Dameng database. (Note: If you first create a schema through create schema, and then create a user with the same name through create user, the creation cannot be successful.)

Table space : It is a concept used to manage data storage. The table space is only related to the data file (ORA or DBF file). The data file is physical. A table space can contain multiple data files, while a data file can only Belongs to a tablespace. A table space can be understood as corresponding to a physical storage area dedicated to storing data files. When instantiating Dameng database, five table spaces MAIN, ROLL, SYSTEM, TEAM and HMAIN will be created by default. The ROLL, SYSTEM, and TEAM table spaces are maintained by the system itself, and tables created by users (if no table space is specified) are placed in the MAIN table space by default. Users can also customize the table space, and then specify it as the default table space when creating a user; or dynamically specify the storage table space of the table by adding the "tablespace SpaceName" statement when creating a table.

Mode (schema) : If you have only used MySQL and have no experience in using ORACLE database, this concept is difficult to understand.

There are a few things to keep in mind about patterns:

  • A user generally corresponds to a schema, and the user's schema name is equal to the user name, and is used as the user's default schema;

  • A user can also use other schemas (Oracle database can also access other schemas through permission management);

  • When the creation mode does not specify a user, the mode is owned by SYSDBA by default;

  • Objects with the same name cannot exist in the same schema, but objects in different schemas can have the same name;

  • Users can directly access schema objects with the same name, but if they want to access other schema objects, they must have object permissions;

  • When users want to access other schema objects, they must append the schema name as a suffix (schema.table);

  • Users are used to connect to database objects, while schemas are used to create and manage objects.

Table : Needless to say, but one point to be explained is that a table can only belong to one table space.

Role: In Dameng database, each user has a role. It determines what authority the user has, such as DBA, has the highest authority. A supplementary note is that the instantiated Dameng database has 3 roles by default: DBA, PUBLIC, RESOURCE. The newly created user only has the PUBLIC role, which is almost useless. Therefore, after creating a new user, he (she) needs to be authorized separately.

Data file : the physical carrier of data.

Here is an analogy to help understand:

"We can think of Database as a large warehouse. The warehouse is divided into many rooms. Schema is one of the rooms. A Schema represents a room. Table can be regarded as a bed in each Schema. Table (bed) is placed In each room, it cannot be placed outside the room. Wouldn’t it be homeless at night, and then many items can be placed on the bed, just like many columns and rows can be placed on the Table, the basic unit of storing data in the database It is a Table. In reality, the basic unit for placing items in each warehouse is the bed, and the User is the owner of each Schema (so the Schema contains Objects, not Users). There is a one-to-one correspondence between users and schemas, and each user is in If there is no special designation, you can only use your own schema (room). If a user wants to use other schema (room), it depends on whether the user (owner) of that schema (room) has given you this permission. Or see if the boss (DBA) of this warehouse has given you this permission. In other words, if you are the owner of a certain warehouse, then the right to use the warehouse and everything in the warehouse is yours (including the room) , you have complete operation rights, you can throw away unused things from each room, and you can also place some useful things in a certain room. You can also assign specific permissions to each User, that is, he can go to a certain room What can be done, whether it can only be read (Read-Only), or can have all the control rights (R/W) like the master, this depends on the role corresponding to the User." — Excerpted from the Internet

SpringBoot+MP integrates DM8

The test uses the one created for us by initializing the Dameng database instance schema:PERSON,表:ADDRESS
insert image description here

CREATE TABLE "PERSON"."ADDRESS"
(
"ADDRESSID" INT IDENTITY(1, 1) NOT NULL,
"ADDRESS1" VARCHAR(60) NOT NULL,
"ADDRESS2" VARCHAR(60),
"CITY" VARCHAR(30) NOT NULL,
"POSTALCODE" VARCHAR(15) NOT NULL,
NOT CLUSTER PRIMARY KEY("ADDRESSID")) STORAGE(ON "BOOKSHOP", CLUSTERBTR) ;

pom

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

<dependencies>
      <!-- 达梦数据库依赖 -->
      <dependency>
          <groupId>com.dameng</groupId>
          <artifactId>DmJdbcDriver18</artifactId>
          <version>8.1.1.193</version>
      </dependency>

      <!-- Mybatis-Plus依赖 -->
      <dependency>
          <groupId>com.baomidou</groupId>
          <artifactId>mybatis-plus-boot-starter</artifactId>
          <version>3.1.0</version>
      </dependency>

      <dependency>
          <groupId>org.apache.commons</groupId>
          <artifactId>commons-lang3</artifactId>
      </dependency>

      <dependency>
          <groupId>org.projectlombok</groupId>
          <artifactId>lombok</artifactId>
          <optional>true</optional>
      </dependency>

      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-test</artifactId>
      </dependency>
  </dependencies>

yml

spring:
  datasource:
    driver-class-name: dm.jdbc.driver.DmDriver
    # 达梦 模式名就相当于库名
    url: jdbc:dm://localhost:5236?schema=PERSON&compatibleMode=mysql&characterEncoding=UTF-8&useUnicode=true&useSSL=false&tinyInt1isBit=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai
    #写法二
    #url: jdbc:dm://localhost:5236/PERSON&compatibleMode=mysql&characterEncoding=UTF-8&useUnicode=true&useSSL=false&tinyInt1isBit=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai
    username: SYSDBA
    password: 123456789
mybatis-plus:
  configuration:
    # 驼峰转换 从数据库列名到Java属性驼峰命名的类似映射
    map-underscore-to-camel-case: false
    # 是否开启缓存
    cache-enable: false
    # 如果查询结果中包含空值的列,则 MyBatis 在映射的时候,不会映射这个字段
    #call-setters-on-nulls: true
    # 打印sql
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

Address

@Data
@TableName(value = "PERSON.ADDRESS")
public class Address {
    
    

    @TableId(value = "ADDRESSID", type = IdType.AUTO)
    private Integer addressId;

    @TableField("ADDRESS1")
    private String address1;

    @TableField("ADDRESS2")
    private String address2;

    @TableField("CITY")
    private String city;

    @TableField("POSTALCODE")
    private String postaLoCode;
}

AddressMapper

public interface AddressMapper extends BaseMapper<Address> {
    
    
}

startup class

@MapperScan(basePackages = "cn.zysheep.mapper")
@SpringBootApplication
public class DmApplication {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(DmApplication.class, args);
    }
}

test class

@SpringBootTest(classes = DmApplication.class)
public class AddressMapperTest{
    
    

    @Autowired
    DataSource dataSource;

    @Autowired(required = false)
    AddressMapper addressMapper;

    @Test
    public void test() throws SQLException {
    
    
        System.out.println(dataSource.getConnection());
    }
}

increase

@Test
public void testSave() {
    
    
    Address address = new Address();
    address.setAddress1("长沙");
    address.setCity("长沙市天心区");
    address.setPostaLoCode("430001");
    addressMapper.insert(address);
}

insert image description here

delete

@Test
public void testDeleteById() {
    
    
    addressMapper.deleteById("17");
}

@Test
public void testDelete() {
    
    
    Address address = new Address();
    address.setAddressId(14);
    address.setCity("长沙");
    detele(address);
}

public void detele(Address address) {
    
    

    //  eq(boolean condition, R column, Object val)
    //  第一个参数条件满足才拼接条件
    LambdaQueryWrapper<Address> queryWrapper = Wrappers.<Address>lambdaQuery()
            .eq(Objects.nonNull(address.getAddressId()), Address::getAddressId, address.getAddressId())
            .eq(StringUtils.isNotBlank(address.getCity()), Address::getCity, address.getCity());


    addressMapper.delete(queryWrapper);
}

insert image description here

change

@Test
public void testUpdateById() {
    
    
    Address address = new Address();
    address.setAddressId(17);
    address.setCity("长沙市芙蓉区");
    address.setPostaLoCode("430002");
    addressMapper.updateById(address);
}

@Test
public void testUpdate() {
    
    
    LambdaUpdateWrapper<Address> wrapper = Wrappers.<Address>lambdaUpdate()
            .set(Address::getCity, "长沙市岳麓区")
            .set(Address::getPostaLoCode,"430001")
            .eq(Address::getAddressId,"1");

    addressMapper.update(null,wrapper);
}

insert image description here

check

@Test
public void testSelect() {
    
    
    List<Address> selectList = addressMapper.selectList(Wrappers.emptyWrapper());
    System.out.println(selectList);
}

insert image description here

yml configuration and entity class @TableName table mapping problem

Writing method 1: application.yml

# 写法一
spring:
  datasource:
   url: jdbc:dm://localhost:5236?schema=PERSON

Entity class, @TableNameschema can not be specified

@Data
@TableName(value = "ADDRESS")
public class Address {
    
    
	
}

Writing method 2: application.yml

# 写法二
spring:
  datasource:
   url: jdbc:dm://localhost:5236/PERSON

Entity class, @TableNameschema must be specified, otherwise the table will not be found

@Data
@TableName(value = "PERSON.ADDRESS")
public class Address {
    
    
	
}

insert image description here

Regardless of the method used for yml configuration in actual development, it is strongly recommended @TableName(value = "PERSON.ADDRESS")to specify the schema.

Guess you like

Origin blog.csdn.net/qq_45297578/article/details/131907240