mybatis:多表查詢級聯查詢

mybatis多表查詢

※利用Set類,將scores類封裝進student類,進行級聯查詢!查詢出來還是student對象,對象裏含有一個HashSet<Scores>用來存放scores對象。

※resultMap高級映射。<association>來從數據庫返回值在裏面編寫一對多的配置。

1.準備兩張mysql的數據庫表t_student、t_scores

在這裏插入圖片描述

2.創建對應的entity(pojo\bean)類

此處略,根據數據庫自己建立
在這裏插入圖片描述

3.Mapper接口添加一個方法

在這裏插入圖片描述

4.對應的映射文件的配置文件xml

在這裏插入圖片描述
在這裏插入圖片描述

	<resultMap id="studentScoresMap" type="student">
		<id property="stuID" column="stuID"/>
		<result property="name" column="name" />
		<result property="birthday" column="birthday" />
		<result property="sex" column="sex" />
		<association property="scoresSet" javaType="Scores">
			<result property="id" column="id"/>
			<result property="math" column="math"/>
		</association>
	</resultMap>
	<!-- 查詢 -->
	<select id="selectStudentByIdWithScores" parameterType="string"
		resultMap="studentScoresMap" >
		select
		<include refid="all"></include>,id,math
		from t_student inner join t_scores
		on t_student.stuID=t_scores.stuID
		 where t_student.stuID = #{stuID}
	</select>

5.Test類測試,略~~~

left.join只取得一條數據

select 
	product.id, uid, u_name, u_sex, u_credit, name, category, origin_price, current_price, quality, place,
	 telephone, delivery, description, edit_time, count, lat, lon ,picture.id pic_id,pid,picurl
from product 
left join 
(select MAX(picture.id) as id,pid,picurl from picture group by pid) picture 
on product.id=picture.pid 
where
1=1
limit 0,20 ;
發佈了51 篇原創文章 · 獲贊 0 · 訪問量 3435
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章