MyBatis-Plus分页连表查询
上代码
小Demo,对于数据量小业务可以使用 练练手(Stram流)
Utils类
package cn.xinzhi.common.utils;
import java.util.ArrayList;
import java.util.List;
public class ListPaging {
/**
* 数据集合的分页方法,根据传入总共的数据跟页码,返回页码所需要显示多少条的数据
* <BR/>采用泛型的方法,即为,list中为什么类型的数据就返回什么类型的数据
* @param f 带有需要进行分页的数据集合
* @param pageNo 第几页
* @param dataSize 显示多少条数据
* @return 进过分页之后返回的数据
*/
public static <F> List<F> datepaging(List<F> f, int pageNo, int dataSize) {
/*
* 经过测试发现当pageNo为0或者小于时,也就是第0页时,程序会报错,所以需要处理一下pageNo的值
*
* 先进行空值的判断,避免程序出现null异常
*
* 当pageNo的值小于等于0时,我们让它的值为1
*/
//参数的校验
if (f == null) {//当传入过来的list集合为null时,先进行实例化
f = new ArrayList<F>();
}
if ((Object) pageNo == null) {//当传入过来的pageNo为null时,先进行赋值操作
pageNo = 1;
}
if ((Object) dataSize == null) {//当传入过来的dataSize为null时,先进行赋值操作
dataSize = 1;
}
if (pageNo <= 0) {
pageNo = 1;
}
//记录一下数据一共有多少条
int totalitems = f.size();
//实例化一个接受分页处理之后的数据
List<F> afterList = new ArrayList<F>();
/*
* 进行分页处理,采用for循环的方式来进行处理
*
* 首先for循环中,i应该从哪里开始:i应该从 (当前是第几页 -1 乘以 条数) 开始
*
* 然后for循环应该到哪里结束,也就是i应该小于:判断(开始的索引+显示条数)是不是大于总条数,如果大于就是总条数,如果小于就是(开始的索引+显示条数)
*
* 然后让i++
*/
for
(int i = (pageNo - 1) * dataSize;
i < (((pageNo - 1) * dataSize) + dataSize >
totalitems ? totalitems : ((pageNo - 1) * dataSize) + dataSize);
i++) {
//然后将数据存入afterList中
afterList.add(f.get(i));
}
//然后将处理后的数据集合进行返回
return afterList;
}
}
Mapper层
public interface HdSpuMapper extends BaseMapper<HdSpu> {
}
Entity层
@Data
@EqualsAndHashCode(callSuper = false)
@ApiModel(value="HdSpu对象", description="")
public class HdSpu implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "器材id")
@TableId(value = "id", type = IdType.AUTO)
private Long id;
@ApiModelProperty(value = "器材名称")
private String equipmentName;
@ApiModelProperty(value = "类别id")
private Integer categoryId;
@ApiModelProperty(value = "品牌id")
private Integer brandId;
@ApiModelProperty(value = "仓库id")
private Integer warehouseId;
}
其它表也一样,这边使用了mybatis-plus代码生成器进行生成代码(切记生成完后将代码生成器依赖删点,以防和mybatis-plus依赖发生冲突)
Service层
@Service
public class HdSpuServiceImpl extends ServiceImpl<HdSpuMapper, HdSpu> implements IHdSpuService {
@Autowired
HdBrandMapper hdBrandMapper;
@Autowired
HdCategoryTableMapper hdCategoryTableMapper;
@Autowired
HdWarehouseMapper hdWarehouseMapper;
@Autowired
HdSpuMapper hdSpuMapper;
//spu四张表查询
public List<HdSpuVo> getSpu(int pageNo, int dataSize){
// 将集合返回给VO层
List<HdSpuVo> hdSpuVos = new ArrayList<>();
// 品牌
List<HdBrand> list = hdBrandMapper.selectList(null);
// 类别
List<HdCategoryTable> list1 = hdCategoryTableMapper.selectList(null);
// 仓库
List<HdWarehouse> list2 = hdWarehouseMapper.selectList(null);
// spu
List<HdSpu> list3 = hdSpuMapper.selectList(null);
for (int i = 0; i <list3.size() ; i++) {
HdSpuVo hdSpuVo = new HdSpuVo();
// 品牌id
Integer brandId = list3.get(i).getBrandId();
// 类别id
Integer categoryId = list3.get(i).getCategoryId();
// 仓库id
Integer warehouseId = list3.get(i).getWarehouseId();
// vo层显示主表数据就行保存
hdSpuVo.setEquipmentName(list3.get(i).getEquipmentName());
// 品牌
List<HdBrand> collect = list.stream()
//将list集合中的getid和品牌表brandid比对,比对成功就筛选成功
.filter(s -> s.getId().equals(brandId)).collect(Collectors.toList());
hdSpuVo.setBrand(collect.get(0).getBrand());
// 类别
List<HdCategoryTable> collect1 = list1.stream()
//将list集合中的getid和类别表brandid比对,比对成功就筛选成功
.filter(s -> s.getId().equals(categoryId)).collect(Collectors.toList());
hdSpuVo.setClassName(collect1.get(0).getClassName());
// 仓库
List<HdWarehouse> collect2 = list2.stream()
//将list集合中的getid和仓库表brandid比对,比对成功就筛选成功
.filter(s -> s.getId().equals(warehouseId)).collect(Collectors.toList());
hdSpuVo.setWareHouse(collect2.get(0).getEntrepot());
//添加到VO层
hdSpuVos.add(hdSpuVo);
}
// 返回分页将所需参数传过去(list集合,第几页,一页所显示的数据)
return ListPaging.datepaging(hdSpuVos,pageNo,dataSize);
}
}
Service实现层
public interface IHdSpuService extends IService<HdSpu> {
}
VO层
@Data
@AllArgsConstructor
@NoArgsConstructor
public class HdSpuVo implements Serializable {
/**
* 器材名称
*/
private String equipmentName;
/**
* 器材类型
*/
private String brand;
/**
* 器材品牌
*/
private String className;
/**
* 存储仓库
*/
private String wareHouse;
}
Controller层
@RestController
@RequestMapping("/sunrise")
public class SpuController {
@Autowired
HdSpuServiceImpl hdSpuService;
//spu数据在页面进行展示
@RequestMapping(value = "/spu/{pageNo}/{dataSize}",produces = "application/json;charset=UTF-8")
public String spu(@PathVariable("pageNo") int pageNo,@PathVariable("dataSize") int dataSize){
List<HdSpuVo> spu = hdSpuService.getSpu(pageNo,dataSize);
return JSON.toJSONString(spu);
}
}
postman测试
使用Postman分页查询成功
在实际开发最后要遵从自己公司(项目)要求来写。切记!!!切记!!!切记!!!