1. 程式人生 > >Mybatis學習總結(四)---一對多對映

Mybatis學習總結(四)---一對多對映

建立house表,一個使用者有多個房子,所以一個使用者對應多條房屋資訊。

以下sql查詢出使用者和所在部門資訊以及其房屋資訊

		SELECT
  		user.*,
  		dept.deptname,
  		dept.description,
  		house.hid,
  		house.housename,
  		house.address,
  		house.uid
  		FROM
  		user,
  		dept,
  		house
  		WHERE user.deptid = dept.did AND house.uid = user.id

結果:

待解決問題:id為1的使用者,重複出現,因為他又多條房屋記錄。

解決辦法:獲取資訊的時候把多條房屋記錄封裝到HouseList裡

程式碼:

House pojo類:

package cn.mybatis.model;

public class House {
    private Integer hid;

    private String housename;

    private String address;

    private Integer uid;

    public Integer getHid() {
        return hid;
    }

    public void setHid(Integer hid) {
        this.hid = hid;
    }

    public String getHousename() {
        return housename;
    }

    public void setHousename(String housename) {
        this.housename = housename == null ? null : housename.trim();
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address == null ? null : address.trim();
    }

    public Integer getUid() {
        return uid;
    }

    public void setUid(Integer uid) {
        this.uid = uid;
    }
}

User pojo類中增加List<House> houses屬性

package cn.mybatis.model;

import java.util.List;

public class User {
    private Integer id;

    private String username;

    private String password;

    private Integer age;

    private Integer deptid;

    private Dept dept;
    
    private List<House> houses;
    
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username == null ? null : username.trim();
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password == null ? null : password.trim();
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Integer getDeptid() {
        return deptid;
    }

    public void setDeptid(Integer deptid) {
        this.deptid = deptid;
    }

    
	public Dept getDept() {
		return dept;
	}

	public void setDept(Dept dept) {
		this.dept = dept;
	}

	public List<House> getHouses() {
		return houses;
	}

	public void setHouses(List<House> houses) {
		this.houses = houses;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", username=" + username + ", password="
				+ password + ", age=" + age + ", deptid=" + deptid + "]";
	}
    
    
}

1.編寫UserMapper.xml,新建一個resultMap2 id為BaseResultMap2,繼承BaseResultMap

  <resultMap type="cn.mybatis.model.User" id="BaseResultMap">
  	<id column="id" property="id"/>
  	<result column="username" property="username"/>
  	<result column="password" property="password"/>
  	<result column="age" property="age"/>
  	<result column="deptid" property="deptid"/>
  	
  	<!-- 配置對映的關聯部門資訊 -->
  	<!-- association用於對映關聯查詢單個物件的資訊 
  	property:要將關聯查詢的使用者資訊對映到表中對應屬性
  	-->
  	<association property="dept" javaType="cn.mybatis.model.Dept">
  		<!-- id:關聯查詢的部門的唯一標誌 -->
  		<!-- column:指定唯一標誌部門的列 -->
  		<!-- javaTypey:對映到dept對應屬性 -->
  		<id column="did" property="did"/>
  		<result column="deptname" property="deptname"/>
  		<result column="description" property="description"/>
  	</association>
  	
  </resultMap>
 
 
  <resultMap type="cn.mybatis.model.User" id="BaseResultMap2" extends="BaseResultMap">
  	<!-- 使用extends繼承,不用在此中配置使用者資訊和部門資訊的對映 -->
  	
  	<!-- 一個使用者關聯查詢出多條房屋資訊 -->	
  	<!-- collection:對關聯查詢出的多條資訊對映到集合物件中 -->
  	<!-- property:將關聯查詢的資訊對映到cn.mybatis.model.User對應屬性 -->
  	<!-- ofType:指定對映到集合屬性中pojo的型別 -->
  	<collection property="houses" ofType="cn.mybatis.model.House">
  		<!-- id:房屋資訊唯一標識 -->
  		<id column="hid" property="hid"/>
  		<result column="housename" property="housename"/>
  		<result column="address" property="address"/>
  		<result column="uid" property="uid"/>
  	</collection>
  		
  		
  </resultMap>

2.編寫UserMapper.xml,增加statement---findAllByResultMap2

<select id="findAllByResultMap2" resultMap="BaseResultMap2">
  		SELECT
  		user.*,
  		dept.deptname,
  		dept.description,
  		house.hid,
  		house.housename,
  		house.address,
  		house.uid
  		FROM
  		user,
  		dept,
  		house
  		WHERE user.deptid = dept.did AND house.uid = user.id
  </select>

3.編寫UserMapper介面,增加findAllByResultMap2方法

//	查詢使用者、部門和使用者房屋資訊使用resultmap
	public List<User> findAllByResultMap2()throws Exception;

4.編寫測試類方法

	@Test
	public void testFindAllByResultMap2() throws Exception {
		
		SqlSession sqlSession =  sqlSessionFactory.openSession();
		
//		建立usermapper物件,mybatis自動生成mapper代理物件
		UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
		
//		呼叫UserMapper的方法
		List<User> userList = userMapper.findAllByResultMap2();
		
		System.out.println(userList);
		
		sqlSession.close();
	}

打斷點,可看到userList中有對應結果

mybatiss使用resultMap的collectiond對關聯查詢多條記錄對映到一個list集合屬性中。

如果使用resultType實現

需要將房屋資訊對映到使用者中的houselists中,自己處理,雙重迴圈遍歷去重,將房屋資訊儲存在houselists。