当前位置: 首页 > article >正文

LambdaQueryWrapper、MybatisPlus提供的基本接口方法、增删改查常用的接口方法、自定义 SQL

DAY26.2 Java核心基础

MybatisPlus提供的基本接口方法

分页查询

导入依赖springboot整合Mybatis-plus

<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.4.3</version>
</dependency>

创建Page对象,通过usermapper里面的selectPage查询

@Test
public void test4(){Page<User> userPage = userMapper.selectPage(new Page<>(0, 10), null);System.out.println(userPage.getRecords());System.out.println(userPage.getCurrent());System.out.println(userPage.getPages());System.out.println(userPage.getSize());
}

测试输出:

image-20250523201508554

可以看见它并没有分页查询,而是把表中所有的数据查询出来了

为什么呢?

因为我们没有配置分页配置类

MybatisPlusConfig配置分页

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class MybatisPlusConfig {@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();// 添加分页插件interceptor.addInnerInterceptor(new PaginationInnerInterceptor());return interceptor;}
}

我们再次查询

image-20250523201712842

可以看见实现了分页查询,只查询了10个数据

增删改查常用的接口方法

查询

@Test
public void test6(){// 查询所有LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();queryWrapper.eq(User::getId,  1);List<User> users1 = userMapper.selectList(queryWrapper);System.out.println(users1);// 批量查询List<User> users2 = userMapper.selectBatchIds(Arrays.asList(1, 2, 3));System.out.println(users2);// 查询数量System.out.println(userMapper.selectCount(null));HashMap<String, Object> stringObjectHashMap = new HashMap<>();stringObjectHashMap.put("id", 1);System.out.println(userMapper.selectByMap(stringObjectHashMap));// 查询单个数据User user = userMapper.selectOne(queryWrapper);
}

增加

@Test
public void test7(){User user = new User();user.setName("张三");user.setRole("admin");user.setAge(18);user.setEmail("111@qq.com");user.setPwd("123456");int insert = userMapper.insert(user);System.out.println(insert);
}

更新

@Test
public void test8() {//修改User user = new User();user.setId(1);user.setName("张三");user.setRole("admin");user.setAge(18);user.setEmail("111@qq.com");user.setPwd("123456");// 根据id更新int update = userMapper.updateById(user);System.out.println(update);// 根据条件更新(wrapper)LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();queryWrapper.eq(User::getName, "张三");System.out.println(userMapper.update(user, queryWrapper));
}

删除

@Test
public void test9() {// 根据id删除int delete = userMapper.deleteById(1);System.out.println(delete);// 根据条件删除(wrapper)LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();queryWrapper.eq(User::getName, "张三");int delete1 = userMapper.delete(queryWrapper);// 批量删除userMapper.deleteBatchIds(Arrays.asList(1, 2, 3));// 根据map键值对删除HashMap<String, Object> stringObjectHashMap = new HashMap<>();stringObjectHashMap.put("id", 1);stringObjectHashMap.put("name", "张三");userMapper.deleteByMap(stringObjectHashMap);
}

自定义 SQL

UserMapper

public interface UserMapper extends BaseMapper<User> {@Select("select * from user where name = #{name}")User queryByName(String name);
}
@Test
public void test10() {User user = userMapper.queryByName("张三");System.out.println(user);
}
http://www.lryc.cn/news/2386632.html

相关文章:

  • 深度学习---可视化
  • 军事大模型及其应用分析
  • c++算法题
  • 云原生安全 SaaS :从基础到实践
  • 《Drain日志解析算法》论文阅读笔记
  • MMAction2重要的几个配置参数
  • Windows系统如何查看ssh公钥
  • UniApp+Vue3微信小程序二维码生成、转图片、截图保存整页
  • 8.2 线性变换的矩阵
  • 【2025】嵌入式软考中级部分试题
  • Antd中Upload组件封装及使用:
  • Linux环境基础开发工具->vim
  • 跳板问题(贪心算法+细节思考)
  • RuoYi前后端分离框架集成UEditorPlus富文本编辑器
  • IPD流程落地:项目任务书Charter开发
  • Vue 2 混入 (Mixins) 的详细使用指南
  • day020-sed和find
  • OpenGL Chan视频学习-4 Vertex Buffers and Drawing a Triangle in OpenGL
  • 数据库事务的四大特性(ACID)
  • 网络安全全知识图谱:威胁、防护、管理与发展趋势详解
  • FreeRTOS 在物联网传感器节点的应用:低功耗实时数据采集与传输方案
  • 解决 iTerm2 中 nvm 不生效的问题(Mac 环境)
  • Linux环境下基于Docker安装 PostgreSQL数据库并配置 pgvector
  • (9)-java+ selenium->元素定位之By name
  • 深浅拷贝?
  • Beckhoff PLC 功能块 FB_CTRL_ACTUAL_VALUE_FILTER (模拟量滤波)
  • Mysql在SQL层面的优化
  • JVM规范之栈帧
  • 【C++指南】string(四):编码
  • 深度学习之序列建模的核心技术:LSTM架构深度解析与优化策略