当前位置: 首页 > news >正文

基于mybatis与PageHelper插件实现条件分页查询(3.19)

实现商品分页例子

        需要先引入mybatis与pagehelper插件,在pom.xml里

<!-- Mybatis -->
<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>3.0.3</version>
</dependency><!--分页插件PageHelper-->
<dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper-spring-boot-starter</artifactId><version>1.4.7</version>
</dependency>

        利用三层架构分别实现对应代码。

Controller层

@Slf4j
@RestController
@RequestMapping("/products")
public class ProductController {@Autowiredprivate ProductService productService;/*** 分页条件查询商品*/@GetMappingpublic Result page(ProductQueryParam queryParam) {log.info("条件分页查询, {}", queryParam);PageBean pageBean = productService.page(queryParam);return Result.success(pageBean);}
}

Service层

public interface ProductService {PageBean page(ProductQueryParam queryParam);
}

ServiceImpl层

@Slf4j
@Service
public class ProductServiceImpl implements ProductService {@Autowiredprivate ProductMapper productMapper;//条件分页查询商品数量@Overridepublic PageBean page(ProductQueryParam queryParam) {PageHelper.startPage(queryParam.getPage(), queryParam.getPageSize());Page<Product> productList = productMapper.page(queryParam);return new PageBean(productList.getTotal(), productList.getResult());}
}

Mapper层

@Slf4j
@Service
public class ProductServiceImpl implements ProductService {//条件分页查询商品数量@Overridepublic PageBean page(ProductQueryParam queryParam) {PageHelper.startPage(queryParam.getPage(), queryParam.getPageSize());Page<Product> productList = productMapper.page(queryParam);return new PageBean(productList.getTotal(), productList.getResult());}
}

Mapper.xml层

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.itheima.mapper.ProductMapper"><!--条件分页查询--><select id="page" resultType="com.itheima.pojo.Product">select p.*, b.name as brandName from tb_product p left join tb_brand b on p.brand_id = b.id<where><if test="name != null and name != ''">name like concat('%', #{name} ,'%')</if><if test="brandId != null">and brand_id = #{brandId}</if><if test="publishStatus != null">and publish_status = #{publishStatus}</if><if test="verifyStatus != null">and verify_status = #{verifyStatus}</if></where>order by create_time desc</select></mapper>

相关文章:

  • iOS中使用AWS上传zip文件到Minio上的oss平台上
  • 使用Matlab工具将RAW文件转化为TXT文件,用于FPGA仿真输入
  • 用Mac M4构建多架构Docker镜像指南
  • 若依框架免登陆、页面全屏显示、打开新标签页(看板大屏)
  • neo4j-community-3.5.5-unix.tar.gz安装
  • SpringBoot + Vue 实现云端图片上传与回显(基于OSS等云存储)
  • map和set封装
  • 开源身份和访问管理(IAM)解决方案:Keycloak
  • 信息收集之hack用的网络空间搜索引擎
  • 快速搭建 Cpolar 内网穿透(Mac 系统)
  • Spark-SQL连接Hive全攻略
  • 基础服务系列-Jupyter Notebook 支持JavaScript
  • 解决使用hc595驱动LED数码管亮度低的问题
  • MetaGPT智能体框架深度解析:记忆模块设计与应用实践
  • Versal Adaptive SoC AI Engine 知识分享6
  • 一图掌握 C++ 核心要点
  • 【阿里云大模型高级工程师ACP习题集】2.1 用大模型构建新人答疑机器人
  • 在CSDN的1095天(创作纪念日)
  • uniapp打ios包
  • 【数据结构和算法】4. 链表 LinkedList
  • 美国土安全部长餐厅遇窃,重要证件被盗走
  • 国家卫健委:无资质机构严禁开展产前筛查
  • 秦洪看盘|新热点涌现,A股活力渐显
  • 新华社经济随笔:把握不确定性中的确定性
  • 法官颁布紧急临时禁止令,中国留学生诉美国政府“首战胜利”
  • 不降息就走人?特朗普试图开先例罢免美联储主席,有无胜算