mybatis逆向工程的CRUD和分页插件
分页插件
导入依赖
<!--MyBatis分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.10</version>
</dependency>
只需要在获取很多数据前面加上PageHelper.startPage(pageNum,pageSize);即可数据分页
在这个之后我们得到的list就是分好页的了,但是只得到分页数据不够,还需要其他数据,这个时候使用PageInfo pageInfo = new PageInfo(list);得到的pageinfo对象内包含了nums,size,total,list等数据
但这个时候我们不需要如此多的数据,我们可以考虑创建一个类专门放我们要传递的数据,在这个类里面还要有转换分页好的list功能的函数
如:
@Data
public class CommonPage<T> {
private Integer pageNum;
private Integer pageSize;
private Integer totalPage;
private Long total;
private List<T> list;
/**
* 将PageHelper分页后的list转为分页信息
*/
public static <T> CommonPage<T> restPage(List<T> list) {
//
CommonPage<T> result = new CommonPage<T>();
PageInfo<T> pageInfo = new PageInfo<T>(list);
result.setTotalPage(pageInfo.getPages());
result.setPageNum(pageInfo.getPageNum());
result.setPageSize(pageInfo.getPageSize());
result.setTotal(pageInfo.getTotal());
result.setList(pageInfo.getList());
return result;
}
}
之后加入数据的时候使用 CommonResult.restPage(list)即可转换
service的CRUD
/*brand-获取全部*/
@Override
public List<PmsBrand> listAllBrand() {
return pmsBrandMapper.selectByExample(new PmsBrandExample());
}
/*brand-添加*/
@Override
public int createBrand(PmsBrand brand) {
return pmsBrandMapper.insert(brand);
}
/*brand-更新*/
@Override
public int updateBrand(Long id, PmsBrand brand) {
brand.setId(id);
return pmsBrandMapper.updateByPrimaryKeySelective(brand);
}
/*brand-删除*/
@Override
public int deleteBrand(Long id) {
return pmsBrandMapper.deleteByPrimaryKey(id);
}
/*brand-分页*/
@Override
public List<PmsBrand> listBrand(int pageNum, int pageSize) {
PageHelper.startPage(pageNum,pageSize);
return pmsBrandMapper.selectByExample(new PmsBrandExample());
}
@Override
public PmsBrand getBrand(Long id) {
return pmsBrandMapper.selectByPrimaryKey(id);
}
控制台的CRUD
这里面的日志依赖已经被springboot包含在内了
private static final Logger LOGGER = LoggerFactory.getLogger(PmsBrandController.class);
@ApiOperation("brand-获取全部")
@RequestMapping(value = "/listAll",method = RequestMethod.GET)
public CommonResult<List<PmsBrand>> getBrandList(){
return CommonResult.success(pmsBrandService.listAllBrand());
}
@ApiOperation("brand-添加")
@RequestMapping(value = "/create",method = RequestMethod.POST)
public CommonResult createBrand(@RequestBody PmsBrand pmsBrand){
int i = pmsBrandService.createBrand(pmsBrand);
if (i !=1){
LOGGER.error("createBrand fail():{}", pmsBrand);
return CommonResult.failed("操作失败");
}
LOGGER.debug("create success():{}",pmsBrand);
return CommonResult.success(null);
}
@ApiOperation("brand-更新")
@PostMapping("/update/{id}")
public CommonResult updateBrand(@PathVariable("id")long id,@RequestBody PmsBrand pmsBrandDto){
int i = pmsBrandService.updateBrand(id, pmsBrandDto);
if (i != 1){
LOGGER.debug("updateBrand failed:{}", pmsBrandDto);
return CommonResult.failed("操作失败");
}
LOGGER.debug("updateBrand success:{}", pmsBrandDto);
return CommonResult.success(pmsBrandDto);
}
@ApiOperation("brand-分页查询")
@RequestMapping(value = "/list", method = RequestMethod.GET)
public CommonResult<CommonPage<PmsBrand>> listBrand(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
@RequestParam(value = "pageSize", defaultValue = "3") Integer pageSize) {
List<PmsBrand> brandList = pmsBrandService.listBrand(pageNum, pageSize);
return CommonResult.success(CommonPage.restPage(brandList));
}
@ApiOperation("brand-单个查询")
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public CommonResult<PmsBrand> brand(@PathVariable("id") Long id) {
return CommonResult.success(pmsBrandService.getBrand(id));
}