多对一查询:
表结构:
CREATE TABLE `teacher` (
`id` INT(10) NOT NULL,
`name` VARCHAR(30) DEFAULT NULL,
PRIMARY KEY (`id`)
)#教师表
CREATE TABLE `student` (
`id` INT(10) NOT NULL,
`name` VARCHAR(30) DEFAULT NULL,
`tid` INT(10) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `fktid` (`tid`),
CONSTRAINT `fktid` FOREIGN KEY (`tid`) REFERENCES `teacher` (`id`)
)#学生表
实体类:
public class Student {
private int id;
private String name;
private Teacher teacher;
}
public class Teacher {
private int id;
private String name;
}
查询需求:(学生id,学生姓名,教师id,教师姓名)
sql语句:
select *from`student`, `teacher`where`teacher`.`id`=`student`.`tid`;
在mybatis中的接口:
public interface StudentMapper {
public List<Student> getStudent1();
public List<Student> getStudent2();
public List<Student> getStudent3();
}
编写maper文件:
<select id="getStudent1" resultMap="StudentTeacher1">
select * from student;
</select>
<resultMap id="StudentTeacher1" type="Student">
<result property="id" column="id"></result>
<result property="name" column="name"/>
<association property="teacher" column="tid" javaType="Teacher" select="getTeacherName"></association>
</resultMap>
<select id="getTeacherName" resultType="Teacher">
select *from teacher where id=#{id};
</select>
<select id="getStudent2" resultMap="StudentTeacher2">
select s.id sid,s.name sname,t.id tid,t.name tname
from student s,teacher t
where s.tid=t.id;
</select>
<resultMap id="StudentTeacher2" type="Student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<association property="teacher" javaType="Teacher">
<result property="name" column="tname"/>
<result property="id" column="tid"/>
</association>
</resultMap>
<select id="getStudent3" resultMap="StudentTeacher3">
select s.*,t.id tid,t.name tname
from student s,teacher t
where s.tid=t.id;
</select>
<resultMap id="StudentTeacher3" type="Student">
<result property="teacher.id" column="tid"/>
<result property="teacher.name" column="tname"/>
</resultMap>
一对多查询:
实体类:
public class Student {
private int id;
private String name;
private int tid;
}
public class Teacher {
private int id;
private String name;
private List<Student> studentList;
}
接口:
public interface TeacherMapper {
Teacher getTeacherById1(@Param("tid") int id);
Teacher getTeacherById2(@Param("tid") int id);
}
xml配置:
<select id="getTeacherById1" resultMap="TeacherStudent1">
select s.id sid,s.name sname,t.id tid,t.name tname
from student s,teacher t
where s.tid=t.id and t.id=#{tid};
</select>
<resultMap id="TeacherStudent1" type="Teacher">
<result property="id" column="tid"/>
<result property="name" column="tname"/>
<collection property="studentList" ofType="Student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<result property="tid" column="tid"/>
</collection>
</resultMap>
<select id="getTeacherById2" resultMap="TeacherStudent2">
select * from teacher where id=#{tid};
</select>
<resultMap id="TeacherStudent2" type="Teacher">
<collection property="studentList" javaType="ArrayList" ofType="Student" select="getStudentByTeacherId" column="id"/>
</resultMap>
<select id="getStudentByTeacherId" resultType="Student">
select * from student where tid = #{id};
</select>