Spring Boot 笔记 013 创建接口_查询文章分类列表

1.1.1 Controller

java 复制代码
package com.geji.controller;

import com.geji.pojo.Category;
import com.geji.pojo.Result;
import com.geji.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

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

    @GetMapping
    public Result<List<Category>> list(){
        List<Category> cs = categoryService.list();
        return Result.success(cs);
    }
}

1.1.2 Service

java 复制代码
package com.geji.service;

import com.geji.pojo.Category;

import java.util.List;

public interface CategoryService {
    void add(Category category);

    List<Category> list();
}

1.1.3 ServiceImpl

java 复制代码
package com.geji.service.impl;

import com.geji.mapper.CategoryMapper;
import com.geji.pojo.Category;
import com.geji.service.CategoryService;
import com.geji.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public void add(Category category) {
        //补充属性值
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());

        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        category.setCreateUser(userId);
        categoryMapper.add(category);
    }

    @Override
    public List<Category> list() {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        return categoryMapper.list(userId);
    }
}

1.1.4 Mapper

java 复制代码
package com.geji.mapper;

import com.geji.pojo.Category;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@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);

    //查询所有
    @Select("select * from category where create_user = #{userId}")
    List<Category> list(Integer userId);
}

2.1.1 postman测试发现日期字符串格式不符合要求

2.1.2 实体类添加注解

java 复制代码
package com.geji.pojo;

import com.fasterxml.jackson.annotation.JsonFormat;
import jakarta.validation.constraints.NotEmpty;
import lombok.Data;

import java.time.LocalDateTime;

@Data
public class Category {

    private Integer id;//主键ID
    @NotEmpty
    private String categoryName;//分类名称
    @NotEmpty
    private String categoryAlias;//分类别名
    private Integer createUser;//创建人ID
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createTime;//创建时间
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime updateTime;//更新时间
}
相关推荐
大明者省7 小时前
四大模态大模型训练体系全解析(架构+范式+分布式+算力成本·)
笔记·分布式·架构
十月的皮皮7 小时前
C语言学习学习笔记20260704-中缀表达式求值(双栈法)
c语言·笔记·学习
智者知已应修善业7 小时前
【 LM358AD方波】2024-12-31
驱动开发·经验分享·笔记·硬件架构·硬件工程
什仙8 小时前
电感规格书全部专业术语完整释义(村田原厂标准定义)
笔记·电感器
生活爱好者!8 小时前
AI加持的笔记工具,比备忘录好用,NAS一键部署blinko
人工智能·笔记
摇滚侠8 小时前
Apache Skywalking 实战 阅读笔记 第一章
笔记·apache·skywalking
AOwhisky8 小时前
Kubernetes(K8s)学习笔记(第十四期):集群存储与有状态应用(下篇):StatefulSet 有状态应用管理
redis·笔记·mysql·云原生·kubernetes·云计算·k8s
来生硬件工程师8 小时前
【硬件笔记】DCDC电源设计—BUCK电路设计要点
笔记·单片机·嵌入式硬件·硬件工程·智能硬件
nongcunqq9 小时前
编辑 cookie 的插件
笔记
AOwhisky9 小时前
kubernetes(K8s)学习笔记:第八期与第九期核心知识点自测与详解
笔记·云原生·kubernetes·云计算·k8s·集群·网络策略