1. 程式人生 > >MyBatis關聯對映關係,一對一,一對多,多對多

MyBatis關聯對映關係,一對一,一對多,多對多

“縱許悠揚度朱戶,終愁人影隔窗紗”

前言

客觀世界中的物件很少有孤立存在的,例如班級,往往與班級的學生存在關聯關係,如果得到某個班級的例項,那麼應該可以直接獲取班級對應的全部學生。反過來,如果已經得到個學生的例項,那麼也應該可以訪問該學生對應的班級。這種例項之間的互相訪問就是關聯關係。
關聯關係是面向物件分析、面向物件設計最重要的知識, My Batis完全可以理解這種關聯關係,如果對映得當, My Batis的關聯對映將可以大大簡化持久層資料的訪問。

示例

這裡只給出對映檔案(XML檔案)中的關鍵程式碼
  • 一對一關係
    例如:一個人對應一個身份證號
    PersonMapper.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="org.arunner.mapper.PersonMapper">

	<resultMap id="personMapper" type="org.arunner.domain.Person">
		<id property="id" column="id"/>
		<result property="name" column="name"/>
		<result property="age" column="age"/>
		<result property="sex" column="sex"/>
		<!--一對一關聯對映-->
		<association property="card" column="card_id" javaType="org.arunner.domain.Card" select="org.arunner.mapper.CardMapper.selectCardById"/>
	</resultMap>

	<!--根據ID查詢人員-->
	<select id="selectPersonById" parameterType="int" resultMap="personMapper">
		select * from tb_person where id = #{id} ;
	</select>
	
</mapper>

CardMapper.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="org.arunner.mapper.CardMapper">


	<!--根據ID查詢身份證資訊-->
	<select id="selectCardById" parameterType="int" resultType="org.arunner.domain.Card">
		select * from tb_card where id = #{id} ;
	</select>

</mapper>
  • 一對多關係
    例如:一個班級對應多個學生
    ClazzMapper.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="org.arunner.mapper.ClazzMapper">

	<resultMap id="clazzResultMap" type="org.arunner.domain.Clazz">
		<id property="id" column="id"/>
		<result property="code" column="code"/>
		<result property="name" column="name"/>
		<!--班級的學生屬性,因為一個班級有多個學生,所以該屬性是一個集合-->
		<collection property="students" javaType="ArrayList" column="id" ofType="org.arunner.domain.Student"
					select="org.arunner.mapper.StudentMapper.selectStudentByClazzId" fetchType="lazy">
			<id property="id" column="id"/>
			<result property="name" column="name"/>
			<result property="age" column="age"/>
			<result property="sex" column="sex"/>
		</collection>
	</resultMap>

	<!--根據班級ID查詢班級-->
	<select id="selectClazzById" parameterType="int" resultMap="clazzResultMap">
		select * from tb_clazz where id = #{id} ;
	</select>
	
</mapper>

StudentMapper.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="org.arunner.mapper.StudentMapper">

	<resultMap id="studentResultMap" type="org.arunner.domain.Student">
		<id property="id" column="id"/>
		<result property="name" column="name"/>
		<result property="age" column="age"/>
		<result property="sex" column="sex"/>
		<!--一對一關係-->
		<association property="clazz" javaType="org.arunner.domain.Clazz">
			<id property="id" column="id"/>
			<result property="code" column="code"/>
			<result property="name" column="name"/>
		</association>
	</resultMap>

	<!--根據班級ID查詢班級-->
	<select id="selectStudentByClazzId" parameterType="int" resultMap="studentResultMap">
		select * from tb_student where clazz_id = #{id} ;
	</select>

	<!--根據ID查詢學生和班級資訊-->
	<select id="selectStudentById" parameterType="int" resultMap="studentResultMap">
		select * from tb_student s,tb_clazz c where c.id = s.clazz_id and s.id= #{id} ;
	</select>

</mapper>
  • 多對多關係
    例如:訂單、商品和使用者的關係
    OrderMapper.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">
<!-- namespace指使用者自定義的名稱空間。 -->
<mapper namespace="org.arunner.mapper.OrderMapper">

	<resultMap type="org.arunner.domain.Order" id="orderResultMap">
		<id property="id" column="oid"/>
	  	<result property="code" column="code"/>
	  	<result property="total" column="total"/>
		<!-- 多對一關聯對映:association   -->
		<association property="user" javaType="org.arunner.domain.User">
			<id property="id" column="id"/>
			<result property="username" column="username"/>
			<result property="loginname" column="loginname"/>
			<result property="password" column="password"/>
			<result property="phone" column="phone"/>
			<result property="address" column="address"/>
		</association>
		<!-- 多對多對映的關鍵:collection   -->
		<collection property="articles" javaType="ArrayList"
	  column="oid" ofType="org.arunner.domain.Article"
	  select="org.arunner.mapper.ArticleMapper.selectArticleByOrderId"
	  fetchType="lazy">
	  	<id property="id" column="id"/>
	  	<result property="name" column="name"/>
	  	<result property="price" column="price"/>
	  	<result property="remark" column="remark"/>
	  </collection>
	</resultMap>
	
	<!-- 注意,如果查詢出來的列同名,例如tb_user表的id和tb_order表的id都是id,同名,需要使用別名區分 -->
  <select id="selectOrderById" parameterType="int" resultMap="orderResultMap">
  	SELECT u.*,o.id AS oid,CODE,total,user_id
  	 FROM tb_user u,tb_order o
  	WHERE u.id = o.user_id
  	 AND o.id = #{id}
  </select>
  
  <!-- 根據userid查詢訂單 -->
  <select id="selectOrderByUserId" parameterType="int" resultType="org.arunner.domain.Order">
  	SELECT * FROM tb_order WHERE user_id = #{id}
  </select>
  
</mapper>

UserMapper.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">
<!-- namespace指使用者自定義的名稱空間。 -->
<mapper namespace="org.arunner.mapper.UserMapper">

	<resultMap type="org.arunner.domain.User" id="userResultMap">
		<id property="id" column="id"/>
		<result property="username" column="username"/>
		<result property="loginname" column="loginname"/>
		<result property="password" column="password"/>
		<result property="phone" column="phone"/>
		<result property="address" column="address"/>
		<!-- 一對多關聯對映:collection   -->
		<collection property="orders" javaType="ArrayList"
	  column="id" ofType="org.arunner.domain.User"
	  select="org.arunner.mapper.OrderMapper.selectOrderByUserId"
	  fetchType="lazy">
	  	<id property="id" column="id"/>
	  	<result property="code" column="code"/>
	  	<result property="total" column="total"/>
	  </collection>
	</resultMap>
	
  <select id="selectUserById" parameterType="int" resultMap="userResultMap">
  	SELECT * FROM tb_user  WHERE id = #{id}
  </select>
  
</mapper>

ArticleMapper.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">
<!-- namespace指使用者自定義的名稱空間。 -->
<mapper namespace="org.arunner.mapper.ArticleMapper">
  
  <select id="selectArticleByOrderId" parameterType="int" 
  resultType="org.arunner.domain.Article">
  	SELECT * FROM tb_article WHERE id IN ( 
		SELECT article_id FROM tb_item WHERE order_id = #{id} 
	) 
  </select>
  

</mapper>