SpringBoot增刪改查(基於註解)
阿新 • • 發佈:2022-03-14
簡單的使用SpringBoot註解實現了增刪改查操作
User類
/** * 使用者類 */ public class User { private Integer id; //編號id private String username; //使用者名稱 private String password; //密碼 private String realName; //真實姓名 private String phone; //聯絡方式 private Boolean bool; //用於判斷使用者和管理員 public User() { }public User(Integer id, String username, String password, String realName, String phone, Boolean bool) { this.id = id; this.username = username; this.password = password; this.realName = realName; this.phone = phone; this.bool = bool; } publicInteger getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() {return password; } public void setPassword(String password) { this.password = password; } public String getRealName() { return realName; } public void setRealName(String realName) { this.realName = realName; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public Boolean getBool() { return bool; } public void setBool(Boolean bool) { this.bool = bool; } @Override public String toString() { return "User{" + "id=" + id + ", username='" + username + '\'' + ", password='" + password + '\'' + ", realName='" + realName + '\'' + ", phone='" + phone + '\'' + ", bool=" + bool + '}'; }
UserMapper介面
import org.apache.ibatis.annotations.*; import org.springframework.stereotype.Repository; import java.util.List; @Mapper @Repository public interface UserMapper { //新增 @Insert("insert into user (username,password,realName,phone) values (#{username},#{password},#{realName},#{phone})") @Options(useGeneratedKeys = true, keyProperty = "id") int insert(User user); //刪除 @Delete("delete from user where id = #{id}") int delete(Integer id); //修改 @Update("update user set username=#{username},password=#{password},realName=#{realName},phone=#{phone},bool=#{bool} where id =#{id}") int update(User user); //查詢全部 @Select("select * from user") List<User> findAll(); //根據id查詢 @Select("select * from user where id = #{id}") User findById(Integer id); //根據使用者名稱查詢 @Select("select * from user where username = #{username}") User findByUsername(String username); }
測試類
import com.study.domain.User; import com.study.mapper.UserMapper; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.util.List; @SpringBootTest class RestApplicationTests { @Autowired private UserMapper userMapper; @Test void testFindAll(){ List<User> users = userMapper.findAll(); System.out.println(users); } @Test void testFindById(){ User user = userMapper.findById(1); System.out.println(user); } @Test void testFindByUsername(){ User user = userMapper.findByUsername("root"); System.out.println(user); } @Test void testInsert(){ User user = new User(); user.setUsername("zhangsan"); user.setPassword("123456"); user.setRealName("zhangsan"); user.setPhone("12345678901"); int result = userMapper.insert(user); System.out.println(result); } @Test void testUpdate(){ User user = userMapper.findById(79); user.setUsername("zhaosi"); user.setPassword("1234"); user.setRealName("zhangsan"); user.setPhone("12345678901"); int result = userMapper.update(user); System.out.println(result); } @Test void testDelete(){ int result = userMapper.delete(79); System.out.println(result); } }