您好,登錄后才能下訂單哦!
本篇內容主要講解“MyBatis Plus分頁時怎么實現排序”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“MyBatis Plus分頁時怎么實現排序”吧!
分頁時排序的方法
后端OrderItems排序
后端Wrapper排序
前端指定排序
排序涉及到的類
排序涉及到Page類的List<OrderItem> orders;成員,OrderItem定義如下:
public class OrderItem implements Serializable { private static final long serialVersionUID = 1L; //需要進行排序的字段 private String column; // 是否正序排列,默認 true private boolean asc = true; ... }
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);
執行后的結果:
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.12.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>MyBatis-Plus_Simple</artifactId> <version>0.0.1-SNAPSHOT</version> <name>MyBatis-Plus_Simple</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.1</version> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> <version>3.0.3</version> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
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; } }
代碼
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; }
接口
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<User> { }
實現
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 ServiceImpl<UserMapper, User> implements UserService { }
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.metadata.OrderItem; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.example.demo.user.entity.User; 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("sort") public class SortController { @Autowired private UserService userService; @ApiOperation("默認順序") @GetMapping("defaultOrder") public IPage<User> defaultOrder(Page<User> page) { return userService.page(page); } @ApiOperation("通過orderItems") @GetMapping("orderItems") public IPage<User> orderItems(Page<User> page) { page.addOrder(OrderItem.desc("create_time")); // 可以指定多列。比如下邊這個:按create_time排序,若create_time相同,則按id排序 // page.addOrder(OrderItem.desc("create_time"), OrderItem.asc("id")); @ApiOperation("通過wrapper") @GetMapping("wrapper") public IPage<User> wrapper(Page<User> page) { LambdaQueryWrapper<User> queryWrapper = Wrappers.<User>lambdaQuery(); // 按create_time排序,若create_time相同,則按id排序 queryWrapper.orderByDesc(User::getCreateTime); queryWrapper.orderByAsc(User::getId); return userService.page(page, queryWrapper); @ApiOperation("前端指定順序") @GetMapping("byFrontEnd") public IPage<User> byFrontEnd(Page<User> page) { }
訪問Knife4j頁面:http://localhost:8080/doc.html
本處前端不傳任何參數進行的測試。
本處前端不傳任何參數進行的測試。
本處前端不傳任何參數進行的測試。
前端指定orders[0].asc和orders[0].column
結果:
到此,相信大家對“MyBatis Plus分頁時怎么實現排序”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。