最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

MyBatis-Plus 使用簡單,內(nèi)置通用 Mapper、通用 Service,僅僅通過少量配置,即可實(shí)現(xiàn)單表大部分 CRUD 操作。下面介紹使用 service 中的 page 方法結(jié)合 Layui 前端框架,較快速的實(shí)現(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。首先編寫好實(shí)體類,然后編寫 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 實(shí)現(xiàn)類,繼承 MyBatis-Plus 的 ServiceImpl ,同時(shí)實(shí)現(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 分頁,必須寫一個(gè)配置類

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();
    }
}

需要一個(gè) 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 頁面代碼實(shí)現(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í)體類屬性名稱保持一致,如果數(shù)據(jù)表格沒有渲染,可以看看瀏覽器解析后的名稱
    done: function(res){
    // 在控制臺(tái)輸出后臺(tái)傳送的數(shù)據(jù)
     console.log(res);
    },
    page: true, //是否顯示分頁
    limits: [5, 7, 10],
    limit: 5 //每頁默認(rèn)顯示的數(shù)量
   });
  });
 </script>
</body>
</html>

頁面效果如下:

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

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

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

  • 老生常談Java中List與ArrayList的區(qū)別

    老生常談Java中List與ArrayList的區(qū)別

    大家都知道List是接口,ArrayList是List接口的一個(gè)實(shí)現(xiàn)類,接下來通過本文給大家介紹Java中List與ArrayList的區(qū)別,需要的朋友可以參考下
    2022-08-08
  • Javaweb動(dòng)態(tài)開發(fā)最重要的Servlet詳解

    Javaweb動(dòng)態(tài)開發(fā)最重要的Servlet詳解

    動(dòng)態(tài)web的核心是Servlet,由tomcat解析并執(zhí)行,本質(zhì)是Java中的一個(gè)類(面向?qū)ο螅┻@個(gè)類的功能十分強(qiáng)大幾乎可以完成全部功能,在Java規(guī)范中只有Servlet實(shí)現(xiàn)類實(shí)例化的對(duì)象才能被瀏覽器訪問,所以掌握Servlet具有重要意義
    2022-08-08
  • java中LinkedList使用迭代器優(yōu)化移除批量元素原理

    java中LinkedList使用迭代器優(yōu)化移除批量元素原理

    本文主要介紹了java中LinkedList使用迭代器優(yōu)化移除批量元素原理,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • mybatisplus自動(dòng)填充屬性值的實(shí)現(xiàn)步驟

    mybatisplus自動(dòng)填充屬性值的實(shí)現(xiàn)步驟

    MyBatis-Plus提供自動(dòng)填充的功能,幫助自定設(shè)置這些字段的值,提升開發(fā)效率,本文就來介紹一下如何使用,感興趣的可以了解一下
    2023-12-12
  • Springboot初始化啟動(dòng)報(bào)錯(cuò)Error?creating?bean?with?name?'dataSource'?defined?in?class?path?resource

    Springboot初始化啟動(dòng)報(bào)錯(cuò)Error?creating?bean?with?name?'da

    這篇文章主要為大家介紹了Springboot初始化啟動(dòng)報(bào)Error?creating?bean?with?name?'dataSource'?defined?in?class?path?resource解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-08-08
  • Spring Boot配置讀取實(shí)現(xiàn)方法解析

    Spring Boot配置讀取實(shí)現(xiàn)方法解析

    這篇文章主要介紹了Spring Boot配置讀取實(shí)現(xiàn)方法解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-08-08
  • Java11 發(fā)布前抓緊掌握這些新特性

    Java11 發(fā)布前抓緊掌握這些新特性

    Java 11即將發(fā)布,你準(zhǔn)備好了? 在這篇文章中,我們討論下在進(jìn)入Java 11之前,你需要了解的Java 8、9和10的一些有用功能,若還在用Java 8以前的版本,那就太落伍了,這里首先要明確,Java 8:是一個(gè)針對(duì)傳統(tǒng)傳統(tǒng)版本的重大轉(zhuǎn)變
    2018-09-09
  • JAVA IO的3種類型區(qū)別解析

    JAVA IO的3種類型區(qū)別解析

    這篇文章主要介紹了JAVA IO的3種類型解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10
  • Java枚舉使用方法詳解

    Java枚舉使用方法詳解

    這篇文章主要為大家詳細(xì)介紹了Java枚舉的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-09-09
  • 最新評(píng)論

    郴州市| 吴桥县| 剑河县| 阿克| 海丰县| 烟台市| 普洱| 辽阳县| 襄城县| 兴宁市| 深州市| 昌宁县| 昌乐县| 南岸区| 汝南县| 洮南市| 江阴市| 工布江达县| 万安县| 抚远县| 马鞍山市| 丹棱县| 沈阳市| 瑞丽市| 望奎县| 天水市| 桃江县| 定安县| 修文县| 朔州市| 肥东县| 惠东县| 平武县| 灵宝市| 筠连县| 青州市| 满洲里市| 谷城县| 阿巴嘎旗| 梨树县| 钟山县|