场景:学生表,和成绩表,一个学生可以有多个成绩,所以建立一对多的关联
学生表(student)
2017001 李晓明0 1001 1998-02-05 MAN
2017002 李晓明1 1001 1998-02-05 MAN
两条数据
成绩表4条数据
2017001 b101 98.0
2017001 b102 110.0
2017001 b103 100.0
2017002 b101 95.0
学生表
private int sno; //学号
private String sname; //姓名
private String cno; //班号
private String birthday; //出生日期
private UserSexEnum sex; //性别
实体成绩表
private int sno;
private String bno;
private Double score;
学生成绩类
private Student student;
private List<SeleCourse> selecourses;
get和set方法略去了
map配置如下
<mapper namespace="com.neo.mapper.StuCourseMapper" >
<resultMap id="CategoryResult" type="com.neo.entity.StuCourse">
<association property="student" javaType="com.neo.entity.Student">
<result property="sno" column="sno"></result>
<result property="sname" column="sname"></result>
<result property="cno" column="cno"></result>
<result property="birthday" column="birthday"></result>
<result property="sex" column="sex"></result>
</association>
<collection property="selecourses" ofType="com.neo.entity.SeleCourse">
<result property="sno" column="sno"></result>
<result property="bno" column="bno"></result>
<result property="score" column="score"></result>
</collection>
</resultMap><select id="getAll" parameterType="java.lang.Integer" resultMap="CategoryResult">
select sc.sno, sname, sex, bno,score from selecourse sc, student s where s.sno = sc.sno
</select>
但是查询结果是四列,不是期望的两列
==> Preparing: select sc.sno, sname, sex, bno,score from selecourse sc, student s where s.sno = sc.sno
==> Parameters:
<== Total: 4
结果集
[{"student":{"sno":2017001,"sname":"李晓明0","cno":null,"birthday":null,"sex":"MAN"},"selecourses":[{"sno":2017001,"bno":"b101","score":98.0}]},{"student":{"sno":2017001,"sname":"李晓明0","cno":null,"birthday":null,"sex":"MAN"},"selecourses":[{"sno":2017001,"bno":"b102","score":110.0}]},{"student":{"sno":2017001,"sname":"李晓明0","cno":null,"birthday":null,"sex":"MAN"},"selecourses":[{"sno":2017001,"bno":"b103","score":100.0}]},{"student":{"sno":2017002,"sname":"李晓明1","cno":null,"birthday":null,"sex":"MAN"},"selecourses":[{"sno":2017002,"bno":"b101","score":95.0}]}]
期望的结果是成绩表是两个对象
有可能吗?