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 字段
相关推荐
IvanCodes4 小时前
Python 数据处理(十三):JSON、CSV 与数据序列化
开发语言·python
Patrick在香港5 小时前
Claude 工具调用返回空:8 次失败里只有 1 次状态码不对,其余全带 200
爬虫·python·api·claude·香港
步行cgn5 小时前
Spring c 命名空间注入详解
java·后端·spring
Web3&Basketball5 小时前
CRM Agent 后训练实战:3 倍更少错误
python·架构·大模型·agent·推理
明月_清风6 小时前
Maven 到底是什么?一篇文章搞懂 Java 项目构建与依赖管理
java·后端·maven
aramae6 小时前
MySQL复合查询(8)
java·c语言·开发语言·后端·算法
Rain的Java大神之路6 小时前
如何快速上传10G文件
java·spring boot·redis·后端·mysql·spring cloud·面试
AIFQuant6 小时前
ETF行情API接入踩坑记:从报错到跑通的七个问题
python·金融·区块链·etf·基金
Wang's Blog7 小时前
Java 接入Redis: 通用命令与键管理
java·服务器·redis
Wang's Blog7 小时前
Java 接入Redis: 列表集合与有序集合操作命令
java·服务器·redis