MyBatis(4)-Pagination

1.1 Pagination

Thinking: Why do you need paging?
When learning persistence layer frameworks such as mybatis, we will often add, delete, modify, and query data. The most used is to query the database. If we query a large amount of data, we often use paging to query, that is, small processing each time. Part of the data, so that the pressure on the database is within the controllable range.

1 limit paging function

SELECT * from user limit startIndex,pageSize

select * from user limit 0,2;

test

//    分页
    List<User> getUserByLimit(Map<String , Integer> map);
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zs.mapper.UserMapper">

    <!--结果集映射-->
    <resultMap id="UserMap" type="user">
        <!--column数据库中的字段,property实体类中的属性-->
        <result column="id" property="id"></result>
        <result column="name" property="name"></result>
        <result column="pwd" property="password"></result>
    </resultMap>

    <select id="getUserByLimit" parameterType="map" resultMap="UserMap">
        SELECT * FROM mybatis_user LIMIT #{
    
    startIndex},#{
    
    pageSize}
    </select>
</mapper>
    @Test
    public void getUserByLimit() {
    
    
        try (SqlSession sqlSesion = MyBatisUtils.getSqlSesion();){
    
    
            UserMapper mapper = sqlSesion.getMapper(UserMapper.class);
            Map<String, Integer> map = new HashMap<>();
            map.put("startIndex",0);
            map.put("pageSize",2);
            List<User> userList = mapper.getUserByLimit(map);
            System.out.println(userList);
        }
    }

Guess you like

Origin blog.csdn.net/zs18753479279/article/details/110821912