1 n+1
1 在teacher中添加List student,
public class Teacher {
private int id;
private String name;
private List<Student> list;
2 在studentMapper.xml中添加通过tid查询
<select id="selByTid" resultType="Student" parameterType="int">
select * from student where tid=#{0}
</select>
3 在TeacherMapper.xml中添加查询全部
<resultMap type="Teacher" id="mymap1">
<id column="id" property="id"/>
<result column="name" property="name"/>
<collection property="list" ofType="Student" select="com.bjsxt.mapper.StudentMapper.selByTid" column="id"></collection>
</resultMap>
<select id="selAll" resultMap="mymap1">
select * from teacher
</select>
其中collection是当属性为集合类型时使用的标签
2 多表联合
<resultMap type="Teacher" id="stumap1">
<id column="tid" property="id"/>
<result column="tname" property="name"/>
<collection property="list" ofType="Student">
<id column="sid" property="id"/>
<result column="sname" property="name"/>
<result column="age" property="age"/>
<result column="tid" property="tid"/>
<association property="teacher" select="com.bjsxt.mapper.TeacherMapper.selById" column="tid"></association>
</collection>
</resultMap>
<select id="selAll1" resultMap="stumap1">
select t.id tid,t.name tname,s.id sid,s.name sname,age,tid from teacher t left join student s on t.id=s.tid
</select>