MyBatis-Plus--分页时的排序--⽅法教程实例
原⽂⽹址:
简介
说明
本⽂⽤⽰例介绍MyBtisPlus分页时排序的⽅法。
分页时排序的⽅法
1. 后端OrderItems排序
2. 后端Wrapper排序
3. 前端指定排序
排序涉及到的类
排序涉及到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_TI
MESTAMP 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', '2021-01-23 09:33:36', '2021-01-23 09:33:36', 0);
INSERT INTO `t_user` VALUES (2, 'sky', '天蓝', '123@qq', '2021-01-24 18:12:21', '2021-01-24 18:12:21', 0);
执⾏后的结果:
依赖l
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0 /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&le</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>digital的名词
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
怎么自己制作模板<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
安卓系统怎么输入代码</build>
</project>
配置
spring:
datasource:
driver-class-name: sql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/mp?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai username: root
password: 222333c语言逗号表达式的运算规则
#mybatis-plus配置控制台打印完整带参数SQL语句
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
分页插件的配置(必须)
fig;
import batisplus.annotation.DbType;
import sion.plugins.MybatisPlusInterceptor;
import t.annotation.Bean;
import t.annotation.Configuration;
import sion.plugins.inner.PaginationInnerInterceptor;
@Configuration
public class MyBatisPlusConfig {
/
**
* 分页插件
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
代码
Entity
ample.ity;
import batisplus.annotation.IdType;
import batisplus.annotation.TableId;
import batisplus.annotation.TableLogic; import batisplus.annotation.TableName; import sion.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;
/**
* 创建时间
霹雳布袋戏在线看樱花*/
mysql语句的执行顺序private LocalDateTime createTime;
/**
* 修改时间
*/
private LocalDateTime updateTime;
/**
* 0:未删除其他:已删除
*/
@TableLogic(delval = "id")
private Long deletedFlag;
}
Service
接⼝
ample.demo.user.service;
import sion.service.IService; ample.ity.User;
public interface UserService extends IService<User> {
}
实现
ample.demo.user.service.impl;
import sion.service.impl.ServiceImpl;
ample.ity.User;
ample.demo.user.mapper.UserMapper;
ample.demo.user.service.UserService;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService { }
Controller
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论