1.分析
getall
在小程序端首先是对招聘信息、招聘会、宣讲会简单信息的一个展示,所以需要一个查询所有信息的接口(如果信息多了后会考虑用分页)
getbyid
当点击展示的某个信息时会进入详细信息页面,所以需要一个根据id查询信息的接口
follow
某个同学对这个招聘信息/招聘会/宣讲会感兴趣时,可以点击关注,这时候该信息的关注人数会+1
unfollow
当然还有取消关注,关注人数-1
2.具体实现(以招聘信息为例)
controller
@Tag(name = "用户 APP - 招聘信息")
@RestController
@RequestMapping("/information/recruitment")
@Validated
public class AppRecruitmentController {
@Resource
private RecruitmentService recruitmentService;
@GetMapping("/getall")
@Operation(summary = "获得全部招聘信息")
public CommonResult<List<AppRecruitmentRespVO>> getAllRecruitments() {
List<RecruitmentDO> list = recruitmentService.getAllRecruitments();
return success(RecruitmentConvert.INSTANCE.appConvertList(list));
}
@GetMapping("/get")
@Operation(summary = "根据id获得招聘信息")
@Parameter(name = "id", description = "编号", required = true, example = "1024")
public CommonResult<AppRecruitmentRespVO> getRecruitment(@RequestParam("id") Long id) {
RecruitmentDO recruitmentDO = recruitmentService.getRecruitment(id);
return success(RecruitmentConvert.INSTANCE.appConvert(recruitmentDO));
}
@GetMapping("/follow")
@Operation(summary = "关注招聘信息")
public CommonResult<Boolean> follow(@RequestParam("id") Long id) {
recruitmentService.follow(id);
return success(true);
}
@GetMapping("/unfollow")
@Operation(summary = "取消关注招聘信息")
public CommonResult<Boolean> unfollow(@RequestParam("id") Long id) {
recruitmentService.unfollow(id);
return success(true);
}
}
convert(用于VO与DO的转换)
简单介绍可以看《VO、DO、DTO的设计以及它们之间的转换》
/**
* 招聘信息 Convert
*
*/
@Mapper
public interface RecruitmentConvert {
RecruitmentConvert INSTANCE = Mappers.getMapper(RecruitmentConvert.class);
//============== APP ====================
AppRecruitmentRespVO appConvert(RecruitmentDO bean);
List<AppRecruitmentRespVO> appConvertList(List<RecruitmentDO> list);
}
service
public interface RecruitmentService {
/**
* 关注招聘信息
*
* @param id 招聘信息id
*/
void follow(Long id);
/**
* 取消关注招聘信息
*
* @param id 招聘信息id
*/
void unfollow(Long id);
/**
* 获得招聘信息
*
* @param id 编号
* @return 招聘信息
*/
RecruitmentDO getRecruitment(Long id);
/**
* 获得全部招聘信息
*
* @return 招聘信息
*/
List<RecruitmentDO> getAllRecruitments();
}
serviceImpl
@Service
@Validated
public class RecruitmentServiceImpl implements RecruitmentService {
@Resource
private RecruitmentMapper recruitmentMapper;
@Override
public void follow(Long id) {
recruitmentMapper.follow(id);
}
@Override
public void unfollow(Long id) {
recruitmentMapper.unfollow(id);
}
@Override
public RecruitmentDO getRecruitment(Long id) {
return recruitmentMapper.selectById(id);
}
@Override
public List<RecruitmentDO> getAllRecruitments() {
// QueryWrapper<RecruitmentDO> wrapper = new QueryWrapper<>();
// //查询所有开启状态的数据
// wrapper.eq("status", 0);
// return recruitmentMapper.selectList(wrapper);
//BaseMapperX进一步封装
return recruitmentMapper.selectList("status",0);
}
}
mapper
@Mapper
public interface JobfairMapper extends BaseMapperX<JobfairDO> {
/**
* 关注招聘会
* 根据id让concerns_number加1
* @param id
*/
default void follow(Long id) {
LambdaUpdateWrapper<JobfairDO> wrapper = new LambdaUpdateWrapper<>();
wrapper.eq(JobfairDO::getId, id)
.setSql("concerns_number = concerns_number + 1");
this.update(null, wrapper);
}
/**
* 取消关注招聘会
* @param id
*/
default void unfollow(Long id) {
LambdaUpdateWrapper<JobfairDO> wrapper = new LambdaUpdateWrapper<>();
wrapper.eq(JobfairDO::getId, id)
.setSql("concerns_number = concerns_number - 1");
this.update(null, wrapper);
}
}
3.Apifox接口调试
4.注意
1.Mybatis-Plus提供了BaseMapper和IService两个相似CRUD操作的接口,但是不要在Controller、Service 中,直接进行 MyBatis-Plus 操作。大量 MyBatis 操作散落在 Service 中,会导致 Service 的代码越来乱,无法聚焦业务逻辑。
2.在资讯模块数据表中设计了“status”字段,来表示信息的开启/关闭,所以在RecruitmentServiceImpl查询所有getAllRecruitments方法中需要判断一下“status”的值。