其他不变:
服务类:
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); int updateByName(); }
package org.example.service.Impl; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; 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); } @Override public int updateByName() { UpdateWrapper<MyUser> updateWrapper = new UpdateWrapper<>(); updateWrapper.eq("name","小文"); //以name=小文为条件 类似于一个条件语句 MyUser user = new MyUser(); user.setAge(18); return userMapper.update(user, updateWrapper); } }
控制器:
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; } @GetMapping(value = "/updateByName") public int updateByIdName() { int insert = myUserService.updateByName(); System.out.println(insert);//受影响行数 return insert; } }
也可以多条件,此处是and条件,都要满足:
标签:return,name,int,spring,org,boot,user,MyUser,import From: https://www.cnblogs.com/xiaobaibailongma/p/17091428.html