一、分组校验Spring Validation1. 核心概念分组校验是 Spring Validation 提供的功能用于在不同业务场景新增 / 更新下对同一个实体类执行不同的校验规则避免重复定义实体类。2. 分组校验实现步骤① 定义分组接口在Category实体类内部定义分组接口Add、Updatejava运行public class Category { // 新增分组 public interface Add {} // 更新分组 public interface Update {} // 后续字段定义... }② 给校验注解指定分组在字段上的校验注解中通过groups属性指定归属的分组java运行Data public class Category { public interface Add {} public interface Update {} // 更新操作必须校验ID非空新增操作不需要 NotNull(groups Update.class, message 分类ID不能为空) private Integer id; // 新增和更新操作都需要校验分类名称 NotEmpty(groups {Add.class, Update.class}, message 分类名称不能为空) Pattern(regexp ^\\S{1,10}$, groups {Add.class, Update.class}, message 分类名称长度为1-10位非空字符) private String categoryName; // 新增和更新操作都需要校验分类别名 NotEmpty(groups {Add.class, Update.class}, message 分类别名不能为空) Pattern(regexp ^\\S{1,10}$, groups {Add.class, Update.class}, message 分类别名长度为1-10位非空字符) private String categoryAlias; private Integer createUser; private LocalDateTime createTime; private LocalDateTime updateTime; }③ 接口校验时指定分组在 Controller 接口的Validated注解中指定要校验的分组java运行// 新增接口校验Add分组的规则 PostMapping public Result add(RequestBody Validated(Category.Add.class) Category category) { categoryService.add(category); return Result.success(); } // 更新接口校验Update分组的规则 PutMapping public Result update(RequestBody Validated(Category.Update.class) Category category) { categoryService.update(category); return Result.success(); }3. 关键补充说明默认分组如果校验注解没有指定groups属性默认属于javax.validation.groups.Default分组分组继承分组接口可以继承例如public interface Add extends Default {}此时Add分组会继承Default分组的所有校验规则二、文章分类详情接口1. Controller 层java运行GetMapping(/detail) public ResultCategory detail(Integer id) { Category category categoryService.findById(id); return Result.success(category); }2. Service 层java运行Override public Category findById(Integer id) { return categoryMapper.findById(id); }3. Mapper 层SQLjava运行Select(select * from category where id #{id}) Category findById(Integer id);三、更新文章分类接口1. Controller 层java运行PutMapping public Result update(RequestBody Validated(Category.Update.class) Category category) { categoryService.update(category); return Result.success(); }2. Service 层java运行Override public void update(Category category) { // 设置更新时间 category.setUpdateTime(LocalDateTime.now()); categoryMapper.update(category); }3. Mapper 层SQLjava运行Update(update category set category_name #{categoryName}, category_alias #{categoryAlias}, update_time #{updateTime} where id #{id}) void update(Category category);四、关键注意事项分组校验生效条件实体类中定义了分组接口Add、Update校验注解通过groups属性指定了归属分组Controller 接口的Validated注解中指定了要校验的分组更新接口安全校验确保更新的分类是当前登录用户创建的避免越权修改可以在 Service 层增加校验逻辑java运行Override public void update(Category category) { // 从 ThreadLocal 获取当前用户ID MapString, Object claims ThreadLocalUtil.get(); Integer userId (Integer) claims.get(id); // 查询原分类信息校验创建人是否为当前用户 Category oldCategory categoryMapper.findById(category.getId()); if (!oldCategory.getCreateUser().equals(userId)) { throw new RuntimeException(无权限修改该分类); } category.setUpdateTime(LocalDateTime.now()); categoryMapper.update(category); }时间字段自动维护新增时createTime和updateTime都设置为当前时间更新时仅修改updateTime字段createTime保持不变
spring boot 11
一、分组校验Spring Validation1. 核心概念分组校验是 Spring Validation 提供的功能用于在不同业务场景新增 / 更新下对同一个实体类执行不同的校验规则避免重复定义实体类。2. 分组校验实现步骤① 定义分组接口在Category实体类内部定义分组接口Add、Updatejava运行public class Category { // 新增分组 public interface Add {} // 更新分组 public interface Update {} // 后续字段定义... }② 给校验注解指定分组在字段上的校验注解中通过groups属性指定归属的分组java运行Data public class Category { public interface Add {} public interface Update {} // 更新操作必须校验ID非空新增操作不需要 NotNull(groups Update.class, message 分类ID不能为空) private Integer id; // 新增和更新操作都需要校验分类名称 NotEmpty(groups {Add.class, Update.class}, message 分类名称不能为空) Pattern(regexp ^\\S{1,10}$, groups {Add.class, Update.class}, message 分类名称长度为1-10位非空字符) private String categoryName; // 新增和更新操作都需要校验分类别名 NotEmpty(groups {Add.class, Update.class}, message 分类别名不能为空) Pattern(regexp ^\\S{1,10}$, groups {Add.class, Update.class}, message 分类别名长度为1-10位非空字符) private String categoryAlias; private Integer createUser; private LocalDateTime createTime; private LocalDateTime updateTime; }③ 接口校验时指定分组在 Controller 接口的Validated注解中指定要校验的分组java运行// 新增接口校验Add分组的规则 PostMapping public Result add(RequestBody Validated(Category.Add.class) Category category) { categoryService.add(category); return Result.success(); } // 更新接口校验Update分组的规则 PutMapping public Result update(RequestBody Validated(Category.Update.class) Category category) { categoryService.update(category); return Result.success(); }3. 关键补充说明默认分组如果校验注解没有指定groups属性默认属于javax.validation.groups.Default分组分组继承分组接口可以继承例如public interface Add extends Default {}此时Add分组会继承Default分组的所有校验规则二、文章分类详情接口1. Controller 层java运行GetMapping(/detail) public ResultCategory detail(Integer id) { Category category categoryService.findById(id); return Result.success(category); }2. Service 层java运行Override public Category findById(Integer id) { return categoryMapper.findById(id); }3. Mapper 层SQLjava运行Select(select * from category where id #{id}) Category findById(Integer id);三、更新文章分类接口1. Controller 层java运行PutMapping public Result update(RequestBody Validated(Category.Update.class) Category category) { categoryService.update(category); return Result.success(); }2. Service 层java运行Override public void update(Category category) { // 设置更新时间 category.setUpdateTime(LocalDateTime.now()); categoryMapper.update(category); }3. Mapper 层SQLjava运行Update(update category set category_name #{categoryName}, category_alias #{categoryAlias}, update_time #{updateTime} where id #{id}) void update(Category category);四、关键注意事项分组校验生效条件实体类中定义了分组接口Add、Update校验注解通过groups属性指定了归属分组Controller 接口的Validated注解中指定了要校验的分组更新接口安全校验确保更新的分类是当前登录用户创建的避免越权修改可以在 Service 层增加校验逻辑java运行Override public void update(Category category) { // 从 ThreadLocal 获取当前用户ID MapString, Object claims ThreadLocalUtil.get(); Integer userId (Integer) claims.get(id); // 查询原分类信息校验创建人是否为当前用户 Category oldCategory categoryMapper.findById(category.getId()); if (!oldCategory.getCreateUser().equals(userId)) { throw new RuntimeException(无权限修改该分类); } category.setUpdateTime(LocalDateTime.now()); categoryMapper.update(category); }时间字段自动维护新增时createTime和updateTime都设置为当前时间更新时仅修改updateTime字段createTime保持不变