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

SpringBoot框架中Mybatis-plus的簡(jiǎn)單使用操作匯總

 更新時(shí)間:2022年02月24日 11:05:26   作者:xsha_h  
這篇文章主要介紹了SpringBoot框架中Mybatis-plus的簡(jiǎn)單使用,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

Mybatis-plus

官網(wǎng)地址:https://baomidou.com/

配置mysql

在配置文件連接mysql

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/cat_house?serverTimezone=GMT%2B8
spring.datasource.username=username
spring.datasource.password=password

# mybatis日志(控制臺(tái)能顯示SQL語(yǔ)句)
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

Mybatis-plus使用方式

依賴導(dǎo)入

<!-- mybatis驅(qū)動(dòng) -->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.0.5</version>
</dependency>
<!-- 數(shù)據(jù)庫(kù)驅(qū)動(dòng) -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

lombok依賴導(dǎo)入

<!-- lombok用來(lái)簡(jiǎn)化實(shí)體類 -->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
</dependency>

Mybatis-plus實(shí)現(xiàn)簡(jiǎn)單的CURD操作

準(zhǔn)備表格(數(shù)據(jù)庫(kù)有相應(yīng)的表格)

準(zhǔn)備實(shí)體(實(shí)體文件夾中有相應(yīng)的實(shí)體類)

package com.xsha.boot.entity;
import lombok.Data;
@Data
public class Topic {
    private int id;
    private String title;
    private String time;
    private int count;
    private int version;
}

準(zhǔn)備映射文件(映射文件夾中有相應(yīng)的映射接口)

package com.xsha.boot.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xsha.boot.entity.Topic;
import org.springframework.stereotype.Repository;
@Repository
public interface TopicMapper extends BaseMapper<Topic> {
}

測(cè)試操作(在test類中進(jìn)行簡(jiǎn)單的單元測(cè)試)

package com.xsha.boot;

