国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

SpringBoot分頁插件(PageHelper)

這篇具有很好參考價(jià)值的文章主要介紹了SpringBoot分頁插件(PageHelper)。希望對大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

PageHelper分頁插件應(yīng)用

一、分頁插件原理

Mybatis給開發(fā)者提供了一個(gè)攔截器接口,只要實(shí)現(xiàn)了該接口,就可以在Mybatis執(zhí)行SQL前,作一些自定義的操作。分頁插件就是在此基礎(chǔ)上開發(fā)出來的,對于一個(gè)需要分頁的SQL,插件會攔截并生成兩段SQL。舉一個(gè)簡單的例子


// 原SQL
select * from table where a = '1'
// 攔截后的
   # 1.查詢總數(shù)SQL:
   select count(1) from table where a = '1'
   # 2.分頁SQL:
   select * from table where a = '1' limit 0,10

這樣我們只需要根據(jù)業(yè)務(wù)邏輯開發(fā)原SQL,不需關(guān)心分頁語法對原SQL的影響,攔截器已經(jīng)為我們處理好了

二、實(shí)現(xiàn)步驟

1.在SpringBoot 的pom文件中引入分頁插件的依賴

<dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.4.2</version>
        </dependency>

如果出現(xiàn)以下報(bào)錯(cuò):
springboot pagehelper,spring boot,java,mybatis

這里說明你現(xiàn)在使用的PageHelper 依賴版本過低,往高提版本就可以

2.修改業(yè)務(wù)邏輯層分頁方法

2.1原始方法

Service層

  //查詢符合條件的總條數(shù)
 Long total = empMapper.selectCount(name, gender, begin, end);
 //查詢符合條件的集合
  Integer start = (page - 1) * pageSize;
  List<Emp> rows = empMapper.selectPage(name, gender, start, pageSize, begin, end);
  PageBean<Emp> pageBean = new PageBean<>(total, rows);

mapper / dao層

	//查詢符合條件的總條數(shù)
	Long selectCount(String name, Integer gender, LocalDate begin, LocalDate end);
  //查詢符合條件的集合
  List<Emp> selectPage(String name, Integer gender, Integer start, Integer pageSize, LocalDate begin, LocalDate end);

XXXmapper.xml

