乐趣区

关于java:分页

应用 limit 分页

外围 sql

1.select * from user limit startIndex,pageSize;
2.select * from user limit 3;  #[0,3)
  1. 接口
// 分页
List<User> getUserByLimit(Map<String,Integer> map);
  1. Mapper.xml
<!-- 分页 -->
<select id="getUserByLimit" parameterType="map" resultType="helloUser">
    select * from mybatis.user limit #{startIndex},#{pageSize}
</select>
  1. 测试
@Test
public void getUserByLimit(){SqlSession sqlSession = MybatisUtils.getSqlSession();

    UserMapper mapper = sqlSession.getMapper(UserMapper.class);

    HashMap<String, Integer> map = new HashMap<>();
    map.put("startIndex",2);
    map.put("pageSize",2);

    List<User> userList = mapper.getUserByLimit(map);
    for (User user : userList) {System.out.println(user);
    }
    sqlSession.close();}

应用 RowBounds 分页

  1. 接口
List<User> getUserByRowBounds();
  1. Mapper.xml
<select id="getUserByRowBounds" resultType="helloUser">
    select * from mybatis.user
</select>
  1. 测试
@Test
public void getUserByRowBounds(){SqlSession sqlSession = MybatisUtils.getSqlSession();

    //RowBounds 实现
    RowBounds rowBounds = new RowBounds(1, 2);

    // 通过 Java 代码层面实现分页
    List<User> userList = sqlSession.selectList("com.sunfl.dao.UserMapper.getUserByRowBounds",null,rowBounds);
    for (User user : userList) {System.out.println(user);
    }
    sqlSession.close();}

分页插件

pageHelper

退出移动版