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(){
List cs = categoryService.list();
return Result.success(cs);
}
}
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 list();
}
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 map = ThreadLocalUtil.get();
Integer userId = (Integer) map.get("id");
category.setCreateUser(userId);
categoryMapper.add(category);
}
@Override
public List list() {
Map map = ThreadLocalUtil.get();
Integer userId = (Integer) map.get("id");
return categoryMapper.list(userId);
}
}
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 list(Integer userId);
}
2.1.1 postman测试发现日期字符串格式不符合要求
2.1.2 实体类添加注解
package com.geji.pojo;
import com.fasterxml.jackson.annotation.JsonFormat;
import jakarta.validation.constraints.NotEmpty;
imp服务器托管网ort lombok.Data;
import java.time服务器托管网.LocalDateTime;
@Data
public class Category {
private Integer id;//主键ID
@NotEmpty
private String categoryName;//分类名称
@NotEmpty
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;//更新时间
}
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
相关推荐: windows下配置mask2former(facebook版)
由于此版本的mask2former官方只提供了macOS和Linux的安装说明,所以windows安装会趟一些坑记录一下 1.安装Anaconda 2.安装PyCharm 3.创建python3.8环境(最高3.8因为有一个依赖包最高支持python3.8) …