封装一个startPage()的方法,表示开启分页查询。Controller层通过接收前端的请求,将请求委托给Service,调用Service层的search()方法,Service层再调用相应的Mapper方法来操作数据库,实现数据的访问。
@Getter
@Setter
@ApiModel(value = "分页模型")
public class PageReq {
@ApiModelProperty(value = "当前第几页",required = true)
@NotNull(message = "当前页码不能为空")
private Integer page;
@ApiModelProperty(value = "每页显示几笔",required = true)
@NotNull(message = "每页笔数不能为空")
private Integer size;
}
注:因当前未写前端页面,所以通过swagger来进行调试,ApiModel、ApiModelProperty表示Swagger的注解。
@Getter
@Setter
@ApiModel(value = "分页响应模型")
public class PageResp<T> {
@ApiModelProperty(value = "总共多少笔数据")
private Long total;
@ApiModelProperty(value = "当前页的数据")
private List<T> rows;
}
@Getter
@Setter
@ToString
@ApiModel(value = "员工查询模型")
public class EmployeeSearchReq extends PageReq {
/**
* 账户名
*/
@ApiModelProperty(value = "账户名")
private String loginName;
}
注:分页查询条件是通过账号进行查询
@Getter
@Setter
@ToString
@ApiModel(value = "员工查询响应模型")
public class EmployeeResp {
/**
* 主键
*/
@ApiModelProperty(value = "编号")
private Long id;
/**
* 登录账户
*/
@ApiModelProperty(value = "登录账户")
private String loginName;
/**
* 性别(0 女 1 男)
*/
@ApiModelProperty(value = "性别(0 女 1 男)")
private String gender;
/**
* 状态(1停用,0正常)
*/
@ApiModelProperty(value = "状态(1停用,0正常)")
private String status;
/**
* 头像
*/
@ApiModelProperty(value = "头像")
private String avatar;
/**
* 创建时间
*/
@ApiModelProperty(value = "创建时间")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
private Date createTime;
/**
* 1超级管理员,0普能后台用户
*/
private String root;
}
/**
* 开启PageHelper分页拦截器
*/
public void startPage(PageReq pageReq){
PageHelper.startPage(pageReq.getPage(),pageReq.getSize());
}
/**
* 泛型放在参数上,分页封装结果
* @param list
* @param <T>
* @return
*/
public <T> R<PageResp<T>> getDataTable(List<T> list){
PageResp<T> pageResp = new PageResp<>();
//判断响应对象的类型,如果你是使用了分页组件以后
if (list instanceof Page){
Page page= (Page) list;
pageResp.setTotal(page.getTotal());
pageResp.setRows(page.getResult());
}else {
pageResp.setTotal((long) list.size());
pageResp.setRows(list);
}
return R.okHasData(pageResp);
}
@RestController
@RequestMapping(value = "/api/employee")
@Api(tags = "后台-员工Api")
public class EmployeeController {
//注入EmployeeService层
@Autowired
private EmployeeService employeeService;
@ApiOperation(value = "分页查询")
@GetMapping
public R<PageResp<EmployeeResp>> search(@Valid EmployeeSearchReq req){
//开启分页
super.startPage(req);
//调用service层的search方法
List<EmployeeResp> list=this.employeeService.search(req);
return getDataTable(list);
}
}
/**
* 查询数据
* @param req
* @return
*/
List<EmployeeResp> search(EmployeeSearchReq req);
/**
* 查询
* @param req
* @return
*/
@Override
public List<EmployeeResp> search(EmployeeSearchReq req) {
return super.baseMapper.search(req);
}
/**
* 查询
* @param req
* @return
*/
List<EmployeeResp> search(@Param(value = "req") EmployeeSearchReq req);
<select id="search" resultType="com.hxzy.controller.admin.employee.resp.EmployeeResp"
parameterType="com.hxzy.controller.admin.employee.req.EmployeeSearchReq">
select id,
login_name as loginName,
gender,status,avatar,
create_time as createTime,
root
from employee
<where>
<if test="req.loginName!=null and req.loginName!=''">
and login_name like concat('%',#{req.loginName},'%')
</if>
</where>
</select>