Mybatis plus入门(十):pagehelper分页插件
案例一
@RequestMapping("/test2") @ResponseBody public String test2(){ PageInfo<User> info = PageHelper.startPage( 1, 2).doSelectPageInfo(() -> mapper.selectById(1) ); List<User> list = info.getList(); System.out.println(list); return "success"; } @RequestMapping("/test3") @ResponseBody public String test3(){ List<Long> ids = Arrays.asList(1L, 2L); PageInfo<User> info = PageHelper.startPage(1, 5) .doSelectPageInfo(() -> mapper.selectList(Wrappers.<User>query().in("id", ids))); List<User> list = info.getList(); System.out.println(list); return "success"; }
案例二
# 导入依赖 <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.11</version> </dependency> # 配置类 @Bean public PageInterceptor pageInterceptor() { return new PageInterceptor(); } List<UserBookRespDTO> findPage(UserBookReqDTO reqDTO); <select id="findPage" resultType="com.ychen.mybatis.model.dto.UserBookRespDTO"> SELECT u.id, u.username, u.password, u.sex, u.birthday, b.bookname, b.author, b.price FROM user AS u LEFT JOIN book AS b ON u.username = b.author <where> <if test="username !=null and username!=''"> or u.username like concat('%',#{username},'%') </if> <if test="sex !=null and sex!=''"> or u.sex like concat('%',#{sex},'%') </if> <if test="bookname != null and bookname!=''"> or b.bookname like concat('%',#{bookname}'%') </if> </where> </select> @GetMapping("/test2") @ResponseBody public String test2(@RequestBody UserBookReqDTO req){ int pageNo = 1; int pageSize = 3; try { PageHelper.startPage(pageNo,pageSize); List<UserBookRespDTO> list = userMapper.findPage(req); PageInfo pageinfo = new PageInfo(list); System.out.println(pageinfo); }catch (Exception e){ e.printStackTrace(); } return "aaa"; } # 控制台 PageInfo{pageNum=1, pageSize=3, size=1, startRow=1, endRow=1, total=1, pages=1, list=Page{count=true, pageNum=1, pageSize=3, startRow=0, endRow=3, total=1, pages=1, reasonable=false, pageSizeZero=false}[UserBookRespDTO(id=1, username=chen1, password=12345, sex=0, birthday=Tue Dec 07 10:40:52 CST 2021, bookname=book1, author=chen1, price=3.99)], prePage=0, nextPage=0, isFirstPage=true, isLastPage=true, hasPreviousPage=false, hasNextPage=false, navigatePages=8, navigateFirstPage=1, navigateLastPage=1, navigatepageNums=[1]}
分类:
后端 / 对象关系映射
标签:
MyBatis Plus
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?