Spring Boot 知识笔记(整合Mybatis续-补充增删改查)
续上篇,补充数据库增删改查的其他场景。
一、Mapper中添加其他场景操作
package net.Eleven.demo.Mapper; import net.Eleven.demo.domain.UserNew; import org.apache.ibatis.annotations.*; import java.util.List; /** * 功能描述:访问数据库的接口,相当于dao层 * @author Eleven */ public interface UserMapper { //推荐使用#{}取值,不要用${},因为存在注入的风险 /** * 向数据库插入一条数据 * @param userNew * @return */ @Insert("INSERT INTO user(name,phone,create_time,age) VALUES(#{name},#{phone},#{creatTime},#{age})") @Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id") //keyProperty java对象的属性;keyColumn表示数据库的字段 int insert(UserNew userNew); /** * 查找全部,功能比较简单,也可以直接跳过service层,直接在controller注入mapper * @return */ @Select("select * from user") @Results({@Result(column = "create-time",property = "createTime")}) List<UserNew> getAllUser(); /** * 根据id查询,返回的是一个user对象 * @param id * @return */ @Select("select * from user where id=#{id}") @Results({@Result(column = "create-time",property = "createTime")}) UserNew findById(int id); /** * 根据id更新对象的name * @param userNew */ @Update("update user set name=#{name} where id=#{id}") void updateById(UserNew userNew); /** * 根据id,删除一个对象 */ @Delete("delete from user where id =#{id}") void deleteById(int id); }
二、impl文件中增加对应的调用mapper的方法
package net.Eleven.demo.Service.impl; import net.Eleven.demo.Mapper.UserMapper; import net.Eleven.demo.Service.UserService; import net.Eleven.demo.domain.UserNew; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.validation.constraints.Null; import java.util.List; @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public int add(UserNew userNew) { userMapper.insert(userNew); int id = userNew.getId(); return id; } //查找所有,对应mapper的getAlluser public Object findAll(){ return userMapper.getAllUser(); } //根据id查找,对应mapper的findById public Object findById(int id){ return userMapper.findById(id); } //更新一个对象 public void updateById(UserNew userNew){ userMapper.updateById(userNew); } //删除一个对象 public void deleteById(int id){ userMapper.deleteById(id); } }
三、controller中增加对应的request请求,通过注入,调用impl的方法,继而执行数据库命令。
package net.Eleven.demo.controller; import net.Eleven.demo.Service.UserService; import net.Eleven.demo.Service.impl.UserServiceImpl; import net.Eleven.demo.domain.JsonData; import net.Eleven.demo.domain.UserNew; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.Date; @RestController @RequestMapping("/api/sql/user") public class UserController { @Autowired private UserService userService; @Autowired private UserServiceImpl userServiceImpl; @GetMapping("add") public Object userAdd(){ UserNew userNew = new UserNew(); userNew.setAge(11); userNew.setCreatTime(new Date()); userNew.setName("Eleven"); userNew.setPhone("18211111111"); int id = userService.add(userNew); return JsonData.buildSuccess(id); } //查找全部 @GetMapping("find_all") public Object findAll(){ return JsonData.buildSuccess(userServiceImpl.findAll()); } //根据id查找对象 @GetMapping("find_by_id") public Object findById(int id){ return JsonData.buildSuccess(userServiceImpl.findById(id)); } //根据id更新对象的name @GetMapping("update_by_id") public Object updateById(String name,int id){ UserNew userNew = new UserNew(); userNew.setName(name); userNew.setId(id); userServiceImpl.updateById(userNew); return JsonData.buildSuccess(); } //根据id删除对象 @GetMapping("delete_by_id") public Object deleteById(int id){ userServiceImpl.deleteById(id); } }
四、执行结果