java中一对多查询_一对多+多对一关系的查询

比如:

一对多: 一个老师对应多个学生

多对一: 多个学生对应一个老师

一、建表+实体类

创建一个学生表和一个老师表

通过学生的tid与老师的id形成联系

SQLyog的建表代码

CREATE TABLE`teacher`(

`id`INT(10) NOT NULL,

`name`VARCHAR(30) DEFAULT NULL,PRIMARY KEY(`id`)

)ENGINE=INNODB DEFAULT CHARSET=utf8

​INSERT INTO teacher(`id`,`name`) VALUES(1,'哈哈');

​CREATE TABLE`student`(

`id`INT(10) NOT NULL,

`name`VARCHAR(30) DEFAULT NULL,

`tid`INT(10) DEFAULT NULL,PRIMARY KEY(`id`),KEY`fktid` (`tid`),CONSTRAINT `fktid` FOREIGN KEY (`tid`) REFERENCES`teacher` (`id`)

)ENGINE=INNODB DEFAULT CHARSET=utf8

​INSERT INTO student (`id`,`name`,`tid`) VALUES ('1','学生1','1');INSERT INTO student (`id`,`name`,`tid`) VALUES ('2','学生2','1');INSERT INTO student (`id`,`name`,`tid`) VALUES ('3','学生3','1');INSERT INTO student (`id`,`name`,`tid`) VALUES ('4','学生4','1');INSERT INTO student (`id`,`name`,`tid`) VALUES ('5','学生5','1');

pojo(使用了Lombok)

student:

packagecom.zy.pojo;

​importlombok.Data;

@Datapublic classStudent {

​private intid;privateString name;

​private inttid;

​privateTeacher teacher;

}

teacher

packagecom.zy.pojo;

​importlombok.Data;

​importjava.util.List;

@Datapublic classTeacher {

​private intid;privateString name;

​private Liststudents;

}

二、多对一

多个学生对应一个老师

StudentMapper

packagecom.zy.mapper;

​importcom.zy.pojo.Student;

​importjava.util.List;

​public interfaceStudentMapper {

ListgetStudentList();

ListgetStudentList2();

ListgetStudentList3();

}

StudentMapper.xml(关键点)

/p>

PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

select * from student​

​select * from student

select * from teacher where id=#{tid}​

​select s.id sid,s.name sname,s.tid stid,t.id tid,t.name tname

from student s,teacher t

where s.tid=t.id

测试

getStudentList:

java中一对多查询_一对多+多对一关系的查询_第1张图片

getStudentList2:

java中一对多查询_一对多+多对一关系的查询_第2张图片

getStudentList3:

java中一对多查询_一对多+多对一关系的查询_第3张图片

getStudentList2与getStudentList3代码不同,效果是相同的

三、一对多

一个老师对应多个学生

TeacherMapper

packagecom.zy.mapper;

​importcom.zy.pojo.Teacher;

​importjava.util.List;

​public interfaceTeacherMapper {

ListgetTeacherList();

ListgetTeacherList2();

ListgetTeacherList3();

}

TeacherMapper.xml(关键点)

/p>

PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

​select * from teacher​select * from teacher

select * from student where tid=#{id}

select t.id tid,t.name tname,s.id sid,s.name sname

from teacher t,student s

where t.id=s.tid

​​

测试

getTeacherList:

java中一对多查询_一对多+多对一关系的查询_第4张图片

getTeacherList2:

a61847335ac9ebc0efa1dbcbd59f7513.png

getTeacherList3:

3f878655c4d8b7785034541760404260.png

getTeacherList2与getTeacherList3代码不同,效果是相同的

无论是一对多还是多对一,重点都在写xml中的代码,合理利用resultMap可以写出多个表的CRUD

select

B.id as blog_id,

B.title as blog_title,

B.author_id as blog_author_id,

A.id as author_id,

A.username as author_username,

A.password as author_password,

A.email as author_email,

A.bio as author_bio,

A.favourite_section as author_favourite_section,

P.id as post_id,

P.blog_id as post_blog_id,

P.author_id as post_author_id,

P.created_on as post_created_on,

P.section as post_section,

P.subject as post_subject,

P.draft as draft,

P.body as post_body,

C.id as comment_id,

C.post_id as comment_post_id,

C.name as comment_name,

C.comment as comment_text,

T.id as tag_id,

T.name as tag_name

from Blog B

left outer join Author A on B.author_id = A.id

left outer join Post P on B.id = P.blog_id

left outer join Comment C on P.id = C.post_id

left outer join Post_Tag PT on PT.post_id = P.id

left outer join Tag T on PT.tag_id = T.id

where B.id = #{id}

你可能感兴趣的:(java中一对多查询)