在 idea 中,mapper 类在引用的时候会提示红色波浪线,这不是错误
    image.png
    引用 mapper 类会提示红色波浪线
    打开设置——编辑器——检查——自动装配 Bean 类,去掉这里的勾即可取消 idea 的错误提示
    image.png
    完善 StuServiceImpl 类

    1. package com.imooc.service.impl;
    2. import com.imooc.mapper.StuMapper;
    3. import com.imooc.pojo.Stu;
    4. import com.imooc.service.StuService;
    5. import org.springframework.beans.factory.annotation.Autowired;
    6. import org.springframework.stereotype.Service;
    7. import org.springframework.transaction.annotation.Propagation;
    8. import org.springframework.transaction.annotation.Transactional;
    9. /**
    10. * @author 92578
    11. * @since 1.0
    12. */
    13. @Service
    14. public class StuServiceImpl implements StuService {
    15. @Autowired
    16. private StuMapper stuMapper;
    17. @Transactional(propagation = Propagation.SUPPORTS)
    18. @Override
    19. public Stu getStuInfo(int id) {
    20. return stuMapper.selectByPrimaryKey(id);
    21. }
    22. @Transactional(propagation = Propagation.REQUIRED)
    23. @Override
    24. public void soveStu() {
    25. Stu stu = new Stu();
    26. stu.setName("jack");
    27. stu.setAge(19);
    28. stuMapper.insert(stu);
    29. }
    30. @Transactional(propagation = Propagation.REQUIRED)
    31. @Override
    32. public void updateStu(int id) {
    33. Stu stu = new Stu();
    34. stu.setId(id);
    35. stu.setName("lucy");
    36. stu.setAge(20);
    37. stuMapper.updateByPrimaryKey(stu);
    38. }
    39. @Transactional(propagation = Propagation.REQUIRED)
    40. @Override
    41. public void deleteStu(int id) {
    42. stuMapper.deleteByPrimaryKey(id);
    43. }
    44. }

    完善 StuFooController 类

    1. package com.imooc.controller;
    2. import com.imooc.service.StuService;
    3. import org.springframework.beans.factory.annotation.Autowired;
    4. import org.springframework.web.bind.annotation.GetMapping;
    5. import org.springframework.web.bind.annotation.PostMapping;
    6. import org.springframework.web.bind.annotation.RestController;
    7. /**
    8. * @author 92578
    9. * @since 1.0
    10. */
    11. @RestController
    12. public class StuFooController {
    13. @Autowired
    14. private StuService stuService;
    15. @GetMapping("/getStu")
    16. public Object getStu(int id) {
    17. return stuService.getStuInfo(id);
    18. }
    19. @PostMapping("/saveStu")
    20. public Object saveStu() {
    21. stuService.saveStu();
    22. return "OK";
    23. }
    24. @PostMapping("/updateStu")
    25. public Object updateStu(int id) {
    26. stuService.updateStu(id);
    27. return "OK";
    28. }
    29. @PostMapping("/deleteStu")
    30. public Object deleteStu(int id) {
    31. stuService.deleteStu(id);
    32. return "OK";
    33. }
    34. }

    【注意】
    编写接口要注意接口的幂等性,简单来说就是多次调用相同接口都是对同一条记录操作。