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

相关文章

Java调用C++动态库超详细步骤讲解(附源码)

《Java调用C++动态库超详细步骤讲解(附源码)》C语言因其高效和接近硬件的特性,时常会被用在性能要求较高或者需要直接操作硬件的场合,:本文主要介绍Java调用C++动态库的相关资料,文中通过代... 目录一、直接调用C++库第一步:动态库生成(vs2017+qt5.12.10)第二步:Java调用C++

Mybatis 传参与排序模糊查询功能实现

《Mybatis传参与排序模糊查询功能实现》:本文主要介绍Mybatis传参与排序模糊查询功能实现,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录一、#{ }和${ }传参的区别二、排序三、like查询四、数据库连接池五、mysql 开发企业规范一、#{ }和${ }传参的

Ubuntu中远程连接Mysql数据库的详细图文教程

《Ubuntu中远程连接Mysql数据库的详细图文教程》Ubuntu是一个以桌面应用为主的Linux发行版操作系统,这篇文章主要为大家详细介绍了Ubuntu中远程连接Mysql数据库的详细图文教程,有... 目录1、版本2、检查有没有mysql2.1 查询是否安装了Mysql包2.2 查看Mysql版本2.

基于SpringBoot+Mybatis实现Mysql分表

《基于SpringBoot+Mybatis实现Mysql分表》这篇文章主要为大家详细介绍了基于SpringBoot+Mybatis实现Mysql分表的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可... 目录基本思路定义注解创建ThreadLocal创建拦截器业务处理基本思路1.根据创建时间字段按年进

Python3.6连接MySQL的详细步骤

《Python3.6连接MySQL的详细步骤》在现代Web开发和数据处理中,Python与数据库的交互是必不可少的一部分,MySQL作为最流行的开源关系型数据库管理系统之一,与Python的结合可以实... 目录环境准备安装python 3.6安装mysql安装pymysql库连接到MySQL建立连接执行S

Java枚举类实现Key-Value映射的多种实现方式

《Java枚举类实现Key-Value映射的多种实现方式》在Java开发中,枚举(Enum)是一种特殊的类,本文将详细介绍Java枚举类实现key-value映射的多种方式,有需要的小伙伴可以根据需要... 目录前言一、基础实现方式1.1 为枚举添加属性和构造方法二、http://www.cppcns.co

MySQL双主搭建+keepalived高可用的实现

《MySQL双主搭建+keepalived高可用的实现》本文主要介绍了MySQL双主搭建+keepalived高可用的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、测试环境准备二、主从搭建1.创建复制用户2.创建复制关系3.开启复制,确认复制是否成功4.同

C#如何动态创建Label,及动态label事件

《C#如何动态创建Label,及动态label事件》:本文主要介绍C#如何动态创建Label,及动态label事件,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录C#如何动态创建Label,及动态label事件第一点:switch中的生成我们的label事件接着,

SpringCloud动态配置注解@RefreshScope与@Component的深度解析

《SpringCloud动态配置注解@RefreshScope与@Component的深度解析》在现代微服务架构中,动态配置管理是一个关键需求,本文将为大家介绍SpringCloud中相关的注解@Re... 目录引言1. @RefreshScope 的作用与原理1.1 什么是 @RefreshScope1.

MyBatis 动态 SQL 优化之标签的实战与技巧(常见用法)

《MyBatis动态SQL优化之标签的实战与技巧(常见用法)》本文通过详细的示例和实际应用场景,介绍了如何有效利用这些标签来优化MyBatis配置,提升开发效率,确保SQL的高效执行和安全性,感... 目录动态SQL详解一、动态SQL的核心概念1.1 什么是动态SQL?1.2 动态SQL的优点1.3 动态S