Student 实体类
package com.tanghong.pojo;
import lombok.Data;
@Data
public class Student {
private int id;
private String name;
private Teacher teacher;
}
package com.tanghong.dao;
import com.tanghong.pojo.Student;
import java.util.List;
public interface StudentMapper {
List<Student> getStudent();
List<Student> getStudent2();
}
Mapper.XML
子查询
<select id="getStudent" resultMap="StudentTeacher">
select * from student
</select>
<resultMap id="StudentTeacher" type="com.tanghong.pojo.Student">
<result property="id" column="id"/>
<result property="name" column="name"/>
<association property="teacher" column="tid" javaType="com.tanghong.pojo.Teacher" select="getTeacher"/>
</resultMap>
<select id="getTeacher" resultType="com.tanghong.pojo.Teacher">
select * from teacher where id= #{id}
</select>
联表查询
<select id="getStudent2" resultMap="StudentTeacher2">
select s.id sid,s.name sname,t.name tname
from student s,teacher t
where s.TID=t.id
</select>
<!--复杂的属性,我们需要单独处理 对象:association 集合:collection
javaType="" 指定属性的类型!
集合中的泛型信息 我们使用ofType获取
-->
<resultMap id="StudentTeacher2" type="com.tanghong.pojo.Student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<association property="teacher" javaType="com.tanghong.pojo.Teacher">
<result property="name" column="tname"/>
</association>
</resultMap>