分页查询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

相关文章

Oracle查询表结构建表语句索引等方式

《Oracle查询表结构建表语句索引等方式》使用USER_TAB_COLUMNS查询表结构可避免系统隐藏字段(如LISTUSER的CLOB与VARCHAR2同名字段),这些字段可能为dbms_lob.... 目录oracle查询表结构建表语句索引1.用“USER_TAB_COLUMNS”查询表结构2.用“a

MySQL中EXISTS与IN用法使用与对比分析

《MySQL中EXISTS与IN用法使用与对比分析》在MySQL中,EXISTS和IN都用于子查询中根据另一个查询的结果来过滤主查询的记录,本文将基于工作原理、效率和应用场景进行全面对比... 目录一、基本用法详解1. IN 运算符2. EXISTS 运算符二、EXISTS 与 IN 的选择策略三、性能对比

MySQL常用字符串函数示例和场景介绍

《MySQL常用字符串函数示例和场景介绍》MySQL提供了丰富的字符串函数帮助我们高效地对字符串进行处理、转换和分析,本文我将全面且深入地介绍MySQL常用的字符串函数,并结合具体示例和场景,帮你熟练... 目录一、字符串函数概述1.1 字符串函数的作用1.2 字符串函数分类二、字符串长度与统计函数2.1

SQL Server跟踪自动统计信息更新实战指南

《SQLServer跟踪自动统计信息更新实战指南》本文详解SQLServer自动统计信息更新的跟踪方法,推荐使用扩展事件实时捕获更新操作及详细信息,同时结合系统视图快速检查统计信息状态,重点强调修... 目录SQL Server 如何跟踪自动统计信息更新:深入解析与实战指南 核心跟踪方法1️⃣ 利用系统目录

MySQL 内存使用率常用分析语句

《MySQL内存使用率常用分析语句》用户整理了MySQL内存占用过高的分析方法,涵盖操作系统层确认及数据库层bufferpool、内存模块差值、线程状态、performance_schema性能数据... 目录一、 OS层二、 DB层1. 全局情况2. 内存占js用详情最近连续遇到mysql内存占用过高导致

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.

Mysql中设计数据表的过程解析

《Mysql中设计数据表的过程解析》数据库约束通过NOTNULL、UNIQUE、DEFAULT、主键和外键等规则保障数据完整性,自动校验数据,减少人工错误,提升数据一致性和业务逻辑严谨性,本文介绍My... 目录1.引言2.NOT NULL——制定某列不可以存储NULL值2.UNIQUE——保证某一列的每一

解密SQL查询语句执行的过程

《解密SQL查询语句执行的过程》文章讲解了SQL语句的执行流程,涵盖解析、优化、执行三个核心阶段,并介绍执行计划查看方法EXPLAIN,同时提出性能优化技巧如合理使用索引、避免SELECT*、JOIN... 目录1. SQL语句的基本结构2. SQL语句的执行过程3. SQL语句的执行计划4. 常见的性能优

SQL Server 中的 WITH (NOLOCK) 示例详解

《SQLServer中的WITH(NOLOCK)示例详解》SQLServer中的WITH(NOLOCK)是一种表提示,等同于READUNCOMMITTED隔离级别,允许查询在不获取共享锁的情... 目录SQL Server 中的 WITH (NOLOCK) 详解一、WITH (NOLOCK) 的本质二、工作

MySQL 强制使用特定索引的操作

《MySQL强制使用特定索引的操作》MySQL可通过FORCEINDEX、USEINDEX等语法强制查询使用特定索引,但优化器可能不采纳,需结合EXPLAIN分析执行计划,避免性能下降,注意版本差异... 目录1. 使用FORCE INDEX语法2. 使用USE INDEX语法3. 使用IGNORE IND