MybatisPlus分页插件的使用

MybatisPlus分页插件的使用

实现医院列表展示

1. 添加service分页接口与实现

在HospitalService类添加分页接口

/**
 * 分页查询
 * @param page 当前页码
 * @param limit 每页记录数
 * @param hospitalQueryVo 查询条件
 * @return
*/
Page<Hospital> selectPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo);

HospitalServiceImpl类实现分页

@Override
public Page<Hospital> selectPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
      Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
//0为第一页
Pageable pageable = PageRequest.of(page-1, limit, sort);

      Hospital hospital = new Hospital();
      BeanUtils.copyProperties(hospitalQueryVo, hospital);

//创建匹配器,即如何使用查询条件
ExampleMatcher matcher = ExampleMatcher.matching() //构建对象
.withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING) //改变默认字符串匹配方式:模糊查询
.withIgnoreCase(true); //改变默认大小写忽略方式:忽略大小写

      //创建实例
Example<Hospital> example = Example.of(hospital, matcher);
      Page<Hospital> pages = hospitalRepository.findAll(example, pageable);

return pages;
   }

2. 添加controller方法

添加com.atguigu.yygh.hosp.controller.HospitalController类

package com.atguigu.yygh.hosp.controller;

@Api(tags = "医院管理接口")
@RestController
@RequestMapping("/admin/hosp/hospital")
public class HospitalController {

@Autowired
private HospitalService hospitalService;

@ApiOperation(value = "获取分页列表")
@GetMapping("{page}/{limit}")
public Result index(
@ApiParam(name = "page", value = "当前页码", required = true)
@PathVariable Integer page,

@ApiParam(name = "limit", value = "每页记录数", required = true)
@PathVariable Integer limit,

@ApiParam(name = "hospitalQueryVo", value = "查询对象", required = false)
                HospitalQueryVo hospitalQueryVo) {
return Result.ok(hospitalService.selectPage(page, limit, hospitalQueryVo));
   }

}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Mybatis-Plus 是 Mybatis 的增强工具,在 Mybatis 的基础上扩展了很多实用的功能,其中包括分页插件使用 Mybatis-Plus 分页插件非常简单,只需要按照以下步骤操作即可: 1. 在 pom.xml 文件中添加 Mybatis-Plus 和分页插件的依赖: ```xml <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.0.6</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-extension</artifactId> <version>3.0.6</version> </dependency> ``` 2. 在 Mybatis 的配置文件中添加分页插件的配置: ```xml <plugins> <plugin interceptor="com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor"> <property name="dialec" value="mysql"/> </plugin> </plugins> ``` 3. 在代码中使用分页查询: ```java // 构造分页对象 Page<User> page = new Page<>(1, 10); // 执行分页查询 IPage<User> userPage = userMapper.selectPage(page, new QueryWrapper<User>().lambda().ge(User::getAge, 18)); // 获取分页结果 List<User> userList = userPage.getRecords(); long total = userPage.getTotal(); ``` 以上代码中,首先构造了一个分页对象 `Page<User>`,该对象表示要查询第 1 页,每页 10 条记录。然后使用 `selectPage` 方法执行分页查询,该方法的第一个参数是分页对象,第二个参数是查询条件。最后通过 `userPage` 对象获取分页结果。 注意,在查询条件中使用了 `QueryWrapper`,这是 Mybatis-Plus 提供的一个方便构建查询条件的工具类。 以上就是使用 Mybatis-Plus 分页插件的基本步骤,希望能帮到你。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值