最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

MyBatis-Plus+達夢數(shù)據(jù)庫實現(xiàn)高效數(shù)據(jù)持久化的示例

 更新時間:2023年08月03日 09:06:07   作者:不掉頭發(fā)的阿水  
這篇文章主要介紹了MyBatis-Plus和達夢數(shù)據(jù)庫實現(xiàn)高效數(shù)據(jù)持久化,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

一、添加依賴

首先,我們需要在項目的 pom.xml 文件中添加 MyBatis-Plus 和達夢數(shù)據(jù)庫的依賴:

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

二、配置數(shù)據(jù)源

在 Spring Boot 的配置文件 application.propertiesapplication.yml 中配置達夢數(shù)據(jù)庫的連接信息:

spring:
  datasource:
    url: jdbc:dm://localhost:5236
    username: 賬號
    password: 密碼
    driver-class-name: dm.jdbc.driver.DmDriver

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

三、創(chuàng)建實體類和 Mapper 接口

創(chuàng)建與數(shù)據(jù)庫表對應(yīng)的實體類,并使用 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;
}

接著,創(chuàng)建繼承自 BaseMapper 的 Mapper 接口:

import com.lps.domain.Student;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
/**
* @author 19449
* @description 針對表【student】的數(shù)據(jù)庫操作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>

四、創(chuàng)建 Service 層

創(chuàng)建 Service 接口和實現(xiàn)類,繼承自 IServiceServiceImpl

import com.lps.domain.Student;
import com.baomidou.mybatisplus.extension.service.IService;
import java.util.List;
/**
 * @author 19449
 * @description 針對表【student】的數(shù)據(jù)庫操作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實現(xiàn)類

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】的數(shù)據(jù)庫操作Service實現(xiàn)
 * @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 操作

現(xiàn)在就可以在業(yè)務(wù)邏輯中使用 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);
    }
    /**
     * 循環(huán)里做插入 主打就是挨訓
     */
    @Test
    void contextLoadsInsert2() {
        //還有優(yōu)化空間 下篇博客見
        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() {
        //還有優(yōu)化空間 下篇博客見
        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();
    }
}

六、總結(jié)

本文介紹了如何結(jié)合 MyBatis-Plus 和達夢數(shù)據(jù)庫來實現(xiàn)高效的數(shù)據(jù)持久化操作。通過配置數(shù)據(jù)源、創(chuàng)建實體類、Mapper 接口和 Service 層,我們可以輕松地完成增刪改查等數(shù)據(jù)庫操作。MyBatis-Plus 的強大功能和簡便的操作方式,大大提高了開發(fā)效率,使得數(shù)據(jù)持久化變得更加輕松愉快。

最重要的就是實體類上要記得加上你的模式名

以上就是MyBatis-Plus+達夢數(shù)據(jù)庫實現(xiàn)高效數(shù)據(jù)持久化的示例的詳細內(nèi)容,更多關(guān)于MyBatis-Plus數(shù)據(jù)持久化的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Java字符串的處理方式及作用介紹

    Java字符串的處理方式及作用介紹

    StringBuilder、StringBuffer?和?String是Java中處理字符串的三種主要方式,每種有不同的特點和適用場景,下面給大家介紹Java字符串的處理方法及作用介紹
    2025-04-04
  • Spring中配置Transaction與不配置的區(qū)別及說明

    Spring中配置Transaction與不配置的區(qū)別及說明

    這篇文章主要介紹了Spring中配置Transaction與不配置的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • Spring Boot示例代碼整合Redis詳解

    Spring Boot示例代碼整合Redis詳解

    SpringBoot對常用的數(shù)據(jù)庫支持外,對NoSQL 數(shù)據(jù)庫也進行了封裝自動化,下面這篇文章主要給大家介紹了關(guān)于springboot使用redis的詳細步驟,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-06-06
  • java 字符串截取的實例詳解

    java 字符串截取的實例詳解

    這篇文章主要介紹了java 字符串截取的實例詳解的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下
    2017-09-09
  • 通過實例了解如何在JavaWeb實現(xiàn)文件下載

    通過實例了解如何在JavaWeb實現(xiàn)文件下載

    這篇文章主要介紹了通過實例了解如何在JavaWeb實現(xiàn)文件下載,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-09-09
  • java bootclasspath的具體用法

    java bootclasspath的具體用法

    本文主要介紹了java bootclasspath的具體用法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-01-01
  • Java獲取接口所有實現(xiàn)類的方式詳解

    Java獲取接口所有實現(xiàn)類的方式詳解

    這篇文章主要介紹了Java獲取接口所有實現(xiàn)類的方式詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-12-12
  • mybatis的使用-Mapper文件各種語法介紹

    mybatis的使用-Mapper文件各種語法介紹

    這篇文章主要介紹了mybatis的使用-Mapper文件各種語法介紹,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • 如何解決IDEA.properties文件中文亂碼問題

    如何解決IDEA.properties文件中文亂碼問題

    這篇文章主要介紹了如何解決IDEA.properties文件中文亂碼問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • IntelliJ IDEA 的使用界面圖文教程

    IntelliJ IDEA 的使用界面圖文教程

    這篇文章主要介紹了IntelliJ IDEA 的使用界面圖文教程,需要的朋友可以參考下
    2018-10-10

最新評論

南城县| 象州县| 衡阳市| 山阳县| 满城县| 枣强县| 新竹县| 白山市| 荆州市| 青冈县| 凤台县| 册亨县| 海南省| 陆川县| 溧水县| 镇安县| 通江县| 曲松县| 安远县| 凤凰县| 三门峡市| 荥阳市| 运城市| 武宣县| 冀州市| 泸州市| 寿宁县| 昌图县| 兴海县| 临颍县| 个旧市| 东阳市| 沈丘县| 建水县| 石楼县| 淮阳县| 宜宾县| 东乡族自治县| 古丈县| 自贡市| 晴隆县|