今天就跟大家聊聊有关Mybatis一对多和多对一处理的区别是什么,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
SQL:
create table teacher(
id int not null,
name varchar(30) default null,
primary key (id)
);
insert into teacher (id, name) values (1, '蔡老师');
create table student(
id int not null ,
name varchar(30) default null,
tid int default null,
constraint fk_tid foreign key (tid) references teacher(id)
);
insert into student(id, name, tid) VALUES (1, '小名', 1);
insert into student(id, name, tid) VALUES (2, '小红', 1);
insert into student(id, name, tid) VALUES (3, '小亮', 1);
insert into student(id, name, tid) VALUES (4, '小兰', 1);
insert into student(id, name, tid) VALUES (5, '笑笑', 1);
多个学生对应一个老师
对于学生这边而言,关联。即多个学生关联一个老师【多对一】
对于老师这边而言,集合。即一个老师有很多的学生【一对多】
//查询所有的学生信息以及对应的老师的信息
List<Student> queryStudentAndTeacher();
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
private int id;
private String name;
//学生需要关联一个老师
private Teacher teacher;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Teacher {
private int id;
private String name;
}
<!--思路:
1.查询所有的学生
2.根据查询出来的学生的tid寻找对应的老师 寻找对应的老师,子查询
-->
<resultMap id="rStuAndTea" type="student">
<result property="id" column="id"/>
<result property="name" column="name"/>
<!-- 复杂的属性我们需要单独处理
指定属性的类型
对象使用association javaType
集合使用collection ofType
-->
<association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/>
</resultMap>
<select id="queryStudentAndTeacher" resultMap="rStuAndTea">
select * from student
</select>
<select id="getTeacher" resultType="teacher">
select * from teacher where id = #{id}
</select>
<!--方式二 按照结果嵌套处理-->
<resultMap id="rStuAndTea2" type="student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<association property="teacher" javaType="Teacher">
<result property="name" column="tname"/>
</association>
</resultMap>
<select id="queryStudentAndTeacher2" resultMap="rStuAndTea2">
select s.id sid, s.name sname, t.name tname
from student s, teacher t
where s.tid = t.id
</select>
子查询
联表查询
一个老师有多个学生
对于老师这边而言,集合。即一个老师有很多的学生【一对多】
//查询指定老师的信息及其所有的学生
Teacher queryTeaAndStu(@Param("tid") int id);
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Teacher {
private int id;
private String name;
//一个老师拥有多个学生
private List<Student> students;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
private int id;
private String name;
private int tid;
}
<!--按照结果嵌套查询-->
<resultMap id="rTeaAndStu" type="teacher">
<result property="id" column="tid"/>
<result property="name" column="tname"/>
<collection property="students" ofType="Student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<result property="tid" column="tid"/>
</collection>
</resultMap>
<select id="queryTeaAndStu" resultMap="rTeaAndStu">
select s.id sid, s.name sname, t.name tname, t.id tid
from student s, teacher t
where s.tid = t.id and t.id = #{tid}
</select>
<!--按照查询嵌套处理-->
<select id="queryTeaAndStu2" resultMap="rTeaAndStu2">
select * from teacher where id = #{tid}
</select>
<resultMap id="rTeaAndStu2" type="teacher">
<collection property="students" javaType="ArrayList" ofType="Student"
select="queryStudentByTeacherId" column="id"/>
</resultMap>
<select id="queryStudentByTeacherId" resultType="Student">
select * from student where tid = #{tid}
</select>
小结
关联 - association 【多对一】
集合 - collection 【一对多】
javaType & ofType
javaType 用来指定实体类中属性的类型
ofType 用来指定映射到List或者集合中的entity类型,泛型中的约束类型
注意点:
保证SQL的可读性,尽量保证通俗易懂
注意一对多和多对一中属性名和字段的问题
如果问题不好排查错误,可以使用LOG4J日志
看完上述内容,你们对Mybatis一对多和多对一处理的区别是什么有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。