本示例的场景:mapper中resultType的值是全限定类名,但只从数据库取部分字段,这是可以的。例如,从数据库中只取了两个字段,但Java类的属性有3个。
例如,类User中定义了三个字段:
package com.thb.model;
public class User {
private int id;
private String name;
private String hometown;
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getHometown () {
return this.hometown;
}
public void setHometown(String hometown) {
this.hometown = hometown;
}
}
在mapper文件中的查询语句,只从数据库取id和name两个字段:
UserMapper接口的定义:
调用片段: