mybatis-plus02

news2024/9/29 15:24:27

目录

  • 一、乐观锁
  • 二、查询构造器
  • 三、分页查询
  • 四、逻辑删除
  • 五、Mybatis的应用

一、乐观锁

在这里插入图片描述
配置示例:

  1. 在数据库表中加入version字段,表示数据版本号
    在这里插入图片描述
  2. 修改实体类,在使用类中加入对应的version字段,并使用是乐观锁
//乐观锁
@Version
private int version;
  1. 配置乐观锁
package com.xnx.mp280.config;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MybatisPlusConfig {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
//        乐观锁
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
//        分页
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        return interceptor;
    }
}
  1. 测试
/测试乐观锁
    @Test
    public void testLock01() {
        User user01 = userMapper.selectById(66L);
        User user02 = userMapper.selectById(66L);
        user01.setName("leguansuo03");
        userMapper.updateById(user01);
        user02.setName("leguansuo04");
        userMapper.updateById(user02);

//        QueryWrapper qu = new QueryWrapper();
//        qu.eq("id",66L);
        select * from user where id = 66L
//        qu.ne("name","xnx");
        select * from user where name <> 'xnx'
//        String sql = " email = ewew@qq.com and version = 2";
//        qu.apply(sql);


    }

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

二、查询构造器

  1. 批量查询多个对象
@Test
    public void testSelectList() {
        //一次查询多个ID
        List<User> users = userMapper.selectBatchIds(Arrays.asList(1L, 2L, 3L));
        users.forEach(t -> {
            System.out.println(t);
        });
    }

    @Test
    public void testSelectByMap() {
        //使用map进行查询
        Map<String, Object> map = new HashMap<>();
        map.put("name", "Tom");
        map.put("age", 28);
        List<User> users = userMapper.selectByMap(map);
        users.forEach(t -> System.out.println(t));
    }

    @Test
    public void testWrapperLike(){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.apply(" hadkjhwajdksahd");
        List<User> users = userMapper.selectList(wrapper);
        users.forEach(t-> System.out.println(t));
    }

    @Test
    public void testUpdateWrapper(){
        UpdateWrapper u = new UpdateWrapper();
//        u.set("name","TT");
        u.eq(true,"id",6L);
        User user = userMapper.selectById(6L);
//        user = new User();
        user.setName("TTT");
        userMapper.update(user,u);
    }

在这里插入图片描述

三、分页查询

  1. 分页配置
package com.xnx.mp280.config;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MybatisPlusConfig {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
//        乐观锁
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
//        分页
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        return interceptor;
    }
}
  1. 分页演示
@Test
    public void testSelectPage() {
        PageDTO<User> page = new PageDTO<>();
        page.setCurrent(3);
        page.setSize(3);
        PageDTO<User> pageDTO = userMapper.selectPage(page, null);
        System.out.println(pageDTO.getTotal());
        List<User> records = pageDTO.getRecords();
        records.forEach(t-> System.out.println(t));
    }

在这里插入图片描述

四、逻辑删除

物理删除使用起来比较简单,仿照查询功能即可,不再赘述。
什么是逻辑删除?
即:标记删除,并不是真的从数据库中删除,而是做个删除标记,在查询时,过滤掉标记为删除的记录
即可。

  1. 数据库表结构调整
    在这里插入图片描述
  2. 修改实体类
//标记该字段为逻辑删除字段
@TableLogic
private int deleted;
  1. 在application.properties (或application.yml)中加入如下配置,
#逻辑删除字段名
mybatis-plus.global-config.db-config.logic-delete-field=deleted
# 1表示逻辑删除
mybatis-plus.global-config.db-config.logic-delete-value=1
# 0 表示未删除
mybatis-plus.global-config.db-config.logic-not-delete-value=0
  1. 测试
@Test
    public void testDeleteLogic() {
        int i = userMapper.deleteById(66L);
        System.out.println(i);
    }

在这里插入图片描述
在这里插入图片描述

五、Mybatis的应用

mybatis-plus是mybatis的升级版,所以在mybatis-plus中使用xml的配置比较简单

  1. 在application.properties配置文件中加入如下配置
mybatis-plus.mapper-locations=classpath:/mapper/**/*.xml
  1. 定义mapper接口,继承BaseMapper接口,以便于使用mybatis-plus提供的方法
package com.xnx.mp280.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xnx.mp280.model.User;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface UserMapperXml extends BaseMapper<User> {
    List<User> list();

    @Select("select * from user u,goods g where u.id = 66L")
    List<User> list2();
}
  1. 根据接口生成xml配置文件,IDE一般有辅助的插件,例如IDEA中的Free Mybatis plugin
