zoukankan      html  css  js  c++  java
  • 11.11 Mybatis分页功能

    11.11 Mybatis分页功能

    Mybatis分页功能实现原理

    • 先查询出所有的记录

    • 再按起始位置和页面容量取出结果

    具体实现

    websiteMapper接口:

    package com.junkingboy.mapper;

    import com.junkingboy.bean.Student;
    import com.junkingboy.bean.User;
    import com.junkingboy.bean.Website;
    import org.apache.ibatis.annotations.*;
    import org.apache.ibatis.type.JdbcType;

    import java.util.List;
    import java.util.Map;

    /**
    * @description:mybatis框架测试接口,该接口定义了.xml文件操作的表用到的方法
    * @data: 2021/11/2 16:35
    * @author: Lucifer
    */
    public interface WebsiteMapper {
       /* 使用mybatis实现分页查询的功能。一开始先查询处所有的结果然后再以结果进行排序最后进行批量读取 */
       List<Website> selectWebsite5(@Param("site") Website site, @Param("from") Integer currentPageNo, @Param("pageSize") Integer pageSize);
    }

    websiteMapper.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.junkingboy.mapper.WebsiteMapper">
       <!-- 使用Mybatis实现分页查询的功能 -->
       <select id="selectWebsite5" resultType="com.junkingboy.bean.Website">
          SELECT id, name, url, age, country
          FROM javawebtest.website
           <trim prefix="where" prefixOverrides="and">
               <if test="site.name != null and site.name != ''">
                  AND name
                  LIKE CONCAT('%', #{site.name}, '%')
               </if>
               <if test="site.url != null and site.url != ''">
                  AND url
                  LIKE CONCAT('%', #{site.url}, '%')
               </if>
          ORDER BY id
          LIMIT #{from}, #{pageSize}
           </trim>
       </select>
    </mapper>

    测试类:

    package com.junkingboy.mapper;

           import com.junkingboy.bean.Website;
           import org.apache.ibatis.annotations.Param;
           import org.apache.ibatis.io.Resources;
           import org.apache.ibatis.session.SqlSession;
           import org.apache.ibatis.session.SqlSessionFactory;
           import org.apache.ibatis.session.SqlSessionFactoryBuilder;

           import java.io.InputStream;
           import java.util.ArrayList;
           import java.util.List;
           import java.util.Map;

    /**
    * @description:WebsiteMapper接口的实现类,实现了WebsiteMapper接口
    * @data: 2021/11/5 17:18
    * @author: Lucifer
    */
    public class WebsiteMapperImpl<T> implements WebsiteMapper {
       //创建表的实现类对象
       Website website = new Website();
       List<Website> websiteList;
       InputStream is = null;
       SqlSessionFactory ssf;
       SqlSession ss;
       Boolean openSwitch = null;
       Integer changeNum = 0;
       WebsiteMapper wm = null;
       /*
       步骤:
       Io流读取配置文件
       使用SqlSessionFactory接口实现类加载配置文件
       使用SqlSession接口开启连接
       获取WebsiteMapper接口定义的方法
       执行接口当中的方法
        */

       /* 获取读取配置文件的方法 */
       private Boolean readProperties() {
           try {
               is = Resources.getResourceAsStream("xml/mybatis-config.xml");
          }catch (Exception e) {
               /*结束方法*/
               System.out.println("找不到配置文件!");
               e.printStackTrace();
               return false;
          }
           return true;
      }

       /* 循环遍历打印结果方法 */
       public void printResult(List<Website> website) {
           for (Website site :
                   website) {
               System.out.println(site);
          }
      }

       @Override
       public List<Website> selectWebsite5(
               @Param("site") Website site, @Param("from") Integer currentPageNo, @Param("pageSize") Integer pageSize
      )
      {
           if (site != null) {
               if (currentPageNo != 0) {
                   if (pageSize != 0 && currentPageNo <= pageSize) {
                       openSwitch = readProperties();
                       try {
                           ssf = new SqlSessionFactoryBuilder().build(is);
                           ss = ssf.openSession();
                           wm = ss.getMapper(WebsiteMapper.class);
                           websiteList = wm.selectWebsite5(site, currentPageNo, pageSize);
                      }catch (Exception e) {
                           e.printStackTrace();
                      }
                  }
              }
          }
           if (websiteList == null) {
               System.out.println("方法执行成功,内容为空!");
          }
           return websiteList;
      }
    }

    参数说明:

    • currentPageNo是指起始位置。pageSize是指页面容量

    Mybatis分页的特点:

    • 属于DAO层操作,不涉及任何业务实现,方法中第一个参数为 limit 的起始位置(下标从 0 开始),而不是用户输入的真正页码(页码从1开始)

    It's a lonely road!!!
  • 相关阅读:
    hdu 3666 差分约束系统
    hdu 1198农田灌溉
    常微分方程(阿諾爾德) Page 45 相空間,相流,運動,相曲線 註記
    高等微積分(高木貞治) 1.4節 例2
    常微分方程(阿諾爾德) Page 45 相空間,相流,運動,相曲線 註記
    解析函數論 Page 29 命題(2) 函數模的有界性
    高等微積分(高木貞治) 1.4節 例2
    解析函數論 Page 29 命題(1) 有界閉集上的一致連續性
    解析函數論 Page 29 命題(3) 模的下界的可達性
    解析函數論 Page 29 命題(2) 函數模的有界性
  • 原文地址:https://www.cnblogs.com/JunkingBoy/p/15543475.html
Copyright © 2011-2022 走看看