欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

MyBatis-Plus實現(xiàn)分頁的方法使用詳解

 更新時間:2022年03月22日 16:25:32   作者:IT利刃出鞘  
這篇文章主要為大家介紹了MyBatis-Plus的分頁的方法使用,包括:不傳參數(shù)時的默認結(jié)果、查詢不存在的數(shù)據(jù)、手動包裝page和自定義SQL,需要的可以參考一下

簡介

本文介紹MyBatis-Plus的分頁的方法。

包括:

  • 不傳參數(shù)時的默認結(jié)果
  • 查詢不存在的數(shù)據(jù)
  • 手動包裝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 '創(chuàng)建時間',
    `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);

依賴

<?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>
 
        <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>

配置

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配置控制臺打印完整帶參數(shù)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;
 
    /**
     * 創(chuàng)建時間
     */
    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<User> {
    @Select("SELECT * FROM t_user ${ew.customSqlSegment}")
    IPage<User> findUser(IPage<User> 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<User> {
}

實現(xiàn)

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

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調(diào)mapper
    @Autowired
    private UserMapper userMapper;
 
    @ApiOperation("不傳參數(shù)")
    @GetMapping("noParam")
    public IPage<User> noParam(Page<User> page) {
        return userService.page(page);
    }
 
    @ApiOperation("查不存在的數(shù)據(jù)")
    @GetMapping("notExist")
    public IPage<User> notExist(Page<User> page) {
        // 如果查出來為空,會返回入?yún)age里邊的數(shù)據(jù),比如:current,size等。不需要自己判空。
        return userService.lambdaQuery()
                .eq(User::getUserName, "abcd")
                .page(page);
    }
 
    @ApiOperation("手動包裝")
    @GetMapping("manualPack")
    public IPage<User> manualPack(Page<User> page) {
        List<User> skyList = userService.lambdaQuery()
                .eq(User::getUserName, "sky")
                .list();
 
        // 因為Page是IPage的實現(xiàn)類,所以可以直接返回page
        // 也可以自己new 一個Page,然后設置值,不過這樣就麻煩了
        return page.setRecords(skyList);
    }
 
    @ApiOperation("自定義SQL")
    @GetMapping("customSQL")
    public IPage<User> customSQLPage(Page<User> page) {
        LambdaQueryWrapper<User> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(User::getUserName, "sky");
 
        // 這樣寫會報錯:MybatisPlusException: can not use this method for "getCustomSqlSegment"
        // LambdaQueryChainWrapper<User> wrapper = userService.lambdaQuery()
        //         .eq(User::getUserName, "sky");
 
        return userMapper.findUser(page, wrapper);
    }
}

測試

訪問knife4j:http://localhost:8080/doc.html

1. 不傳參數(shù)

2. 查不存在的數(shù)據(jù)

3. 手動包裝

4. 自定義SQL

以上就是MyBatis-Plus實現(xiàn)分頁的方法使用詳解的詳細內(nèi)容,更多關(guān)于MyBatis-Plus分頁的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • IDEA啟動服務提示端口被占用,Web?server?failed?to?start.Port?was?already?in?use.

    IDEA啟動服務提示端口被占用,Web?server?failed?to?start.Port?was?al

    這篇文章主要介紹了IDEA啟動服務提示端口被占用,Web?server?failed?to?start.Port?was?already?in?use.,本文給大家分享解決方案,分為linux系統(tǒng)和windows系統(tǒng)解決方案,需要的朋友可以參考下
    2023-07-07
  • java中的IO流

    java中的IO流

    這篇文章主要介紹了java中的IO流的相關(guān)資料,需要的朋友可以參考下文
    2021-08-08
  • 詳解SpringBoot如何自定義一個Starter

    詳解SpringBoot如何自定義一個Starter

    小伙伴們曾經(jīng)可能都經(jīng)歷過整天寫著CURD的業(yè)務,都沒寫過一些組件相關(guān)的東西,這篇文章記錄一下SpringBoot如何自定義一個Starter。原理和理論就不用多說了,可以在網(wǎng)上找到很多關(guān)于該方面的資料,這里主要分享如何自定義
    2022-11-11
  • Java 語言守護線程 Daemon Thread使用示例詳解

    Java 語言守護線程 Daemon Thread使用示例詳解

    這篇文章主要為大家介紹了Java 語言守護線程 Daemon Thread使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-10-10
  • Windows中Tomcat整合到Eclipse的圖文教程

    Windows中Tomcat整合到Eclipse的圖文教程

    下面小編就為大家?guī)硪黄猈indows中Tomcat整合到Eclipse的圖文教程。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-10-10
  • springboot 錯誤處理小結(jié)

    springboot 錯誤處理小結(jié)

    在 java web開發(fā)過程中,難免會有一些系統(tǒng)異?;蛉藶楫a(chǎn)生一些異常。在 RESTful springboot 項目中如何優(yōu)雅的處理?下面腳本之家小編給大家?guī)砹藄pringboot 錯誤處理小結(jié),感興趣的朋友一起看看吧
    2018-03-03
  • 基于Java的Scoket編程

    基于Java的Scoket編程

    本文詳細講解了基于Java的Scoket編程,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-12-12
  • SpringBoot中配置文件及切換方式

    SpringBoot中配置文件及切換方式

    這篇文章主要介紹了SpringBoot中配置文件及切換方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • springboot 集成支付寶支付的示例代碼

    springboot 集成支付寶支付的示例代碼

    這篇文章主要介紹了springboot 集成支付寶支付的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-06-06
  • MyBatis?Plus實現(xiàn)中文排序的兩種有效方法

    MyBatis?Plus實現(xiàn)中文排序的兩種有效方法

    在MyBatis?Plus項目開發(fā)中,針對中文數(shù)據(jù)的排序需求是一個常見的挑戰(zhàn),尤其是在需要按照拼音或特定語言邏輯排序時,本文整合了兩種有效的方法,旨在幫助開發(fā)者克服MyBatis?Plus在處理中文排序時遇到的障礙,需要的朋友可以參考下
    2024-08-08

最新評論