1、分页

思考:为什么要分页?

  • 减少数据的处理量

使用Limit分页

#语法:
SELECT * FROM `user` LIMIT startIndex,pageSize;

# [0,3]
SELECT * FROM `user` LIMIT 3;

使用Mybatis实现分页,核心SQL
1.接口

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

2.Mapper.xml

<resultMap id="UserMap" type="User">
    <!--column数据库中的字段,property实体类中的属性-->
    <result column="id" property="id"/>
    <result column="name" property="name"/>
    <result column="pwd" property="password"/>
</resultMap>
<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(UserDao.class);

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

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

    //关闭
    sqlSession.close();
    
}

RYGAR
1 声望0 粉丝