当前位置:   article > 正文

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

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

1.1.1 实体类添加校验

  1. package com.geji.pojo;
  2. import jakarta.validation.constraints.NotEmpty;
  3. import lombok.Data;
  4. import java.time.LocalDateTime;
  5. @Data
  6. public class Category {
  7. private Integer id;//主键ID
  8. @NotEmpty
  9. private String categoryName;//分类名称
  10. @NotEmpty
  11. private String categoryAlias;//分类别名
  12. private Integer createUser;//创建人ID
  13. private LocalDateTime createTime;//创建时间
  14. private LocalDateTime updateTime;//更新时间
  15. }

1.1.2 Controller

  1. package com.geji.controller;
  2. import com.geji.pojo.Category;
  3. import com.geji.pojo.Result;
  4. import com.geji.service.CategoryService;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.validation.annotation.Validated;
  7. import org.springframework.web.bind.annotation.PostMapping;
  8. import org.springframework.web.bind.annotation.RequestBody;
  9. import org.springframework.web.bind.annotation.RequestMapping;
  10. import org.springframework.web.bind.annotation.RestController;
  11. @RestController
  12. @RequestMapping("/category")
  13. public class CategoryController {
  14. @Autowired
  15. private CategoryService categoryService;
  16. @PostMapping
  17. public Result add(@RequestBody @Validated Category category) {
  18. categoryService.add(category);
  19. return Result.success();
  20. }
  21. }

1.1.3 Service

  1. package com.geji.service;
  2. import com.geji.pojo.Category;
  3. public interface CategoryService {
  4. void add(Category category);
  5. }

1.1.4 ServiceImpl

  1. package com.geji.service.impl;
  2. import com.geji.mapper.CategoryMapper;
  3. import com.geji.pojo.Category;
  4. import com.geji.service.CategoryService;
  5. import com.geji.utils.ThreadLocalUtil;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.stereotype.Service;
  8. import java.time.LocalDateTime;
  9. import java.util.Map;
  10. @Service
  11. public class CategoryServiceImpl implements CategoryService {
  12. @Autowired
  13. private CategoryMapper categoryMapper;
  14. @Override
  15. public void add(Category category) {
  16. //补充属性值
  17. category.setCreateTime(LocalDateTime.now());
  18. category.setUpdateTime(LocalDateTime.now());
  19. Map<String,Object> map = ThreadLocalUtil.get();
  20. Integer userId = (Integer) map.get("id");
  21. category.setCreateUser(userId);
  22. categoryMapper.add(category);
  23. }
  24. }

1.1.5 mapper

  1. package com.geji.mapper;
  2. import com.geji.pojo.User;
  3. import org.apache.ibatis.annotations.Insert;
  4. import org.apache.ibatis.annotations.Mapper;
  5. import org.apache.ibatis.annotations.Select;
  6. import org.apache.ibatis.annotations.Update;
  7. @Mapper
  8. public interface UserMapper {
  9. //根据用户名查询用户
  10. @Select("select * from user where username=#{username}")
  11. User findByUserName(String username);
  12. //添加
  13. @Insert("insert into user(username,password,create_time,update_time)" +
  14. " values(#{username},#{password},now(),now())")
  15. void add(String username, String password);
  16. @Update("update user set nickname=#{nickname},email=#{email},update_time=#{updateTime} where id=#{id}")
  17. void update(User user);
  18. @Update("update user set user_pic=#{avatarUrl},update_time=now() where id=#{id}")
  19. void updateAvatar(String avatarUrl,Integer id);
  20. @Update("update user set password=#{md5String},update_time=now() where id=#{id}")
  21. void updatePwd(String md5String, Integer id);
  22. }

1.1.6 测试

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/86759?site
推荐阅读
相关标签
  

闽ICP备14008679号