分页查询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测试接口

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/bicheng/12966.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

47 tcp网络程序

网路聊天 API详解 下面用到的API&#xff0c;都在sys/socket.h中 socket (): socket() 打开一个网络通讯端口&#xff0c;如果成功的话&#xff0c;就像open() 一样返回一个文件描述符应用程序可以像读文件一样用read/write在网络上收发数据如果调用出错返回-1对于IPv4&am…

Day28 代码随想录打卡|栈与队列篇---逆波兰表达式求值

题目&#xff08;leecode T150&#xff09;&#xff1a; 给你一个字符串数组 tokens &#xff0c;表示一个根据 逆波兰表示法 表示的算术表达式。 请你计算该表达式。返回一个表示表达式值的整数。 注意&#xff1a; 有效的算符为 、-、* 和 / 。每个操作数&#xff08;运算…

机械手避障如何选择激光雷达?

在选择用于机械手避障的激光雷达时&#xff0c;应该考虑以下主要技术参数&#xff1a; 测量范围&#xff1a;激光雷达的测量范围决定了它能够检测到的最大距离。您需要根据机械手的应用场景和工作环境来选择合适的测量范围。 精度&#xff1a;精度是激光雷达测量结果的重要参数…

ENZO--Leptin (human) ELISA kit

瘦素(Leptin)是由ob基因编码、在脂肪组织中生成的一种脂肪代谢调控产物&#xff0c;在代谢和调控体重等方面发挥重要作用。它通过下丘脑中的瘦素受体发出信号&#xff0c;降低食欲&#xff0c;增加能量消耗。在外周组织中&#xff0c;瘦素能拮抗胰岛素信号传导&#xff0c;增加…

Windows只能安装在GPT磁盘上

转换磁盘分区形式 步骤1. 先按照正常流程使用Windows系统安装光盘或系统U盘引导计算机。 步骤2. 在Windows安装程序中点击“开始安装”&#xff0c;然后按ShiftF10打开命令提示符。 步骤3. 依次输入以下命令&#xff0c;并在每一行命令后按一次Enter键执行。 步骤4. 等待转换…

黑马基于Web-socket的java聊天室基本解析

要是用Web-socket协议&#xff0c;我们要前端upgrade升级成web-socket协议 首先我们要引入springboot的websocket起步依赖&#xff0c;这样子方便使用&#xff0c;自己指定版本注意 <dependency><groupId>org.springframework.boot</groupId><artifactId&…

Django视图Views

Views视图 HttpRequest 和HttpResponse Django中的视图主要用来接受web请求&#xff0c;并做出响应。视图的本质就是一个Python中的函数视图的响应分为两大类 1)以Json数据形式返回(JsonResponse) 2)以网页的形式返回 2.1)重定向到另一个网页 (HttpRe…

Mini Cheetah 代码分析(八)基于零空间的任务分级

一、主要公式 二、源代码注释 三、相关原理解释 一、主要公式 二、源代码注释 该功能的实现在文件KinWBC.cpp中的FindConfiguration函数&#xff0c;主要看注释&#xff0c;与公式是能够对应起来的&#xff0c;由第0个任务&#xff0c;也就是接触任务开始进行迭代&#xff0…

Java类和对象(二)—— 封装,static 关键字与代码块

前言 在面向对象的编程语言中&#xff0c;有三大特性&#xff1a;封装、继承和多态~~ 今天我们就来学习封装的知识 封装 什么是封装 在现实生活中&#xff0c;我们经常使用手机来进行沟通与交流&#xff0c;实际上我们拿到的手机是被封装好的&#xff0c;精美的屏幕&a…

关键字详解

1.用于定义访问权限修饰符的关键字 面向对象程序三大特性&#xff1a;封装、继承、多态。 1.1 访问权限符 Java 中主要通过类和访问权限来实现封装&#xff1a; 类可以将数据以及封装数据的方法结合在一起 &#xff0c;更符合人类对事物的认知&#xff0c;而访问权限用来控制…

