SubList分页-009-SubListStudentDAOImpl类
程序员文章站
2024-03-18 13:49:10
...
概述
1.extends DAO implements StudentDAO
2.List getAllStudent方法
3.Pager findStudent方法
测试代码
package top.itcourse.page.dao;
import java.util.ArrayList;
import java.util.List;
import com.sun.xml.internal.bind.v2.runtime.reflect.opt.Const;
import top.itcourse.page.model.Constant;
import top.itcourse.page.model.Pager;
import top.itcourse.page.model.Student;
/*
* SubListStudentDAOImpl类:
* 1.extends DAO<Student> implements StudentDAO
* 2.List<Student> getAllStudent方法
* 3.Pager<Student> findStudent方法
*/
public class SubListStudentDAOImpl extends DAO<Student> implements StudentDAO {
/**
*
* @Title: findStudent
* @Description: 根据条件,查询学生分页信息
* @param @param searchModel : 封装的查询条件
* @param @param currentPage :查询的第几页数据
* @param @param pageSize : 每页显示的记录数目
* @param @return 返回查询结果
* @return Pager<Student> 返回类型
*/
@Override
public Pager<Student> findStudent(Student searchModel, int currentPage, int pageSize) {
// 1.获取所有学生信息
List<Student> dataList = getAllStudent(searchModel);
// 2.构造Pager对象
Pager<Student> pager = new Pager<>(dataList, currentPage, pageSize);
return pager;
}
/**
*
* @Title: getAllStudent
* @Description: 模仿获取所有数据
* @param @param searchModel : 查询参数
* @param @return 查询结果
*/
public List<Student> getAllStudent(Student searchModel) {
// 参数列表
List<Object> paramList = new ArrayList<>();
// 按姓名、性别过滤学生信息
String name = searchModel.getName();
int gender = searchModel.getGender();
// SQL
StringBuilder sql = new StringBuilder("select * from student where 1 = 1 ");
if( name != null && !name.equals("")) {
sql.append(" and name like ? ");
paramList.add("%" + name + "%");
}
// 男性,女性
if( gender == Constant.GENDEL_MALE || gender == Constant.GENDEL_FEMALE ) {
sql.append(" and gender = ? ");
paramList.add(gender);
}
// 查询
List<Student> result = getForList(sql.toString(), paramList.toArray());
for (Student student : result) {
System.out.println(student);
}
return result;
}
}
源码下载
关注下方的微信公众号,回复:java_div_page.code