MyBatis-Plus实现分页的方法使用详解
目录
简介建库建表依赖配置代码EntityMapperServiceController测试简介
本文介绍MyBatis-Plus的分页的方法。
包括:
不传参数时的默认结果查询不存在的数据手动包装page自定义SQL建库建表
DROP DATABASE IF EXISTS mp; CREATE DATABASE mp DEFAULT CHARACTER SET utf8; USE mp; DROP TABLE IF EXISTS `t_user`; SET NAMES utf8mb4; CREATE TABLE `t_user` ( `id` BIGINT(0) NOT NULL AUTO_INCREMENT, `user_name` VARCHAR(64) NOT NULL COMMENT "用户名(不能重复)", `nick_name` VARCHAR(64) NULL COMMENT "昵称(可以重复)", `email` VARCHAR(64) COMMENT "邮箱", `create_time` DATETIME(0) NULL DEFAULT CURRENT_TIMESTAMP COMMENT "创建时间", `update_time` DATETIME(0) NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT "修改时间", `deleted_flag` BIGINT(0) NOT NULL DEFAULT 0 COMMENT "0:未删除 其他:已删除", PRIMARY KEY (`id`) USING BTREE, UNIQUE KEY `index_user_name_deleted_flag` (`user_name`, `deleted_flag`), KEY `index_create_time`(`create_time`) ) ENGINE = InnoDB COMMENT = "用户"; INSERT INTO `t_user` VALUES (1, "knife", "刀刃", "abc@qq.com", "2021-01-23 09:33:36", "2021-01-23 09:33:36", 0); INSERT INTO `t_user` VALUES (2, "sky", "天蓝", "123@qq.com", "2021-01-24 18:12:21", "2021-01-24 18:12:21", 0);
依赖
4.0.0 org.springframework.boot spring-boot-starter-parent 2.3.12.RELEASE com.example MyBatis-Plus_Simple 0.0.1-SNAPSHOT MyBatis-Plus_Simple Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-web com.baomidou mybatis-plus-boot-starter 3.5.1 org.springframework.boot spring-boot-starter-test test mysql mysql-connector-java org.projectlombok lombok com.github.xiaoymin knife4j-spring-boot-starter 3.0.3 org.springframework.boot spring-boot-maven-plugin
配置
application.yml
spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/mp?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai username: root password: 222333 #mybatis-plus配置控制台打印完整带参数SQL语句 mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
分页插件配置(必须)
package com.example.demo.config; import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; @Configuration public class MyBatisPlusConfig { /** * 分页插件 */ @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); return interceptor; } }
代码
Entity
package com.example.demo.user.entity; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableLogic; import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.extension.activerecord.Model; import lombok.Data; import java.time.LocalDateTime; @Data @TableName(value = "t_user") public class User { @TableId(value = "id", type = IdType.AUTO) private Long id; /** * 用户名(不能重复) */ private String userName; /** * 昵称(可以重复) */ private String nickName; /** * 邮箱 */ private String email; /** * 创建时间 */ private LocalDateTime createTime; /** * 修改时间 */ private LocalDateTime updateTime; /** * 0:未删除 其他:已删除 */ @TableLogic(delval = "id") private Long deletedFlag; }
Mapper
package com.example.demo.user.mapper; import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.toolkit.Constants; import com.example.demo.user.entity.User; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; import org.springframework.stereotype.Repository; @Repository public interface UserMapper extends BaseMapper{ @Select("SELECT * FROM t_user ${ew.customSqlSegment}") IPage findUser(IPage page, @Param(Constants.WRAPPER) Wrapper wrapper); }
Service
接口
package com.example.demo.user.service; import com.baomidou.mybatisplus.extension.service.IService; import com.example.demo.user.entity.User; public interface UserService extends IService{ }
实现
package com.example.demo.user.service.impl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.example.demo.user.entity.User; import com.example.demo.user.mapper.UserMapper; import com.example.demo.user.service.UserService; import org.springframework.stereotype.Service; @Service public class UserServiceImpl extends ServiceImplimplements UserService { }
Controller
package com.example.demo.user.controller; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.example.demo.user.entity.User; import com.example.demo.user.mapper.UserMapper; import com.example.demo.user.service.UserService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; 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.RestController; import java.util.List; @Api(tags = "分页") @RestController @RequestMapping("page") public class PageController { @Autowired private UserService userService; // 我为了简单直接注入mapper,项目中controller要通过service调mapper @Autowired private UserMapper userMapper; @ApiOperation("不传参数") @GetMapping("noParam") public IPagenoParam(Page page) { return userService.page(page); } @ApiOperation("查不存在的数据") @GetMapping("notExist") public IPage notExist(Page page) { // 如果查出来为空,会返回入参page里边的数据,比如:current,size等。不需要自己判空。 return userService.lambdaQuery() .eq(User::getUserName, "abcd") .page(page); } @ApiOperation("手动包装") @GetMapping("manualPack") public IPage manualPack(Page page) { List skyList = userService.lambdaQuery() .eq(User::getUserName, "sky") .list(); // 因为Page是IPage的实现类,所以可以直接返回page // 也可以自己new 一个Page,然后设置值,不过这样就麻烦了 return page.setRecords(skyList); } @ApiOperation("自定义SQL") @GetMapping("customSQL") public IPage customSQLPage(Page page) { LambdaQueryWrapper wrapper = Wrappers.lambdaQuery(); wrapper.eq(User::getUserName, "sky"); // 这样写会报错:MybatisPlusException: can not use this method for "getCustomSqlSegment" // LambdaQueryChainWrapper wrapper = userService.lambdaQuery() // .eq(User::getUserName, "sky"); return userMapper.findUser(page, wrapper); } }
测试
访问knife4j:http://localhost:8080/doc.html
1. 不传参数
2. 查不存在的数据
3. 手动包装
4. 自定义SQL
以上就是MyBatis-Plus实现分页的方法使用详解的详细内容,更多关于MyBatis-Plus分页的资料请关注脚本之家其它相关文章!
X 关闭
X 关闭
- 15G资费不大降!三大运营商谁提供的5G网速最快?中国信通院给出答案
- 2联想拯救者Y70发布最新预告:售价2970元起 迄今最便宜的骁龙8+旗舰
- 3亚马逊开始大规模推广掌纹支付技术 顾客可使用“挥手付”结账
- 4现代和起亚上半年出口20万辆新能源汽车同比增长30.6%
- 5如何让居民5分钟使用到各种设施?沙特“线性城市”来了
- 6AMD实现连续8个季度的增长 季度营收首次突破60亿美元利润更是翻倍
- 7转转集团发布2022年二季度手机行情报告:二手市场“飘香”
- 8充电宝100Wh等于多少毫安?铁路旅客禁止、限制携带和托运物品目录
- 9好消息!京东与腾讯续签三年战略合作协议 加强技术创新与供应链服务
- 10名创优品拟通过香港IPO全球发售4100万股 全球发售所得款项有什么用处?