5月15日,机器人任务挑战赛(无人协同系统)第二期培训即将开启!

一.大赛培训通知 本月起&#xff0c;卓翼飞思实验室将针对机器人任务挑战赛&#xff08;无人协同系统&#xff09;赛项内容开启赛事培训计划&#xff0c;采用“线上线下”相结合的培训模式&#xff0c;围绕赛事关键技术&#xff0c;让您轻松应对比赛。本期培训为第二期&#x…

Go微服务: 日志系统ELK核心架构设计

微服务日志系统建设 1 &#xff09;为什么需要日志系统 业务发展越来越庞大&#xff0c;服务器越来越多各种访问日志&#xff0c;应用日志&#xff0c;错误日志量越来越多&#xff0c;无法管理开发人员排查问题&#xff0c;需要到服务器上查日志 2 &#xff09;Elastic Stack…

惠普打印机无线网络连接设置

休息一下&#xff0c;灌个水。这次没多少内容&#xff0c;具体步骤惠普官网上都有&#xff0c;唯一增加的是对安装过程中踩的坑做了一个说明。 一&#xff0e;打印机无线网络连接设置步骤 惠普打印机设置无线网络连接&#xff0c;共16个步骤。 1. 在电脑上打开任意浏览器&am…

HAProxy系列文章二《Patroni+ETCD+PG14+HAProxy的安装部署》

瀚高数据库 目录 文档用途 详细信息 文档用途 本文主要介绍Patroni架构下单点HAProxy的安装部署&#xff0c;通过单点HAProxy实现数据库的负载均衡。本文为HAProxy系列文章之一&#xff0c;其他相关文章请点击文档下方的相关文档链接进行详细查看&#xff0c;文章内不在赘述。…

Spring MVC(四) 数据校验

在开发过程中有一环必不可少的部分就是数据校验&#xff0c;用户在页面中填写的数据通过表单提交时&#xff0c;前端的JS可以做一些是否合法性的验证&#xff0c;比如是否为空、两次密码是否一致、格式是否正确等等验证。当数据到了后台控制器&#xff0c;为了确保程序的健壮性…

内网环境ubuntu设置静态ip、DNS、路由,不影响网络访问

内网环境通常是有线的&#xff0c;通过服务器的ip、mac、dns地址访问网络才生效的&#xff0c;如果ip地址变了&#xff0c;就不能访问网络了。 如果你的ip地址变了&#xff0c;或者要防止ip变更影响网络访问&#xff0c;就要设置 1、依次点击右上角的电源-设置&#xff0c;在打…

公司里的“卷王”,是主动卷还是迫于无奈?

先来唠唠 “卷” 这个词是近几年流行起来的网络用语&#xff0c;它是内卷的简化形式&#xff0c;“内卷”本来是一个名不见经传的普通词语&#xff0c;经网络流传&#xff0c;很多人就用其来指代非理性的内部竞争或“被自愿”竞争。 在现代职场&#xff0c;有一群人被戏称为&…

三分钟快速上手SpringSecurity框架

导入依赖框架 web 框架(spring-boot-starter-web) <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency> springSecurity 框架(spring-boot-starter-security) <de…

基于单片机的空气质量检测系统设计(51+4G版)-设计说明书

设计摘要&#xff1a; 本设计是基于单片机的空气质量检测系统设计涉及以下主要功能&#xff0c;旨在监测甲烷和一氧化碳的浓度&#xff0c;并在浓度过高时采取相应措施&#xff0c;以确保室内空气质量的安全。该系统使用传感器对甲烷和一氧化碳的浓度进行检测。传感器将收集到…

人物介绍模板 PSD 源文件免费获取

免费获取 下载链接在最后&#xff01; 下载链接在最后&#xff01; 下载链接在最后&#xff01; 下载链接在最后&#xff01; 下载链接在最后&#xff01; 链接&#xff1a;https://pan.baidu.com/s/1sq3e6djMdZt76Sh_uqVxWg 提取码&#xff1a;naun