spring boot 10

一、新增文章分类接口

1. 核心需求

  • 使用 Spring Validation 完成参数校验(分类名 / 别名非空)
  • Service 层自动填充 createUsercreateTimeupdateTime
  • 插入数据到 category

2. 实体类(Category.java

java

运行

复制代码
@Data
public class Category {
    private Integer id; // 主键ID

    @NotEmpty(message = "分类名称不能为空")
    private String categoryName; // 分类名称

    @NotEmpty(message = "分类别名不能为空")
    private String categoryAlias; // 分类别名

    private Integer createUser; // 创建人ID
    private LocalDateTime createTime; // 创建时间
    private LocalDateTime updateTime; // 更新时间
}

3. Controller 层(新增接口)

java

运行

复制代码
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public Result add(@RequestBody @Validated Category category) {
        categoryService.add(category);
        return Result.success();
    }
}

4. Service 层(业务逻辑)

java

运行

复制代码
@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public void add(Category category) {
        // 1. 从 ThreadLocal 获取当前登录用户ID
        Map<String, Object> claims = ThreadLocalUtil.get();
        Integer userId = (Integer) claims.get("id");
        
        // 2. 自动填充字段
        category.setCreateUser(userId);
        LocalDateTime now = LocalDateTime.now();
        category.setCreateTime(now);
        category.setUpdateTime(now);
        
        // 3. 调用 Mapper 插入数据
        categoryMapper.add(category);
    }
}

5. Mapper 层(SQL)

java

运行

复制代码
@Mapper
public interface CategoryMapper {
    @Insert("insert into category(category_name, category_alias, create_user, create_time, update_time) " +
            "values(#{categoryName}, #{categoryAlias}, #{createUser}, #{createTime}, #{updateTime})")
    void add(Category category);
}

二、文章分类列表接口

1. 核心需求

  • 查询当前用户创建的所有文章分类
  • 无需参数,直接返回分类列表

2. Controller 层(列表接口)

java

运行

复制代码
@GetMapping
public Result<List<Category>> list() {
    List<Category> categoryList = categoryService.list();
    return Result.success(categoryList);
}

3. Service 层(业务逻辑)

java

运行

复制代码
@Override
public List<Category> list() {
    // 从 ThreadLocal 获取当前登录用户ID
    Map<String, Object> claims = ThreadLocalUtil.get();
    Integer userId = (Integer) claims.get("id");
    return categoryMapper.list(userId);
}

4. Mapper 层(SQL)

java

运行

复制代码
@Select("select * from category where create_user = #{userId}")
List<Category> list(Integer userId);

三、关键注意事项

  1. 参数校验生效

    • 实体类字段添加 @NotEmpty 注解
    • Controller 接口参数前添加 @Validated
    • 确保已引入 spring-boot-starter-validation 依赖
  2. 用户 ID 安全获取

    • 不要由前端传入用户 ID,统一从 ThreadLocal 中获取,避免越权操作
  3. 时间字段自动填充

    • 新增时 createTimeupdateTime 都设置为当前时间
    • 后续更新时,只修改 updateTime 字段
相关推荐
Rauser Mack16 小时前
不懂编程,但是vibe coding一个扫雷游戏
人工智能·python·游戏·html·prompt
白露与泡影16 小时前
2026秋招冲刺:1000道Java高频面试题(各大厂考点汇总)
java·开发语言·面试
IT龟苓膏16 小时前
Java 并发基础:进程、线程、线程状态、synchronized、volatile 一篇讲清
java·开发语言·jvm
weixin_4467291616 小时前
java中class类没有打进war包中
java
郝学胜-神的一滴16 小时前
Python 高级编程 019:类变量与实例变量彻底解析
开发语言·python·程序人生·软件构建
哭哭啼17 小时前
pgSql 事务篇
java·数据库·postgresql
架构源启17 小时前
Spring AI进阶系列(17)- 未来展望与职业发展:Java 工程师迈向 AI 工程化与智能体架构的路线图
java·人工智能·spring
我登哥MVP17 小时前
Spring Boot 从“会用”到“精通”:SpringBoot MVC 请求处理全流程
java·spring boot·后端·spring·mvc·maven·intellij-idea
CTA量化套保17 小时前
期货量化临期合约还能不能做:程序化到期禁开与强平写法
python·区块链
我登哥MVP17 小时前
Spring Boot 从“会用”到“精通”:ReturnValueHandler原理
java·spring boot·后端·spring·java-ee·maven·intellij-idea