当前位置: 首页 > news >正文

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 {@Autowiredprivate CategoryService categoryService;@PostMappingpublic Result add(@RequestBody @Validated(Category.Add.class) Category category) {categoryService.add(category);return Result.success();}@GetMappingpublic 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);}@PutMappingpublic 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 {@Autowiredprivate CategoryMapper categoryMapper;@Overridepublic 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);}@Overridepublic List<Category> list() {Map<String,Object> map = ThreadLocalUtil.get();Integer userId = (Integer) map.get("id");return categoryMapper.list(userId);}@Overridepublic Category findById(Integer id) {Category c = categoryMapper.findById(id);return c;}@Overridepublic 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测试

http://www.lryc.cn/news/300176.html

相关文章:

  • 【Java基础题型】判断是否是回文数
  • Linux paste命令教程:并行合并文件的利器(附案例详解和注意事项)
  • 用163邮箱或者outlook接收国科大邮箱的邮件
  • VitePress-15- 配置- description 的作用详解
  • 寒假学习记录17:包管理器(包管理工具)
  • 【AIGC】Stable Diffusion的常见错误
  • 线段树解决-----P1161 开灯 P1047 [NOIP2005 普及组] 校门外的树 python解法
  • 学习总结16
  • 问题:从完整的问题解决过程来看,( )是首要环节。A.理解问题 B.提出假设C.发现问题 D.检验假设 #学习方法#学习方法
  • 服务器感染了.mallox勒索病毒,如何确保数据文件完整恢复?
  • Android java基础_多态性
  • 面试前的准备
  • 前端架构: 本地调试脚手架的2种方式
  • 现阶段适用于 单一架构 还是 分布式架构 ?
  • 掌握Go并发:Go语言并发编程深度解析
  • 创建一个多进程服务器和多线程服务器
  • 相机图像质量研究(18)常见问题总结:CMOS期间对成像的影响--CFA
  • 18.谈谈你对JSON的理解
  • 绝地求生:“觉醒之旅”通行证曝光,西游主题通行证及成长型武器即将上线
  • JS如何判断普通函数与异步(async)函数
  • ndk-r20b 编译 boost 1.74。
  • 尚硅谷最新Node.js 学习笔记(四)
  • 掌握XGBoost:GPU 加速与性能优化
  • 【2024年毕设系列】如何使用Anaconda和Pycharm
  • Blazor OIDC 单点登录授权实例5 - 独立SSR App (net8 webapp ) 端授权
  • 基于蒙特卡洛的电力系统可靠性分析matlab仿真,对比EDNS和LOLP
  • Spring boot整合redisson报错
  • 【AIGC】Stable Diffusion的ControlNet插件
  • 【蓝桥杯单片机入门记录】认识单片机
  • Rust 数据结构与算法:3栈:用栈实现符号匹配