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

Spring Boot 笔记 012 创建接口_添加文章分类

1.1.1 实体类添加校验

package com.geji.pojo;import jakarta.validation.constraints.NotEmpty;
import lombok.Data;import java.time.LocalDateTime;@Data
public class Category {private Integer id;//主键ID@NotEmptyprivate String categoryName;//分类名称@NotEmptyprivate String categoryAlias;//分类别名private Integer createUser;//创建人IDprivate LocalDateTime createTime;//创建时间private LocalDateTime updateTime;//更新时间
}

1.1.2 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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/category")
public class CategoryController {@Autowiredprivate CategoryService categoryService;@PostMappingpublic Result add(@RequestBody @Validated Category category) {categoryService.add(category);return Result.success();}
}

1.1.3 Service

package com.geji.service;import com.geji.pojo.Category;public interface CategoryService {void add(Category category);
}

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

1.1.5 mapper

package com.geji.mapper;import com.geji.pojo.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;@Mapper
public interface UserMapper {//根据用户名查询用户@Select("select * from user where username=#{username}")User findByUserName(String username);//添加@Insert("insert into user(username,password,create_time,update_time)" +" values(#{username},#{password},now(),now())")void add(String username, String password);@Update("update user set nickname=#{nickname},email=#{email},update_time=#{updateTime} where id=#{id}")void update(User user);@Update("update user set user_pic=#{avatarUrl},update_time=now() where id=#{id}")void updateAvatar(String avatarUrl,Integer id);@Update("update user set password=#{md5String},update_time=now() where id=#{id}")void updatePwd(String md5String, Integer id);
}

1.1.6 测试

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

相关文章:

  • Spring-面试题
  • Flink理论—容错之状态
  • 【数据结构】链表OJ面试题5《链表的深度拷贝》(题库+解析)
  • 智慧校园规划建设方案
  • 003 - Hugo, 创建文章
  • HCIA-HarmonyOS设备开发认证V2.0-IOT硬件子系统-GPIO
  • 《Java 简易速速上手小册》第7章:Java 网络编程(2024 最新版)
  • 用keras对电影评论进行情感分析
  • 每日OJ题_算法_递归④力扣24. 两两交换链表中的节点
  • 110 C++ decltype含义,decltype 主要用途
  • PYTHON 120道题目详解(85-87)
  • 【Linux】Linux编译器-gcc/g++ Linux项目自动化构建工具-make/Makefile
  • sqlserver 子查询 =,in ,any,some,all的用法
  • 基于MapVGL的地理信息三维度数据增长可视化
  • 天锐绿盾|防泄密系统|计算机文件数据\资料安全管理软件
  • leetcode刷题(罗马数字转数字)
  • 什么是NAT网关?联通云NAT网关有什么优势
  • CVE-2023-41892 漏洞复现
  • 【每日一题】06 排序链表
  • 【精品】关于枚举的高级用法
  • Vue2学习第一天
  • HAL STM32通过multi_button库处理按键事件
  • 随机过程及应用学习笔记(一)概率论(概要)
  • 洛谷_P1059 [NOIP2006 普及组] 明明的随机数_python写法
  • 爆火的人工智能开源open-interpreter源码解析
  • POM设计模式思路,详解POM:概述与介绍,POM思路梳理+代码示例(全)
  • 1、学习 Eureka 注册中心
  • 何为分账系统?
  • 机器学习10-特征缩放
  • Java基于微信小程序的医院挂号小程序,附源码