Mybatis中通过<association>来配置返回单个对象的关联查询,<collection>配置返回多个对象的关联查询。
关联查询的触发,必须是返回类型指定为resultMap
关联查询的要点,步骤,按着这四个步骤一步一步写,基本上没错;
1)实体类中建立引用关联 (Student类中添加HClass 属性)
2)指定查询返回结果为ResultMap
3)在指定的ResultMap配置关联查询标签。单个对象==>association,集合==>collection
4)编写关联的查询接口方法和sql
①一对一:通过查询学生信息关联来查到学生详细信息;
Student2.java页面 //一对一:通过查询学生信息关联来查到学生详细信息 private StudentInFo studentInFo;
StudemtMapper2.java页面 //通过查询学生信息关联来查到学生详细信息 Student2 selectByIdss(Integer id);
StudentMapper2.xml页面
<resultMap id="StudentResult" type="Student2">
<id column="id" property="id"></id>
<result property="studentName" column="name"></result>
<result property="classid" column="classid"></result>
<result property="version" column="version"></result>
<association property="studentInFo" javaType="StudentInFo" column="id"
select="com.seecen.mybatis3.mapper.StudentInFoMapper.selectById"></association>
</resultMap>
<select id="selectByIdss" resultMap="StudentResult">
select * from h_student where id=#{id}
</select>
StudentInFoMapper.java页面 @Select("select * from h_student_info where id=#{id}") StudentInFo selectById(Integer id);
测试代码文件: System.out.println("============测试查询学生信息时把学生详细信息查出============="); Student2 student23 = mapper.selectByIdss(2); System.out.println(student23); System.out.println(student23.getStudentInFo());
②一对多:查询班级时把班级下面所有的学生查询出来;
HClass.java文件: //一对多:查询班级的时候把班级下的所有学生查询出来 private List<Student2> student2s;
HClassMapper.java文件 //查询班级的时候把班级下的所有学生查询出来 HClass selectWithStudent(Integer id);
HClassMapper.xml文件: <resultMap id="studentResult" type="HClass"> <id column="id" property="id"></id> <result property="name" column="name"></result> <result property="city" column="city"></result> <collection property="student2s" column="id" ofType="Student2" select="com.seecen.mybatis3.mapper.StudentMapper2.selectByClassId" ></collection> </resultMap> <select id="selectWithStudent" resultMap="studentResult"> select * from h_class where id=#{id} </select>
StuentMapper2.java文件: //查询班级的时候把学生信息查询出来 List<Student2> selectByClassId(Integer id);
StudentMapper.xml文件: <select id="selectByClassId" resultType="Student2"> select t2.id,t2.name as studentName,t2.classid,t2.version from h_class t1,h_student t2 where t1.id=t2.classid and t1.id=#{id} </select>
测试代码文件: System.out.println("==========查询班级的时候把对应的学生查询出来=========="); HClass hClass1=mapper.selectWithStudent(21); System.out.println(hClass1); System.out.println(hClass1.getStudent2s());
③多对多:一个班级有多个老师,一个老师教多个班级;
HClass.java文件: //多对多 // 1 hclass 写一个查询(resultMap ) // 2 在resultMap配置 关联查询 // 3 teacherMapper写一个根据班级ID查询老师信息的方法 private List<Teacher> teachers;
HClassMapper.java文件: //查询班级信息并关联查询老师信息的 HClass selectWithTeacher(Integer id);
HClassMapper.xml文件: <resultMap id="classResult" type="HClass"> <id column="id" property="id"></id> <result property="name" column="name"></result> <result property="city" column="city"></result> <!-- 关联查询,返回集合--> <!-- ofType:指定集合中的泛型类型 --> <collection property="teachers" column="id" ofType="Teacher" select="com.seecen.mybatis3.mapper.TeacherMapper.selectByClassId" ></collection> </resultMap> <select id="selectWithTeacher" resultMap="classResult"> select * from h_class where id=#{id} </select>
TeacherMapper.java文件: //班级信息查询老师信息 List<Teacher> selectByClassId(Integer id);
TeacherMapper.xml文件: <select id="selectByClassId" resultType="Teacher"> select t1.* from h_teacher t1,h_class_teacher t2 where t1.id=t2.teacherid and t2.classid=#{classid} </select>
测试代码文件: System.out.println("=======查询班级的时候把对应的老师查询出来========="); HClass hClass = mapper.selectWithTeacher(21); System.out.println(hClass); System.out.println(hClass.getTeachers());
④多对一:多个学生对应一个班级;
在Student2.java页面
//多对一 组合/继承 private HClass hClass;
在StudentMapper2.java页面
Student2 selectByIdMappingWithResultMap(Integer id);
在Student2Mapper.xml页面
<!--定义结果集映射,通过该映射来指定表的列与类的属性的映射关系-->
<!--id:唯一表示; type:关联到实体类-->
<resultMap id="studentResultMap" type="Student2">
<!--id:配置主键与实体类中属性的映射关系-->
<id column="ID" property="id" jdbcType="DECIMAL"></id>
<!--result:配置非主键列-->
<result column="name" property="studentName"></result>
<result column="classid" property="classid"></result>
<result column="version" property="version"></result>
<!--配置关联查询,返回单个结果
javaType:指定返回的java类型 select:指定关联的查询(关联到另外一个Mapper的查询方法)
column:指定要传递的列(会将该列的值,当做参数传递到关联的查询方法中)
-->
<association property="hClass" javaType="HClass" column="classid"
select="com.seecen.mybatis3.mapper.HClassMapper.selectById"></association>
</resultMap>
<!--resultMap:指定返回的结果集映射的ID值-->
<select id="selectByIdMappingWithResultMap" resultMap="studentResultMap">
select * from h_student where id=#{id}
</select>
HClassMapper.java文件:
HClass selectById(Integer id);
HClassMapper.xml文件: <select id="selectById" parameterType="integer" resultType="HClass"> select * from h_class where id=#{id} </select>
测试代码文件: System.out.println("===========测试关联查询=============="); Student2 student22 = mapper.selectByIdMappingWithResultMap(6); System.out.println(student22); System.out.println(student22.gethClass());