分页查询PageHelper插件分页条件查询(xml映射文件,动态SQL)

2024-05-15 20:44

本文主要是介绍分页查询PageHelper插件分页条件查询(xml映射文件,动态SQL),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

黑马程序员JavaWeb开发教程

文章目录

  • 一、分页查询-分析
  • 二、分页查询-实现
    • 1. 实现思路
      • 1.1 controller
      • 1.2 service
      • 1.3 mapper
    • 1.4 postman测试接口
  • 三、分页查询-PageHelper插件
    • 1. 引入pageHelper插件的依赖
    • 2. 修改原来的代码
      • 2.1 mapper
      • 2.2 serviceimpl
      • 2.3 postman测试接口
  • 四、分页查询-条件查询
    • 1. 首先根据分页查询的需求写出查询的SQL语句
    • 2. 创建动态SQL
      • 2.1 创建xml映射文件
      • 2.2 controller
      • 2.3 service
      • 2.4 mapper
      • 2.5 postman测试接口

一、分页查询-分析

在这里插入图片描述

  • 实体类PageBean
package com.itheima.mytlias.pojo;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;import java.util.List;@Data
@AllArgsConstructor
@NoArgsConstructor
public class PageBean {private Long total;//总记录数private List rows;//当前页的数据
}

二、分页查询-实现

1. 实现思路

  • 请求参数:页码、每页展示记录数
  • 响应结果:总记录数、结果列表

在这里插入图片描述

1.1 controller

package com.itheima.mytlias.controller;import com.itheima.mytlias.pojo.PageBean;
import com.itheima.mytlias.pojo.Result;
import com.itheima.mytlias.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {@Autowiredprivate EmpService empService;@GetMapping//@RequestParam:为了给形参指定默认值public Result page(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "2") Integer pageSize) {//打印日志//调用servicePageBean pageBean = empService.page(page, pageSize);//返回结果return Result.success(pageBean);}
}

1.2 service

  1. service
package com.itheima.mytlias.service;import com.itheima.mytlias.pojo.PageBean;public interface EmpService {/*** 分页查询* @return*/PageBean page(Integer page,Integer pageSize);
}
  1. impl
