10.1 添加分页插件的依赖分页插件是一个独立于MyBatis框架之外的第三方插件
PageHelper
10.2 配置插件com.github.pagehelper pagehelper 5.1.10
在MyBatis的主配置文件mybatis-config.xml中通过plugins标签进行配置
10.3 分页查询信息
对学生信息进行查询
@Test
public void testListStudentsPage(){
StudentDao studentDao = MyBatisUtils.getMapper(StudentDao.class);
PageHelper.startPage(2,4);
List students = studentDao.listStudentsPage();
PageInfo pageInfo = new PageInfo<>(students);
List list = pageInfo.getList();
for(Student student:list){
System.out.println(student);
}
System.out.println(pageInfo.getPages());
}
带条件分页查询
(一个条件和多个条件,单表)
- StudentDao.java
public interface StudentDao {
//在MyBatis中进行条件查询
//1、如果操作方法只有一个简单类型或者字符串类型参数,在Mapper中可以直接通过#{str}直接获取参数(str表示除数字外的任意字符串)
//2、如果操作方法有一个对象类型参数,在Mapper配置中可以直接通过#{attrName}获取对象的指定属性值(attrName必须是参数对象的属性)
//3、如果操作方法有一个map类型的参数,在Mapper操作中直接通过#{key}获取key对应的value
//4、如果操作方法有多个参数,
public List listByGender(String gender);
public List listByNameAndGender(@Param("name") String name,@Param("gender") String Gender);
}
- StudentMapper.xml
select from tb_students where stu_gender=#{stuGender}
- Test
@Test
public void testListByGender(){
StudentDao studentDao = MyBatisUtils.getMapper(StudentDao.class);
PageHelper.startPage(1,5);
List students = studentDao.listByGender("女");
PageInfo pageInfo = new PageInfo<>(students);
List list = pageInfo.getList();
for(Student student:list){
System.out.println(student);
}
}
@Test
public void testListByNameAndGender(){
StudentDao studentDao = MyBatisUtils.getMapper(StudentDao.class);
PageHelper.startPage(1,5);
List students = studentDao.listByNameAndGender("孙七", "男");
PageInfo pageInfo = new PageInfo<>(students);
List list = pageInfo.getList();
for (Student student:list){
System.out.println(student);
}
}



