SpringBoot增删改查(基于注解)

简单的使用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;
    }

    public Integer 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);
    }

}
posted @ 2022-03-14 16:10  雪前  阅读(290)  评论(0)    收藏  举报