First look paging sql statement:
SELEC * FROM 表名 LIMIT startIndex,pageSize
tips:
* StartIndex: starting position (from which the element start page)
* PageSize: how many elements per page
E.g:
select * from user limit 0,2: query the user table all the elements, the starting position is the first zero element, two elements per page
select * from user limit 3: query the user table all the elements, the starting position is the first zero elements, the elements per page 3
Use mabatis implement paging:
(1) defines the interface
package com.kuang.dao; import com.kuang.pojo.User; import java.util.List; import java.util.Map; public interface UserMapper { List<User> getUserByLimit(Map<String ,Object> map); }
(2) mapping file to write
<?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.kuang.dao.UserMapper"> <select id="getUserByLimit" resultMap="user" parameterType="map"> SELECT * FROM mybatis.user LIMIT #{startIndex},#{pageSize} </select> </mapper>
(3) Writing Test Methods
@Test public void getUserByLimit(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); HashMap<String, Object> map = new HashMap<String, Object>(); map.put("startIndex",0); map.put("pageSize",2); List<User> userList = mapper.getUserByLimit(map); for (User user : userList) { System.out.println(user); } sqlSession.close(); }