实体类
//Teacher的pojo类
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Teacher {
private int id;
private String name;
}
//Student的pojo类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
private int id;
private String name;
private Teacher teacher;
}
配置文件
<typeAliases>
<typeAlias alias="Teacher" type="com.chao.pojo.Teacher"/>
<typeAlias alias="Student" type="com.chao.pojo.Student"/>
</typeAliases>
<mappers>
<mapper resource="StudentMapper.xml"/>
<mapper resource="TeacherMapper.xml"/>
</mappers>
Mapper.xml
按照查询嵌套处理
<mapper namespace="com.chao.mapper.TeacherMapper">
<select id="getTeacher" resultType="Teacher">
select * from ts.teacher
</select>
</mapper>
<mapper namespace="com.chao.mapper.StudentMapper">
<select id="getStudent" resultMap="StudentTeacher">
select * from ts.student ;
</select>
<resultMap id="StudentTeacher" type="Student">
<result property="id" column="id"/>
<result property="name" column="name"/>
<association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/>
</resultMap>
<select id="getTeacher" resultType="Teacher">
select * from ts.teacher where id = #{id}
</select>
</mapper>
按照结果嵌套处理
<mapper namespace="com.chao.mapper.StudentMapper">
<select id="getStudent" resultMap="StudentTeacher">
select s.id sid, s.name sname, t.name tname ,t.id teid
from ts.student s,ts.teacher t
where s.tid = t.id;
</select>
<resultMap id="StudentTeacher" 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="teid"/>
</association>
</resultMap>
</mapper>
测试
@Test
public void getTeacher(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
List<Student> studentList = mapper.getStudent();
for (Student student : studentList){
System.out.println(student);
}
sqlSession.close();
}
实体类
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Teacher {
private int id;
private String name;
private List<Student> students;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
private int id;
private String name;
private int tid;
}
配置文件
<typeAliases>
<typeAlias alias="Teacher" type="com.chao.pojo.Teacher"/>
<typeAlias alias="Student" type="com.chao.pojo.Student"/>
</typeAliases>
<mappers>
<mapper resource="StudentMapper.xml"/>
<mapper resource="TeacherMapper.xml"/>
</mappers>
Mapper.xml
<mapper namespace="com.chao.mapper.TeacherMapper">
<select id="getTeacher" resultMap="TeacherS">
select t.id tid, t.name tname,s.name sname, s.id sid
from ts.teacher t,ts.student s
where t.id = s.tid and t.id = #{tid}
</select>
<resultMap id="TeacherS" type="Teacher">
<result property="name" column="tname"/>
<result property="id" column="tid"/>
<collection property="students" ofType="Student">
<result property="name" column="sname"/>
<result property="id" column="sid"/>
</collection>
</resultMap>
</mapper>
测试
@Test
public void getTeacher(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
Teacher teacher = mapper.getTeacher(1);
System.out.println(teacher);
sqlSession.close();
}
原文:https://www.cnblogs.com/chaostudy/p/12984691.html