Spring Boot 笔记 014 创建接口_查询单个文章分类详情

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

    @GetMapping("/detail")
    public Result<Category> detail(Integer id){
        Category c = categoryService.findById(id);
        return Result.success(c);
    }
}

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

    Category findById(Integer id);
}

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

    @Override
    public Category findById(Integer id) {
        Category c = categoryMapper.findById(id);
        return c;
    }

}

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

    //根据id查询
    @Select("select * from category where id = #{id}")
    Category findById(Integer id);
}

1.1.5 postman测试

相关推荐

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

    2024-02-17 08:26:02       14 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-17 08:26:02       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-17 08:26:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-17 08:26:02       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-17 08:26:02       20 阅读

热门阅读

  1. Rust 初体验5

    2024-02-17 08:26:02       28 阅读
  2. truncate、delete、drop的区别?

    2024-02-17 08:26:02       33 阅读
  3. logger 记录

    2024-02-17 08:26:02       26 阅读
  4. Why Not Http?

    2024-02-17 08:26:02       30 阅读
  5. 注册 Hugging Face 后的官网创建模型的教程

    2024-02-17 08:26:02       32 阅读
  6. 使用Cargo国内镜像提升Rust开发效率

    2024-02-17 08:26:02       35 阅读
  7. STM32

    STM32

    2024-02-17 08:26:02      32 阅读