Limit实现分页
接口类开发:
这里我们传入的类型选择Map类型:(使用HashMap传递startIndex和pageSize)
public interface UserMapper { // 分页查询 List<User> getUserByLimit(Map<String, Integer> map); }
UserMapper.xml:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "https://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="top.imustctf.dao.UserMapper"> <resultMap id="UserMap" type="User"> <result column="id" property="id"/> <result column="name" property="name"/> <result column="password" property="pwd"/> </resultMap> <select id="getUserByLimit" resultMap="UserMap" parameterType="map"> select * from mybatis.user limit #{startIndex},#{pageSize} </select> </mapper>
测试类开发:
public class UserMapperTest { @Test public void test() { SqlSession sqlSession = MybatisUtils.getSqlSession(); try { UserMapper userMapper = sqlSession.getMapper(UserMapper.class); HashMap<String, Integer> map = new HashMap<>(); map.put("startIndex", 0); map.put("pageSize", 2); List<User> userByLimit = userMapper.getUserByLimit(map); for (User user : userByLimit) { System.out.println(user); } } catch (Exception e) { throw new RuntimeException(e); } finally { sqlSession.close(); } } }