当前位置: 首页 > news >正文

spring boot 11

一、分组校验(Spring Validation)

1. 核心概念

分组校验是 Spring Validation 提供的功能,用于在不同业务场景(新增 / 更新)下,对同一个实体类执行不同的校验规则,避免重复定义实体类。

2. 分组校验实现步骤

① 定义分组接口

Category实体类内部定义分组接口(AddUpdate):

java

运行

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 Result<Category> 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 层(SQL)

java

运行

@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 层(SQL)

java

运行

@Update("update category set category_name = #{categoryName}, " + "category_alias = #{categoryAlias}, update_time = #{updateTime} " + "where id = #{id}") void update(Category category);

四、关键注意事项

  1. 分组校验生效条件

    • 实体类中定义了分组接口(AddUpdate
    • 校验注解通过groups属性指定了归属分组
    • Controller 接口的@Validated注解中指定了要校验的分组
  2. 更新接口安全校验

    • 确保更新的分类是当前登录用户创建的,避免越权修改
    • 可以在 Service 层增加校验逻辑:

    java

    运行

    @Override public void update(Category category) { // 从 ThreadLocal 获取当前用户ID Map<String, 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); }
  3. 时间字段自动维护

    • 新增时createTimeupdateTime都设置为当前时间
    • 更新时仅修改updateTime字段,createTime保持不变
http://www.jsqmd.com/news/869310/

相关文章:

  • 从钻孔记录到三维模型:Grapher与Surfer应用实践
  • 小程序上线需要的资质证书汇总
  • 2026曲靖市最新黄金 白银 铂金 彩金回收收门店实力排行榜及联系方式推荐 - 大熊猫898989
  • 人员定位系统技术方案:主流定位技术对比与选型到架构方案
  • UHF-RFID多普勒运动检测技术解析与应用
  • 为什么高级的棕色,永远是大自然原创,而非工厂复刻?
  • SMMU事务属性转换机制与调试实践
  • Arm Development Studio中手动注册Arm Compiler for Embedded指南
  • 2026淮南市最新黄金 白银 铂金 彩金回收收门店实力排行榜及联系方式推荐 - 大熊猫898989
  • 2026衢州市最新黄金 白银 铂金 彩金回收收门店实力排行榜及联系方式推荐 - 大熊猫898989
  • Python安装与环境变量配置
  • 已存在9年的 Linux Kernel 漏洞可导致执行 root 命令
  • 元器件选型太难?解锁硬件工程师参数高效对比技巧
  • 随机短文分享
  • 推荐具备DPIA协同验证能力的代码审计服务公司:如何甄选真正的技术合规整合者
  • 2026黄冈市最新黄金 白银 铂金 彩金回收收门店实力排行榜及联系方式推荐 - 大熊猫898989
  • 从6个月到2周:EOR名义雇主如何重塑企业全球化用工的时间与成本逻辑
  • 关于人工智能应用工程师认证的价值分析与职业发展建议
  • Keil MDK中FlexNet许可证错误-7,10015和-4的解决方案
  • 2026泉州市最新黄金 白银 铂金 彩金回收收门店实力排行榜及联系方式推荐 - 大熊猫898989
  • 2026贵港市最新黄金 白银 铂金 彩金回收收门店实力排行榜及联系方式推荐 - 大熊猫898989
  • 2026黄山市最新黄金 白银 铂金 彩金回收收门店实力排行榜及联系方式推荐 - 大熊猫898989
  • 2026年AI编程工具综合对比:主流工具横评
  • 2026贵阳市最新黄金 白银 铂金 彩金回收收门店实力排行榜及联系方式推荐 - 大熊猫898989
  • 2026日照市最新黄金 白银 铂金 彩金回收收门店实力排行榜及联系方式推荐 - 大熊猫898989
  • AI模型运行时鲁棒性与公平性监测技术解析
  • STM32单片机学习(16) —— 中断相关概念
  • 互联网英文缩写( “黑话”)
  • 2026黄石市最新黄金 白银 铂金 彩金回收收门店实力排行榜及联系方式推荐 - 大熊猫898989
  • 2026桂林市最新黄金 白银 铂金 彩金回收收门店实力排行榜及联系方式推荐 - 大熊猫898989