奶茶精Gaaa 2024-06-23 21:18 采纳率: 47.4%
浏览 3

怎么解决springboot这个问题?

怎么解决springboot这个问题?

img

![img](


package com.itheima.springbootquickstart.service.impl;

import com.itheima.springbootquickstart.mapper.CategoryMapphttps://img-mid.csdnimg.cn/release/static/image/mid/ask/06a30f66ece04c63b25e4949153a3f6f.png "#left")
er;
import com.itheima.springbootquickstart.pojo.Category;
import com.itheima.springbootquickstart.service.CategoryService;
import com.itheima.springbootquickstart.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 {
@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);

    }


package com.itheima.springbootquickstart.service;

import com.itheima.springbootquickstart.pojo.Category;

public interface CategoryService {
//    新增分类
    void add(Category category);
}

package com.itheima.springbootquickstart.pojo;

import lombok.Data;

import java.time.LocalDateTime;
@Data
public class Category {
    private Integer id;//主键ID
    private String categoryName;//分类名称
    private String categoryAlias;//分类别名
    private Integer createUser;//创建人ID
    private LocalDateTime createTime;//创建时间
    private LocalDateTime updateTime;//更新时间
}
package com.itheima.springbootquickstart.mapper;

import com.itheima.springbootquickstart.pojo.Category;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;



@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);
}
package com.itheima.springbootquickstart.controller;

import com.itheima.springbootquickstart.pojo.Category;
import com.itheima.springbootquickstart.pojo.Result;
import com.itheima.springbootquickstart.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
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 {
    @Autowired
    private CategoryService categoryService;
    @PostMapping
    public Result add(@RequestBody Category category){
categoryService.add(category);
return Result.success();
    }
}



}

  • 写回答

4条回答 默认 最新

  • 星期五Plus 2024-06-25 09:27
    关注

    Integer userId=(Integer)map.get("id");换成:String.valueOf(map.get("id"));试试,直接强转有问题

    评论

报告相同问题?

问题事件

  • 创建了问题 6月23日

悬赏问题

  • ¥20 有偿:在ubuntu上安装arduino以及其常用库文件。
  • ¥15 请问用arcgis处理一些数据和图形,通常里面有一个根据点划泰森多边形的命令,直接划的弊端是只能执行一个完整的边界,但是我们有时候会用到需要在有很多边界内利用点来执行划泰森多边形的命令
  • ¥30 在wave2foam中执行setWaveField时遇到了如下的浮点异常问题,请问该如何解决呢?
  • ¥20 看图片)删除这个自动化录屏脚本就一直报错找不到脚本文件,如何解决?(相关搜索:bat文件)
  • ¥750 关于一道数论方面的问题,求解答!(关键词-数学方法)
  • ¥200 csgo2的viewmatrix值是否还有别的获取方式
  • ¥15 Stable Diffusion,用Ebsynth utility在视频选帧图重绘,第一步报错,蒙版和帧图没法生成,怎么处理啊
  • ¥15 请把下列每一行代码完整地读懂并注释出来
  • ¥15 pycharm运行main文件,显示没有conda环境
  • ¥15 寻找公式识别开发,自动识别整页文档、图像公式的软件