在Spring Boot中使用MyBatis实现复杂查询和分页功能

ops/2024/9/24 6:46:59/

要在Spring Boot中使用MyBatis实现复杂查询和分页功能,通常会用到动态SQL以及分页插件。以下是具体的实现方法:

1. 增加复杂查询功能

使用MyBatis的<script>标签和<if>标签,可以实现动态SQL。这里我们以一个条件查询为例,根据ipsourceusername进行条件查询。

复杂查询Mapper方法:
import org.apache.ibatis.annotations.*;import java.util.List;@Mapper
public interface AlarmCdnBlockipMapper {// 其他方法保持不变@Select("<script>" +"SELECT * FROM alarm_cdn_blockip " +"WHERE 1=1 " +"<if test='ip != null and !ip.isEmpty()'>" +"AND ip = #{ip} " +"</if>" +"<if test='source != null and !source.isEmpty()'>" +"AND source = #{source} " +"</if>" +"<if test='username != null and !username.isEmpty()'>" +"AND username = #{username} " +"</if>" +"ORDER BY create_time DESC" +"</script>")List<AlarmCdnBlockip> findByConditions(@Param("ip") String ip, @Param("source") String source, @Param("username") String username);
}
  • 动态SQL<script>标签包含动态SQL语句,<if>标签用于条件判断。可以根据传入的参数生成不同的SQL语句。
  • 条件查询:这个查询方法会根据ipsourceusername进行条件过滤。如果某个参数未传入,则该条件不会出现在SQL语句中。

2. 增加分页功能

分页功能可以通过MyBatis的插件来实现,常用的分页插件是PageHelper。首先,需要添加PageHelper依赖。

Maven依赖:
<dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper-spring-boot-starter</artifactId><version>1.4.6</version>
</dependency>
配置分页插件:

application.properties中配置PageHelper插件。

# PageHelper配置
pagehelper.helper-dialect=mysql
pagehelper.reasonable=true
pagehelper.support-methods-arguments=true
pagehelper.params=count=countSql
使用分页功能:

在Service层中使用PageHelper.startPage()方法来设置分页参数。

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class AlarmCdnBlockipService {@Autowiredprivate AlarmCdnBlockipMapper alarmCdnBlockipMapper;// 其他方法保持不变public PageInfo<AlarmCdnBlockip> findByConditionsWithPaging(String ip, String source, String username, int pageNum, int pageSize) {// 开启分页PageHelper.startPage(pageNum, pageSize);// 执行查询List<AlarmCdnBlockip> result = alarmCdnBlockipMapper.findByConditions(ip, source, username);// 封装分页信息return new PageInfo<>(result);}
}
  • PageHelper.startPage(pageNum, pageSize):设置分页的页码和每页的条数。
  • PageInfo:封装了分页结果及相关的分页信息,如总条数、总页数、当前页等。

3. Controller层增加分页查询接口

Controller层可以增加分页查询的接口,将分页参数传递给Service层。

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;@RestController
@RequestMapping("/alarm-cdn-blockip")
public class AlarmCdnBlockipController {@Autowiredprivate AlarmCdnBlockipService alarmCdnBlockipService;// 其他方法保持不变@GetMapping("/search")public PageInfo<AlarmCdnBlockip> search(@RequestParam(required = false) String ip,@RequestParam(required = false) String source,@RequestParam(required = false) String username,@RequestParam(defaultValue = "1") int pageNum,@RequestParam(defaultValue = "10") int pageSize) {return alarmCdnBlockipService.findByConditionsWithPaging(ip, source, username, pageNum, pageSize);}
}
  • @RequestParam(defaultValue = "1") int pageNum:通过RequestParam接受页码和每页条数,默认值为1和10。
  • PageInfo:返回分页信息,包含查询结果和分页元数据。

4. 总结

通过以上实现,已经完成了复杂查询和分页功能:

  • 复杂查询:动态SQL实现灵活的条件查询。
  • 分页功能:通过PageHelper插件进行分页查询,支持大数据量的查询和分页返回。

http://www.ppmy.cn/ops/94698.html

相关文章

NoSQL 详细讲解

目录 NoSQL 概述 1.1 NoSQL 的定义1.2 NoSQL 与关系型数据库的区别1.3 NoSQL 的历史与发展 NoSQL 的特点与优势 2.1 灵活的数据模型2.2 高扩展性与分布式存储2.3 高性能2.4 大数据处理与实时分析 NoSQL 的类型 3.1 键值存储3.2 列族存储3.3 文档存储3.4 图数据库 NoSQL 的适…

[FBCTF2019]RCEService1

打开题目 它给出了提示要求json格式&#xff0c;先尝试一下{"cmd":"ls"} 的确可以执行。接下来就记录过滤了那些关键字。发现键盘上有的特殊符号好像都被过滤。 flag在home目录下&#xff0c;不知道为什么find命令返回为空。。。 cat在这里仍然不能用。…

如何用 LangChain 实现一个Zero Shot智能决策器(附源码)

写在前面 最近一直在研究Agent和Tool的使用&#xff0c;今天给大家带来一篇何枝大佬&#xff08;知乎何枝&#xff09;的文章《如何用LangChain实现一个Zero Shot智能决策器》&#xff0c;并附上源码。 知乎&#xff1a;https://zhuanlan.zhihu.com/p/627333499LangChain是当…

MySQL——单表查询(二)按条件查询(2)带 IN 关键字的查询

IN 关键字用于判断某个字段的值是否在指定集合中&#xff0c;如果字段的值在集合中&#xff0c;则满足条件&#xff0c;该字段所在的记录将被查询出来。其语法格式如下所示&#xff1a; SELECT *|字段名 1,字段名 2,… FROM 表名 WHERE 字段名 [NOT〕IN(元素 1,元素 2,…) 在上…

WMS如何实现与TMS的双向信息流?

要实现仓储管理系统&#xff08;WMS&#xff09;与运输管理系统&#xff08;TMS&#xff09;之间的双向信息流&#xff0c;可以通过以下几个步骤进行集成&#xff1a; —————————————————— 1、需求分析&#xff1a; 1):确定WMS和TMS之间需要传输的数据类型&…

Vue2 element-ui引入 及定制element-ui主题

一. 完整引入 1.安装element-ui npm install element-ui -S 2.页面配置&#xff08;在 main.js 中&#xff09; // 引入... import ElementUI from element-ui; import element-ui/lib/theme-chalk/index.css;// 注册 Vue.use(ElementUI);new Vue({el: #app,render: h > h…

Docker部署superset SUPERSET_SECRET_KEY报错

Docker部署superset过程&#xff0c;记录一下 docker安装&#xff0c;镜像源替换 安装好docker&#xff0c;不详细说明&#xff0c;这里主要提一下拉镜像有问题的&#xff0c;替换一下镜像源vi /etc/docker/daemon.json {"registry-mirrors": ["https://docker.…

【鸿蒙学习】HarmonyOS应用开发者基础 - 构建更加丰富的页面之Navigation(二)

学完时间&#xff1a;2024年8月14日 一、前言叨叨 学习HarmonyOS的第六课&#xff0c;人数又成功的降了500名左右&#xff0c;到了3575人了。 本文接上一文章【鸿蒙学习】HarmonyOS应用开发者基础 - 构建更加丰富的页面&#xff08;一&#xff09;&#xff0c;继续记录构建更…