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

MyBatis-Plus結(jié)合Layui實現(xiàn)分頁方法

 更新時間:2021年08月05日 09:35:16   作者:與先生  
MyBatis-Plus 使用簡單,本文主要介紹使用 service 中的 page 方法結(jié)合 Layui 前端框架實現(xiàn)分頁效果,具有一定的參考價值,感興趣的可以了解一下

MyBatis-Plus 使用簡單,內(nèi)置通用 Mapper、通用 Service,僅僅通過少量配置,即可實現(xiàn)單表大部分 CRUD 操作。下面介紹使用 service 中的 page 方法結(jié)合 Layui 前端框架,較快速的實現(xiàn)分頁效果。

在 pom.xml 中引入依賴

<!--  mybatisplus -->
<dependency>
 <groupId>com.baomidou</groupId>
 <artifactId>mybatis-plus-boot-starter</artifactId>
 <version>${mybatisplus.version}</version>
</dependency>

使用 MyBatis-Plus 內(nèi)置的 mapper。首先編寫好實體類,然后編寫 mapper 接口,并繼承 BaseMapper。BaseMapper 中包含大部分的 CRUD 方法,不需要編寫 mapper.xml 。如果需要多表查詢的話,可根據(jù)自己的業(yè)務(wù)需要編寫 mapper.xml 。

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.systop.pojo.School;
import org.springframework.stereotype.Repository;

/**
 * @author: Miranda
 * @Date: 2021/8/2
 * @description:
 */
@Repository
public interface SchoolMapper extends BaseMapper<School> {

}

使用 MyBatis-Plus 內(nèi)置的 service。編寫 service 接口,并繼承 IService。

import com.baomidou.mybatisplus.extension.service.IService;
import com.systop.pojo.School;

/**
 * @author: Miranda
 * @Date: 2021/8/2
 * @description:
 */
public interface SchoolService extends IService<School> {

}

編寫 service 實現(xiàn)類,繼承 MyBatis-Plus 的 ServiceImpl ,同時實現(xiàn) SchoolService 接口。

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.systop.mapper.SchoolMapper;
import com.systop.pojo.School;
import com.systop.service.SchoolService;
import org.springframework.stereotype.Service;

/**
 * @author: Miranda
 * @Date: 2021/8/2
 * @description:
 */
@Service
public class SchoolServiceImpl extends ServiceImpl<SchoolMapper, School> implements SchoolService {

}

使用 MyBatis-plus 分頁,必須寫一個配置類

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author: Miranda
 * @Date: 2021/8/3
 * @description:
 */
@Configuration
@MapperScan("com.systop.mapper")
public class MybatisPlusConfig {
    /**
     * 分頁插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
}

需要一個 Layui 返回值的類

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;

/**
 * @author: Miranda
 * @Date: 2021/8/2
 * @description:
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class LayuiPage<T> {

    private int code;
    private String msg;
    private Long count;
    private List<T> data;

    /**
     * 只有總條數(shù)和分頁數(shù)據(jù)的構(gòu)造方法
     * @param count 總條數(shù)
     * @param data 分頁數(shù)據(jù)
     */
    public LayuiPage( Long count, List<T> data) {
        this.count = count;
        this.data = data;
    }
}

controller 類

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.systop.pojo.School;
import com.systop.service.SchoolService;
import com.systop.utils.LayuiPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author: Miranda
 * @Date: 2021/8/2
 * @description:
 */
@Controller
public class SchoolController {

    @Autowired
    private SchoolService schoolService;
   
    @RequestMapping("schoolList")
    @ResponseBody
    public LayuiPage schoolList(int page,int limit){
        //傳入分頁的屬性
        Page<School> pager = new Page<>(page,limit);
        //分頁查詢學(xué)校信息
        IPage<School> schoolPage = schoolService.page(pager, new QueryWrapper<>());
        // schoolPage.getTotal() 信息總條數(shù)
        // schoolPage.getRecords() 分頁數(shù)據(jù)
        return new LayuiPage(schoolPage.getTotal(),schoolPage.getRecords());
    }
}

Layui 頁面代碼實現(xiàn)

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
 <meta charset="utf-8">
 <title>學(xué)校信息管理</title>
 <meta name="renderer" content="webkit">
 <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
 <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
 <!-- 引入layuiadmin的樣式 -->
 <link rel="stylesheet" href="../layuiadmin/layui/css/layui.css" rel="external nofollow"  th:href="@{layuiadmin/layui/css/layui.css}" rel="external nofollow"  media="all">
 <link rel="stylesheet" href="../layuiadmin/style/admin.css" rel="external nofollow"  th:href="@{layuiadmin/style/admin.css}" rel="external nofollow"   media="all">
