上一章说了多对一,很多学生被一个老师教,这一章是一个老师教很多学生

目录基本没有变化只是改了配置文件:

Mybatis入门(六)联查之一对多-LMLPHP

2、配置文件:

TeacherMapper接口类:

package com.hdlf.dao;

import com.hdlf.pojo.student;
import com.hdlf.pojo.teacher; import java.util.List; public interface TeacherMapper {
//方式一
teacher getteacher(int tid);
//方式二
teacher getteacher2(int tid); }

teacher实体类:

package com.hdlf.pojo;

import lombok.Data;

import java.util.List;

public class teacher {
private int tid;
private String tname;
//因为需要获取到学生所以在这里加一个List泛型集合
List<student> students; public List<student> getStudents() {
return students;
} public void setStudents(List<student> students) {
this.students = students;
} public teacher() {
} public teacher(int tid, String tname, List<student> students) {
this.tid = tid;
this.tname = tname;
this.students = students;
} public int getTid() {
return tid;
} public void setTid(int tid) {
this.tid = tid;
} public String getTname() {
return tname;
} public void setTname(String tname) {
this.tname = tname;
} @Override
public String toString() {
return "teacher{" +
"tid=" + tid +
", tname='" + tname + '\'' +
", students=" + students +
'}';
}
}

student实体类因为是根据老师查教过多少学生,学生类不用写特别的类:

package com.hdlf.pojo;

public class student {
private int sid;
private String sname;
private int tid; @Override
public String toString() {
return "student{" +
"sid=" + sid +
", sname='" + sname + '\'' +
", tid=" + tid +
'}';
} public int getSid() {
return sid;
} public void setSid(int sid) {
this.sid = sid;
} public String getSname() {
return sname;
} public void setSname(String sname) {
this.sname = sname;
} public int getTid() {
return tid;
} public void setTid(int tid) {
this.tid = tid;
} public student(int sid, String sname, int tid) {
this.sid = sid;
this.sname = sname;
this.tid = tid;
} public student() {
}
}

TeacherMapper.xml配置类:

这个是方式一:

复杂的属性,需要单独处理 对象使用:association

集合使用:collection

javaType 指定属性类型,集合中泛型信息使用ofType获取

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hdlf.dao.TeacherMapper"> <select id="getteacher" resultMap="getstudent">
select t.tid, t.tname ,s.sname,s.sid from student s,teacher t where s.tid = t.tid and t.tid = #{tid};
</select> <resultMap id="getstudent" type="teacher">
<result property="tid" column="tid"></result>
<result property="tname" column="tname"></result> <!--复杂的属性,需要单独处理 对象:association 集合:collection
javaType 指定属性类型
集合中泛型信息使用ofType获取
这个studens就是teacher实体类中的students-->
<collection property="students" ofType="student">
<result property="sid" column="sid"></result>
<result property="sname" column="sname"></result>
</collection> </resultMap>
</mapper>

测试结果:

teacher{tid=0, tname='王老师', students=[student{sid=1, sname='张三', tid=1}, student{sid=2, sname='李四', tid=1}, student{sid=3, sname='王五', tid=1}, student{sid=4, sname='吴六', tid=1}, student{sid=5, sname='赵七', tid=1}]}

方式二个人理解其实都一样:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hdlf.dao.TeacherMapper">
<select id="getteacher2" resultMap="getallteacher">
select * from mybatis.teacher where tid = #{tid}
</select>
<resultMap id="getallteacher" type="teacher">
<collection property="students" select="getallstudent" javaType="ArrayList" ofType="studnet" column="tid"></collection>
</resultMap>
<select id="getallstudent" resultType="student">
select * from mybatis.student;
</select> </mapper>

这个就是一对多的处理,小编还是没有领悟到其中的精髓,希望过几天可以顿悟。

05-18 02:53