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

怎么解决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日