Mybatis如何给字段起别名?
假如有一个学生表,有一个字段是class,你的实体类变量肯定不能用class,那么如何起别名?
通过以下代码实现
@Result(column = "class",property = "clas")
mapper代码
public interface StudentDao {
@Insert("insert into student(id,name,sex,clas) values(#{name},#{money})")
void save(Student student);
@Update("upate student set name=#{name},sex=#{sex} where id=#{id}")
void update(Student student);
@Delete("delete from student where id=#{id}")
void delete(String id);
@Select("select * from student where id=#{id}")
Student findById(String id);
@Select("select * from student")
@Result(column = "class",property = "clas")
List<Student> findAll();
}
实体类
public class Student {
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getClas() {
return clas;
}
public void setClas(String clas) {
this.clas = clas;
}
@Override
public String toString() {
return "Student{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
", clas='" + clas + '\'' +
'}';
}
private String id;
private String name;
private String sex;
private String clas;
}