前端写好页数和每页显示多少数据,如果搜索条件不为空的话,加上搜索条件。
initEvaluations(){
let url = '/evaluation/?page=' + this.page + '&size=' + this.size;
if (this.employeeName) {
url += '&employeeName=' + this.employeeName;
}
this.getRequest(url).then(resp=>{
if(resp){
this.evaluations = resp.data;
this.total = resp.total;
}
});
}
Controller层接收请求,调用Service处理。
@RestController
@RequestMapping("/evaluation")
public class EvaluationController {
@Autowired
EvaluationService evaluationService;
@GetMapping("/")
public RespPageBean getEvaluationByPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size, Evaluation evaluation, Date[] beginDateScope) {
return evaluationService.getAllEvaluation(page,size,evaluation,beginDateScope);
}
}
Service层把页数和每页条数进行处理,变成从第几条开始,需要几条数据。一般除了数据本身外,还需要数据总数,方便展示。
@Service
public class EvaluationService {
@Autowired
EvaluationMapper evaluationMapper;
public RespPageBean getAllEvaluation(Integer page, Integer size, Evaluation evaluation, Date[] dateScope){
if (page != null && size != null) {
page = (page - 1) * size;
}
List<Evaluation> data = evaluationMapper.selectAllEvaluationByPage(page,size,evaluation,dateScope);
Long total = evaluationMapper.count(evaluation,dateScope);
RespPageBean bean = new RespPageBean();
bean.setData(data);
bean.setTotal(total);
return bean;
}
}
Mapper接口声明方法。
public interface EvaluationMapper {
List<Evaluation> selectAllEvaluationByPage( Integer page, Integer size, Evaluation evaluation, Date[] dateScope);
Long count(Evaluation evaluation, Date[] dateScope);
}
xml文件实现接口,其实就是创建了一个代理类,来实现Mapper接口。实现的方式是用SQL写的。 这里的page其实已经不是页数的意思了,为了代码可读性可以不用这么做。 用<if test标签来实现条件查询。
<resultMap id="EvaluationWithName" type="org.javaboy.vhr.model.Evaluation">
<id column="id" property="id" jdbcType="INTEGER"/>
<result column="evaluation" property="evaluation" jdbcType="LONGVARCHAR"/>
<result column="evaluationTime" property="evaluationTime" jdbcType="TIMESTAMP"/>
<result column="employeeName" property="employeeName" jdbcType="VARCHAR"/>
<result column="hrName" property="hrName" jdbcType="VARCHAR"/>
</resultMap>
<select id="selectAllEvaluationByPage" resultMap="EvaluationWithName">
select evaluation.id,evaluation.evaluation,evaluation.evaluationTime,employee.`name` 'employeeName',hr.`name` 'hrName',hr.`id` 'hrId'
from evaluation,employee,hr
where evaluation.employeeId = employee.id
and evaluation.hrId = hr.id
<if test="evaluation.employeeName !=null and evaluation.employeeName!=''">
and employee.`name` like concat('%',#{evaluation.employeeName},'%')
</if>
ORDER BY evaluationTime desc
<if test="page !=null and size!=null">
limit #{page},#{size}
</if>
</select>
<select id="count" resultType="java.lang.Long">
select count(1)
from evaluation,employee,hr
where evaluation.employeeId = employee.id
and evaluation.hrId = hr.id
<if test="evaluation.employeeName !=null and evaluation.employeeName!=''">
and employee.`name` like concat('%',#{evaluation.employeeName},'%')
</if>
</select>
|