MyBatis-Plus+达梦数据库实现高效数据持久化的示例

 更新时间:2023年08月03日 09:06:07   作者:不掉头发的阿水  
这篇文章主要介绍了MyBatis-Plus和达梦数据库实现高效数据持久化,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

一、添加依赖

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

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

二、配置数据源

在 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<Student> {
}
 

完成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>

四、创建 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<Student> {
    List<Student> selectAll();
    void insert(Student student);
    void deleteBatch(List<Student> 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<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);
    }
}

五、进行 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<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();
    }
}

六、总结

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

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

以上就是MyBatis-Plus+达梦数据库实现高效数据持久化的示例的详细内容,更多关于MyBatis-Plus数据持久化的资料请关注脚本之家其它相关文章!

相关文章

  • Mybatis-Plus使用@TableField实现自动填充日期的代码示例

    Mybatis-Plus使用@TableField实现自动填充日期的代码示例

    数据库中经常有create_time,update_time两个字段,在代码中设置时间有点太麻烦了 mybatis-plus可以帮我们自动填充,本文主要介绍了Mybatis-Plus使用@TableField实现自动填充日期的代码示例,感兴趣的可以了解一下
    2022-04-04
  • java web实现简单登录注册功能全过程(eclipse,mysql)

    java web实现简单登录注册功能全过程(eclipse,mysql)

    前期我们学习了javaweb项目用JDBC连接数据库,还有数据库的建表功能,下面这篇文章主要给大家介绍了关于java web实现简单登录注册功能的相关资料,需要的朋友可以参考下
    2022-07-07
  • 浅谈xml配置spring profiles的几个注意点

    浅谈xml配置spring profiles的几个注意点

    这篇文章主要介绍了浅谈xml配置spring profiles的几个注意点,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-07-07
  • springboot项目连接多种数据库该如何操作详析

    springboot项目连接多种数据库该如何操作详析

    在Spring Boot应用中连接多个数据库或数据源可以使用多种方式,下面这篇文章主要给大家介绍了关于springboot项目连接多种数据库该如何操作的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2024-08-08
  • SpringBoot中Filter没有生效原因及解决方案

    SpringBoot中Filter没有生效原因及解决方案

    Servlet 三大组件 Servlet、Filter、Listener 在传统项目中需要在 web.xml 中进行相应的配置,这篇文章主要介绍了SpringBoot中Filter没有生效原因及解决方案,需要的朋友可以参考下
    2024-04-04
  • SpringBoot Aop 详解和多种使用场景解析

    SpringBoot Aop 详解和多种使用场景解析

    aop面向切面编程,是编程中一个很重要的思想本篇文章主要介绍的是SpringBoot切面Aop的使用和案例,对SpringBoot Aop相关知识感兴趣的朋友跟随小编一起看看吧
    2021-08-08
  • java中循环删除list中元素的方法总结

    java中循环删除list中元素的方法总结

    下面小编就为大家带来一篇java中循环删除list中元素的方法总结。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-12-12
  • Maven项目如何在pom文件中引入lib下的第三方jar包并打包进去

    Maven项目如何在pom文件中引入lib下的第三方jar包并打包进去

    在使用Maven进行项目开发时,引入第三方私有的Jar包可能会遇到问题,一种常见的解决方案是将Jar包添加到项目的lib目录,并通过IDE进行配置,但这需要每个开发者单独操作,效率低下,更好的方法是通过Maven的pom.xml文件管理这些Jar包
    2024-09-09
  • Java中双向链表详解及实例

    Java中双向链表详解及实例

    这篇文章主要介绍了Java中双向链表详解及实例的相关资料,需要的朋友可以参考下
    2017-04-04
  • 如何实现Java监听器详解

    如何实现Java监听器详解

    今天带大家了解Java监听器是如何实现的及实现原理是什么,文中有非常详细的说明,对正在学习的小伙伴们很有帮助,需要的朋友可以参考下
    2021-06-06

最新评论