<?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.xnx.mp280.mapper.UserMapperXml">
    <select id="list" resultType="com.xnx.mp280.model.User">
select * from user;
</select>
</mapper>

也可以不使用xml配置文件,使用注解来定义sql语句
如UserMapperXml的list2()
4. 测试

package com.xnx.mp280;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.xnx.mp280.mapper.UserMapper;
import com.xnx.mp280.mapper.UserMapperXml;
import com.xnx.mp280.model.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SpringBootTest
public class SampleTest2 {

    @Autowired
    private UserMapperXml userMapper;

    @Test
    public void test1(){
        List<User> list = userMapper.list();
        list.forEach(System.out::println);
    }
}

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/186239.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

数据结构 第六章 二叉树与树(二叉树的性质)

玫瑰少年&#xff1a;点击收听 1 基本知识点 1、根结点&#xff1a;在一棵树中&#xff0c;(唯一)一个没有前驱的结点 2、叶子结点&#xff1a;在一棵树中&#xff0c;没有后继的结点 3、结点的度&#xff1a;是指结点后继的数量 4、树的度&#xff1a;是指所有结点度的最大值…

VMware ESXi 6.7 U3 Unlocker OEM BIOS 集成 REALTEK 网卡驱动和 NVMe 驱动 (集成驱动版)

ESXi-6.7.0-20221004001 Build 20497097 请访问原文链接&#xff1a;https://sysin.org/blog/vmware-esxi-6-sysin/&#xff0c;查看最新版。原创作品&#xff0c;转载请保留出处。 作者主页&#xff1a;www.sysin.org VMware ESXi 6.7.0 Update 3 Build 20497097 Unlocker &…

[Vulnhub] DC-7

下载链接&#xff1a;https://download.vulnhub.com/dc/DC-7.zip git信息泄露Drupal-CMS 拿shellnc反弹shell反弹shell写入root用户的cron定时任务&#xff0c;root执行提权 目录 <1> 信息搜集 (1) nmap扫靶机ip&端口服务 (2) github上DC-7项目信息泄露 <2&g…

洛谷 P1417 烹调方案 01背包题解 动态规划

真的好久没有写题解了 上次写还是在上次呢 先赞后看好习惯&#xff01; 昨天打了一月份的USACO(美国信奥赛) 给我一个感触&#xff08;也算是感想吧&#xff09;&#xff1a; 我是蒟蒻 我是蒟蒻 我是蒟蒻 我是蒟蒻 水内容&#xff08;&#xff09; 言归正传看今天这道题 题…

电脑键盘打字错乱怎么办?按键混乱的5种解决方法

有人问电脑无故乱打字&#xff0c;明明按的正确的键&#xff0c;打出来的却是错误的字母。换个键盘是不是就能解决呢&#xff1f; 不过如果你的手边恰好没有新键盘&#xff0c;又立刻要用电脑&#xff0c;可以先试试下面的5种方法。 重新连接键盘和电脑禁用键盘上的Num Lock运…

爬虫进阶(web逆向之b站)

文章目录 简介分析nowh5获取动态参数模拟 now模拟 h5小结简介 前面在《web逆向初步》练习了一些 JS 加密操作,这篇进入基础实战部分,通过给b沾刷播放量的例子,体会逆向的作用!当然,这里不是恶意刷流量,而是通过程序模拟一个正常用户,给视频增加一次播放量分析 一个正常用…

【深度学习框架-Paddle】丝滑安装PaddlePaddle,无缝衔接使用多卡

目录Paddle爱恨史PaddleCloud多卡Paddle爱恨史 Paddle是由百度开发的国内的深度学习框架&#xff0c;PaddlePaddle支撑了PaddleOCR、PaddleNLP等一系列领域内的开源工具包&#xff0c;为国内深度学习的落地与实践做出了大量贡献。 但是&#xff0c;PaddlePaddle安装问题一直都…

wpa_supplicant EAPOL状态机分析

协议 EEE 802.1X-2004协议&#xff1a;基于端口的网络接入控制协议&#xff08;port based network access control protocol&#xff09;。在LAN口对所接入的用户设备进行认证和控制&#xff0c;如果通过认证则端口打开&#xff0c;可以访问局域网中的资源。 状态机设计原理…

TYPE-C接口引脚详解

Type-C口有4对TX/RX分线&#xff0c;2对USBD/D-&#xff0c;一对SBU&#xff0c;2个CC&#xff0c;另外还有4个VBUS和4个地线。 1、当Type-C接口仅用作传输DP信号时&#xff0c;则可利用4对TX/RX&#xff0c;从而实现4Lane传输&#xff0c;这种模式称为DPonly模式&#xff1b;…

