Python入门之——getpass模块
681
2022-08-18
MyBatisPlus分页时排序的实现
目录简介建库建表依赖配置代码EntityServiceController测试
简介
说明
本文用示例介绍MyBtisPlus分页时排序的方法。
分页时排序的方法
后端OrderItems排序后端Wrapper排序前端指定排序
排序涉及到的类
排序涉及到Page类的List
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
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
配置
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;
}
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 ServiceImpl
}
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.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
return userService.page(page);
}
@ApiOperation("通过orderItems")
@GetMapping("orderItems")
public IPage
page.addOrder(OrderItem.desc("create_time"));
// 可以指定多列。比如下边这个:按create_time排序,若create_time相同,则按id排序
// page.addOrder(OrderItem.desc("create_time"), OrderItem.asc("id"));
return userService.page(page);
}
@ApiOperation("通过wrapper")
@GetMapping("wrapper")
public IPage
LambdaQueryWrapper
// 按create_time排序,若create_time相同,则按id排序
queryWrapper.orderByDesc(User::getCreateTime);
queryWrapper.orderByAsc(User::getId);
return userService.page(page, queryWrapper);
}
@ApiOperation("前端指定顺序")
@GetMapping("byFrontEnd")
public IPage
return userService.page(page);
}
}
测试
访问Knife4j页面:http://localhost:8080/doc.html
1.不指定顺序
本处前端不传任何参数进行的测试。
2.后端OrderItem排序(create_time倒序)
本处前端不传任何参数进行的测试。
3.后端Wrapper排序(create_time倒序,id升序)
本处前端不传任何参数进行的测试。
4.前端指定排序(create_time倒序)
前端指定orders[0].asc和orders[0].column
结果:
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~