MyBatis-Plus 和达梦数据库实现高效数据持久化

MyBatis-Plus 和达梦数据库实现高效数据持久化_第1张图片

 

一、添加依赖

首先,我们需要在项目的 pom.xml 文件中添加 MyBatis-Plus 和达梦数据库的依赖:

    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
        
            com.dm
            DmJdbcDriver
            1.8.0
        
        
            com.baomidou
            mybatis-plus-boot-starter
            3.5.2
        
        
            org.projectlombok
            lombok
        
    

二、配置数据源

在 Spring Boot 的配置文件 application.propertiesapplication.yml 中配置达梦数据库的连接信息:

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

 之后可以使用MyBatisX生成以下代码

三、创建实体类和 Mapper 接口

创建与数据库表对应的实体类,并使用 MyBatis-Plus 注解标注主键和表名等信息:

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;
    
}

接着,创建继承自 BaseMapper 的 Mapper 接口:

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 {

}

完成mapper.xml





    
            
            
            
    

    
        id,name,age
    

四、创建 Service 层

创建 Service 接口和实现类,继承自 IServiceServiceImpl

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 {

    List selectAll();

    void insert(Student student);

    void deleteBatch(List studentList);
    
    void deleteAll();

}

service实现类

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
        implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

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

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

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

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

五、进行 CRUD 操作

现在就可以在业务逻辑中使用 YourService 进行增删改查操作了:

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 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 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 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 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 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();
    }

}

六、总结

本文介绍了如何结合 MyBatis-Plus 和达梦数据库来实现高效的数据持久化操作。通过配置数据源、创建实体类、Mapper 接口和 Service 层,我们可以轻松地完成增删改查等数据库操作。MyBatis-Plus 的强大功能和简便的操作方式,大大提高了开发效率,使得数据持久化变得更加轻松愉快。

 

最重要的就是实体类上要记得加上你的模式名

MyBatis-Plus 和达梦数据库实现高效数据持久化_第2张图片

你可能感兴趣的:(SQL,SpringBoot,MyBatis,mybatis,数据库)