向数据库中新增一条数据,出现
?controller:
@Autowired
private ArticleService articleService;
@PostMapping
public Result add(@RequestBody Article article){
articleService.add(article);
return Result.success();
}
Service:
@Service
public class ArticleServiceImpl implements ArticleService {
private ArticleMapper articleMapper;
@Override
public void add(Article article) {
//补充属性值
article.setCreateTime(LocalDateTime.now());
article.setUpdateTime(LocalDateTime.now());
Map<String,Object> map = ThreadLocalUtil.get();
Integer userId = (Integer) map.get("id");
article.setCreateUser(userId);
// System.out.println(article);
articleMapper.add(article);
}
}
mapper:
@Mapper
public interface ArticleMapper {
//新增
@Insert("insert into article(title,content,cover_img,state,category_id,create_user,create_time,update_time)" +
"values(#{title},#{content},#{coverImg},#{state},#{categoryId},#{createUser},#{createTime},#{updateTime})")
void add(Article article);
}
空指针异常,一般是对象没有实现。
看了半天,没看出来哪错了。
无意间,看见在Service层的使用Mapper没进行自动注入,原因就在这
在Service层的上面加上@Autowired
@Autowired
private ArticleMapper articleMapper;
问题解决。