package com.itheima.mytlias.service.impl;import com.itheima.mytlias.mapper.EmpMapper;
import com.itheima.mytlias.pojo.Emp;
import com.itheima.mytlias.pojo.PageBean;
import com.itheima.mytlias.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class EmpServiceImpl implements EmpService {@Autowiredprivate EmpMapper empMapper;/*** 分页查询** @return*/@Overridepublic PageBean page(Integer page, Integer pageSize) {//调用mapper进行分页查询//列表数据List<Emp> empList = empMapper.list(page, pageSize);//总数Long count = empMapper.count();PageBean pageBean = new PageBean(count, empList);return pageBean;}
}

1.3 mapper

package com.itheima.mytlias.mapper;import com.itheima.mytlias.pojo.Emp;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.web.bind.annotation.PathVariable;import java.util.List;@Mapper
public interface EmpMapper {/*** 查询总记录数** @return*/@Select("select count(*) from emp")public Long count();/*** 查询本页员工列表** @param start* @param pageSize* @return*/@Select("select * from emp limit #{start},#{pageSize}")public List<Emp> list(@Param("start") Integer start, @Param("pageSize") Integer pageSize);
}

1.4 postman测试接口

在这里插入图片描述

三、分页查询-PageHelper插件

1. 引入pageHelper插件的依赖

  • 在pom.xml中添加以下代码
<!--        pagehelper分页插件--><dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper-spring-boot-starter</artifactId><version>1.4.2</version></dependency>

2. 修改原来的代码

  • 除了EmpMapper和EmpServiceImpl中的代码意外不用修改其他的代码

2.1 mapper

package com.itheima.mytlias.mapper;import com.itheima.mytlias.pojo.Emp;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.web.bind.annotation.PathVariable;import java.util.List;@Mapper
public interface EmpMapper {/*** 使用pageHelper的话,只需要定义一个简单的查询就可以* @return*/@Select("select * from emp")public List<Emp> list();
}

2.2 serviceimpl

package com.itheima.mytlias.service.impl;import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mytlias.mapper.EmpMapper;
import com.itheima.mytlias.pojo.Emp;
import com.itheima.mytlias.pojo.PageBean;
import com.itheima.mytlias.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class EmpServiceImpl implements EmpService {@Autowiredprivate EmpMapper empMapper;/*** 分页查询** @return*/@Overridepublic PageBean page(Integer page, Integer pageSize) {//使用pagehelper指定查询页码,和每页查询数据PageHelper.startPage(page,pageSize);//执行查询List<Emp> empList = empMapper.list();Page<Emp> p= (Page<Emp>)empList;//强制转换成Page类型Long count=p.getTotal();List<Emp> result = p.getResult();//封装为 PageBeanPageBean pageBean = new PageBean(count,result);return pageBean;}
}

2.3 postman测试接口

在这里插入图片描述

四、分页查询-条件查询

1. 首先根据分页查询的需求写出查询的SQL语句

select * from emp
wherename like concat('%','张','%')and gender=1and entrydate between '2000-01-01' and '2010-01-01'
order by update_time desc;

2. 创建动态SQL

2.1 创建xml映射文件

  1. 同包同名
    在这里插入图片描述
  2. 去mybatis中文网复制配置信息,就是下边的
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><!--        namespace:EmpMapper的全类名-->
<mapper namespace="org.mybatis.example.BlogMapper"><!--    id:与方法名一致resultType:返回单条记录的全类名--><select id="selectBlog" resultType="Blog">select * from Blog where id = #{id}</select>
</mapper>
  1. 创建动态SQL
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--        namespace:EmpMapper的全类名-->
<mapper namespace="com.itheima.mytlias.mapper.EmpMapper"><!--    带条件的分页查询-动态查询--><!--    id:与方法名一致resultType:返回单条记录的全类名--><select id="list" resultType="com.itheima.mytlias.pojo.Emp">select * from emp<where><if test="name!=null">name like concat('%',#{name},'%')</if><if test="gender!=null">and gender=#{gender}</if><if test="begin!=null and end!=null">and entrydate between #{begin} and #{end}</if></where>order by update_time desc</select>
</mapper>

2.2 controller

package com.itheima.mytlias.controller;import com.itheima.mytlias.pojo.PageBean;
import com.itheima.mytlias.pojo.Result;
import com.itheima.mytlias.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;import java.time.LocalDate;@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {@Autowiredprivate EmpService empService;@GetMapping//@RequestParam:为了给形参指定默认值//@DateTimeFormat:日期时间类型的参数,需要使用该注解指定格式public Result page(String name, Short gender,@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end,@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "2") Integer pageSize) {//打印日志log.info("参数 {},{},{},{},{},{}", name, gender, begin, end, page, pageSize);//调用servicePageBean pageBean = empService.page(name, gender, begin, end, page, pageSize);//返回结果return Result.success(pageBean);}
}

2.3 service

  1. service
package com.itheima.mytlias.service;import com.itheima.mytlias.pojo.PageBean;import java.time.LocalDate;public interface EmpService {/*** 分页查询** @return*/PageBean page(String name, Short gender, LocalDate begin, LocalDate end, Integer page, Integer pageSize);
}
  1. impl
package com.itheima.mytlias.service.impl;import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mytlias.mapper.EmpMapper;
import com.itheima.mytlias.pojo.Emp;
import com.itheima.mytlias.pojo.PageBean;
import com.itheima.mytlias.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.time.LocalDate;
import java.util.List;@Service
public class EmpServiceImpl implements EmpService {@Autowiredprivate EmpMapper empMapper;/*** 分页查询** @return*/@Overridepublic PageBean page(String name, Short gender, LocalDate begin, LocalDate end, Integer page, Integer pageSize) {//使用pagehelper指定查询页码,和每页查询数据PageHelper.startPage(page, pageSize);//执行查询List<Emp> empList = empMapper.list(name, gender, begin, end);Page<Emp> p = (Page<Emp>) empList;//强制转换成Page类型Long count = p.getTotal();List<Emp> result = p.getResult();//封装为 PageBeanPageBean pageBean = new PageBean(count, result);return pageBean;}
}

2.4 mapper

package com.itheima.mytlias.mapper;import com.itheima.mytlias.pojo.Emp;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.web.bind.annotation.PathVariable;import java.time.LocalDate;
import java.util.List;@Mapper
public interface EmpMapper {/*** 使用pageHelper的话,只需要定义一个简单的查询就可以** @return*/
//    @Select("select * from emp")public List<Emp> list(@Param("name") String name, @Param("gender") Short gender, @Param("begin") LocalDate begin, @Param("end") LocalDate end);
}

2.5 postman测试接口

在这里插入图片描述

这篇关于分页查询PageHelper插件分页条件查询(xml映射文件,动态SQL)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/992872

相关文章

SQL BETWEEN 语句的基本用法详解

《SQLBETWEEN语句的基本用法详解》SQLBETWEEN语句是一个用于在SQL查询中指定查询条件的重要工具,它允许用户指定一个范围,用于筛选符合特定条件的记录,本文将详细介绍BETWEEN语... 目录概述BETWEEN 语句的基本用法BETWEEN 语句的示例示例 1:查询年龄在 20 到 30 岁

MySQL DQL从入门到精通

《MySQLDQL从入门到精通》通过DQL,我们可以从数据库中检索出所需的数据,进行各种复杂的数据分析和处理,本文将深入探讨MySQLDQL的各个方面,帮助你全面掌握这一重要技能,感兴趣的朋友跟随小... 目录一、DQL 基础:SELECT 语句入门二、数据过滤:WHERE 子句的使用三、结果排序:ORDE

MySQL MCP 服务器安装配置最佳实践

《MySQLMCP服务器安装配置最佳实践》本文介绍MySQLMCP服务器的安装配置方法,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下... 目录mysql MCP 服务器安装配置指南简介功能特点安装方法数据库配置使用MCP Inspector进行调试开发指

mysql中insert into的基本用法和一些示例

《mysql中insertinto的基本用法和一些示例》INSERTINTO用于向MySQL表插入新行,支持单行/多行及部分列插入,下面给大家介绍mysql中insertinto的基本用法和一些示例... 目录基本语法插入单行数据插入多行数据插入部分列的数据插入默认值注意事项在mysql中,INSERT I

一文详解MySQL如何设置自动备份任务

《一文详解MySQL如何设置自动备份任务》设置自动备份任务可以确保你的数据库定期备份,防止数据丢失,下面我们就来详细介绍一下如何使用Bash脚本和Cron任务在Linux系统上设置MySQL数据库的自... 目录1. 编写备份脚本1.1 创建并编辑备份脚本1.2 给予脚本执行权限2. 设置 Cron 任务2

SQL Server修改数据库名及物理数据文件名操作步骤

《SQLServer修改数据库名及物理数据文件名操作步骤》在SQLServer中重命名数据库是一个常见的操作,但需要确保用户具有足够的权限来执行此操作,:本文主要介绍SQLServer修改数据... 目录一、背景介绍二、操作步骤2.1 设置为单用户模式(断开连接)2.2 修改数据库名称2.3 查找逻辑文件名

SQL Server数据库死锁处理超详细攻略

《SQLServer数据库死锁处理超详细攻略》SQLServer作为主流数据库管理系统,在高并发场景下可能面临死锁问题,影响系统性能和稳定性,这篇文章主要给大家介绍了关于SQLServer数据库死... 目录一、引言二、查询 Sqlserver 中造成死锁的 SPID三、用内置函数查询执行信息1. sp_w

SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志

《SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志》在SpringBoot项目中,使用logback-spring.xml配置屏蔽特定路径的日志有两种常用方式,文中的... 目录方案一:基础配置(直接关闭目标路径日志)方案二:结合 Spring Profile 按环境屏蔽关

canal实现mysql数据同步的详细过程

《canal实现mysql数据同步的详细过程》:本文主要介绍canal实现mysql数据同步的详细过程,本文通过实例图文相结合给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的... 目录1、canal下载2、mysql同步用户创建和授权3、canal admin安装和启动4、canal

SQL中JOIN操作的条件使用总结与实践

《SQL中JOIN操作的条件使用总结与实践》在SQL查询中,JOIN操作是多表关联的核心工具,本文将从原理,场景和最佳实践三个方面总结JOIN条件的使用规则,希望可以帮助开发者精准控制查询逻辑... 目录一、ON与WHERE的本质区别二、场景化条件使用规则三、最佳实践建议1.优先使用ON条件2.WHERE用