- A、使用场景
当前一个选修课,很多学生报名了这个课程,当我们查询这个选修课的时候,把对应的多个学生查询出来
{
"id": 3,
"name": "php",
"code": "123323333333",
"studentList": [
{
"id": 3,
"name": "ww",
"sex": "男",
"age": 23
},
{
"id": 4,
"name": "aa",
"sex": "男",
"age": 23
}
]
}
- 2、数据库实现
drop table if EXISTS tb_stu_course;
create table tb_stu_course (
id BIGINT(20) not null auto_increment,
name VARCHAR(10) DEFAULT '',
code VARCHAR(20) DEFAULT '',
PRIMARY KEY (id)
) engine=InnoDB default charset=utf8 COMMENT '';
insert into tb_stu_course (code, name) VALUES ('1233244444', 'python');
insert into tb_stu_course (code, name) VALUES ('123326666666', 'java');
insert into tb_stu_course (code, name) VALUES ('123323333333', 'php');
-- 这个时候就不能设置stu_course_id为unique了
-- 表中的stu_course_id作为外键参照表tb_stu_course的主键id
drop table if EXISTS tb_student;
create table tb_student (
id bigint(20) not null auto_increment,
name VARCHAR(10) DEFAULT '',
sex VARCHAR(5) DEFAULT '',
age int(3) DEFAULT 0,
stu_course_id bigint(20),
PRIMARY key (id),
-- 关联一个外接,用于连表查询
foreign key (stu_course_id) REFERENCES tb_stu_course (id)
) ENGINE = INNODB DEFAULT CHARSET = utf8 COMMENT '人';
insert into tb_student (name,sex,age,stu_course_id) VALUES ('ll', '男', 23, 1);
insert into tb_student (name,sex,age,stu_course_id) VALUES ('jj', '男', 23, 2);
insert into tb_student (name,sex,age,stu_course_id) VALUES ('ww', '男', 23, 3);
insert into tb_student (name,sex,age,stu_course_id) VALUES ('aa', '男', 23, 3);
insert into tb_student (name,sex,age,stu_course_id) VALUES ('aa', '男', 23, 2);
insert into tb_student (name,sex,age,stu_course_id) VALUES ('aa', '男', 23, 1);
- 3、modle实现
@Data
public class Student {
private BigInteger id;
private String name;
private String sex;
private Integer age;
}
@Data
public class StuCourse {
private BigInteger id;
private String name;
private String code;
private List<Student> studentList;
}
- 4、xml实现
// getCourse查询对应id课程的时候,resultMap的type是stuCourse
<!-- 一对多 开始-->
<select id="getStudent" parameterType="string" resultType="student">
select * from tb_student where stu_course_id = #{id}
</select>
<select id="getCourse" parameterType="string" resultMap="courseMap">
select * from tb_stu_course where id = #{id}
</select>
// property对应于stuCourse中的studentList
// column来自于数据库中的课程的id
// ofType集合中的类型
// javaType对应stuCourse属性中对应的studentList类型
// select调用查询的方法
// 这里student测试有不同的属性和数据字段不匹配的时候,转不过来
// fetchType="lazy"需要用到studentList这个属性的时候,才会发送sql去查询学生。使用了好像报错,需要在配置中增加东西
<resultMap id="courseMap" type="stuCourse">
<id property="id" column="id"/>
<collection
property="studentList"
column="id"
ofType="student"
javaType="list"
fetchType="lazy"
select="getStudent">
</collection>
</resultMap>
<!-- 一对多 结束-->
在配置中增加的东西。好像还会出错,待解除。。。。。。。
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
- 5、方法调用
@RequestMapping("/cor/{id}")
public StuCourse getCourse(@PathVariable("id") String id){
return relateService.getCourse(id);
}
- B、使用场景 多表联合查询
<!-- 多表联合查询-->
<select id="getNewStudent" parameterType="string" resultMap="newStuMap">
select * from tb_stu_course c, tb_student s
where c.id = s.stu_course_id
and s.id = #{id}
</select>
<resultMap id="newStuMap" type="student">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="sex" column="sex"/>
<result property="age" column="age"/>
<association
property="stuCourse"
javaType="stuCourse">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="code" column="code"/>
</association>
</resultMap>