zoukankan      html  css  js  c++  java
  • MyBatis(6)——分页的实现

    分页的实现

    a)通过mysql的分页查询语句:

    说明:sql的分页语句格式为select * from aaa limit #{startIndex},#{pageSize}

    //------------映射文件------------//
    //*设置传入参数类型为Map,parameterType="Map"
    
    
    <!--查询语句,分页查询-->
    <!-- 因为字段名不一致,此处的resultType换成结果集映射resultMap -->
    <select id="selectAll" parameterType="Map" resultMap="UserMap">
    select * from users limit #{startIndex},#{pageSize}
    </select>
    
    
    //------------实体逻辑处理类:dao------------//
    //*新建Map参数并传入
    
    
    public List<User> getAll(int currentPage,int pageSize) throws IOException
    {
      SqlSession session=MyBatisUtil.getSession();
      Map<String, Integer> map=new HashMap<String, Integer>();
      map.put("startIndex", (currentPage-1)*pageSize);
      map.put("pageSize", pageSize);
      List<User> list=session.selectList("cn.lxy.entity.UserMapper.selectAll",map);
      session.close();
      return list;
    }
    

    注:不需要通过新建实体类

    b)通过RowBounds:

    //------------映射文件------------//
    
    <!-- 利用rowbounds实现分页查询 -->
    <select id="getAll" resultType="User">
    select * from users
    </select>
    

    dao中需新建rowBouns对象,构建格式为rowBounds(index,pageSize)

    //------------dao类------------//
    
    //分页查询所有的值2,利用rowbounds(原理同上)
      public List<User> getAll(int currentPage,int pageSize) throws IOException
      {
        SqlSession session=MyBatisUtil.getSession();
        RowBounds rowBounds=new RowBounds((currentPage-1)*pageSize, pageSize);
        List<User> list=session.selectList("cn.lxy.entity.UserMapper.getAll",null,rowBounds);
        session.close();
        return list;
      }
    
  • 相关阅读:
    mysql导入导出数据
    Linux符号连接的层数过多
    win10下docker安装和配置镜像仓库
    PHP资源列表(转)
    php中正则案例分析
    基于CSS3自定义美化复选框Checkbox组合
    基于HTML5 Canvas粒子效果文字动画特效
    基于jQuery商品分类选择提交表单代码
    基于jquery右侧悬浮加入购物车代码
    基于jquery带时间轴的图片轮播切换代码
  • 原文地址:https://www.cnblogs.com/inkqx/p/12316498.html
Copyright © 2011-2022 走看看