mybatis處理一對一映射

一、使用嵌套結果ResultMap方式來處理一對一映射,詳見代碼

1)實體

package com.tarena.djs.entity;

import java.io.Serializable;
import java.util.Date;
/**
 * 用戶
 */
public class User implements Serializable{
	private static final long serialVersionUID = 112596782083832677L;
	private Integer id;			//編號
	private String email; 		        //郵箱
	private String realName; 	        //真實姓名
	private String telephone;               //電話號碼
	private String password;                //密碼
	
	private TeamInfo teamInfo ;             //團隊信息
	private WorksInfo worksInfo; 	        //作品信息
	
	// 以下是get,set方法
	...	
}
/**
 * 隊伍信息
 */
 public class TeamInfo implements Serializable{
	private static final long serialVersionUID = -595214407157962899L;
	private Integer id;		 //隊伍編號
	private Integer userId;  //隊長id
	private String teamName; //隊伍名稱
	private String teamUser; //隊伍成員信息
	// 以下是get,set方法
	...	
 }	
 
/**
 * 參賽作品信息
 */ 
 public class WorksInfo implements Serializable{
	private static final long serialVersionUID = 1610181029330869297L;
	private Date uploadDate; // 上傳時間
	private Date updateDate; // 更新時間
	// 以下是get,set方法
	...
 }

2)dao層接口

package com.tarena.djs.dao;

import com.tarena.djs.annotation.MyBatisReponsitory;
import com.tarena.djs.entity.User;

@MyBatisReponsitory
public interface TestDao {
	/**
	 * result嵌套實現一對一映射
	 * @param id
	 * @return
	 */
	User getUserWithTeamInfoAndWroksInfo(Integer id);
}

3)dao層mapper映射文件

<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"      
 "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="com.tarena.djs.dao.TestDao">
	<resultMap type="com.tarena.djs.entity.User" id="userWithTeamInfoAndWorksInfoResult">
		<id property="id" column="id"/>
		<result property="email" column="email"/>
		<result property="realName" column="realName"/>
		<result property="telephone" column="telephone"/>
		<result property="password" column="password"/>
		<!-- 元素association被用來導入一個“has-one”類型的關聯 -->
		<association property="teamInfo" resultMap="teamInfoResultMap"/>
		<association property="worksInfo" resultMap="worksInfoResultMap"/>
	</resultMap>
	<resultMap type="com.tarena.djs.entity.TeamInfo" id="teamInfoResultMap">
		<id property="id" column="id"/>
		<result property="teamName" column="teamName"/>
		<result property="teamUser" column="teamUser"/>
	</resultMap>
	<resultMap type="com.tarena.djs.entity.WorksInfo" id="worksInfoResultMap">
		<id property="id" column="id"/>
		<result property="uploadDate" column="uploadDate"/>
		<result property="updateDate" column="updateDate"/>
	</resultMap>
	<!--  result嵌套實現一對一映射  -->
	<select id="getUserWithTeamInfoAndWroksInfo" parameterType="int" resultMap="userWithTeamInfoAndWorksInfoResult">
		select u.id,u.email,u.realName,u.telephone,u.password,t.id tid ,t.userId,t.teamName,t.teamUser,w.uploadDate,w.updateDate
		from user u left join teamInfo t on u.id = t.userId 
		left join worksInfo w on u.id = w.userId
		where u.id = #{id}
	</select>
</mapper>

4)測試方法

package com.tarena.djs.util;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.tarena.djs.dao.TestDao;
import com.tarena.djs.entity.User;
import com.tarena.djs.service.test.TestMybatisService;

public class Test {
	/**
	 * 嵌套實現一對一映射
	 */
	@Test
	public void test1(){
		ApplicationContext ac = new ClassPathXmlApplicationContext("config/applicationContext.xml");
		TestDao tms = ac.getBean("testDao",TestDao.class);
		User user = tms.getUserWithTeamInfoAndWroksInfo(460);
		System.out.println(user);
		System.out.println(user.getTeamInfo().getTeamName()+"---"+user.getTeamInfo().getTeamUser());
		System.out.println(user.getWorksInfo().getUpdateDate()+"----"+user.getWorksInfo().getUploadDate());
	}
}

二、使用嵌套查詢的方式實現一對一映射

1)實體信息如上

2)dao層接口如上

3)dao層mapper映射文件

<!--teamInfo resultMap定義-->
<resultMap type="com.tarena.djs.entity.TeamInfo" id="teamInfoResultMap">
	<id property="id" column="id"/>
	<result property="teamName" column="teamName"/>
	<result property="teamUser" column="teamUser"/>
</resultMap>
<!-- teamInfo查詢語句 -->
<select id="findTeamInfoByUserId" parameterType="int" resultMap="teamInfoResultMap">
	select teamName,teamUser from teamInfo where userId = #{userId}
</select>
<!--worksInfo resultMap定義-->
<resultMap type="com.tarena.djs.entity.WorksInfo" id="worksInfoResultMap">
	<id property="userId" column="userId"/>
	<result property="uploadDate" column="uploadDate"/>
	<result property="updateDate" column="updateDate"/>
</resultMap>
<!-- worksInfo查詢語句-->
<select id="findWorksInfoByUserId" parameterType="int" resultMap="worksInfoResultMap">
	select updateDate,uploadDate from worksInfo where userId = #{userId}
</select>
<!-- 嵌套查詢的方式實現一對一映射 -->
<resultMap type="com.tarena.djs.entity.User" id="userWithTeamInfoAndWorksInfoResult1">
	<id property="id" column="id"/>
	<result property="email" column="email"/>
	<result property="realName" column="realName"/>
	<result property="telephone" column="telephone"/>
	<result property="password" column="password"/>
	<association property="teamInfo" column="id" select="findTeamInfoByUserId"/>
	<association property="worksInfo" column="id" select="findWorksInfoByUserId"/>
</resultMap>
<!--user查詢-->
<select id="findUserWithTeamInfoAndWroksInfo" parameterType="int" resultMap="userWithTeamInfoAndWorksInfoResult1">
	select * from user where id = #{id}
</select>

測試方法:

package com.tarena.djs.util;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.tarena.djs.dao.TestDao;
import com.tarena.djs.entity.User;
import com.tarena.djs.service.test.TestMybatisService;

public class TestAPP {
	/**
	 * 嵌套resultMap實現一對一映射
	 */
	@Test
	public void testApp(){
		ApplicationContext ac = new ClassPathXmlApplicationContext("config/applicationContext.xml");
		TestDao tms = ac.getBean("testDao",TestDao.class);
		User user = tms.findUserWithTeamInfoAndWroksInfo(460);
		System.out.println(user);
		System.out.println(user.getTeamInfo().getTeamName()+"---"+user.getTeamInfo().getTeamUser());
		System.out.println(user.getWorksInfo().getUpdateDate()+"----"+user.getWorksInfo().getUploadDate());
	}
}

注意:

在此方式中,兩個<association>元素的select 屬性被設置成了id 爲findTeamInfoByUserId和findWorksInfoByUserId的語句。這裏,三個分開的SQL語句將會在數據庫中執行,第一個調用 findUserWithTeamInfoAndWorksinfo加載User信息(這個方法名忘了改了,可能會產生歧義),而第二個調用 findTeamInfoByUserId來加載TeamInfo信息,第三個調用findWorksInfoByUserId加載wokrsInfo信息。userId列的值將會被作爲輸入參數傳遞給 findTeamInfoByUserId,findWorksInfoByUserId語句


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章