为了便捷的操作。不按照前面几篇的方式。直接在controller中进行mapper注入和操作。
package org.example.controller; import org.example.Entity.MyUser; import org.example.mapper.UserMapper; import org.example.service.MyUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController public class MyUserController { @Autowired private MyUserService myUserService; @Autowired private UserMapper userMapper; @GetMapping(value = "/findAll") public List<MyUser> findAll() { // 查询所有用户信息 List<MyUser> users = myUserService.findAll(); for (MyUser user : users) { System.out.println(user.getId()+" "+user.getName()+" "+user.getAge()); } return users; } @GetMapping(value = "/save") public int save() { MyUser user = new MyUser(); user.setName("小文"); user.setAge(21); int insert = myUserService.save(user);//如果没有设置id,那么会自动生成id System.out.println(insert);//受影响行数 System.out.println(user);//id会自动回填 return insert; } @GetMapping(value = "/updateById") public int updateById() { MyUser user = new MyUser(); user.setId(1014); user.setName("小文改"); user.setAge(222); int insert = myUserService.update(user); System.out.println(insert);//受影响行数 return insert; } @GetMapping(value = "/updateByName") public int updateByIdName() { int insert = myUserService.updateByName(); System.out.println(insert);//受影响行数 return insert; } @GetMapping(value = "/findById") public MyUser findById() { MyUser user = userMapper.selectById("1015"); System.out.println(user); return user; } }
标签:insert,spring,boot,System,plus,user,MyUser,println,import From: https://www.cnblogs.com/xiaobaibailongma/p/17091538.html