mybatis 多表關聯查詢

    這篇博文介紹的是多表中的一對多表關聯查詢
    建立兩張表:一張是用戶,一張是用戶所對應的移動手機,一戶用戶能夠有部移動手機。
      這是用戶t_user表 html

              
        這是移動電話t_mobile表
              
           在Java實體對象對中,一對多能夠根據List和Set來實現,二者在mybitis中都是經過collection標籤來配合使用,稍後會作詳細配置介紹 java

          建立表對應的JavaBean對象 sql

Mobile  Bean session

[java]  view plain copy
  1. public class Mobile {  
  2.     private int id;  
  3.     private String telnumber;  
  4.   
  5.     public int getId() {  
  6.         return id;  
  7.     }  
  8.   
  9.     public void setId(int id) {  
  10.         this.id = id;  
  11.     }  
  12.   
  13.     public String getTelnumber() {  
  14.         return telnumber;  
  15.     }  
  16.   
  17.     public void setTelnumber(String telnumber) {  
  18.         this.telnumber = telnumber;  
  19.     }  
  20.   
  21. }  


User Bean mybatis

[java]  view plain copy
  1. package com.jefry;  
  2.   
  3. import java.util.List;  
  4.   
  5. public class User {  
  6.     private int id;  
  7.     private String userName;  
  8.     private String password;  
  9.           private List<Mobile> mobiles; //這裏也能夠是Set集合  
  10.   
  11.     public List<Mobile> getMobiles() {  
  12.         return mobiles;  
  13.     }  
  14.   
  15.     public void setMobiles(List<Mobile> mobiles) {  
  16.         this.mobiles = mobiles;  
  17.     }  
  18.   
  19.     public String getUserName() {  
  20.         return userName;  
  21.     }  
  22.   
  23.     public void setUserName(String userName) {  
  24.         this.userName = userName;  
  25.     }  
  26.   
  27.     public String getPassword() {  
  28.         return password;  
  29.     }  
  30.   
  31.     public void setPassword(String password) {  
  32.         this.password = password;  
  33.     }  
  34.   
  35.     public int getId() {  
  36.         return id;  
  37.     }  
  38.   
  39.     public void setId(int id) {  
  40.         this.id = id;  
  41.     }  
  42.   
  43. }  

 

在上一篇的基礎上改寫映射文件: app

[html]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE mapper  
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
  5. <mapper namespace="user">  
  6.      <resultMap id="userResultMap" type="User">   
  7.         <id property="id" column="id" javaType="int" jdbcType="INTEGER" />  
  8.         <result property="userName" column="name" javaType="string" jdbcType="VARCHAR"/>  
  9.         <result property="password" column="pass" javaType="string" jdbcType="VARCHAR"/>  
  10.         <collection property="mobiles" column="userid" ofType="Mobile">    
  11.             <id property="id" column="id" javaType="int" jdbcType="INTEGER"/>    
  12.             <result property="telnumber" column="telnumber" javaType="string" jdbcType="VARCHAR"/>    
  13.         </collection>    
  14.      </resultMap>  
  15.       
  16.     <!--多表查詢操做-->  
  17.     <select id="selectUser" parameterType="int"  resultMap="userResultMap" >  
  18.         <!--分別爲mobile的主鍵id與user的主鍵id賦值別名,避免由於兩個表字段名稱相同而注入到對應對象名稱衝突-->  
  19.         select m.id m_id,m.telnumber,u.id u_id,u.name,u.pass from t_mobile m,t_user u where m.userid = u.id and u.id = #{id}   
  20.     </select>  
  21. </mapper>  

 

最後,經過測試OK 測試

 

[java]  view plain copy
  1. public class Test {  
  2.     static String resource = "mybatis-config.xml";  
  3.   
  4.     public static void main(String[] args) throws IOException {  
  5.         InputStream inputStream = Resources.getResourceAsStream(resource);  
  6.         SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);  
  7.         SqlSession session = sqlSessionFactory.openSession();  
  8.         try {  
  9.             User user = session.selectOne("user.selectUser"1);  
  10.             List<Mobile> mobiles = user.getMobiles();  
  11.             for(Mobile mobile : mobiles) {  
  12.                 System.out.println("user:" + user.getUserName() + ",tel:" + mobile.getTelnumber());  
  13.             }  
  14.               
  15.               
  16.         } finally {  
  17.             session.close();  
  18.         }  
  19.     }  
  20.   
  21. }  
相關文章
相關標籤/搜索