/**
* 优化多表查询并进行分页显示
* 书籍列表,条件为不同管理员
* @param aid 管理员id
* @param page 前端传递的页数(第几页)
* @param size 每一页显示的数量
* @return
*/
public Result findBookByAdmin2(Integer aid,Integer page,Integer size){
Result result = new Result();
result.setCode(ResultCode.FAILURE);
if (aid == null || IntegerUtil.toInt(page)<=0){
result.setMessage("传输的传输有误");
return result;
}
//size设置默认值
if (IntegerUtil.toInt(size)<=0) size = 10;
Model<ATestType> typeModel = ModelFactory.getModel(ATestType.class);
Model<ATestBook> bookModel = ModelFactory.getModel(ATestBook.class);
//通过管理员id获取他管理的所有书籍并实现分页效果
List<ATestBook> books = bookModel
.where("aid", aid)
.limit((page - 1) * size, size)
.list();
List<ATestType> types = typeModel.where("aid", aid).list();
int count = books.size();
if (count<=0){
result.setMessage("没有数据");
return result;
}
Map<Integer,ATestType> map1 = new HashMap<>();
for (ATestType type : types) {
map1.put(type.getTid(),type);
}
//根据书籍查找对应的分类数据
List<ATestTypeAndBook> allBook = new ArrayList<>();
for (ATestBook book : books) {
//将书籍信息赋予到vo
ATestTypeAndBook vo = new ATestTypeAndBook();
BeanUtils.copyProperties(book,vo);
//根据书籍类中的分类id获取分类信息
ATestType t = map1.get(book.getTid());
vo.setTypeName(t.getTypeName());
allBook.add(vo);
}
//存储分页所需数据
Map<String,Object> map = new HashMap<>();
map.put("count",count);
map.put("pageCount",count%size==0?count/size:count/size+1);
map.put("data",allBook);
result.setCode(ResultCode.SUCCESS);
result.setMessage("成功");
result.setObject(map);
return result;
}
SSM开发第二天——多表查询优化
最新推荐文章于 2021-02-05 09:55:35 发布