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

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);
}

相关文章:

  • QML学习06Button
  • ArrayList 与 LinkedList 区别?
  • CMake学习笔记(六)可以在CMake的function中修改function函数体之外的变量值吗?
  • 探索智能仓颉
  • git push出现 “HTTP 400 curl 22 The requested URL returned error: 400...“错误
  • Ntfs!NtfsReadBootSector函数分析之nt!CcGetVacbMiss中得到一个nt!_VACB结构
  • 白皮精读:全国统一数据资产登记体系建设白皮书【附全文阅读】
  • MQTT-SpringBoot整合
  • 6.4.3_有向无环图描述表达式
  • JAVA 项目中 maven pom.xml 和 properties 配置文件、spring 配置文件,以及环境变量的关系
  • 深入理解Istio:全面解析与实践指南
  • 向量数据库选型实战指南:Milvus架构深度解析与技术对比
  • Lua 脚本在 Redis 中的运用-22
  • 每日Prompt:龙虎斗
  • Oracle附加日志概述
  • 华为OD机试真题——字符串序列判定(2025B卷:100分)Java/python/JavaScript/C/C++/GO最佳实现
  • Go语言中常见的6个设计模式
  • 非常适合初学者的Golang教程
  • pyhton基础【4】判断
  • 位运算的小结
  • 毕业设计做网站想法/东莞网站推广技巧
  • 商业网站建设常识/网时代教育培训机构怎么样
  • 网站建设后端/武汉网站排名提升
  • 哪几个网站做acm题目比较好/百度如何优化
  • 要想浏览国外网站 应该怎么做/武汉网络推广网络营销
  • 小本本教你做网站/google浏览器官方