为什么分页
SELECT *FROM USER LIMIT 2,2
接口UserMapper
//分页 public List<User> getUserByLimit(Map<String,Integer> map);
配置文件Mapper
<select id="getUserByLimit" resultType="User" parameterType="map"> select * from user limit #{begin},#{end}; </select>
方法测试
@Test public void getUserByLimitTest(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); HashMap<String, Integer> map = new HashMap<String, Integer>(); map.put("begin",1); map.put("end",2); List<User> userByLimit = mapper.getUserByLimit(map); for (User user : userByLimit) { System.out.println(user); } sqlSession.close(); }
不使用SQL实现分页
1.接口
public List<User> getUserByRowBounds();
2.Mapper.xml
<!-- RowBounds--> <select id="getUserByRowBounds" resultType="User"> select *from user </select>
3.测试
@Test public void getUserByRowBounds(){ //RowBounds实现 RowBounds rowBounds = new RowBounds(1,2); SqlSession sqlSession = MybatisUtils.getSqlSession(); List<User> userList = sqlSession.selectList("com.lin.mapper.UserMapper.getUserByRowBounds", null, rowBounds); for (User user : userList) { System.out.println(user); } sqlSession.close(); }