实体类不变:
package org.example.Entity; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; @TableName("users") //不一致时,需要映射 public class MyUser { @TableId(type = IdType.AUTO) private int id; private String name; private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "MyUser{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + '}'; } }
服务类:
package org.example.service; import org.example.Entity.MyUser; import java.util.List; public interface MyUserService { // 查询所有用户 List<MyUser> findAll(); int save(MyUser myUser); int update(MyUser myUser); }
package org.example.service.Impl; 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.stereotype.Service; import java.util.List; @Service public class MyUserServiceImpl implements MyUserService { //继承了BaseMapper所有的方法,可以编写自己的扩展方法 @Autowired private UserMapper userMapper; /** * 查询所有用户信息 * @return */ @Override public List<MyUser> findAll() { return userMapper.selectList(null); } /** * 增加一个用户信息 * @param myUser * @return */ @Override public int save(MyUser myUser) { return userMapper.insert(myUser); } /** * 通过id修改用户信息 * @param myUser * @return */ @Override public int update(MyUser myUser) { return userMapper.updateById(myUser); } }
控制器类:
package org.example.controller; import org.example.Entity.MyUser; 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; @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; } }
标签:int,spring,boot,plus,user,org,import,public,MyUser From: https://www.cnblogs.com/xiaobaibailongma/p/17091377.html