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;
      }
    
  • 相关阅读:
    [Leetcode] Merge Intervals
    [Leetcode] Sort Colors
    junit
    DBUnit的使用
    xml简介---来自百度百科
    今天开始深入学习XML
    Java 用Myeclipse部署项目基础坏境搭建
    properties配置文件读取方法
    Java web做服务器之间的通信方法
    Java Socket简单的客服端及其服务器端
  • 原文地址:https://www.cnblogs.com/inkqx/p/12316498.html
Copyright © 2011-2022 走看看