第一步:书写controller类
//查询bgm所有数据
@PostMapping("/queryBgmList")
public PagedResult queryBgmList(Integer page) {
return videoService.queryBgmList(page, 10);
}
第二步:书写service类
public interface VideoService {
//返回封装后的分页属性
public PagedResult queryBgmList(Integer page,Integer pageSize);
}
第三步:书写service实现类
package com.lpy.service.impl;
import java.util.List;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.miemiedev.mybatis.paginator.domain.PageList;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lpy.mapper.BgmMapper;
import com.lpy.pojo.Bgm;
import com.lpy.pojo.BgmExample;
import com.lpy.service.VideoService;
import com.lpy.utils.PagedResult;
@Service
public class VideoServiceImpl implements VideoService {
@Autowired
private BgmMapper bgmMapper;
@Autowired
private Sid sid;
@Override
public PagedResult queryBgmList(Integer page, Integer pageSize) {
//第一步:开始分页
PageHelper.startPage(page,pageSize);
//第二步:查询的条件
BgmExample example=new BgmExample();
//第三步:查询数据库的数据list
List<Bgm> list = bgmMapper.selectByExample(example);
//第四步:将查询出来的数据进行封装
PageInfo<Bgm> pageList=new PageInfo<>(list);
//第五步:将封装好的数据给我们自己写的帮助类里面,并返回
PagedResult result=new PagedResult();
result.setTotal(pageList.getPages());//总的页数
result.setRows(list);//每一列
result.setPage(page);//当前的页数
result.setRecords(pageList.getTotal());//总的数量
return result;
}
}
第三步涉及到了帮助类
package com.lpy.utils;
import java.util.List;
/**
* @Description: 封装分页后的数据格式
*/
public class PagedResult {
private int page; // 当前页数
private int total; // 总页数
private long records; // 总记录数
private List<?> rows; // 每行显示的内容
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public long getRecords() {
return records;
}
public void setRecords(long records) {
this.records = records;
}
public List<?> getRows() {
return rows;
}
public void setRows(List<?> rows) {
this.rows = rows;
}
}
package com.lpy.utils;
/**
* @Description: 自定义响应数据结构
* 这个类是提供给门户,ios,安卓,微信商城用的
* 门户接受此类数据后需要使用本类的方法转换成对于的数据类型格式(类,或者list)
* 其他自行处理
* 200:表示成功
* 500:表示错误,错误信息在msg字段中
* 501:bean验证错误,不管多少个错误都以map形式返回
* 502:拦截器拦截到用户token出错
* 555:异常抛出信息
*/
public class LpyJSONResult {
// 响应业务状态
private Integer status;
// 响应消息
private String msg;
// 响应中的数据
private Object data;
private String ok; // 不使用
public static LpyJSONResult build(Integer status, String msg, Object data) {
return new LpyJSONResult(status, msg, data);
}
public static LpyJSONResult ok(Object data) {
return new LpyJSONResult(data);
}
public static LpyJSONResult ok() {
return new LpyJSONResult(null);
}
public static LpyJSONResult errorMsg(String msg) {
return new LpyJSONResult(500, msg, null);
}
public static LpyJSONResult errorMap(Object data) {
return new LpyJSONResult(501, "error", data);
}
public static LpyJSONResult errorTokenMsg(String msg) {
return new LpyJSONResult(502, msg, null);
}
public static LpyJSONResult errorException(String msg) {
return new LpyJSONResult(555, msg, null);
}
public LpyJSONResult() {
}
public LpyJSONResult(Integer status, String msg, Object data) {
this.status = status;
this.msg = msg;
this.data = data;
}
public LpyJSONResult(Object data) {
this.status = 200;
this.msg = "OK";
this.data = data;
}
public Boolean isOK() {
return this.status == 200;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public String getOk() {
return ok;
}
public void setOk(String ok) {
this.ok = ok;
}
}