Spring Boot 笔记 015 创建接口_更新文章分类

这篇具有很好参考价值的文章主要介绍了Spring Boot 笔记 015 创建接口_更新文章分类。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

1.1.1 实体类id增加NotNull注释,并做分组校验

1.1.1.1 定义分组

1.1.1.2 实体类中指定校验项属于哪个分组

如果说某个校验项没有指定分组,默认属于Default分组
分组之间可以继承, A extends B  那么A中拥有B中所有的校验项
package com.geji.pojo;

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

import java.time.LocalDateTime;

@Data
public class Category {
    @NotNull(groups = Update.class)
    private Integer id;//主键ID
    @NotEmpty(groups={Add.class,Update.class})
    private String categoryName;//分类名称
    @NotEmpty(groups={Add.class,Update.class})
    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;//更新时间

    //如果说某个校验项没有指定分组,默认属于Default分组
    //分组之间可以继承, A extends B  那么A中拥有B中所有的校验项


    public interface Add extends Default {

    }

    public interface Update extends Default {
    }
}

1.1.1.3 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.Add.class) 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);
    }

    @PutMapping
    public Result update(@RequestBody @Validated(Category.Update.class) Category category){
        categoryService.update(category);
        return Result.success();
    }


}

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

    void update(Category category);
}

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

    @Override
    public void update(Category category) {
        category.setUpdateTime(LocalDateTime.now());
        categoryMapper.update(category);
    }

}

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 org.apache.ibatis.annotations.Update;

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

    //更新
    @Update("update category set category_name=#{categoryName},category_alias=#{categoryAlias},update_time=#{updateTime} where id=#{id}")
    void update(Category category);

}

1.1.5 postman测试

Spring Boot 笔记 015 创建接口_更新文章分类,Spring Boot,spring boot,笔记,java文章来源地址https://www.toymoban.com/news/detail-834739.html

到了这里,关于Spring Boot 笔记 015 创建接口_更新文章分类的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Spring Boot 笔记 019 创建接口_文件上传

    1.1 创建阿里OSS bucket OSS Java SDK 兼容性和示例代码_对象存储(OSS)-阿里云帮助中心 (aliyun.com) 1.2 编写工具类 1.3 创建文件上传controller 1.4 postman测试

    2024年02月22日
    浏览(37)
  • Spring boot+Vue博客平台:文章列表展示、文章分类与标签管理模块实现

    本文将详细介绍如何实现博客平台中的文章列表展示、文章分类与标签管理功能,包括前端的Vue组件设计和后端的Spring Boot接口实现。在阅读本文后,您将了解如何设计和实现高效、易用的文章列表展示、文章分类与标签管理功能。 1.设计思路 在设计文章列表展示功能时,我

    2023年04月11日
    浏览(43)
  • Spring Boot 笔记 028 文章列表

    1.1 导入中文语言包 1.2 显示文章分类 1.3.1 article.js增加文章列表接口 1.3.2 页面调用接口显示数据 1.3.3 表单绑定categoryname 1.3.4 搜索按钮,重置按钮,分页按钮 1.4 添加文章按钮 1.4.1 编辑页面,绑定数据,添加样式 1.4.2 文本编辑器 1.4.2.1 安装vue-quill 1.4.2.2 导入组件和样式 1.4.2

    2024年02月21日
    浏览(27)
  • java spring boot 注解、接口和问题解决方法(持续更新)

    @RestController         是SpringMVC框架中的一个注解,它结合了@Controller和@ResponseBody两个注解的功能,用于标记一个类或者方法,表示该类或方法用于处理HTTP请求,并将响应的结果直接返回给客户端,而不需要进行视图渲染 @Controller         是Spring Framework中的注解,用于

    2024年02月06日
    浏览(41)
  • 【笔记】Spring Boot 历史官方文档学习(持续更新)

    Spring Boot 2014正式发布1.0版本,距今已经快10年了。看历史官方文档了解重点feature, 帮助自己建立知识网络。 与 Spring 5 官网历史文档学习 一样,尽量保证不误解文档作者的原意,不好翻译的会有原文摘录(包括一些专有名词),并辅以自己的理解。限于篇幅原因,只摘录工作

    2024年02月10日
    浏览(28)
  • Spring Boot Elasticsearch7.6.2实现创建索引、删除索引、判断索引是否存在、获取/添加/删除/更新索引别名、单条/批量插入、单条/批量更新、删除数据、递归统计ES聚合的数据

    注意:我的版本是elasticsearch7.6.2、spring-boot-starter-data-elasticsearch-2.5.6 引入依赖 有时候你可能需要查询大批量的数据,建议加上下面配置文件

    2024年02月13日
    浏览(52)
  • DevOps系列文章之 Spring Boot Docker打包

    应用准备容器化,因为几十个应用从测试到发布太麻烦了,而且还会因为环境的因素导致部署中出现各种问题。为了在开发、测试、生产都能保持一致的环境,就引进了容器技术,而目前常用的应用使用基于spring boot的。 在Spring Boot应用中,我们可以约定不同的标识来定义不

    2024年02月11日
    浏览(37)
  • 【Spring Boot学习】Spring Boot的创建,第一个Spring Boot页面.

    前言: 大家好,我是 良辰丫 ,前面几篇文章,我们系统的学习了Spring框架,今天开始,我们就要学习更高级的SpringBoot框架了,不要着急哦,我们一起畅游SpringBoot框架的世界.💌💌💌 🧑个人主页:良辰针不戳 📖所属专栏:javaEE进阶篇之框架学习 🍎励志语句:生活也许会让我们遍体

    2024年02月08日
    浏览(33)
  • Spring Boot + Vue的网上商城之商品分类

    在网上商城中,商品分类是非常重要的一个功能,它可以帮助用户更方便地浏览和筛选商品。本文将介绍如何使用Spring Boot和Vue来实现商品分类的功能,包括一级分类和二级分类的管理以及前台按分类浏览商品的实现。 数据库设计:设计商品分类表和商品表,商品分类表包含

    2024年02月09日
    浏览(30)
  • 【Spring Boot学习一】创建项目 && Spring Boot的配置文件

    目录 一、安装插件 二、创建Spring Boot项目 1、创建项目 1.1 使用IDEA创建  1.2 网页版本创建 2、项目目录介绍与运行 三、Sping Boot的配置文件(重点) 🌷1、.properties配置文件 (1)基础语法:Key = value (2)读取配置⽂件中的内容,@Value 注解使⽤“${}”的格式读取; 🌷2、.y

    2024年02月16日
    浏览(30)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包