1. 分页查询

方案一:MyBatis XML

 MyBatis 内置的使用方式,步骤如下:

① 创建 AdminUserMapper.xml 文件,编写两个 SQL 查询语句:

<?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="cn.iocoder.yudao.module.system.dal.mysql.user.AdminUserMapper"><select id="selectPage01List"resultType="cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO" >SELECT * FROM system_users<where><if test="reqVO.username != null and reqVO.username !=''">AND username LIKE CONCAT('%',#{reqVO.username},'%')</if><if test="reqVO.createTime != null">AND create_time BETWEEN #{reqVO.createTime[0]}, #{reqVO.createTime[1]},</if><if test="reqVO.status != null">AND status = #{reqVO.status}</if></where>ORDER BY id DESCLIMIT #{reqVO.pageNo}, #{reqVO.pageSize}</select><select id="selectPage01Count" resultType="Long" >SELECT COUNT(1) FROM system_users<where><if test="reqVO.username != null and reqVO.username !=''">AND username LIKE CONCAT('%',#{reqVO.username},'%')</if><if test="reqVO.createTime != null">AND create_time BETWEEN #{reqVO.createTime[0]}, #{reqVO.createTime[1]},</if><if test="reqVO.status != null">AND status = #{reqVO.status}</if></where></select></mapper>

② 在 AdminUserMapper 创建这两 SQL 对应的方法:

@Mapper
public interface AdminUserMapper extends BaseMapperX<AdminUserDO> {/*** 查询分页的列表*/List<AdminUserDO> selectPage01List(@Param("reqVO") UserPageReqVO reqVO);/*** 查询分页的条数*/Long selectPage01Count(@Param("reqVO") UserPageReqVO reqVO);}

其中 UserPageReqVO.java (opens new window)是分页查询的请求 VO。

③ 在 AdminUserServiceImplService 层,调用这两个方法,实现分页查询:

@Service
@Slf4j
public class AdminUserServiceImpl implements AdminUserService {@Overridepublic PageResult<AdminUserDO> getUserPage(UserPageReqVO reqVO) {return new PageResult<>(userMapper.selectPage01List(reqVO),userMapper.selectPage01Count(reqVO));}
}

④ 简单调用下,可以在 IDEA 控制台看到 2 条 SQL:

方案二:MyBatis Plus XML

MyBatis Plus 拓展的使用方式,可以简化只需要写一条 SQL,步骤如下:

① 创建 AdminUserMapper.xml 文件,只编写一个 SQL 查询语句:

<?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="cn.iocoder.yudao.module.system.dal.mysql.user.AdminUserMapper"><select id="selectPage02"resultType="cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO" >SELECT * FROM system_users<where><if test="reqVO.username != null and reqVO.username !=''">AND username LIKE CONCAT('%',#{reqVO.username},'%')</if><if test="reqVO.createTime != null">AND create_time BETWEEN #{reqVO.createTime[0]}, #{reqVO.createTime[1]},</if><if test="reqVO.status != null">AND status = #{reqVO.status}</if></where>ORDER BY id DESC</select></mapper>

注意,不需要写 LIMIT 分页语句噢

② 在 AdminUserMapper 创建这一 SQL 对应的方法:

@Mapper
public interface AdminUserMapper extends BaseMapperX<AdminUserDO> {IPage<AdminUserDO> selectPage02(IPage<AdminUserDO> page, @Param("reqVO") UserPageReqVO reqVO);}

第一个参数、返回结果必须都是 IPage 类型,第二个参数可以放查询条件。

③ 在 AdminUserServiceImplService 层,调用这一个方法,实现分页查询:

@Service
@Slf4j
public class AdminUserServiceImpl implements AdminUserService {@Overridepublic PageResult<AdminUserDO> getUserPage(UserPageReqVO reqVO) {// 必须使用 MyBatis Plus 的分页对象IPage<AdminUserDO> page = new Page<>(reqVO.getPageNo(), reqVO.getPageSize());userMapper.selectPage02(page, reqVO);return new PageResult<>(page.getRecords(), page.getTotal());}
}

因为项目使用 PageParam 和 PageResult 作为分页对象,所以需要和 IPage 做下转换。

④ 简单调用下,可以在 IDEA 控制台看到 2 条 SQL:

本质上,MyBatis Plus 是基于我们在 XML 编写的这条 SQL,计算出获得分页数量的 SQL。

一般情况下,建议采用方案二:MyBatis Plus XML,因为它开发效率更高,并且在分页数量为 0 时,就不多余查询分页的列表,一定程度上可以提升性能。

2. 联表查询

对于需要链表查询的场景,建议也是写 MyBatis XML。

除了 XML 这种方式外,项目也集成了 MyBatis Plus Join (opens new window)框架,通过 Java 代码实现联表查询。

这里,以查询 system_users 和 system_dept 联表,查询部门名为 芋道源码、用户状态为开启的用户列表。

案例一:字段平铺

① 创建 AdminUserDetailDO 类,继承 AdminUserDO 类,并添加 deptName 平铺。代码如下:

@Data
public class AdminUserDetailDO extends AdminUserDO {private String deptName;}

② 在 AdminUserMapper 创建 selectListByStatusAndDeptName 方法,代码如下:

@Mapper
public interface AdminUserMapper extends BaseMapperX<AdminUserDO> {default List<AdminUserDetailDO> selectList2ByStatusAndDeptName(Integer status, String deptName) {return selectJoinList(AdminUserDetailDO.class, new MPJLambdaWrapper<AdminUserDO>() // 查询 List.selectAll(AdminUserDO.class) // 查询 system_users 表的 all 所有字段.selectAs(DeptDO::getName, AdminUserDetailDO::getDeptName) // 查询 system_dept 表的 name 字段,使用 deptName 字段“部分”返回.eq(AdminUserDO::getStatus, status) // WHERE system_users.status = ? 【部门名为 `芋道源码`】.leftJoin(DeptDO.class, DeptDO::getId, AdminUserDO::getDeptId) // 联表 WHERE system_users.dept_id = system_dept.id .eq(DeptDO::getName, deptName) // WHERE system_dept.name = ? 【用户状态为开启】);}}

案例二:字段内嵌

① 创建 AdminUserDetailDO 类,继承 AdminUserDO 类,并添加 dept 部门。代码如下:

@Data
public class AdminUserDetail2DO extends AdminUserDO {private DeptDO dept;}

② 在 AdminUserMapper 创建 selectListByStatusAndDeptName 方法,代码如下:

@Mapper
public interface AdminUserMapper extends BaseMapperX<AdminUserDO> {default List<AdminUserDetail2DO> selectListByStatusAndDeptName(Integer status, String deptName) {return selectJoinList(AdminUserDetail2DO.class, new MPJLambdaWrapper<AdminUserDO>().selectAll(AdminUserDO.class).selectAssociation(DeptDO.class, AdminUserDetail2DO::getDept) // 重点差异点:查询 system_dept 表的 name 字段,使用 dept 字段“整个”返回.eq(AdminUserDO::getStatus, status).leftJoin(DeptDO.class, DeptDO::getId, AdminUserDO::getDeptId).eq(DeptDO::getName, deptName));}}

2.3 总结

MyBatis Plus Join 相比 MyBatis XML 来说,一开始肯定是需要多看看它的文档 (opens new window)。

但是熟悉后,我还是更喜欢使用 MyBatis Plus Join 哈~

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

如若内容造成侵权/违法违规/事实不符,请联系英文站点网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

使用 Spring AI Alibaba构建 AI Code Review 应用

很早的时候就想着用AI来做Code Review&#xff0c;最近也看到了一些不错的实现&#xff0c;但是没有一个使用Java来构建的&#xff0c;看的比较费劲&#xff0c;虽然说语言只是一种工具&#xff0c;但是还是想用Java重新写一遍&#xff0c;正好最近Spring AI Alibaba出了正式版…

力扣1590. 使数组和能被 P 整除

这一题的难点在于模运算&#xff0c;对模运算足够了解&#xff0c;对式子进行变换就很容易得到结果&#xff0c;本质上还是一道前缀和哈希表的题 这里重点讲一下模运算。 常见的模运算的用法 (a-b)%k0等价于 a%kb%k 而在这一题中由于多了一个len&#xff0c;&#xff08;数组的…

FPGA内部资源介绍

FPGA内部资源介绍 目录 逻辑资源块LUT&#xff08;查找表&#xff09;加法器寄存器MUX&#xff08;复用器&#xff09;时钟网络资源 全局时钟网络资源区域时钟网络资源IO时钟网络资源 时钟处理单元BLOCK RAMDSP布线资源接口资源 用户IO资源专用高速接口资源 总结 1. 逻辑资源…

CSS 列表

CSS 列表 引言 CSS 列表是网页设计中常用的一种布局方式&#xff0c;它能够帮助我们以更灵活、更美观的方式展示数据。本文将详细介绍 CSS 列表的创建、样式设置以及常用技巧&#xff0c;帮助您更好地掌握这一重要技能。 CSS 列表概述 CSS 列表主要包括两种类型&#xff1a…

spring中的@Cacheable缓存

1. 使用方法 在方法上面加上注解Cacheable&#xff0c; OverrideCacheable(cacheNames "userCache", key "#id")public User getUserById(Long id) {System.out.println("查询数据库了");return getById(id);}如果你的项目中引入了&#xff…

Node.js特训专栏-实战进阶:9.MySQL连接池配置与优化

🔥 欢迎来到 Node.js 实战专栏!在这里,每一行代码都是解锁高性能应用的钥匙,让我们一起开启 Node.js 的奇妙开发之旅! Node.js 特训专栏主页 专栏内容规划详情 MySQL连接池配置与优化:提升数据库交互性能的关键 一、MySQL连接池基础概念 1.1 什么是连接池? 连接池是…

【innovus基础】- 如何手动画线?

后端实现的过程就是将逻辑连线变为物理的金属连线的过程。 1、打开Pin shape的Visible 和 Selected开关&#xff0c;使其可见并可选 2、选中想要画线的IOCell 3、鼠标选中对应的pin 4、使用dbGet 获取此pin脚逻辑连线net的名字&#xff1b; dbGet selected.net.name 5、使用画…

element-plus限制日期可选范围(这里以7天为例)

element-plus日期范围限制功能实现逻辑 1. 需求&#xff1a;通过限制时间的可选范围减少请求的数据量 2. 实现效果&#xff1a; 日期选择器做限制 3. 代码逻辑&#xff1a; 思路&#xff1a;通过calendar-change获取开始日期&#xff0c;然后通过disabled-date禁用不满足条件…

机器学习2-梯度下降与反向传播

损失函数 与 平均方差函数 傻傻分不清 损失函数是概念&#xff1b;平均方差函数是具体的实现 损失函数&#xff08;如均方误差 MSE&#xff09;用于衡量模型预测值与真实值之间的差距。损失越小&#xff0c;说明模型对当前数据的拟合越好。 但模型并非拟合度越高越好&#xf…

安全生产风险管控平台:企业安全管理的智能化解决方案

在工业生产、建筑施工、能源化工等领域&#xff0c;安全生产是企业可持续发展的基石。然而&#xff0c;传统安全管理模式依赖人工巡检、纸质记录和事后处理&#xff0c;难以满足现代化企业的高效风险管控需求。安全生产风险管控平台应运而生&#xff0c;它利用物联网、大数据、…

如何保证数据库与 Redis 缓存的一致性?

在现代互联网应用中&#xff0c;Redis 缓存几乎是性能优化的标配。但在使用过程中&#xff0c;一个绕不过去的问题就是&#xff1a; 如何保证 Redis 缓存与数据库之间的数据一致性&#xff1f; 特别是在高并发场景下&#xff0c;读写操作错位可能导致缓存中出现脏数据&#xff…

现代 JavaScript (ES6+) 入门到实战(三):字符串与对象的魔法升级—模板字符串/结构赋值/展开运算符

在前两篇&#xff0c;我们升级了变量和函数。今天&#xff0c;我们要给 JavaScript 中最常用的两种数据类型——字符串&#xff08;String&#xff09;和对象&#xff08;Object&#xff09;——装备上 ES6 带来的强大魔法。 准备好告别丑陋的 号拼接和重复的对象属性赋值了吗…

GitLab 备份恢复与配置迁移详尽教程(实战版)

&#x1f6e0; GitLab 备份恢复与配置迁移详尽教程&#xff08;实战版&#xff09; &#x1f9f1; 一、环境准备 1.1 检查版本一致性 恢复目标机 GitLab 版本必须与备份文件所用版本一致或兼容&#xff08;推荐相同版本&#xff09; 查看当前 GitLab 版本&#xff1a; sudo g…

英飞凌高性能BMS解决方案助力汽车电动化

随着电动汽车越来越被大众接受&#xff0c;车辆电气化、智能化程度越来越高&#xff0c;如何提高电动汽车的续航里程&#xff0c;同时保障车辆安全可靠持久运行是当前最主要的技术难题之一。而先进的电池管理系统 (BMS)有助于克服电动汽车广泛普及的关键障碍&#xff1a;续航里…

react + ant-design实现数字对比动画效果:当新获取的数字比之前展示的数字多或少2时,显示“+2”或“-2”的动画效果

react ant-design实现数字对比动画效果&#xff1a;当新获取的数字比之前展示的数字多或少2时&#xff0c;显示“2”或“-2”的动画效果 1. 创建独立的 AnimatedValue 组件 // components/AnimatedValue/index.jsx import React, { useState, useEffect, useRef } from reac…

自动化测试--Appium和ADB及常用指令

1.Appium Appium工具库&#xff1a; appium server&#xff1a;服务器&#xff08;类似于浏览器的驱动&#xff09;&#xff0c;核心进行客户端命令的接受&#xff0c;完成设备的自动化指令 appium client&#xff1a;客户端&#xff0c;让代码进行调用&#xff0c;发送自动化的…

2025.6.29总结

有一点我很好奇&#xff0c;工作后&#xff0c;我该拿什么去衡量自己的进步呢&#xff1f; 在我的大学四年&#xff0c;确实有个量化的标准&#xff0c;读了多少本书&#xff0c;写了多少篇总结&#xff0c;多少篇技术博客&#xff0c;多少行代码&#xff0c;运动了多少公里&a…

Docker 部署 Kong云原生API网关

Docker 部署 Kong云原生API网关 本指南提供了在 Docker Compose 上配置 Kong Gateway 的步骤&#xff0c;基于有数据库模式的配置。本指南中使用的数据库是 PostgreSQL。 前置条件 准备一台Ubuntu服务器&#xff1a; 节点IP: 192.168.73.11操作系统&#xff1a; Ubuntu 24…

深度剖析 Apache Pulsar:架构、优势与选型指南

Apache Pulsar 是一款云原生分布式消息流平台&#xff0c;融合了消息队列、流处理和存储能力&#xff0c;采用独特的“存储计算分离”架构&#xff08;Broker 无状态 BookKeeper 持久化存储&#xff09;。以下从核心特性、对比优势及适用场景展开分析&#xff1a; 一、Pulsar…

java 导出word 实现循环表格

如果是固定的值 用 {{}} 即可 但是如果是循环表格&#xff0c;那么就需要制定模板为如图 然后在处理表格数据时候&#xff1a; /*** 传入 节点对象 返回生成的word文档* param flangeJoint* return* throws IOException*/private XWPFTemplate getXwpfTemplate(CmComplaintEn…