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

MyBatis-Plus實現(xiàn)對查詢結(jié)果進行分頁的基本步驟

 更新時間:2024年08月28日 11:11:57   作者:威哥愛編程  
MyBatis-Plus 是一個 MyBatis 的增強工具,在 MyBatis 的基礎(chǔ)上只做增強不做改變,為簡化開發(fā)、提高效率而生,MyBatis-Plus 支持多種數(shù)據(jù)庫的分頁查詢,其分頁功能是通過 Page 類實現(xiàn)的,本文介紹了使用 MyBatis-Plus 實現(xiàn)分頁查詢的基本步驟,需要的朋友可以參考下

MyBatis-Plus 是一個 MyBatis 的增強工具,在 MyBatis 的基礎(chǔ)上只做增強不做改變,為簡化開發(fā)、提高效率而生。MyBatis-Plus 支持多種數(shù)據(jù)庫的分頁查詢,其分頁功能是通過 Page 類實現(xiàn)的。

以下是使用 MyBatis-Plus 實現(xiàn)分頁查詢的基本步驟:

  • 添加依賴:首先確保你的項目中已經(jīng)添加了 MyBatis-Plus 的依賴。

  • 配置 Mapper 接口:創(chuàng)建一個 Mapper 接口,該接口繼承自 BaseMapper<T>,其中 T 是你的實體類。

  • 創(chuàng)建 Service:在 Service 層中,你可以注入 Mapper 接口,并調(diào)用其分頁查詢的方法。

  • 使用 Page 類:創(chuàng)建一個 Page 對象,設(shè)置當前頁碼和每頁顯示的記錄數(shù)。

  • 調(diào)用分頁查詢:在 Mapper 接口中定義一個分頁查詢的方法,使用 @Select 注解或者 XML 映射文件來指定查詢語句。然后在 Service 層調(diào)用這個方法,傳入 Page 對象。

  • 處理結(jié)果:分頁查詢的結(jié)果會返回一個 PageInfo<T> 對象,其中包含了當前頁的數(shù)據(jù)和分頁信息。

好的,用一個案例來具體看一下,如何使用 MyBatis-Plus 進行分頁查詢。假設(shè)我們有一個需求是,在用戶管理系統(tǒng),需要對用戶列表進行分頁顯示。

  • 實體類(User.java)
public class User {
    private Long id;
    private String name;
    private Integer age;
    private String email;
    // 省略其他字段和getter/setter方法
}
  • Mapper 接口(UserMapper.java)
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

public interface UserMapper extends BaseMapper<User> {
    // 這里可以添加自定義的查詢方法,例如按狀態(tài)查詢
    Page<User> selectByState(Page<User> page, @Param("state") Integer state);
}
  • Mapper XML 文件(UserMapper.xml)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mapper.UserMapper">
    <!-- 按狀態(tài)查詢用戶的分頁結(jié)果 -->
    <select id="selectByState" resultType="com.example.entity.User">
        SELECT * FROM user WHERE state = #{state}
    </select>
</mapper>
  • Service 接口(IUserService.java)
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.entity.User;

public interface IUserService extends IService<User> {
    IPage<User> getUserListByState(Integer state, int current, int size);
}
  • Service 實現(xiàn)(UserServiceImpl.java)
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mapper.UserMapper;
import com.example.entity.User;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Override
    public IPage<User> getUserListByState(Integer state, int current, int size) {
        IPage<User> page = this.page(new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(current, size), null);
        if (state != null) {
            return this.baseMapper.selectByState(page, state);
        }
        return page;
    }
}
  • Controller(UserController.java)
import com.example.service.IUserService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {

    @Autowired
    private IUserService userService;

    @GetMapping("/users")
    public IPage<User> getUsers(@RequestParam(defaultValue = "0") Integer state,
                                 @RequestParam(defaultValue = "1") int current,
                                 @RequestParam(defaultValue = "10") int size) {
        return userService.getUserListByState(state, current, size);
    }
}
  • 啟動類(Application.java)
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
  • application.properties
# 配置數(shù)據(jù)庫連接信息
spring.datasource.url=jdbc:mysql://localhost:3306/userdb?useSSL=false&serverTimezone=UTC
spring.datasource.username=weige
spring.datasource.password=wg123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

# 配置 MyBatis-Plus
mybatis-plus.mapper-locations=classpath:/mapper/*.xml
mybatis-plus.type-aliases-package=com.vin.entity

以上就是MyBatis-Plus實現(xiàn)對查詢結(jié)果進行分頁的基本步驟的詳細內(nèi)容,更多關(guān)于MyBatis-Plus查詢結(jié)果分頁的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論