In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces SpringBoot how to achieve multi-table query function, the article introduces in great detail, has a certain reference value, interested friends must read it!
Entity class:
Emp class:
@ Data@NoArgsConstructor@AllArgsConstructorpublic class Emp {private int id; private String lastname; private String email; private int gender; private int did; private Dept dept; private Date birth = new Date ();}
Dept class:
@ Data@AllArgsConstructor@NoArgsConstructorpublic class Dept {private int id; private String dname;}
Mapper interface
EmpMapper:
/ / this annotation indicates that this is a mybatis mapper class @ Mapper@Repositorypublic interface EmpMapper {void addEmp (Emp emp); void deleteEmp (int id); void updateEmp (Emp emp); Emp queryEmpById (int id); List queryEmpList ();}
DeptMapper:
Mapper@Repositorypublic interface DeptMapper {List queryDeptList (@ Param ("cid") int cid);}
EmpMapper.xml profile
Select * from emp_dept.employees
DeptMapper.xml profile
Select * from emp_dept.department where id = # {id}
Front-end page (part)
Edit deletion
Query results:
The above we use step-by-step query.
Let's use association nested mapping
In fact, a strange thing will appear in the query here.
Because we now have the id field in both tables, when we use to find the department's id when mapping, we find that it is the employee's id, because the employee's id field name is the same as the department's id field name.
EmpMapper:
Select emp.*,dept.* from emp_dept.employees emp,emp_dept.department dept where emp.did = dept.id
The results of the above implementation are as follows
We found that it was the employee id, and we now change the id of the department table in the database to pid
Modify configuration files and entity classes
@ Data@AllArgsConstructor@NoArgsConstructorpublic class Dept {private int pid; private String dname;} select emp.*,dept.* from emp_dept.employees emp,emp_dept.department dept where emp.did = dept.pid
Execution result:
We found that it was the department's id that we were looking for.
However, in order to solve the above problem, we changed the department's id field name to did (did also exists in the employee table). At this time, we go to query and find that we can still query the department id.
This question is outrageous, is it because id is the primary key, did is not the primary key? It's outrageous.
The above is all the contents of the article "how to achieve multi-table query function in SpringBoot". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.