美文网首页工作生活
MYSQL的一对多和多对一

MYSQL的一对多和多对一

作者: CoderLJW | 来源:发表于2019-07-02 14:46 被阅读0次
  • 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>

相关文章

  • MYSQL的一对多和多对一

    A、使用场景当前一个选修课,很多学生报名了这个课程,当我们查询这个选修课的时候,把对应的多个学生查询出来 2、数据...

  • MySQL一对一:一对多:多对多: 实例

    [MySQL一对一:一对多:多对多: 实例!!!!] 学生表和课程表可以多对多 一个学生可以学多门课程 一门课程可...

  • 02MySQL的多表操作

    MySQL的多表操作 1 多表关系 MySQL多表之间的关系可以概括为:一对一、一对多/多对一关系,多对多 1.1...

  • MySQL 一对多查询

    导语 这次要实现的是一对多查询,使用 MySQL 的 group_concat 函数实现。 group_conca...

  • MySql多表关系

    layout: posttitle: MySql多表关系subtitle: 一对一,一对多,...

  • MyBatis一对多和多对一

    在学习MyBatis3的过程中,文档上面一直在强调一个id的东西!在做这个实验的时候,也因为没有理解清楚id含义而...

  • 关于一对多和多对一

    声明:所有文章只作为学习笔记用,转载非原创 大概可以这样类比:程序集好比一个人定居的国度,命名空间好比这个人的国籍...

  • Flask 中的一对多和多对多

    1.ForeignKey flsk中的一对多 在多的一方需要写一个字段s_g 在其中写上db.ForeignKey...

  • 八:关联关系

    表的关系:MySQL相互关联的表之间存在一对一,一对多(多对一),多对多的关系 1.一对一的关系:表1中的一条数据...

  • MyBatis一对一,一对多,多对一,多对多

    一对一使用associate,一个类根据关联字段对应着一个类,实体类里声明另一个实体类 一对多使用collecti...

网友评论

    本文标题:MYSQL的一对多和多对一

    本文链接:https://www.haomeiwen.com/subject/ihczcctx.html