Mybatis分页
作者:互联网
Mybatis分页
1、Limit分页:
#sql中Limit的语法:select * from mybatis.user LIMIT statIndex,pageSize;
接口编写
//limit分页 List<User> getlimit(Map<String,Integer> map);
xxxMapper.xml
<select id="getlimit" parameterType="map" resultType="User"> select * from mybatis.user limit #{statIndex},#{pagSize} </select>
测试
@Test public void test2(){ SqlSession sqlSession = MybatisUtils.getsqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); HashMap<String, Integer> map = new HashMap<>(); map.put("statIndex",1); map.put("pagSize",2); List<User> getlimit = mapper.getlimit(map); for (User user : getlimit) { System.out.println(user); } }
2、RowBounds分页
接口编写
//RowBounds分页 List<User> getRowBounds();
xxxMapper.xml
//RowBounds是先把数据全部查询到ResultSet,然后从ResultSet中取出offset和limit之间的数据,这就实现了分页查询。 <select id="getRowBounds" resultMap="xxx"> select * from mybatis.user </select>
测试
@Test public void test3(){ qlSession sqlSession = MybatisUtils.getsqlSession(); RowBounds rowBounds = new RowBounds(0,2); List<User> list = sqlSession.selectList("com.xu.dao.UserMapper.getRowBounds",null,rowBounds); for (User user : list) { logger.info(user); } sqlSession.close(); } //RowBounds:offset:起始 || limit:页大小 //不推荐使用
标签:map,分页,List,sqlSession,user,Mybatis,RowBounds 来源: https://www.cnblogs.com/whitespaces/p/12442338.html