Mybatis ---- data cascade query (Multiple-to-one)

Source: Internet
Author: User

Project directory structure:


There are two tables: an article table article and a user table user.

Create table article (id int (11) not null auto_increment, userid int (11) not null, title varchar (100) not null, content text not null, primary key (id )) ENGINE = InnoDB default charset = utf8; insert into article (id, userid, title, content) values (1, 1, 'test _ title', 'text _ content '); insert into article (id, userid, title, content) values (2, 1, 'test _ title_2 ', 'text _ content_2'); insert into article (id, userid, title, content) values (, 'test _ title_3 ', 'text _ content_3'); insert into article (id, userid, title, content) values, 'test _ title_4 ', 'text _ content_4'); create table user (id int (11) not null auto_increment, userName varchar (50) default null, userAge int (11) default null, userAddress varchar (200) default null, primary key (id) ENGINE = InnoDB default charset = utf8; insert into user (id, userName, userAge, userAddress) values (1, 'summer ', '123', 'shanghai ');
Now, you need to query the articles owned by the user based on the user ID.You need to write the following SQL statement:
select a.id, a.userName ,a.userAddress ,b.id aid, b.title,b.content                                          from user a,article b                                          where a.id=b.userid and a.id=#{id}

Now I will paste the relevant JAVA code and ing files, all of which are in the annotations.

User. java

package com.mybatis.model;public class User { private int id; private String userName; private String userAge; private String userAddress; public int getId() {return id;}public void setId(int id) {this.id = id;}public String getUserName() {return userName;}public void setUserName(String userName) {this.userName = userName;}public String getUserAge() {return userAge;}public void setUserAge(String userAge) {this.userAge = userAge;}public String getUserAddress() {return userAddress;} public void setUserAddress(String userAddress) {this.userAddress = userAddress;  } }

Article. java

Package com. mybatis. model; public class Article {private int id; private User user; // The document defines a User object, instead of the int type 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 ;}}

Interface Class

IUserDao. java

package com.mybatis.dao;import java.util.List;import com.mybatis.model.Article;public interface IUserDao { public List getUserArticles(int id);}

Configution. xml

    
   
      
           
            
     
      
    
       
            
             
                   
                    
                    
                    
              
         
     
      
       
          
     
    
 

User. xml

     
        
         
               
                
                
                             
                
                      
                      
                      
                      
                 
  Select. id,. userName,. userAddress, B. id aid, B. title, B. content from user a, article B where. id = B. userid and. id = # {id}
 

Compile a test class.

Package com. mybatis. test; import java. io. IOException; import java. util. list; import org. apache. ibatis. io. resources; import org. apache. ibatis. session. sqlSession; import org. apache. ibatis. session. sqlSessionFactory; import org. apache. ibatis. session. sqlSessionFactoryBuilder; import com. mybatis. dao. IUserDao; import com. mybatis. model. article; public class Test {/***** obtain MyBatis SqlSessionFactory * SqlSessionFactory to create SqlSession. Once created successfully, you can use the SqlSession instance to execute the ing statement *, commit, rollback, close and other methods * @ return */private static SqlSessionFactory getSessionFactory () {SqlSessionFactory sessionFactory = null; String resource = "configuration. xml "; try {sessionFactory = new SqlSessionFactoryBuilder (). build (Resources. getResourceAsReader (resource);} catch (IOException e) {e. printStackTrace ();} return sessionFactory;}/*** main method * @ param args */public static void main (String [] args) {SqlSession session = getSessionFactory (). openSession (); try {IUserDao userDao = session. getMapper (IUserDao. class); // input the user's id = 1 List listArticle = userDao. getUserArticles (1); for (Article article: listArticle) {System. out. println (article. getTitle () + ":" + article. getContent () + ": Author:" + article. getUser (). getUserName () + ": Address:" + article. getUser (). getUserAddress () ;}} catch (Exception e) {e. printStackTrace ();} finally {session. close ();}}}

The result is as follows:

Test_title: text_content: Author: summer: Address: Shanghai test_title_2: text_content_2: Author: summer: Address: Shanghai test_title_3: text_content_3: Author: summer: Address: Shanghai Region: text_content_4: author: summer: Address: Shanghai



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.