在使用mybatis的时候我们经常要用到联合查询,现在就对一表对多表和一对一表的xml文件配置进行说明
Student.xml
<resultMap type="many.one.entiry.Student" id="StudentResult">
<id column="s_id" jdbcType="INTEGER" property="id"/>
<result column="o_id" jdbcType="INTEGER" property="oid"/>
<result column="s_name" jdbcType="VARCHAR" property="name"/>
<result column="s_sex" jdbcType="VARCHAR" property="sex"/>
<association property="teacher" javaType="many.one.entiry.Teacher" resultMap="many.one.mapper.TeacherMapper.TeacherResult" ></association>
<collection property="courseList" ofType="many.one.entiry.Course" resultMap="many.one.mapper.CourseMapper.CourseResult" />
</resultMap>
StudentBean
public class Student {
private Integer id;
private String name;
private String sex;
private Integer oid;
private Teacher teacher;//一对一
private List<Course> courseList;//一对多
public Student() {
}
public Integer getId() {
return id;
}
public void setId(Integer 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 Integer getOid() {
return oid;
}
public void setOid(Integer oid) {
this.oid = oid;
}
public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
public Achievement getAchievement() {
return achievement;
}
public void setAchievement(Achievement achievement) {
this.achievement = achievement;
}
public List<Course> getCourseList() {
return courseList;
}
public void setCourseList(List<Course> courseList) {
this.courseList = courseList;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", sex=" + sex
+ ", oid=" + oid + ", teacher=" + teacher + ", achievement="
+ achievement + ", courseList=" + courseList + "]";
}
}
一对一在Bean中我们可以将其对象作为自身实体类的一个成员变量,一对多返回的结果是一个集合所以将其集合作为成员变量。
CourseBean
public class Course {
private Integer c_id;
private Integer c_sid;
private String biology;
private String physic;
private String chemistry;
public Course(){}
public Integer getC_id() {
return c_id;
}
public void setC_id(Integer c_id) {
this.c_id = c_id;
}
public Integer getC_sid() {
return c_sid;
}
public void setC_sid(Integer c_sid) {
this.c_sid = c_sid;
}
public String getBiology() {
return biology;
}
public void setBiology(String biology) {
this.biology = biology;
}
public String getPhysic() {
return physic;
}
public void setPhysic(String physic) {
this.physic = physic;
}
public String getChemistry() {
return chemistry;
}
public void setChemistry(String chemistry) {
this.chemistry = chemistry;
}
@Override
public String toString() {
return "Course [c_id=" + c_id + ", c_sid=" + c_sid + ", biology="
+ biology + ", physic=" + physic + ", chemistry=" + chemistry
+ "]";
}
}
TeacherBean
public class Teacher {
private Integer t_id;
private String username;
private String password;
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_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;
}
@Override
public String toString() {
return "Teacher [t_id=" + t_id + ", username=" + username
+ ", password=" + password + "]";
}
}
Course.xml
<resultMap type="many.one.entiry.Course" id="CourseResult">
<id column="c_id" jdbcType="INTEGER" property="c_id" />
<result column="c_sid" jdbcType="INTEGER" property="c_sid" />
<result column="c_biology" jdbcType="VARCHAR" property="biology" />
<result column="c_physic" jdbcType="VARCHAR" property="physic" />
<result column="c_chemistry" jdbcType="VARCHAR" property="chemistry" />
</resultMap>
teacher.xml
<resultMap type="many.one.entiry.Teacher" id="TeacherResult">
<id column="t_id" jdbcType="INTEGER" property="t_id" />
<id column="t_name" jdbcType="VARCHAR" property="username" />
<result column="t_password" jdbcType="VARCHAR" property="password" />
</resultMap>
association标签是用来配置一对一表查询的,collection是配置一对多表查询的。
一对一查询中mybatis会将查询结果封装到相应的对象中,如teacher对象,而一对多将返回一个List集合如List<courseList>.