认清现实,放弃幻想,准备斗争
需求
一、后端接口实现
1、创建查询对象
UserInfoQuery
@Data @ApiModel(description="会员搜索对象") public class UserInfoQuery { @ApiModelProperty(value = "手机号") private String mobile; @ApiModelProperty(value = "状态") private Integer status; @ApiModelProperty(value = "1:出借人 2:借款人") private Integer userType; }
2、Service
接口:UserInfoService
IPage<UserInfo> listPage(Page<UserInfo> pageParam, UserInfoQuery userInfoQuery);
实现:UserInfoServiceImpl
@Override public IPage<UserInfo> listPage(Page<UserInfo> pageParam, UserInfoQuery userInfoQuery) { String mobile = userInfoQuery.getMobile(); Integer status = userInfoQuery.getStatus(); Integer userType = userInfoQuery.getUserType(); QueryWrapper<UserInfo> userInfoQueryWrapper = new QueryWrapper<>(); if(userInfoQuery == null){ return baseMapper.selectPage(pageParam, null); } userInfoQueryWrapper .eq(StringUtils.isNotBlank(mobile), "mobile", mobile) .eq(status != null, "status", userInfoQuery.getStatus()) .eq(userType != null, "user_type", userType); return baseMapper.selectPage(pageParam, userInfoQueryWrapper); }
注意点
这里由于查询信息不一定会传这个参数过来需要,也可能传过来的对象的属性不全,所以这里需要对传递过来的对象属性进行一个判断操作再封装查询
3、Controller
AdminUserInfoController
@Api(tags = "会员管理") @RestController @RequestMapping("/admin/core/userInfo") @Slf4j @CrossOrigin public class AdminUserInfoController { @Resource private UserInfoService userInfoService; @ApiOperation("获取会员分页列表") @GetMapping("/list/{page}/{limit}") public R listPage( @ApiParam(value = "当前页码", required = true) @PathVariable Long page, @ApiParam(value = "每页记录数", required = true) @PathVariable Long limit, @ApiParam(value = "查询对象", required = false) UserInfoQuery userInfoQuery) { Page<UserInfo> pageParam = new Page<>(page, limit); IPage<UserInfo> pageModel = userInfoService.listPage(pageParam, userInfoQuery); return R.ok().data("pageModel", pageModel); } }
注意点:
在restful风格中查询对应是get请求,但这里的话 UserInfoQuery userInfoQuery是作为一个对象,不能把这个对象封装在respondBody中,这个时候前端要进行一定处理
4、LocalDateTime的json格式化问题
service-base中添加json格式化配置文件
@Configuration public class LocalDateTimeSerializerConfig { @Value("${spring.jackson.date-format:yyyy-MM-dd HH:mm:ss}") private String pattern; public LocalDateTimeSerializer localDateTimeDeserializer() { return new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(pattern)); } @Bean public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() { return builder -> builder.serializerByType(LocalDateTime.class, localDateTimeDeserializer()); } }
上面的方案全局生效,当全局的格式化方式无法满足我们需求时,我们对日期格式要做特殊的处理:在类的属性上添加注解
@JsonFormat(pattern = "yyyy-MM-dd") @ApiModelProperty(value = "创建时间") private LocalDateTime createTime;