spring boot 10

一、新增文章分类接口

1. 核心需求

  • 使用 Spring Validation 完成参数校验(分类名 / 别名非空)
  • Service 层自动填充 createUser、createTime、updateTime
  • 插入数据到 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. 时间字段自动填充:

    • 新增时 createTime 和 updateTime 都设置为当前时间
    • 后续更新时,只修改 updateTime 字段
相关推荐
captain3762 小时前
MyBatis(入门5)其他查询操作
java·数据库·spring boot·mybatis
Novproxy_Z2 小时前
商用代理避坑:如何识别商家拿机房 IP 冒充住宅 IP
java·网络·tcp/ip
前端精髓3 小时前
结构化输出with_structured_output(抽卡)
python·langchain
文丑颜不良啊3 小时前
LeetCode1481~1500题目解析
java·leetcode
2601_962885723 小时前
quotes 字段全解:涨跌幅、振幅、换手率的口径与单位
java·前端·spring
前端精髓4 小时前
调用openrouter的免费API模型
java·前端·javascript
尹人入圣4 小时前
目前热门的IP驱动产业新场景新工具
网络·python·网络协议·tcp/ip
步行cgn4 小时前
Spring MVC 拦截器 Interceptor 详解:从执行流程到实战应用
java·spring·mvc
文丑颜不良啊4 小时前
LeetCode1381~1400题目解析
java·leetcode
开开心心_Every4 小时前
快速隐藏程序工具自定义快捷键一键切换
java·开发语言·前端·spring·智能手机·word·intellij-idea