</head>
<body>
 <div class="layui-fluid">
  <div class="layui-row layui-col-space15">
   <div class="layui-col-md12">
    <div class="layui-card">
     <div class="layui-card-body">
      <!-- id="test-table-simple" -->
      <table class="layui-table" id="test-table-simple" lay-filter="curd" ></table>
     </div>
    </div>
   </div>
  </div>
 </div>
 <script src="../layuiadmin/layui/layui.js" th:src="@{layuiadmin/layui/layui.js}"></script>
 <script>
  layui.use(['layer', 'table', 'element','form', 'layedit','util'], function(){
   var layer = layui.layer, //彈層
     table = layui.table, //表格
     element = layui.element, //元素操作
     form = layui.form,
     layedit = layui.layedit,
     util = layui.util;
   table.render({
    elem: '#test-table-simple',
    url: 'schoolList',
    method: 'post',
    cellMinWidth: 80, //全局定義常規(guī)單元格的最小寬度
    cols: [
     [{type: 'checkbox'},
     {field: 'sid', title: 'ID', sort: true, align: 'center', width:80},
     {field: 'sname', title: '名稱', align: 'center'},
     {field: 'arrangement', title: '層次', align: 'center'},
     {title: '操作', align: 'center', toolbar: '#bar', width:150, fixed: 'right'}]
    ],
    // field 的值和實體類屬性名稱保持一致,如果數(shù)據(jù)表格沒有渲染,可以看看瀏覽器解析后的名稱
    done: function(res){
    // 在控制臺輸出后臺傳送的數(shù)據(jù)
     console.log(res);
    },
    page: true, //是否顯示分頁
    limits: [5, 7, 10],
    limit: 5 //每頁默認(rèn)顯示的數(shù)量
   });
  });
 </script>
</body>
</html>

頁面效果如下:

排雷:
剛開始定義 Layui 返回數(shù)據(jù)類的時候,將 code 定義成 Integer 類型,并且在 controller 類中使用的是兩個參數(shù)的構(gòu)造方法,導(dǎo)致傳給前臺數(shù)據(jù)中 code 的值是 null,所以數(shù)據(jù)渲染一直報 “返回的數(shù)據(jù)狀態(tài)異?!薄?br />

解決:
將 code 定義成 int 類型,或者在 controller 中使用時,傳四個參數(shù)。

到此這篇關(guān)于MyBatis-Plus結(jié)合Layui實現(xiàn)分頁方法的文章就介紹到這了,更多相關(guān)MyBatis-Plus Layui分頁內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java 反射getClass .class 的使用方法示例

    java 反射getClass .class 的使用方法示例

    這篇文章主要介紹了java 反射getClass .class 的使用方法,結(jié)合實例形式分析了java類反射機制的相關(guān)操作技巧,需要的朋友可以參考下
    2019-11-11
  • java 出現(xiàn)Zipexception 異常的解決辦法

    java 出現(xiàn)Zipexception 異常的解決辦法

    這篇文章主要介紹了java 出現(xiàn)Zipexception 異常的解決辦法的相關(guān)資料,出現(xiàn) java.util.zip.ZipException: error in opening zip file 異常的原因及解決方法,需要的朋友可以參考下
    2017-08-08
  • java?安全ysoserial?URLDNS利用鏈分析

    java?安全ysoserial?URLDNS利用鏈分析

    這篇文章主要為大家介紹了java?安全ysoserial?URLDNS利用鏈分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-10-10
  • Eclipse項目怎么導(dǎo)入IDEA并運行(超詳細)

    Eclipse項目怎么導(dǎo)入IDEA并運行(超詳細)

    這篇文章主要介紹了Eclipse項目怎么導(dǎo)入IDEA并運行(超詳細),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • java CompletableFuture實現(xiàn)異步編排詳解

    java CompletableFuture實現(xiàn)異步編排詳解

    這篇文章主要為大家介紹了java CompletableFuture實現(xiàn)異步編排詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-01-01
  • SpringBoot統(tǒng)計、監(jiān)控SQL運行情況的方法詳解

    SpringBoot統(tǒng)計、監(jiān)控SQL運行情況的方法詳解

    這篇文章主要給大家介紹了關(guān)于SpringBoot統(tǒng)計、監(jiān)控SQL運行情況的相關(guān)資料,文中通過實例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2022-02-02
  • java實現(xiàn)拉鉤網(wǎng)上的FizzBuzzWhizz問題示例

    java實現(xiàn)拉鉤網(wǎng)上的FizzBuzzWhizz問題示例

    這篇文章主要介紹了java實現(xiàn)拉鉤網(wǎng)上的FizzBuzzWhizz問題示例,需要的朋友可以參考下
    2014-05-05
  • Python__雙劃線參數(shù)代碼實例解析

    Python__雙劃線參數(shù)代碼實例解析

    這篇文章主要介紹了python__雙劃線參數(shù)代碼實例解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-02-02
  • 圖文詳解Java的反射機制

    圖文詳解Java的反射機制

    反射就是Reflection,Java的反射是指程序在運行期可以拿到一個對象的所有信息。反射機制是框架的靈魂,一個java程序員不能不會使用反射,本文就來和大家一起詳細聊聊Java的反射機制
    2022-08-08
  • Java的NIO之通道channel詳解

    Java的NIO之通道channel詳解

    這篇文章主要介紹了Java的NIO之通道channel詳解,通道channel由java.nio.channels 包定義的,Channel 表示IO源與目標(biāo)打開的連接,Channel類類似于傳統(tǒng)的"流",只不過Channel本身不能直接訪問數(shù)據(jù),Channel只能與Buffer進行交互,需要的朋友可以參考下
    2023-10-10

最新評論