如何对项目健康度进行测量?评估项目健康状况

项目驱动变革&#xff0c;大部分公司逐步由运营驱动转变为项目驱动&#xff0c;带来更多重新和商业价值。对组织而言&#xff0c;从商业角度看&#xff0c;项目旨在推动组织从一个状态转到另一个状态&#xff0c;从而达成特定目标。项目的健康情况如何关乎项目和变革的成本&…

生成package.json文件报错“系统找不到指定的路径”

文章目录一、提出问题二、解决问题一、提出问题 package.json文件作为Web工程的入口&#xff0c;到底有多少配置是和我们的日常开发相关的&#xff1f;使用npm或yarn命令生成一个最简单的package.json文件。在命令行执行命令&#xff1a;yarn init -y 执行命令hadoop version也…

IB数学还能做什么IA

苦恼着不知道怎么写数学科IA。对呀&#xff0c;数学不就是做做题&#xff0c;算算数吗&#xff0c;加减乘除神马的&#xff0c;到底还可以做什么课题&#xff01;&#xff1f; 第一问&#xff1a;“到底怎么才是一个没有bug的数学IA题目呢&#xff1f;” 面对这个问题&#xff…

快速构建和安装干净的 ESXi 8 镜像指南

申请的 ESXi 8 的免费授权到了&#xff0c;所以趁着春节假期最后一天&#xff0c;折腾一把。这篇文档支持 ESXi 8 及以下版本的安装镜像构建&#xff0c;无需麻烦的依赖安装和解决环境问题。 相比较安装运行网上已经构建好的黑盒镜像&#xff0c;为什么不自己进行构建呢&#…

ARM uboot 主Makefile 分析

一、uboot 主Makefile分析1 1、uboot version 确定&#xff08;Makefile 的 24-29 行&#xff09; (1) uboot 的版本号分 3 个级别&#xff1a; VERSION&#xff1a;主板本号 PATCHLEVEL&#xff1a;次版本号 SUBLEVEL&#xff1a;再次版本号 EXTRAVERSION : 另外附加的版本信…

Redis对不起是我肤浅了(基础和应用篇):位图(Bitmaps)的妙用和深入分析每个子命令的用法

一、前言 在Redis 4.0 版本之前&#xff0c;Redis是单线程程序&#xff0c;主要是指Redis的网络I/O线程。Redis的持久化、集群同步等操作&#xff0c;则是由另外的线程来执行的。但在Redis 4.0 版本之后&#xff0c;Redis添加了多线程的支持&#xff0c;这时的多线程主要体现在…

温湿度传感器不同输出方式的优异对比

温湿度传感器装有湿敏和热敏元件&#xff0c;多以温湿度一体式的探头作为测温元件&#xff0c;将温度和湿度信号采集出来&#xff0c;经过稳压滤波、运算放大、非线性校正、V/I转换、恒流及反向保护等电路处理后&#xff0c;转换成与温度和湿度成线性关系的电流信号或电压信号输…

1.2.2存储结构:Cache--高速缓存

1.2.2存储结构&#xff1a;Cache--高速缓存Cache--高速缓存&#xff08;相联存储器&#xff09;Cache特点Cache改善系统性能局部性原理Cache–高速缓存&#xff08;相联存储器&#xff09; CPU中的寄存器和内存对比的话&#xff0c;其容量和速度差距是非常大的&#xff0c;因此…

数据结构 - 学习笔记 - 红黑树

数据结构 - 学习笔记 - 红黑树定义简介知识点1. 结点属性2. 前驱、后继3. 旋转查找插入父结点为黑色父结点为红色1. 有4种情形只需要变色&#xff08;对应234树4结点&#xff09;1.1. 变色实现平衡1.2. 递归调整颜色2. 有4种情形需要旋转 变色&#xff08;对应234树3结点&…

[JavaWeb]CSS

目录1. CSS语法1.1 常用样式-字体颜色1.2 常用样式-边框border1.3 常用样式-字体样式1.4 常用样式-超链接去下划线1.5 常用样式-列表去除修饰2.CSS 使用三种方式2.1 在标签的 style 属性上设置 CSS 样式2.2 在head 标签中,使用style 标签来定义需要的CSS样式2.3 把 CSS 样式写成…

线程的几种状态转换

线程在一定条件下&#xff0c;状态会发生变化。线程一共有以下几种状态&#xff1a; 1、新建状态(New)&#xff1a;新创建了一个线程对象。 2、就绪状态(Runnable)&#xff1a;线程对象创建后&#xff0c;其他线程调用了该对象的start()方法。该状态的线程位于“可运行线程池…