import com.xsha.boot.entity.Topic;
import com.xsha.boot.mapper.TopicMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
public class MainApplicationTest {
    @Autowired
    // 可在指定的接口上添加注解Repository,就不會(huì)爆紅了
    private TopicMapper topicMapper;
    // 查詢所有數(shù)據(jù)
    @Test
    public void findAll() {
        List<Topic> topics = topicMapper.selectList(null);
        for (int i = 0; i < topics.size(); i++) {
            System.out.println(topics.get(i));
        }
    }
    // 添加操作
    public void addTopic() {
//        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
//        Date date = new Date();
        Topic topic = new Topic();
        topic.setTitle("SSM框架整合了哪些主流框架");
        // 時(shí)間的添加可以采用mybatis-plus框架實(shí)現(xiàn),可查看Controller中接口實(shí)現(xiàn)和實(shí)體類屬性的注解
//        topic.setTime(ft.format(date));
        int row = topicMapper.insert(topic);
        System.out.println("添加的行數(shù):"+row);
    // 修改操作
    public void updateTopic() {
        topic.setId(20);
        topic.setCount(10);
        int row = topicMapper.updateById(topic);
        System.out.println("修改的行數(shù)"+row);
}

Mybatis-plus自動(dòng)填充策略

主鍵自動(dòng)填充

// 可以在id屬性上添加TableId注解可以修改id唯一鍵值的策略(自動(dòng)填充),如@TableId(type=IdType.AUTO)
// @TableId(type=IdType.ID_WORKER) 生成19位唯一數(shù)字的鍵值
// @TableId(type=IdType.ID_WORKER_STR) 生成19位唯一字符串的鍵值
private int id;

時(shí)間自動(dòng)填充

實(shí)體類屬性添加注解

// 采用mybatis-plus框架的策略自動(dòng)填充時(shí)間
// @TableField(fill=FieldFill.INSERT)  表示自動(dòng)填充創(chuàng)建時(shí)間
// @TableField(fill=FieldFill.INSERT_UPDATE) 表示自動(dòng)填充更新時(shí)間
@TableField(fill = FieldFill.INSERT)
private String time;

Controller類繼承接口實(shí)現(xiàn)時(shí)間自動(dòng)填充方法

package com.xsha.boot.controller;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Date;
@Component
public class MyMetaObjectController implements MetaObjectHandler {
    // 使用mybatis-plus實(shí)現(xiàn)添加操作,這個(gè)方法自動(dòng)調(diào)用
    @Override
    public void insertFill(MetaObject metaObject) {
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date date = new Date();
        // 第一個(gè)參數(shù)不是表格的字段名稱,而是實(shí)體類的屬性名稱
        this.setFieldValByName("time", ft.format(date), metaObject);
    }
    // 使用mybatis-plus實(shí)現(xiàn)更新操作,這個(gè)方法自動(dòng)調(diào)用
    public void updateFill(MetaObject metaObject) {
        // 更新時(shí)間可根據(jù)需求實(shí)現(xiàn)
}

樂(lè)觀鎖的具體實(shí)現(xiàn)

  • 表格添加字段version,作為樂(lè)觀鎖版本號(hào)
  • 對(duì)應(yīng)實(shí)體類添加版本號(hào)屬性,并且在屬性上面添加注解Version(baomidou下的Version)
  • 在配置類中添加樂(lè)觀鎖插件
@Configuration
@MapperScan("com.xsha.boot.mapper")
public class MyConfig {
    // 樂(lè)觀鎖插件
    @Bean
    public OptimisticLockerInterceptor optimisticLockerInterceptor() {
        return new OptimisticLockerInterceptor();
    }
}

Mybatis-plus查詢操作(簡(jiǎn)單)

// 單個(gè)id查詢
@Test
public void selectTopic() {
    Topic topic = topicMapper.selectById(20);
    System.out.println(topic);
}

// 多個(gè)id批量查詢
@Test
public void selectTopics() {
    List<Topic> topics = topicMapper.selectBatchIds(Arrays.asList(1, 2, 3, 4));
    for (int i = 0; i < topics.size(); i++) {
        System.out.println(topics.get(i));
    }
}

Mybatis-plus實(shí)現(xiàn)分頁(yè)操作

在配置類中配置分頁(yè)插件

// 分頁(yè)插件
@Bean
public PaginationInterceptor paginationInterceptor() {
    return new PaginationInterceptor();
}

編寫(xiě)分頁(yè)代碼

// 分頁(yè)查詢
@Test
public void selectByPage() {
    // 1.創(chuàng)建page對(duì)象,傳遞當(dāng)前頁(yè)和每頁(yè)記錄數(shù)的兩個(gè)參數(shù)
    Page<Topic> page = new Page<>(1, 3);
    // 2.調(diào)用mybatis-plus分頁(yè)查詢的方法,把分頁(yè)所有的數(shù)據(jù)封裝到page對(duì)象里面,第二個(gè)參數(shù)是條件
    topicMapper.selectPage(page, null);
    // 3.通過(guò)page對(duì)象獲取分頁(yè)數(shù)據(jù)
    System.out.println(page.getCurrent());  // 當(dāng)前頁(yè)
    System.out.println(page.getRecords());  // 每頁(yè)數(shù)據(jù)list集合
    System.out.println(page.getSize());  // 每頁(yè)顯示記錄數(shù)
    System.out.println(page.getTotal());  // 總記錄數(shù)
    System.out.println(page.getPages());  // 總頁(yè)數(shù)
    System.out.println(page.hasNext());  // 是否有下一頁(yè)
    System.out.println(page.hasPrevious());  // 是否有上一頁(yè)
}

Mybatis-plus刪除操作(簡(jiǎn)單)

物理刪除

 // 單個(gè)id刪除
    @Test
    public void deleteTopic() {
        int row = topicMapper.deleteById(20);
        System.out.println(row);
    }
    
    // 多個(gè)id批量刪除
    @Test
    public void deleteTopics() {
        int rows = topicMapper.deleteBatchIds(Arrays.asList(1, 2, 3, 4));
        System.out.println(rows);
    }

邏輯刪除

表格中添加標(biāo)志位字段,供邏輯刪除使用

表格字段設(shè)置默認(rèn)值,就不能使用mybatis-plus的自動(dòng)填充使用mybatis-plus的自動(dòng)填充

使用mybatis-plus的自動(dòng)填充時(shí),在實(shí)體類屬性上添加TableLogic注解

@TableLogic
private int delete;

在配置類中配置邏輯刪除插件

// 邏輯刪除插件
@Bean
public ISqlInjector sqlInjector() {
    return new LogicSqlInjector();
}

在配置文件中添加邏輯刪除與否的默認(rèn)值(可有可無(wú))

# 邏輯刪除與否的默認(rèn)值
mybatis-plus.global-config.db-config.logic-delete-value=1
mybatis-plus.global-config.db-config.logic-not-delete-value=0

代碼編寫(xiě)

// 邏輯刪除
@Test
public void logicDeleteTopic() {
    int row = topicMapper.deleteById(21);
    System.out.println(row);
}

注意:采用mybatis-plus的邏輯刪除方式時(shí),之后查詢數(shù)據(jù)時(shí)就不會(huì)包括邏輯刪除的數(shù)據(jù)

性能分析

在配置類中添加性能分析插件

/**
 * SQL執(zhí)行性能分析插件
 * 開(kāi)發(fā)環(huán)境使用,線上不推薦。maxTime指的是sql最大執(zhí)行時(shí)長(zhǎng)
 *
 * 三種環(huán)境:dev開(kāi)發(fā)環(huán)境、test測(cè)試環(huán)境、prod生成環(huán)境
 * @return
 */
@Bean
@Profile({"dev", "test"})   // 設(shè)置dev,test的環(huán)境開(kāi)啟
public PerformanceInterceptor performanceInterceptor() {
    PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();
    performanceInterceptor.setMaxTime(100); // 數(shù)值單位為毫秒ms
    performanceInterceptor.setFormat(true);
    return performanceInterceptor;
}

在配置文件中配置環(huán)境

# 環(huán)境設(shè)置:dev  test  prod
spring.profiles.active=dev

Mybatis-plus實(shí)現(xiàn)復(fù)雜條件查詢

使用QueryWrapper類對(duì)象構(gòu)造條件(還有其他的)

// mybatis-plus實(shí)現(xiàn)復(fù)雜查詢
@Test
public void querySelect() {
    // 1.創(chuàng)建QueryWrapper對(duì)象
    QueryWrapper<Topic> queryWrapper = new QueryWrapper<>();
    // 2.通過(guò)QueryWrapper設(shè)置條件
    // ge(>=)、gt(>)、le(<=)、lt(<)
    queryWrapper.ge("count", 3);
    List<Topic> topics1 = topicMapper.selectList(queryWrapper);
    System.out.println("FIRST");
    System.out.println(topics1);

    // eq(==)、ne(!=)
    queryWrapper.ne("deleted", 0);
    List<Topic> topics2 = topicMapper.selectList(queryWrapper);
    System.out.println("SECOND");
    System.out.println(topics2);
    // between(在···和···之間)
    queryWrapper.between("time", "2021-10-12 07:05:29.546779", "2021-10-27 15:02:09.458571");
    List<Topic> topics3 = topicMapper.selectList(queryWrapper);
    System.out.println("THIRD");
    System.out.println(topics3);
    // like(模糊查詢)
    queryWrapper.like("title", "SSM");
    List<Topic> topics4 = topicMapper.selectList(queryWrapper);
    System.out.println("FORTH");
    System.out.println(topics4);
    // 排序 orderByDesc   orderByAsc
    queryWrapper.orderByDesc("count");
    List<Topic> topics5 = topicMapper.selectList(queryWrapper);
    System.out.println("FIFTH");
    System.out.println(topics5);
    // 指定要查詢的列 last拼接sql語(yǔ)句
    queryWrapper.select("id", "title", "count");
    queryWrapper.last("limit 2");
    List<Topic> topics6 = topicMapper.selectList(queryWrapper);
    System.out.println("SIXTH");
    System.out.println(topics6);
}

到此這篇關(guān)于SpringBoot框架中Mybatis-plus的簡(jiǎn)單使用的文章就介紹到這了,更多相關(guān)SpringBoot Mybatis-plus使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java數(shù)據(jù)結(jié)構(gòu)排序算法之歸并排序詳解

    java數(shù)據(jù)結(jié)構(gòu)排序算法之歸并排序詳解

    這篇文章主要介紹了java數(shù)據(jù)結(jié)構(gòu)排序算法之歸并排序,結(jié)合具體實(shí)例形式詳細(xì)分析了歸并排序的原理、實(shí)現(xiàn)技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2017-05-05
  • 利用java制作簡(jiǎn)單的音樂(lè)播放器

    利用java制作簡(jiǎn)單的音樂(lè)播放器

    這篇文章主要為大家詳細(xì)介紹了利用java的swing技術(shù)制作簡(jiǎn)單的音樂(lè)播放器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • springboot的緩存技術(shù)的實(shí)現(xiàn)

    springboot的緩存技術(shù)的實(shí)現(xiàn)

    這篇文章主要介紹了springboot的緩存技術(shù)的實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-05-05
  • java如何用遞歸生成樹(shù)形結(jié)構(gòu)

    java如何用遞歸生成樹(shù)形結(jié)構(gòu)

    作者分享了自己在使用腳本之家資源進(jìn)行編程時(shí)的經(jīng)驗(yàn),包括準(zhǔn)備實(shí)體對(duì)象、測(cè)試數(shù)據(jù)、構(gòu)造樹(shù)形結(jié)構(gòu)遞歸函數(shù)、測(cè)試以及輸出結(jié)果等步驟,作者希望這些經(jīng)驗(yàn)?zāi)軐?duì)大家有所幫助,并鼓勵(lì)大家支持腳本之家
    2025-03-03
  • 詳解如何保證Java本地緩存的一致性