//查詢符合條件的集合
<select id="selectPage" resultType="com.pw.tlias.pojo.Emp">
        select id, username, password, name, gender, image, job, entrydate, dept_id, create_time, update_time
        from emp
        <where>
            <if test="name != null and name != ''">
                and name like concat('%',#{name},'%')
            </if>
            <if test="gender != null">
                and gender = #{gender}
            </if>
            <if test="begin != null and end != null">
                and entrydate between #{begin} and #{end}
            </if>
        </where>
        limit #{start},#{pageSize}
    </select>
//查詢符合條件的總條數(shù)    
    <select id="selectCount" resultType="java.lang.Long">
        select count(1) emp_count
        from emp
        <where>
            <if test="name != null and name != ''">
                and name like concat('%',#{name},'%')
            </if>
            <if test="gender != null">
                and gender = #{gender}
            </if>
            <if test="begin != null and end != null">
                and entrydate between #{begin} and #{end}
            </if>
        </where>
    </select>

原始方法代碼復(fù)雜

2.2使用分頁插件提供的方法

Service層

 // 分頁插件分頁
 // 設(shè)置分頁參數(shù)
 PageHelper.startPage(page,pageSize);
 // PageHelper.startPage() 后直接跟查詢語句
 List<Emp> emps = empMapper.selectPage(name, gender, begin, end);
 // Page 就是分頁包裝的結(jié)果對象
 Page<Emp> p = (Page<Emp>) emps;
 // 封裝數(shù)據(jù)
 PageBean<Emp> pageBean = new PageBean<>(p.getTotal(),p.getResult());

mapper / dao層

List<Emp> selectPage(String name,Integer gender,LocalDate begin,LocalDate end);

XXXmapper.xml

<select id="selectPage" resultType="com.pw.tlias.pojo.Emp">
        select id, username, password, name, gender, image, job, entrydate, dept_id, create_time, update_time
        from emp
        <where>
            <if test="name != null and name != ''">
                and name like concat('%',#{name},'%')
            </if>
            <if test="gender != null">
                and gender = #{gender}
            </if>
            <if test="begin != null and end != null">
                and entrydate between #{begin} and #{end}
            </if>
        </where>
    </select>

附加

springboot pagehelper,spring boot,java,mybatis文章來源地址http://www.zghlxwxcb.cn/news/detail-847714.html

到了這里,關(guān)于SpringBoot分頁插件(PageHelper)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • Spring與Mybatis整合&&aop整合pageHelper分頁插件

    Spring與Mybatis整合&&aop整合pageHelper分頁插件

    Spring與MyBatis整合 的意義在于提供了一種結(jié)合優(yōu)勢的方式,以便更好地 開發(fā)和管理持久層(數(shù)據(jù)庫訪問)代碼 。 這里也是總結(jié)了幾點(diǎn)主要意義 簡化配置:Spring與MyBatis整合后,可以通過Spring的配置文件來管理和配置MyBatis的相關(guān)配置,例如數(shù)據(jù)源、事務(wù)管理等,而不需要額外

    2024年02月11日
    瀏覽(159)
  • MyBatis與Spring整合以及AOP和PageHelper分頁插件整合

    MyBatis與Spring整合以及AOP和PageHelper分頁插件整合

    目錄 前言 一、MyBatis與Spring整合的好處以及兩者之間的關(guān)系 1.好處 2.關(guān)系 ?二、MyBatis和Spring集成 1.導(dǎo)入pom.xml 2.編寫配置文件? 3.利用mybatis逆向工程生成模型層代碼 三、常用注解 ?四、AOP整合pageHelper分頁插件 創(chuàng)建一個(gè)切面 測試 MyBatis是一個(gè)開源的持久層框架,而Spring是一個(gè)

    2024年02月11日
    瀏覽(23)
  • SpringBoot第26講:SpringBoot集成MySQL - MyBatis PageHelper分頁

    前文中,我們展示了Spring Boot與MyBatis的集成,但是沒有展示分頁實(shí)現(xiàn)。本文是SpringBoot第26講,專門介紹分頁相關(guān)知識體系和基于MyBatis的 物理分頁P(yáng)ageHelper

    2024年02月13日
    瀏覽(23)
  • MyBatis與Spring集成&常用注解以及AOP和PageHelper分頁插件整合

    MyBatis與Spring集成&常用注解以及AOP和PageHelper分頁插件整合

    目錄 前言 一、MyBatis與Spring整合的好處以及兩者之間的關(guān)系 1.好處 2.關(guān)系 ?二、MyBatis和Spring集成 1.導(dǎo)入pom.xml 2.編寫配置文件? 3.利用mybatis逆向工程生成模型層代碼 三、常用注解 ?四、AOP整合pageHelper分頁插件 創(chuàng)建一個(gè)切面 測試 MyBatis是一個(gè)開源的持久層框架,而Spring是一個(gè)

    2024年02月07日
    瀏覽(28)
  • 【SpringBoot】MyBatis與MyBatis-Plus分頁查詢 & github中的PageHelper

    【SpringBoot】MyBatis與MyBatis-Plus分頁查詢 & github中的PageHelper

    ??????? 筆者寫這篇博客是因?yàn)榻谟龅降年P(guān)于兩者之間的分頁代碼差距,其實(shí)之前也遇見過但是沒有去整理這篇博客,但由于還是被困擾了小一會兒時(shí)間,所以還是需要 加深記憶 。其實(shí)會看前后端傳參解決這個(gè)問題很快、不麻煩。關(guān)于這兩個(gè)框架的分頁代碼問題主要就

    2024年02月03日
    瀏覽(25)
  • 6.Mybatis分頁插件(PageHelper),解決PageHelper.startPage()不安全分頁

    Mybatis專欄目錄(點(diǎn)擊進(jìn)入…) @TOC 引入分頁插件有下面2種方式,推薦使用Maven方式 (1)引入Jar包 可以從下面的地址中下載最新版本的jar包 https://oss.sonatype.org/content/repositories/releases/com/github/pagehelper/pagehelper/ http://repo1.maven.org/maven2/com/github/pagehelper/pagehelper/ 由于使用了sql解析

    2024年02月03日
    瀏覽(31)
  • Mybatis---分頁插件PageHelper

    目錄 一、分頁插件的概念 二、PageHelper的介紹 (1)引入依賴 (2)Mybatis配置文件配置PageHelper插件 (3)測試方法 開發(fā)過程中如果要進(jìn)行分頁查詢,需要傳入頁數(shù)和每頁條數(shù)。返回頁面數(shù)據(jù),總條數(shù),總頁數(shù),當(dāng)前頁面,每頁條數(shù)等數(shù)據(jù)。此時(shí)使用PageHelper插件可以快速幫助我

    2024年02月09日
    瀏覽(22)
  • Mybatis 分頁插件 PageHelper

    Mybatis 分頁插件 PageHelper

    今天記錄下 Mybatis 分頁插件 pageHelper 的使用。 有一個(gè)員工表(employee),現(xiàn)在要使用 pageHelper 插件實(shí)現(xiàn)員工的分頁查詢。 Employee employee類定義代碼如下 EmployeeDTO EmployeeDTO定義代碼如下 PageResult 最后返回給前端的數(shù)據(jù)封裝在 PageResult 類中, PageHelper 類定義如下: EmployeePageQueryDTO 分

    2024年01月19日
    瀏覽(28)
  • tk-mybatis使用介紹,springboot整合tk-mybatis、PageHelper實(shí)現(xiàn)分頁查詢

    tk-mybatis使用介紹,springboot整合tk-mybatis、PageHelper實(shí)現(xiàn)分頁查詢

    Mybatis-Plus極大簡化了我們的開發(fā),作為mybatis的增強(qiáng)版,Mybatis-Plus確實(shí)幫我們減少了很多SQL語句的編寫,通過其提供的API,可以方便地完成增刪查改操作。但是,其實(shí)除了Mybatis-Plus以外,還有一個(gè)技術(shù)tk-mybatis可以達(dá)到同樣的效果,只不過隨著MP的興起,tk-mybatis已經(jīng)被漸漸淡忘

    2024年02月12日
    瀏覽(24)
  • 【SpringBoot】Spring Boot 項(xiàng)目中整合 MyBatis 和 PageHelper

    目錄 前言? ? ? ?? 步驟 1: 添加依賴 步驟 2: 配置數(shù)據(jù)源和 MyBatis 步驟 3: 配置 PageHelper 步驟 4: 使用 PageHelper 進(jìn)行分頁查詢 IDEA指定端口啟動 總結(jié) ????????Spring Boot 與 MyBatis 的整合是 Java 開發(fā)中常見的需求,特別是在使用分頁插件如 PageHelper 時(shí)。PageHelper 是一個(gè)針對 MyBat

    2024年04月25日
    瀏覽(32)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包