Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the difference between Mybatis one-to-many and many-to-one processing

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Today, I would like to talk to you about the difference between Mybatis one-to-many and many-to-one handling. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can gain something according to this article.

Build a table

SQL:

Create table teacher (id int not null, name varchar (30) default null, primary key (id)); insert into teacher (id, name) values (1, 'Miss Cai'); 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, 'nickname', 1) Insert into student (id, name, tid) VALUES (2, 'Xiao Hong', 1); insert into student (id, name, tid) VALUES (3, 'Xiao Liang', 1); insert into student (id, name, tid) VALUES (4, 'Xiaolan', 1); insert into student (id, name, tid) VALUES (5, 'Xiaoxiao', 1); many-to-one processing

Multiple students correspond to one teacher.

For the students' side, the connection. That is, multiple students are associated with a teacher [many to one]

For the teacher's side, gather around. That is, a teacher has a lot of students [one to many]

Mapper// queries all student information and corresponding teacher information List queryStudentAndTeacher (); entity class @ Data@AllArgsConstructor@NoArgsConstructorpublic class Student {private int id; private String name; / / students need to associate a teacher private Teacher teacher;} @ Data@AllArgsConstructor@NoArgsConstructorpublic class Teacher {private int id; private String name } select * from student select * from teacher where id = # {id} according to query nesting processing select s.id sid, s.name sname, t.name tname from student s, teacher t where s.tid = t.id Review Mysql many-to-one query mode

Subquery

Join table query

One-to-many processing

A teacher has multiple students.

For the teacher's side, gather around. That is, a teacher has a lot of students [one to many]

Mapper// queries the designated teacher's information and all its students Teacher queryTeaAndStu (@ Param ("tid") int id); entity class @ Data@AllArgsConstructor@NoArgsConstructorpublic class Teacher {private int id; private String name; / / A teacher has multiple students private List students;} @ Data@AllArgsConstructor@NoArgsConstructorpublic class Student {private int id; private String name; private int tid } handle 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} according to query nesting processing select * from teacher where id = # {tid} select * from student where tid = # {tid} result mapping

Summary

Association-association [many to one]

Collection-collection [one-to-many]

JavaType & ofType

JavaType is used to specify the type of attribute in the entity class

OfType is used to specify entity types that map to List or collections, and constraint types in generics.

Note:

Ensure the readability of SQL and make it as easy to understand as possible

Note the problem of attribute names and fields in one-to-many and many-to-one

If the problem is difficult to troubleshoot, you can use the LOG4J log

After reading the above, do you have any further understanding of the difference between Mybatis one-to-many and many-to-one processing? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report