Let me first talk about how I manipulate mybatis’s multi table associated queries.
I did not nested queries in SQL statements, and multiple table queries were all implemented in code through single table queries.
For example, I usually write this in the Controller layer
DomainA a = serviceA.find(1); DomainB b = serviceB.find(a.getBid()); modelMap.put("a",a); modelMap.put("b",b);
Is it the right way to write this?
There may be ambiguity in my original paragraph.
Should I write multiple table associations in SQL? If I do, the data returned can not be wrapped by object wrapped on Model level.
Amend it into:
Should I write multi table associations in SQL? The result set is a result set of different tables and different fields. The Model of a single table is already unable to satisfy.
Some of the answers can be that a new Model contains two tables, and if I cross the 4,5 table, can you build a new class that has a very large number of fields for a query (a member variable may be more than 200)?
One more question, is ModelMap written on the Service layer
You may forget that mybatis’s XML file can write ResultMap.
After multiple table queries, the results are mapped to the ResultMap
If two tables are in a library, since mybatis is used, join is allowed if data volume is allowed.
Use subqueries or join, and then build a new dto object to wrap up the attributes needed in DomainA and DomainB, and add new properties as the service increases.
In SQL, multi table query, Model class will return data as long as there are corresponding fields.