mybatis association巢狀association的兩級巢狀問題
阿新 • • 發佈:2018-12-10
今天遇到了一個雙表連線查詢以及自關聯的問題,由於第一次遇到,所以在這記下,日後好查閱
針對一個表的關聯屬性本身也有自關聯的情況下,可以用association巢狀association的方法來處理。
以下是程式碼:
<select id="selectNewsLabels" resultMap="newslabelMap"> select id,label_name,label_content from newlabel where id=#{xxx} </select> <resultMap type="NewManage" id="newmanageMap"> <id property="id" column="id"/> <result property="title" column="title"/> <result property="content" column="content"/> <result property="time" column="time"/> <association property="newsLabel" javaType="NewsLabel"> <id property="id" column="id"/> <result property="name" column="label_name"/> <association property="parent" javaType="NewsLabel" select="selectNewsLabels" column="pid"> <id property="id" column="id"/> <result property="name" column="label_name"/> </association> </association> </resultMap> <select id="selectCurrentPage" resultMap="newmanageMap"> select l.pid,l.id,l.label_name,m.id,m.labelid,m.title,m.content,m.time from newlabel l,newmanage m <where> l.id=m.labelid <if test="title != null and title != ''"> and title like '%${title}%' </if> <if test="labelid != null and labelid != 0"> and labelid=#{labelid} </if> </where> limit #{pageStartIndex},#{pageSize} </select>
這樣的話,就不用去另外去寫一個VO類去封裝這些屬性了 ,簡單,方便
在頁面上可以用 物件.屬性.屬性.屬性 來獲得這個屬性值
我第一次寫的時候,把第一個association也加上select了,導致結果不正確。