mysql一对多_MYSQL的一对多和多对一

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 studentList;

}

4、xml实现

// getCourse查询对应id课程的时候,resultMap的type是stuCourse

select * from tb_student where stu_course_id = #{id}

select * from tb_stu_course where id = #{id}

// property对应于stuCourse中的studentList

// column来自于数据库中的课程的id

// ofType集合中的类型

// javaType对应stuCourse属性中对应的studentList类型

// select调用查询的方法

// 这里student测试有不同的属性和数据字段不匹配的时候,转不过来

// fetchType="lazy"需要用到studentList这个属性的时候,才会发送sql去查询学生。使用了好像报错,需要在配置中增加东西

property="studentList"

column="id"

ofType="student"

javaType="list"

fetchType="lazy"

select="getStudent">

在配置中增加的东西。好像还会出错,待解除。。。。。。。

5、方法调用

@RequestMapping("/cor/{id}")

public StuCourse getCourse(@PathVariable("id") String id){

return relateService.getCourse(id);

}

B、使用场景 多表联合查询

select * from tb_stu_course c, tb_student s

where c.id = s.stu_course_id

and s.id = #{id}

property="stuCourse"

javaType="stuCourse">

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值