    詳解如何保證Java本地緩存的一致性

    所謂的一致性是指在同時(shí)使用緩存和數(shù)據(jù)庫(kù)的場(chǎng)景下,要確保數(shù)據(jù)在緩存與數(shù)據(jù)庫(kù)中的更新操作保持同步,那么,怎么保證Java本地緩存的一致性?所以本文將給大家介紹了如何保證Java本地緩存的一致性,需要的朋友可以參考下
    2024-01-01
  • scala中的隱式類型轉(zhuǎn)換的實(shí)現(xiàn)

    scala中的隱式類型轉(zhuǎn)換的實(shí)現(xiàn)

    這篇文章主要介紹了scala中的隱式類型轉(zhuǎn)換的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • Hadoop之Mapreduce序列化

    Hadoop之Mapreduce序列化

    本文主要帶我們了解Mapreduce序列化,序列化就是把內(nèi)存中的對(duì)象,轉(zhuǎn)換成字節(jié)序列(或其他數(shù)據(jù)傳輸協(xié)議)以便于存儲(chǔ)到磁盤(pán)(持久化)和網(wǎng)絡(luò)傳輸。想進(jìn)一步了解更多的小伙伴,可以參考閱讀本文
    2023-03-03
  • springboot @ConfigurationProperties和@PropertySource的區(qū)別

