public interface AdChannelService extends IService<AdChannel> {
/**
* 根据名称分页查询频道列表
PageResult<ChannelDto> findByPage(ChannelDto dto);
}
package com.heima.admin.service.impl;
@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {
@Override
public PageResult<ChannelDto> findByPage(ChannelDto channelDto) {
// 构造分页条件
Page<AdChannel> page = new Page(channelDto.getPage(),channelDto.getSize());
// 构造查询条件
QueryWrapper<AdChannel> queryWrapper = new QueryWrapper<>();
if(StringUtils.isNotBlank(channelDto.getName())){
queryWrapper.lambda().like(AdChannel::getName,channelDto.getName());
}
// 分页查询
IPage<AdChannel> adChannelIPage = this.page(page, queryWrapper);
// 如果没有返回值
if(CollectionUtils.isEmpty(adChannelIPage.getRecords())){
return null;
}
// 如果有返回值,对象转换
List<ChannelDto> channelDtoList = BeanHelper.
copyWithCollection(adChannelIPage.getRecords(), ChannelDto.class);
return new PageResult<>(channelDto.getPage(),
channelDto.getSize(),
adChannelIPage.getTotal(),
channelDtoList);
}
}
package com.heima.admin.controller.v1;
@RestController
@RequestMapping("/api/v1/channel")
public class ChannelController {
@Autowired
private AdChannelService channelService;
@PostMapping("/list")
public PageResult<ChannelDto> findByPage(@RequestBody ChannelDto dto){
return channelService.findByPage(dto);
}
}