Article From:https://segmentfault.com/q/1010000012149855
Question:

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

Answer 0:

You may forget that mybatis’s XML file can write ResultMap.

After multiple table queries, the results are mapped to the ResultMap

Answer 1:

If two tables are in a library, since mybatis is used, join is allowed if data volume is allowed.

Answer 2:

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.

Answer 3:

In SQL, multi table query, Model class will return data as long as there are corresponding fields.

Leave a Reply

Your email address will not be published. Required fields are marked *