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

1.1.1 Controller

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

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

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

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 实体类添加注解

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;//更新时间
}

相关推荐

  1. springboot接口提高查询速度方法

    2024-02-16 06:42:03       39 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-16 06:42:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-16 06:42:03       101 阅读
  3. 在Django里面运行非项目文件

    2024-02-16 06:42:03       82 阅读
  4. Python语言-面向对象

    2024-02-16 06:42:03       91 阅读

热门阅读

  1. uniapp实现手机横屏(方法二)

    2024-02-16 06:42:03       50 阅读
  2. 华为HCIE课堂笔记第十八章 SR技术

    2024-02-16 06:42:03       46 阅读
  3. 人机之间的不同交互

    2024-02-16 06:42:03       48 阅读
  4. 设计模式-装饰模式 Decorator

    2024-02-16 06:42:03       44 阅读
  5. 23种设计模式之原型模式

    2024-02-16 06:42:03       47 阅读
  6. Python课堂11——字典

    2024-02-16 06:42:03       56 阅读
  7. ABC340(A-C)

    2024-02-16 06:42:03       53 阅读
  8. 「数据结构」绪论

    2024-02-16 06:42:03       47 阅读
  9. STM32介绍

    2024-02-16 06:42:03       49 阅读