package mybatis.dao;
import mybatis.pojo.User;
import org.apache.ibatis.annotations.*;
import java.util.List;
public interface UserMapper {
//查询所有
@Select("select * from user")
List<User> getUsers();
//通过ID查询,方法存在多个参数时,所有参数前必须加上Param注解
/**
*关羽@Param注解
* 1、基本类型的参数或者String类型,需要加上
* 2、引用类型不需要加
* 3、如果只有一个基本类型的话,可以忽略。
* 4、在SQL中引用的就是这里的@Param中的设定的属性
*/
@Select("select * from user where id = #{id}")
User getUserById(@Param("id") int id);
//插入
@Insert("insert into user(id,name,pwd) values (#{id},#{name},#{pwd})")
int addUser(User user);
//修改
@Update("update user set name = #{name},pwd = #{pwd} where id = #{id}")
int updateUser(User user);
//删除
@Delete("delete from user where id = #{id}")
int deleteUser(@Param("id") int id);
}
User
package mybatis.pojo;
import org.apache.ibatis.type.Alias;
public class User {
private int id;
private String name;
private String pwd;
public User(){
}
public User(int id, String name, String pwd) {
this.id = id;
this.name = name;
this.pwd = pwd;
}
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 String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", pwd='" + pwd + '\'' +
'}';
}
}