The main points of
- It’s mainly a result set map.
- Association tag: association of a complex type; Many results will be wrapped as nested result maps of this type (JavaBean), and associations can be resultMap elements or references to other result maps
- Collection tag: a List of complex types nested result maps. A collection can be a resultMap element or a reference to another resultMap
One-to-many (association)
- Database structure
Tid is the foreign key of student, which is the ID of teacher table
- JavaBean
public class Student {
private int id;
private String name;
private Teacher teacher;
}
public class Teacher {
private int id;
private String name;
}
Copy the code
- mapper.java
public interface StudentMapper {
List<Student> getStudent(a);
List<Student> getStudent2(a);
}
Copy the code
- There is a teacher in the Student class, and to find out the teacher in the Student you need a mapping.
There are two ways to do this: (1)
<select id="getStudent" resultMap="StudentTeacher">
select * from mybatis.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>
<! -- subquery -->
<select id="getTeacher" resultType="Teacher">
select * from mybatis.teacher where id=#{Anything}
</select>
Copy the code
(2)
<select id="getStudent2" resultMap="StudentTeacher2">
select * from mybatis.student as s ,mybatis.teacher as t where s.tid=t.id
</select>
<resultMap id="StudentTeacher2" type="Student">
<result property="id" column="id"/>
<result property="name" column="name"/>
<association property="teacher" javaType="Teacher">
<result property="id" column="tid"/>
<result property="name" column="name"/>
</association>
</resultMap>
Copy the code
For one more
- JavaBean
public class Teacher2 {
private int id;
private String name;
// One teacher has multiple students
private List<Student2> students;
}
public class Student2 {
private int id;
private String name;
private int tid;
}
Copy the code
- mapper.java
public interface TeacherMapper2 {
List<Teacher2> getTeacher(@Param("id") int id);
}
Copy the code
- mapper.xml
<! DOCTYPEmapper
PUBLIC "- / / mybatis.org//DTD Mapper / 3.0 / EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mybatis.DAO.TeacherMapper2">
<select id="getTeacher" parameterType="int" resultMap="teacherS">
select s.id, s.name, s.tid,t.id as tid, t.name as tname from mybatis.student as s ,mybatis.teacher as t where s.tid=t.id and t.id=#{id}
</select>
<resultMap id="teacherS" type="teacher2">
<result property="id" column="tid"/>
<result property="name" column="tname"/>
<collection property="students" ofType="student2">
<result property="id" column="id"/>
<result property="name" column="name"/>
<result property="tid" column="tid"/>
</collection>
</resultMap>
</mapper>
Copy the code
summary
- There’s not much difference between one-to-many and many-to-one
- It’s the difference between an association and a collection
- There is also a difference between ofType and javaType
- If the query result does not meet expectations, set an alias and try it