Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to sort pages in MyBatisPlus

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to sort pages in MyBatisPlus. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Brief introduction

Description

This article uses an example to introduce the method of sorting when MyBtisPlus pagination.

The method of sorting when paging

Back-end OrderItems sorting

Back-end Wrapper sorting

The front end specifies the sort

Sort the classes involved

Sorting involves List orders; members of the Page class, and OrderItem is defined as follows:

Public class OrderItem implements Serializable {private static final long serialVersionUID = 1L; / / whether the fields to be sorted private String column; / / are arranged in positive order. Default is true private boolean asc = true;...} build database and table 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 `tuser` (`id` BIGINT (0) NOT NULL AUTO_INCREMENT, `create_ name` VARCHAR (64) NOT NULL COMMENT 'username (cannot be repeated)', `nick_ name`username (64) NULL COMMENT 'nickname (repeatable)', `email`VARCHAR (64) COMMENT 'mailbox', `create_ time`DATETIME (0) NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'creation time' `user_ time` DATETIME (0) NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'modification time', `deleted_ time` BIGINT (0) NOT NULL DEFAULT 0 COMMENT'0: not deleted other: deleted', PRIMARY KEY (`id`) USING BTREE, UNIQUE KEY `index_user_name_deleted_ time` (`user_ name`, `deleted_ time`), KEY `index_create_ time` (`create_ time`) ENGINE = InnoDB COMMENT = 'user' INSERT INTO `t _ user`VALUES (1, 'knife',' Blade', 'abc@qq.com',' 2021-01-23 09 knife', 33 knife', 36, '2021-01-23 09 knife', 33 knife', 36, 0); INSERT INTO `t _ user` knife', (2,' sky', 'Sky Blue', '123 qq.compositions,' 2021-01-24 18 18 knife', 21 colors, '2021-01-24 18 18 abc@qq.com', 21 colors, 0)

The result after execution:

Dependence

Pom.xml

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 configuration

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 configuration console prints complete SQL statement with parameters mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

Configuration of the paging plug-in (required)

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; @ Configurationpublic class MyBatisPlusConfig {/ * paging plug-in * / @ Bean public MybatisPlusInterceptor mybatisPlusInterceptor () {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor () Interceptor.addInnerInterceptor (new PaginationInnerInterceptor (DbType.MYSQL)); return interceptor;}} Code Entitypackage 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; / * username (cannot be repeated) * / private String userName; / * * nickname (repeatable) * / private String nickName; / * * mailbox * / private String email / * creation time * / private LocalDateTime createTime; / * Modification time * / private LocalDateTime updateTime; / * 0: other: deleted * / @ TableLogic (delval = "id") private Long deletedFlag;} Service

Interface

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 {}

Realize

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; @ Servicepublic class UserServiceImpl extends ServiceImpl implements UserService {} Controllerpackage 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 = "sort") @ RestController@RequestMapping ("sort") public class SortController {@ Autowired private UserService userService; @ ApiOperation ("default order") @ GetMapping ("defaultOrder") public IPage defaultOrder (Page page) {return userService.page (page) } @ ApiOperation ("through orderItems") @ GetMapping ("orderItems") public IPage orderItems (Page page) {page.addOrder (OrderItem.desc ("create_time")); / / multiple columns can be specified. For example, the following one: sort by create_time, if create_time is the same, sort by id (OrderItem.desc ("create_time"), OrderItem.asc ("id"); return userService.page (page);} @ ApiOperation ("through wrapper") @ GetMapping ("wrapper") public IPage wrapper (Page page) {LambdaQueryWrapper queryWrapper = Wrappers.lambdaQuery () / sort by create_time, or sort by id if the create_time is the same: queryWrapper.orderByDesc (User::getCreateTime); queryWrapper.orderByAsc (User::getId); return userService.page (page, queryWrapper);} @ ApiOperation ("front end specified order") @ GetMapping ("byFrontEnd") public IPage byFrontEnd (Page page) {return userService.page (page);}} Test

Visit the Knife4j page: http://localhost:8080/doc.html

1. Do not specify order

The front end of the department does not transmit any parameters for testing.

two。 Backend OrderItem sort (create_time reverse)

The front end of the department does not transmit any parameters for testing.

3. Backend Wrapper sort (create_time reverse, id ascending)

The front end of the department does not transmit any parameters for testing.

4. The front end specifies the sort (create_time reverse order)

The front end specifies orders [0] .asc and orders [0] .column

Results:

This is the end of the article on "how to sort pages in MyBatisPlus". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report