mybatis关联查询

一、实体类:

 

public class Father {
    
    private Integer f_id;
    private String f_name ;
    
    public Set<Son> sons= new HashSet<Son>(0);
}




public class Son {
    
    private Integer id ;
    
    private String name ;
    
    private Father father;
}

二、dao:

 

import com.wa.mybatis.model.Son;

public interface SonDao {
    
    
    public Son getSonById(int id);
    
    public Son getSon(int id);

}


import com.wa.mybatis.model.Father;

public interface FatherDao {
    
    public Father getById(int id);

}

三.SonMapper.xml:

<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE mapper PUBLIC   
    "-//mybatis.org//DTD Mapper 3.0//EN"  
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
<mapper namespace="com.wa.mybatis.dao.SonDao">
 
  <!-- 一对一的关联关系映射 -->
   <select id="getSonById" resultMap="getSonMap" >
   select * from son s,father f where s.father_id=f.f_id and  s.id=#{id}
   </select>
   
   <resultMap type="Son" id="getSonMap">
    <id column="id" property="id"/>
   <result column="name" property="name"/>
   <!-- 两个实体类的字段尽量不要一直,否则封装的数据混乱 -->
    <association property="father" javaType="Father" >
    <id column="f_id"  property="f_id"/>
    <result column="f_name" property="f_name"/>
    </association>
   </resultMap>
   
   
   
   <!--嵌套查询 -->
   
   <select id="getSon" parameterType="Integer" resultMap="getSonMap2" >
   select * from son s where s.id=#{id}
   </select>
   
   <select id="getFather" parameterType="Integer" resultType="Father" >
    select * from father f where  f.f_id= #{id}
   </select>
   
   
   <resultMap type="Son" id="getSonMap2">
    <id column="id" property="id"/>
   <result column="name" property="name"/>
   <!-- 两个实体类的字段尽量不要一直,否则封装的数据混乱 -->
    <association property="father" column="father_id" select="getFather" >
    </association>
   </resultMap>
 
 
 </mapper>
    

FatherMapper.xml:

<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE mapper PUBLIC   
    "-//mybatis.org//DTD Mapper 3.0//EN"  
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
  <!--这块等于dao接口的实现  namespace必须和接口的类路径一样-->  
<mapper namespace="com.wa.mybatis.dao.FatherDao">

   <select id="getById" parameterType="Integer" resultMap="fatherMap">
    
     select * from son s , father f where s.father_id=f.f_id and f.f_id =#{id}
   
   </select>
   
   <resultMap type="Father" id="fatherMap">
     
     
       <id column="f_id" property="f_id"/>
       
       <result column="f_name" property="f_name"/>
       <!-- 一对多关联查询 -->
       <collection property="sons" ofType="Son"  >
        <id column="id" property="id"/>
        <result column="name" property="name"/>
      
       </collection>
   
   </resultMap>

   </mapper>   
    
    

四、测试类:

package com.wa.mybatis.test;

import java.io.IOException;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Test;

import com.wa.mybatis.dao.FatherDao;
import com.wa.mybatis.dao.SonDao;
import com.wa.mybatis.model.Father;
import com.wa.mybatis.model.Son;
import com.wa.mybatis.util.SessionFactoryUtil;

public class Test3 {
    /**
     * 一对一关联查询
     * @throws IOException
     */
    @Test
    public void test1() throws IOException{
        
        SqlSessionFactory sessionFactory = SessionFactoryUtil.getFactory();
        SqlSession session = sessionFactory.openSession();
        SonDao dao = session.getMapper(SonDao.class);
        Son son= dao.getSonById(1);
        System.out.println(son);
    }
    
    /**
     * 一对一关联嵌套查询
     * @throws IOException
     */
    @Test
    public void test2() throws IOException{
        
        SqlSessionFactory sessionFactory = SessionFactoryUtil.getFactory();
        SqlSession session = sessionFactory.openSession();
        SonDao dao = session.getMapper(SonDao.class);
        Son son= dao.getSon(2);
        System.out.println(son);
    }
    
    /**
     * 一对多关联查询
     * @throws IOException
     */
    @Test
    public void test3() throws IOException{
        
        SqlSessionFactory sessionFactory = SessionFactoryUtil.getFactory();
        SqlSession session = sessionFactory.openSession();
        FatherDao dao = session.getMapper(FatherDao.class);
        Father father = dao.getById(1);
        System.out.println(father);
    }

}

表结构图:

     

 

posted @ 2015-01-03 14:48  snow__wolf  阅读(184)  评论(0)    收藏  举报