zoukankan      html  css  js  c++  java
  • mybatis_7分页查询

    思考:为什么要分页?

    • 减少数据的处理量

    7.1、使用Limit分页

    语法:SELECT * from user limit startIndex,pageSize;
    SELECT * from user limit 3;  #[0,n]
    

    使用Mybatis实现分页,核心SQL

    1. 接口

      //分页
      List<User> getUserByLimit(Map<String,Integer> map);
      
    2. Mapper.xml

      <!--//分页-->
      <select id="getUserByLimit" parameterType="map" resultMap="UserMap">
          select * from  mybatis.user limit #{startIndex},#{pageSize}
      </select>
      
    3. 测试

      @Test
      public void getUserByLimit(){
      SqlSession sqlSession = MybatisUtils.getSqlSession();
      UserMapper mapper = sqlSession.getMapper(UserMapper.class);
      
      HashMap<String, Integer> map = new HashMap<String, Integer>();
          map.put("startIndex",1);
          map.put("pageSize",2);
      
          List<User> userList =  mapper.getUserByLimit(map);
          for (User user : userList) {
          System.out.println(user);
          }
      
          sqlSession.close();
          }
      
      

    7.2、RowBounds分页

    不再使用SQL实现分页,使用java代码实现分页,不推荐使用

    1. 接口

      //分页2
      List<User> getUserByRowBounds();
      
    2. mapper.xml

      <!--分页2-->
      <select id="getUserByRowBounds" resultMap="UserMap">
          select * from  mybatis.user
      </select>
      
    3. 测试

      @Test
      public void getUserByRowBounds(){
      SqlSession sqlSession = MybatisUtils.getSqlSession();
      
      //RowBounds实现
      RowBounds rowBounds = new RowBounds(1, 2);
      
      //通过Java代码层面实现分页
      List<User> userList = sqlSession.selectList("com.loading.dao.UserMapper.getUserByRowBounds",null,rowBounds);
      
          for (User user : userList) {
          System.out.println(user);
          }
      
          sqlSession.close();
          }
      

    7.3、分页插件

    mybaits-pagehelper

  • 相关阅读:
    HDU2045_LELE的RPG难题
    HDU2050_折线分割平面数
    HDU1159_最长公共子序列
    ASP.NET 页生命周期概述
    Hadoop编译
    .Hadoop NameNode单点问题解决方案之二 AvatarNode 部署
    Pig调试环境
    HADOOP综合应用架构之一 配置Secondarynamenode在另一台机器运行
    JAVA采用远程连接Hive
    Windows Server 2003 FTP服务器配置详解
  • 原文地址:https://www.cnblogs.com/loading955/p/13795005.html
Copyright © 2011-2022 走看看