MyBatis-Plus and Dameng database achieve efficient data persistence

 

1. Add dependencies

pom.xmlFirst, we need to add the dependencies of MyBatis-Plus and Dameng database to the project file:

    <dependencies>
        <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>
        </dependency>
        <!--  添加dm8 jdbc jar 包依赖-->
        <dependency>
            <groupId>com.dm</groupId>
            <artifactId>DmJdbcDriver</artifactId>
            <version>1.8.0</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

Two, configure the data source

Configure the connection information of Dameng database in the Spring Boot configuration file application.propertiesor :application.yml

spring:
  datasource:
    url: jdbc:dm://localhost:5236
    username: 账号
    password: 密码
    driver-class-name: dm.jdbc.driver.DmDriver

 You can then use MyBatisX to generate the following code

3. Create entity classes and Mapper interfaces

Create an entity class corresponding to the database table, and use MyBatis-Plus annotations to annotate information such as the primary key and table name:

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @TableName student
 */
@TableName(value = "lps.student")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student implements Serializable {
    /**
     * 
     */
    @TableId
    private String id;

    /**
     * 
     */
    private String name;

    /**
     * 
     */
    private Integer age;
    
}

Next, create BaseMappera Mapper interface that inherits from :

import com.lps.domain.Student;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;

/**
* @author 19449
* @description 针对表【student】的数据库操作Mapper
* @createDate 2023-08-01 16:10:31
* @Entity com.lps.domain.Student
*/
@Mapper
public interface StudentMapper extends BaseMapper<Student> {

}

Complete mapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.lps.mapper.StudentMapper">

    <resultMap id="BaseResultMap" type="com.lps.domain.Student">
            <id property="id" column="id" jdbcType="VARCHAR"/>
            <result property="name" column="name" jdbcType="VARCHAR"/>
            <result property="age" column="age" jdbcType="OTHER"/>
    </resultMap>

    <sql id="Base_Column_List">
        id,name,age
    </sql>
</mapper>

4. Create the Service layer

Create a Service interface and implementation class, inherited from IServiceand ServiceImpl:

import com.lps.domain.Student;
import com.baomidou.mybatisplus.extension.service.IService;

import java.util.List;

/**
 * @author 19449
 * @description 针对表【student】的数据库操作Service
 * @createDate 2023-08-01 16:10:31
 */
public interface StudentService extends IService<Student> {

    List<Student> selectAll();

    void insert(Student student);

    void deleteBatch(List<Student> studentList);
    
    void deleteAll();

}

service implementation class

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lps.domain.Student;
import com.lps.service.StudentService;
import com.lps.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 19449
 * @description 针对表【student】的数据库操作Service实现
 * @createDate 2023-08-01 16:10:31
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student>
        implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

    @Override
    public List<Student> selectAll() {
        return studentMapper.selectList(null);
    }

    @Override
    public void insert(Student student) {
        studentMapper.insert(student);
    }

    @Override
    public void deleteBatch(List<Student> studentList) {
        studentMapper.deleteBatchIds(studentList.stream().map(students -> students.getId()).collect(Collectors.toList()));
    }

    @Override
    public void deleteAll() {
        studentMapper.delete(null);
    }
    
}

5. Perform CRUD operations

Now you can use in business logic to YourServiceadd, delete, modify and check:

package com.lps;

import com.lps.domain.Student;
import com.lps.service.StudentService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.List;

@SpringBootTest
class SpringBootDm7ApplicationTests {
    @Autowired
    StudentService studentService;

    /**
     * 查询所有
     */
    @Test
    void contextLoadSelectAll() {
        List<Student> students = studentService.selectAll();
        for (Student student : students) {
            System.out.println(student);
        }
    }
    /**
     * 单条插入
     */
    @Test
    void contextLoadsInsert() {
        Student student = new Student("666","刘品水",18);
        studentService.insert(student);
    }

    /**
     * 循环里做插入 主打就是挨训
     */
    @Test
    void contextLoadsInsert2() {
        //还有优化空间 下篇博客见
        List<Student> studentList=new ArrayList<>();

        for (int i = 1; i <= 100000; i++) {
            Student student1 = new Student(i+"","刘品水"+i,1+i);
            studentList.add(student1);
        }
        System.out.println(studentList.size());
        long beginTime = System.currentTimeMillis();
        for (Student student : studentList) {
            studentService.insert(student);
        }
        long endTime = System.currentTimeMillis();
        long spendTime = endTime - beginTime;
        System.out.println("用时:"+spendTime+"毫秒");
    }
    /**
     * 批量插入
     */
    @Test
    void contextLoadsSaveBatch() {
        //还有优化空间 下篇博客见
        List<Student> studentList=new ArrayList<>();

        for (int i = 1; i <= 1000000; i++) {
            Student student1 = new Student(i+"","刘品水"+i,1+i);
            studentList.add(student1);
        }
        System.out.println(studentList.size());
        long beginTime = System.currentTimeMillis();
        studentService.saveBatch(studentList,1000000);
        long endTime = System.currentTimeMillis();
        long spendTime = endTime - beginTime;
        System.out.println("用时:"+spendTime+"毫秒");
    }
    /**
     * 批量保存或者批量更新
     */
    @Test
    void contextLoadSaveOrUpdateBatch() {
        List<Student> studentList=new ArrayList<>();
        Student student1 = new Student("668","吴彦祖",18);
        Student student2 = new Student("669","彭于晏",18);
        Student student3 = new Student("670","霍建华",18);
        studentList.add(student1);
        studentList.add(student2);
        studentList.add(student3);
        studentService.saveOrUpdateBatch(studentList);
    }
    /**
     * 批量删除
     */
    @Test
    void contextLoadDeleteBatch() {
        List<Student> studentList=new ArrayList<>();
        Student student1 = new Student("123456","刘品水",18);
        Student student2 = new Student("654321","刘品水",18);
        Student student3 = new Student("77777","刘品水",18);
        studentList.add(student1);
        studentList.add(student2);
        studentList.add(student3);
        studentService.deleteBatch(studentList);
    }
    /**
     * 删除所有
     */
    @Test
    void contextLoadDeleteBatchAll() {
        studentService.deleteAll();
    }

}

6. Summary

This article introduces how to combine MyBatis-Plus and Dameng database to achieve efficient data persistence operations. By configuring data sources, creating entity classes, Mapper interfaces, and Service layers, we can easily complete database operations such as adding, deleting, modifying, and checking. The powerful functions and easy operation of MyBatis-Plus greatly improve development efficiency and make data persistence easier and more enjoyable.

 

The most important thing is to remember to add your schema name to the entity class

Guess you like

Origin blog.csdn.net/lps12345666/article/details/132072420