一对一使用的是标签,这个标签有column、javaType和property三个属性,其中column对应的是外键id(使用查询结果的字段),javaType是外键对象的类型,property是外键对象在主对象中的属性名称。这个标签包含两个子标签,分别是其中有
一个老师对象对应一门课程,老师对象有老师工号(teacherId)、老师姓名(teacherName)、和一个课程对象(cursor),课程对象有课程编号(cursorId)、课程名称(cursorName)。
public class Teacher{
private String teacherId;
private String teacherName;
private Cursor cursor;
public void setTeacherId(String teacherId){
this.teacherId=teacherId;
}
public String getTeacherId(){
return this.teacherId;
}
public void setTeacherName(String teacherName){
this.teacherName=teacherName;
}
public String getTeacherName(){
return this.teacherName;
}
public void setCursor(Cursor cursor){
this.cursor=cursor;
}
public String getCursor(){
return this.cursor;
}
}
public class Cursor{
private String cursorId;
private String cursorName;
public void setCursorId(String cursorId){
this.cursorId=cursorId;
}
public String getCursorId(){
return this.cursorId;
}
public void setCursorName(String cursorName){
this.cursorName=cursorName;
}
public String getCursorName(){
return this.cursorName;
}
}
id="teacherMap" type="Teacher">
<id column="teacher_id" jdbcType="VARCHAR" property="teacherId"/>
<result column="teacher_name" jdbcType="VARCHAR" property="teacherName"/>
"cursor_id" javaType="Cursor" property="cursor">
<id column="cursor_id" jdbcType="VARCHAR" property="cursorId"/>
<result column="cursor_name" jdbcType="VARCHAR" property="cursorName"/>
select a.teacher_id,a.teacher_name,b.cursor_id,b.cursor_name from teacher a left join cursor b on a.cursor_id=b.id
一对多使用的是标签,此标签有column、ofType、property三个属性,column对应的是外键(查询结果的字段),ofType是外键对象的类型,property是指外键对象在主对象中的名称,这个标签包含两个子标签,分别是其中有
一个老师对应多名学生,老师对象包含有老师工号(teacherId),老师姓名(teacherName),学生对象集合(Liststudents),学生对象包含有学生学号(student_id),学生姓名(student_name)
public class Teacher{
private String teacherId;
private String teacherName;
private List students;
public void setTeacherId(String teacherId){
this.teacherId=teacherId;
}
public String getTeacherId(){
return this.teacherId;
}
public void setTeacherName(String teacherName){
this.teacherName=teacherName;
}
public String getTeacherName(){
return this.teacherName;
}
public void setStudents(List students){
this.students=students;
}
public String getStudents(){
return this.students;
}
}
public class Student{
private String cursorId;
private String cursorName;
public void setStudentId(String studentId){
this.studentId=studentId;
}
public String getStudentId(){
return this.studentId;
}
public void setStudentName(String studentName){
this.studentName=studentName;
}
public String getStudentName(){
return this.studentName;
}
}
id="teacherMap" type="Teacher">
<id column="teacher_id" jdbcType="VARCHAR" property="teacherId"/>
<result column="teacher_name" jdbcType="VARCHAR" property="teacherName"/>
"student_id" ofType="student" property="students">
<id column="student_id" jdbcType="VARCHAR" property="studentId"/>
<result column="student_name" jdbcType="VARCHAR" property="studentName">
select a.teacher_id,a.teacher_name,b.student_id,b.student_name from teacher a left join student b on a.teacher_id=b.teacher_id
以上内容没有提供具体的数据库数据和创建数据的sql请自行创建。