sql中limit的语法

SELECT * FROM table LIMIT [offset,] rows | rows OFFSET offset

先在接口mapper中定义方法。

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

然后到mapper.xml中进行注册

 <select id="getUserByLimit" parameterType="map" resultMap="UserMap">
        select * from mybatis.user limit #{value1},#{value2}
    </select>

这里面的value1,value2可以随便写,因为只是map中的value值,只要跟测试的时候对得上就行。

    @Test
    public void testLimit(){
        Map<String, Integer> map = new HashMap<>();
        map.put("value1",0);
        map.put("value2",3);
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> userByLimit = mapper.getUserByLimit(map);
        for (User user : userByLimit) {
            System.out.println(user);
        }
        sqlSession.close();
    }

更多推荐

mybatis中使用limit进行分页