尊重个人劳动成果,转载请注明出处:
http://blog.csdn.net/czd3355/article/details/71404940
1. 无条件分页查询
StudentMapper.xml 代码:
<select id="findByFY" parameterType="map" resultType="com.czd.mybatis01.bean.Student">
SELECT id,`name` FROM stu
limit #{start},#{size}
</select>
提示:limit #{start},#{size}
中的 start,size 都是 map 集合的 key ,不是 value。
java 关键代码:
@Test
public void main() throws Exception {
StudentDao studentDao = new StudentDao();
// 输出无条件分页查询结果
System.out.println(studentDao.testFindByFY(0, 3));
System.out.println(studentDao.testFindByFY(3, 3));
System.out.println(studentDao.testFindByFY(6, 3));
}
// 无条件分页查询
public List<Student> testFindByFY(int start, int size) throws Exception {
SqlSession sqlSession = sqlSessionFactory.openSession();
Map<String, Object> map = new LinkedHashMap<String, Object>();
map.put("start", start);
map.put("size", size);
return sqlSession.selectList(nameSpace + ".findByFY", map);
}
输出结果:
2. 有条件分页查询
StudentMapper.xml 代码:
<select id="findByFYWithName" parameterType="map" resultType="com.czd.mybatis01.bean.Student">
SELECT id,`name` FROM stu
WHERE `name` LIKE '${name}%'
limit #{start},#{size}
</select>
java 关键代码:
@Test
public void main() throws Exception {
StudentDao studentDao = new StudentDao();
// 输出有条件分页查询结果
System.out.println(studentDao.testFindByFYWithName(0,2,"czd"));
System.out.println(studentDao.testFindByFYWithName(2,2,"czd"));
}
// 有条件分页查询
public List<Student> testFindByFYWithName(int start, int size, String name) throws Exception {
SqlSession sqlSession = sqlSessionFactory.openSession();
Map<String, Object> map = new LinkedHashMap<String, Object>();
map.put("start", start);
map.put("size", size);
map.put("name",name);
return sqlSession.selectList(nameSpace + ".findByFYWithName", map);
}
输出结果: