在 idea 中,mapper 类在引用的时候会提示红色波浪线,这不是错误
引用 mapper 类会提示红色波浪线
打开设置——编辑器——检查——自动装配 Bean 类,去掉这里的勾即可取消 idea 的错误提示
完善 StuServiceImpl 类
package com.imooc.service.impl;
import com.imooc.mapper.StuMapper;
import com.imooc.pojo.Stu;
import com.imooc.service.StuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
/**
* @author 92578
* @since 1.0
*/
@Service
public class StuServiceImpl implements StuService {
@Autowired
private StuMapper stuMapper;
@Transactional(propagation = Propagation.SUPPORTS)
@Override
public Stu getStuInfo(int id) {
return stuMapper.selectByPrimaryKey(id);
}
@Transactional(propagation = Propagation.REQUIRED)
@Override
public void soveStu() {
Stu stu = new Stu();
stu.setName("jack");
stu.setAge(19);
stuMapper.insert(stu);
}
@Transactional(propagation = Propagation.REQUIRED)
@Override
public void updateStu(int id) {
Stu stu = new Stu();
stu.setId(id);
stu.setName("lucy");
stu.setAge(20);
stuMapper.updateByPrimaryKey(stu);
}
@Transactional(propagation = Propagation.REQUIRED)
@Override
public void deleteStu(int id) {
stuMapper.deleteByPrimaryKey(id);
}
}
完善 StuFooController 类
package com.imooc.controller;
import com.imooc.service.StuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author 92578
* @since 1.0
*/
@RestController
public class StuFooController {
@Autowired
private StuService stuService;
@GetMapping("/getStu")
public Object getStu(int id) {
return stuService.getStuInfo(id);
}
@PostMapping("/saveStu")
public Object saveStu() {
stuService.saveStu();
return "OK";
}
@PostMapping("/updateStu")
public Object updateStu(int id) {
stuService.updateStu(id);
return "OK";
}
@PostMapping("/deleteStu")
public Object deleteStu(int id) {
stuService.deleteStu(id);
return "OK";
}
}
【注意】
编写接口要注意接口的幂等性,简单来说就是多次调用相同接口都是对同一条记录操作。