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.properties
或 application.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)類,繼承自 IService
和 ServiceImpl
:
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)文章
Mybatis-Plus使用@TableField實現(xiàn)自動填充日期的代碼示例
數(shù)據(jù)庫中經(jīng)常有create_time,update_time兩個字段,在代碼中設(shè)置時間有點太麻煩了?mybatis-plus可以幫我們自動填充,本文主要介紹了Mybatis-Plus使用@TableField實現(xiàn)自動填充日期的代碼示例,感興趣的可以了解一下2022-04-04java?web實現(xiàn)簡單登錄注冊功能全過程(eclipse,mysql)
前期我們學習了javaweb項目用JDBC連接數(shù)據(jù)庫,還有數(shù)據(jù)庫的建表功能,下面這篇文章主要給大家介紹了關(guān)于java?web實現(xiàn)簡單登錄注冊功能的相關(guān)資料,需要的朋友可以參考下2022-07-07springboot項目連接多種數(shù)據(jù)庫該如何操作詳析
在Spring Boot應(yīng)用中連接多個數(shù)據(jù)庫或數(shù)據(jù)源可以使用多種方式,下面這篇文章主要給大家介紹了關(guān)于springboot項目連接多種數(shù)據(jù)庫該如何操作的相關(guān)資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2024-08-08java中循環(huán)刪除list中元素的方法總結(jié)
下面小編就為大家?guī)硪黄猨ava中循環(huán)刪除list中元素的方法總結(jié)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-12-12Maven項目如何在pom文件中引入lib下的第三方j(luò)ar包并打包進去
在使用Maven進行項目開發(fā)時,引入第三方私有的Jar包可能會遇到問題,一種常見的解決方案是將Jar包添加到項目的lib目錄,并通過IDE進行配置,但這需要每個開發(fā)者單獨操作,效率低下,更好的方法是通過Maven的pom.xml文件管理這些Jar包2024-09-09