其他分享
首页 > 其他分享> > 9.分页查询

9.分页查询

作者:互联网

1.limit做分页

1.接口中的写法
    public interface PersonMapper {
        ...
        List<Person> getPersonByLimit(@Param("startIndex") int startIndex,@Param("pageSize") int pageSize);
        ...
    }

2.mapper.xml中的写法
    ...
    <resultMap id="personResultMap" type="wmd">
        <result column="name" property="person_name"/>
    </resultMap>
    ...
    <select id="getPersonByLimit" resultMap="personResultMap">
        重点1:limit startIndex(从哪个下标开始) pageSize(每页查询多少)
        select * from mybatis.person limit #{startIndex},#{pageSize}
    </select>
    ...
    
3.实体类的写法
    @Alias("wmd")
    public class Person {
        private int id;
        private String person_name;
        private int age;
        private String message;
        ....
    }
    
4.测试类的写法
    @org.junit.Test
    public void testGetPersonByLimit(){
        SqlSession sqlSession=MybatisUtil.getsSqlSession();
        PersonMapper personMapper=sqlSession.getMapper(PersonMapper.class);
        List<Person> personList = personMapper.getPersonByLimit(2, 2);
        for (Person person : personList) {
            System.out.println(person);
        }
        sqlSession.close();
    }
5.输出:
    Person{id=3, person_name='吴振', age=18, message='吴振留言'}
    Person{id=4, person_name='吴东霞', age=18, message='吴东霞留言'}

 

标签:...,分页,pageSize,int,private,查询,person,startIndex
来源: https://www.cnblogs.com/wmd-l/p/16267951.html