Explain the latest version of MyBatis-3.5.6_ 5. MyBatis one-to-one & one-to-many cascading query

Before introducing this cascading query, let’s talk about one-to-one and one-to-many.

One, one-to-one, one-to-many

Let's think about a problem, a problem between you and Apple.

For example, now you have an apple, then there is a one-to-one correspondence between this apple and you.

If we go to design the table at this time, we can directly treat the apple as an attribute and store it together with the main table of people. as follows

However, there can be multiple apples, and you can only have one. In other words, you may also have multiple apples in the follow-up period. In this case, the relationship between apples and you is many-to-one.

Then, we have no way to use the above table, we need to create two tables to maintain this relationship.

OK, after knowing this, we will start our learning.

Two, one to one

Let's modify the previous example here. Didn't we add a school? Here we add a school entity. One of its attributes is the school id and the other is the school name.

OK, create it yourself.

After creating it, let's start the association. First, we create an object in the student table, which is the school object, and then the corresponding get and set are all done.

Then we modify the StudentMapper, we add a resultMap.

But this is not enough, we still need to use something, open the official website of MyBatis, let's take a look at this:

For us, just use this to set up the association, and then we can check out the school by the way when we inquire.

After we cascade through association, we associate the content of school with our students. Here we need to tell the association property attribute, which is the corresponding mapping relationship. For details, we can see the official website

Then there is the label in the association. This is the attribute of the corresponding school. Note here that the column corresponds to the school id in the database, and the property is the attribute in the corresponding entity class.

Then we run it.

OK.

Three, one-to-many

Here we can query students through the school in reverse, so that we can achieve one-to-many query, let's try it.

Here we need to add a collection of students to the school entity.

Then add the get set method and toString method by yourself.

With this, we go to create the Mapper interface.

Here we have one more resultMap, if we don’t want to write, we can copy one from our previous xml file

Let's test it next.

You can see that there is no problem.

Four, optimization

The two one-to-one and one-to-many methods we described above can be used, but they are not good. Let's optimize them.

1. One to One

First of all, let’s look at the official document first, and we will modify it according to this

Then we find StudentMapper.xml, according to the official website, we commented out, and then changed to our

Of course, here we are missing the select method, so we need to add it in the School

Of course, add it yourself in the Java interface! ! !

Here we mainly pay attention to select, because we are writing in two Mappers, so we need to specify it, otherwise we won’t find the getSchool method~~~

2. One-to-many

One-to-many is actually very similar to this one-to-one, let’s first go to the official website example

Similarly, we also continue to modify, first add our query in the student mapper.

Then school mapper

Then we write the interface to test it.

In the future, we will use the second method to perform related queries, so that our code is highly reusable, saving a lot of writing everywhere.

 

So far, our MyBatis one-to-one & one-to-many cascading query is complete. There are a lot of things, everyone should digest it.

You can check it out for yourself, if you don’t understand, please contact me QQ: 2100363119

Welcome everyone to visit my website: https://www.lemon1234.com

If you can, pay attention to my official account, it is on my website, updated every day~~~, unlimited resources to enjoy Java, thank you~

Guess you like

Origin blog.csdn.net/weixin_45908370/article/details/113928200