当前位置: 首页 > 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插件进行分页查询,支持大数据量的查询和分页返回。

http://www.mrgr.cn/news/200.html

相关文章:

  • Java 多线程练习2 (抽奖比较Runnable写法)
  • Nuxt3【项目配置】nuxt.config.ts
  • C++ //练习 17.18 修改你的程序,忽略包含“ei“但并非拼写错误的单词,如“albeit“和“neighbor“。
  • [Linux] 关于执行文件路径的变量:$PATH
  • 悬浮球 可拖拽-支持鼠标和触控
  • Python做统计图之美
  • 仕考网:考外省公务员可以调回本地吗?
  • 《Unity3D网络游戏实战》通用服务器框架
  • 【AI大模型】解锁AI智能:从注意力机制到Transformer,再到BERT与GPT的较量
  • 全球最强AI程序员 “Genie” 横空出世
  • eNSP 华为ACL配置
  • 初识Linux · 基本指令(1)
  • LLMs之Framework:Hugging Face Accelerate后端框架之FSDP和DeepSpeed的对比与分析
  • 深入理解 GO 语言并发
  • IO多路复用中的水平触发和边缘触发、Java NIO中的水平触发举例
  • 如何快速掌握一款MCU
  • Polars简明基础教程十二:可视化(二)
  • Spire.PDF for .NET【文档操作】演示:检测 PDF 文件是否为 PDF/A
  • SpringBoot 整合线程池如此简单
  • [每周一更]-(第110期):QT开发最佳实战(php/go/python/javascript)