更改包名
将之前的 entity 更改为 domain:
将之前的 validator 包当中的校验分组接口移动到 common 模块当中,因为其它模块也需要使用就放到公共当中进行存储。
更改完毕之后在修改下 Course.java 的校验注解的作用范围:
/** * 课程ID */ @TableId(value = "course_id", type = IdType.AUTO) @NotNull(message = "{course.id.notnull}") private Integer courseId; /** * 课程类型 1.舞蹈类 2.游泳类 3.拳击类 */ @NotNull(message = "{course.type.notnull}", groups = {EditGroup.class, CreateGroup.class}) private Integer type; /** * 课程名称 */ @NotBlank(message = "{course.name.notnull}", groups = {EditGroup.class, CreateGroup.class}) private String name; /** * 课程价格 */ @NotNull(message = "{course.price.notnull}", groups = {EditGroup.class, CreateGroup.class}) @Min(value = 0, message = "{course.price.invalid}", groups = {EditGroup.class, CreateGroup.class}) private Double price; /** * 课程适用人群 */ @NotNull(message = "{course.applyTo.notnull}", groups = {EditGroup.class, CreateGroup.class}) private Integer applyTo; /** * 课程简介 */ @NotBlank(message = "{course.info.notnull}", groups = {EditGroup.class, CreateGroup.class}) private String info;
ValidationMessages.properties
course.id.notnull=课程ID不能为空
CourseController.java
@PreAuthorize("hasPermission('tienchin:course:edit')") @Log(title = "课程管理", businessType = BusinessType.UPDATE) @PutMapping AjaxResult edit(@Validated(EditGroup.class) @RequestBody Course course) { return AjaxResult.success(iCourseService.updateCourse(course) > 0 ? "更新成功" : "更新失败"); } @PreAuthorize("hasPermission('tienchin:course:edit')") @GetMapping("/{courseId}") AjaxResult getInfo(@PathVariable Long courseId) { return AjaxResult.success(iCourseService.getById(courseId)); }
ICourseService.java
/** * 更新课程 * * @param course 课程 * @return 影响行数 */ int updateCourse(Course course);
CourseServiceImpl.java
@Override public int updateCourse(Course course) { // 设置不需要更新的字段 course.setCreateBy(null); course.setCreateTime(null); course.setDelFlag(null); // 设置需要更新的字段 course.setUpdateBy(SecurityUtils.getUsername()); course.setUpdateTime(LocalDateTime.now()); return updateById(course) ? 1 : 0; }