    springboot @ConfigurationProperties和@PropertySource的區(qū)別

    這篇文章主要介紹了springboot @ConfigurationProperties和@PropertySource的區(qū)別,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Java利用apache ftp工具實(shí)現(xiàn)文件上傳下載和刪除功能

    Java利用apache ftp工具實(shí)現(xiàn)文件上傳下載和刪除功能

    這篇文章主要為大家詳細(xì)介紹了Java利用apache ftp工具實(shí)現(xiàn)文件上傳下載、刪除功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-06-06
  • SpringBoot靜態(tài)資源路徑配置及主頁(yè)顯示

    SpringBoot靜態(tài)資源路徑配置及主頁(yè)顯示

    這篇文章主要介紹了SpringBoot靜態(tài)資源路徑配置及主頁(yè)顯示,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-04-04

最新評(píng)論

金乡县| 定西市| 博野县| 五莲县| 潼关县| 怀柔区| 讷河市| 河东区| 大荔县| 渝中区| 西华县| 普陀区| 即墨市| 郑州市| 磴口县| 安泽县| 察雅县| 治县。| 九江市| 清流县| 翼城县| 离岛区| 靖远县| 九龙坡区| 铜鼓县| 大埔区| 大冶市| 黑龙江省| 玛多县| 孙吴县| 盐池县| 临武县| 斗六市| 团风县| 嘉义县| 慈溪市| 彰化县| 汪清县| 黄山市| 商洛市| 敦化市|