MyBatis Learning (4) query to implement associated data

Source: Internet
Author: User

with the basis of the previous chapters, some simple applications can be processed, but in the actual project, is often related to the table query, such as the most common to a multi-pair, one-to-many. How these queries are handled is a matter of speaking. We first create a article this table and initialize the data.
Program code
Drop TABLE IF EXISTS ' article ';
Create TABLE ' article ' (
' id ' int (one) not NULL auto_increment,
' userid ' int (one) is not NULL,
' title ' varchar (+) not NULL,
' Content ' text is not NULL,
PRIMARY KEY (' id ')
) Engine=innodb auto_increment=5 DEFAULT Charset=utf8;

-- ----------------------------
--Add a few test data
-- ----------------------------
Insert into ' article ' VALUES (' 1 ', ' 1 ', ' test_title ', ' test_content ');
Insert into ' article ' VALUES (' 2 ', ' 1 ', ' test_title_2 ', ' test_content_2 ');
Insert into ' article ' VALUES (' 3 ', ' 1 ', ' test_title_3 ', ' test_content_3 ');
Insert into ' article ' VALUES (' 4 ', ' 1 ', ' test_title_4 ', ' test_content_4 ');

you should find that the corresponding UserID for these articles is 1, so you need to have id=1 data in user tables. You can modify the data to meet your own criteria. According to the ORM rules, the table has been created, then it is necessary to have an object corresponding to it, so we add a article class
Program code
Package Com.yihaomen.mybatis.model;

public class Article {

private int id;
private user user;
Private String title;
Private String content;

public int getId () {
return ID;
}
public void setId (int id) {
This.id = ID;
}

Public User GetUser () {
return user;
}
public void SetUser (user user) {
This.user = user;
}
Public String GetTitle () {
return title;
}
public void Settitle (String title) {
This.title = title;
}
Public String getcontent () {
return content;
}
public void SetContent (String content) {
this.content = content;
}

}


Notice how the user of the article is defined and is a direct definition of a user object. Instead of an int type.

Many-to-one implementations
Scenario: Reads all articles published by a user. Of course, it is necessary to configure the SELECT statement inside the User.xml, but the focus is on what data the resultmap of this select corresponds to. This is the point, here to introduce association see the definition as follows:
Program code
<!--User Union article to configure one of the query methods (many-to-one way)--
<resultmap id= "Resultuserarticlelist" type= "article" >
<id property= "id" column= "aid"/>
<result property= "title" column= "title"/>
<result property= "Content" column= "content"/>

<association property= "user" javatype= "user" >
<id property= "id" column= "id"/>
<result property= "UserName" column= "UserName"/>
<result property= "useraddress" column= "useraddress"/>
</association>
</resultMap>

< select Id= "Getuserarticles" parametertype= "int" resultmap= "resultuserarticlelist" >
Select User.id,user.username,user.useraddress,article.id aid,article.title,article.content from User,article
where User.id=article.userid and User.id=#{id}
</select>

Once this is configured, it is possible to combine the SELECT statement with the mapping of the resultmap to see. Use Association to get the associated user, this is a many-to-one situation, because all the articles are the same user.

there is another way to reuse the resultmap we've defined earlier, and we've defined a resultlistuser to see how this second approach is implemented:
Program code
<resultmap type= "User" id= "Resultlistuser" >
<id column= "id" property= "id"/>
<result column= "UserName" property= "UserName"/>
<result column= "Userage" property= "Userage"/>
<result column= "useraddress" property= "useraddress"/>
</resultMap>

<!--User Union article Two-way configuration of Query method (many-to-one approach)--
<resultmap id= "ResultUserArticleList-2" type= "article" >
<id property= "id" column= "aid"/>
<result property= "title" column= "title"/>
<result property= "Content" column= "content"/>
<association property= "user" javatype= "user" resultmap= "Resultlistuser"/>
</resultMap>

<select id= "getuserarticles" parametertype= "int" resultmap= "resultuserarticlelist" >
Select User.id,user.username,user.useraddress,article.id aid,article.title,article.content from User,article
where User.id=article.userid and User.id=#{id}
</select>

The corresponding mappings in association can be extracted independently to achieve the purpose of reuse.

OK, now write the test code in the tests class:
Program code
public void getuserarticles (int userid) {
sqlsession session = Sqlsessionfactory.opensession ();
try {
Iuseroperation Useroperation=session.getmapper (Iuseroperation.class);
List<article> articles = Useroperation.getuserarticles (userid);
for (article article:articles) {
System.out.println (Article.gettitle () + ":" +article.getcontent () +
": The author is:" +article.getuser (). GetUserName () + ": Address:" +
Article.getuser (). getuseraddress ());
}
} finally {
Session.close ();
}
}


missing a bit, we must be in the Iuseroperation interface, add the select corresponding to the ID name of the same method:
Public list<article> getuserarticles (int id);

and then run it to test.

MyBatis Learning (4) query to implement associated data

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.