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开始)

posted @ 2021-11-12 10:59  俊king  阅读(82)  评论(0编辑  收藏  举报