Learn Mybatis step by step-implement multi-table joint query (4)

Source: Internet
Author: User

In the previous chapter, we have completed the CRUD operation on a single table. Today we will talk about the application of Mybatis in Multi-table queries, after all, there are many combined queries for multiple tables in the actual business ~

Do you remember that we created a Website table at the beginning? There is a VisitorId field in the table, which indicates the websites visited by visitors, now we first establish the weban object about Website according to the requirements in the previous one.

Create a Website class under the david. mybatis. model package for data persistence. Rewrite the corresponding toString () method to facilitate the use of the test program.

CreateTime = Website (String name ,. name =. visitorId = 1 = setId (. id =. visitor =. name = setStatus (. status =. createTime = id = 0 (visitor === setVisitorId (. visitorId = StringBuilder (String. format ("Website => {Id: % d, Name: % s, CreateTime: % s} \ r \ n" SimpleDateFormat ("yyyy-MM-dd HH: mm: ss "(visitor! = "Visitor => % s"Website entity class

Create corresponding operation Interfaces under david. mybatis. demo:

Delete (Website query (List <Website>Website operation interface

Create a new WebsiteMapper. xml ing file in the mapper folder and put the configuration of adding, deleting, modifying, and querying a single table as described in the previous one, so that you can build some test data. As follows:

WebsiteMapper Configuration

Here we mainly talk about the query. Now we want to query the website and take out the corresponding visitor information separately. How can we do this? refer to the query in the configuration, write the SQL statement of the subjoin Table query,

.

What will I get if I configure it like below?

Configuration that may cause errors

It was found that the Visitor Id also changed to 2. In fact, It maps the Website ID by default, because the result of the SQL statement query is changed to 2, someone may ask why it is not 4, because it matches the first Website by default. id and Visit. the location of the Id.

So you need to create an alias to avoid this situation, so that you will find that there is only one truth:

You can see that the method for processing resultMap from multiple tables is the same as that for a single table, you can see another resultMap in the <resultMap> node of the Website. This is the entity that represents the vising required by the Visit object. You can associate it using the following method:

 

The visitor is the name of the visit field in the Website object. It must be consistent with the name. Otherwise, There is no getter for property named 'xxx' in 'class david. mybatis. model. website 'exception

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.