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

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

要在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插件进行分页查询,支持大数据量的查询和分页返回。

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • Java基础之循环嵌套
  • K8S学习笔记(自用,不定期更新)
  • Docker-命令
  • Tomcat初篇
  • Java 网络编程练习
  • 新天龙八部3江山策仿官方+单机联网+GM工具
  • 【抓包】- Fiddler抓包教程,使用Fiddle抓取B站视频;ffmpeg的使用方法
  • Eureka入门指南:微服务注册与发现的基础概念
  • 软件开发感悟
  • go注册到eureka微服务
  • 构建AI知识库:4大优势+无限可能,共绘在线教育蓝图!
  • 【代码随想录算法训练营第42期 第七天 | LeetCode454.四数相加II、383. 赎金信、15. 三数之和、18. 四数之和】
  • 【Python快速入门和实践012】Python常用脚本-目标检测之查看数据集标签类别及对应数量
  • Python爬虫使用实例
  • 人脸操作:从检测到识别的全景指南
  • 实现windows 窗体的自己画,网上摘抄的,学习了
  • [原]深入对比数据科学工具箱:Python和R 非结构化数据的结构化
  • android 一些 utils
  • android高仿小视频、应用锁、3种存储库、QQ小红点动画、仿支付宝图表等源码...
  • CNN 在图像分割中的简史:从 R-CNN 到 Mask R-CNN
  • CSS盒模型深入
  • EOS是什么
  • HTTP--网络协议分层,http历史(二)
  • JavaScript的使用你知道几种?(上)
  • js递归,无限分级树形折叠菜单
  • js正则,这点儿就够用了
  • Laravel 菜鸟晋级之路
  • Laravel5.4 Queues队列学习
  • Leetcode 27 Remove Element
  • maya建模与骨骼动画快速实现人工鱼
  • Odoo domain写法及运用
  • orm2 中文文档 3.1 模型属性
  • PAT A1120
  • Python socket服务器端、客户端传送信息
  • Spring声明式事务管理之一:五大属性分析
  • Wamp集成环境 添加PHP的新版本
  • XML已死 ?
  • 百度小程序遇到的问题
  • 程序员最讨厌的9句话,你可有补充?
  • 发布国内首个无服务器容器服务,运维效率从未如此高效
  • 坑!为什么View.startAnimation不起作用?
  • 聊聊redis的数据结构的应用
  • 每个JavaScript开发人员应阅读的书【1】 - JavaScript: The Good Parts
  • 排序(1):冒泡排序
  • 如何将自己的网站分享到QQ空间,微信,微博等等
  • 如何解决微信端直接跳WAP端
  • 微服务入门【系列视频课程】
  • #define用法
  • #java学习笔记(面向对象)----(未完结)
  • #QT(一种朴素的计算器实现方法)
  • $.proxy和$.extend
  • (1)(1.11) SiK Radio v2(一)
  • (12)目标检测_SSD基于pytorch搭建代码
  • (2)空速传感器
  • (3)医疗图像处理:MRI磁共振成像-快速采集--(杨正汉)