欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

mybatis的一對多映射-創(chuàng)新互聯(lián)

    延續(xù)mybatis的一對一問題,還是上面一對一舉得那個例子(http://fengcl.blog.51cto.com/9961331/1875657),

創(chuàng)新互聯(lián)公司專注于企業(yè)營銷型網站、網站重做改版、含山網站定制設計、自適應品牌網站建設、HTML5商城開發(fā)、集團公司官網建設、外貿營銷網站建設、高端網站制作、響應式網頁設計等建站業(yè)務,價格優(yōu)惠性價比高,為含山等各大城市提供網站開發(fā)制作服務。

如果一個用戶有多個作品怎么辦?這就涉及到了一對多的問題。同樣的,mybatis一對多依然可以分為兩種方式來解決。

一、使用內嵌的ResultMap實現(xiàn)一對多映射

1)實體

public class User implements Serializable{     private static final long serialVersionUID = 112596782083832677L;     private Integer id; //編號     private String email;  //郵箱     private String realName;  //真實姓名     private String telephone;   //電話號碼          private List<WorksInfo> worksInfos; //作品     //get,set方法     ... } public class WorksInfo implements Serializable{     private Integer id;     private  Integer userId;     private Date uploadDate; //上傳時間     private Date updateDate; //更新時間     //get,set方法     ... }

2)dao接口省略...

3)mapper映射文件

<resultMap type="com.tarena.djs.entity.WorksInfo" id="worksInfoResultMap">     <id column="id" property="id" />     <result column="uploadDate" property="uploadDate" />     <result column="updateDate" property="updateDate" /> </resultMap> <resultMap type="com.tarena.djs.entity.User" id="UserResult">     <id column="id" property="id" />      <result column="email" property="email" />     <result column="telephone" property="telephone" />     <result  column="realName"  property="realName"/>     <collection property="worksInfos" resultMap="worksInfoResultMap" /> </resultMap> <select id="findTutorById" parameterType="int" resultMap="UserResult">     select u.*,w.*      from user u      left join worksInfo w      on u.id = w.userId      where u.id = #{id}     </select>

4)測試省略

二、嵌套查詢方式實現(xiàn)一對多

1)實體類如上

2)dao層接口省略

3)mapper文件映射

<resultMap type="com.tarena.djs.entity.WorksInfo" id="worksInfoResultMap">     <id column="id" property="id" />     <result column="uploadDate" property="uploadDate" />     <result column="updateDate" property="updateDate" /> </resultMap> <select id="findWorksInfoByUserId" parameterType="int" resultMap="worksInfoResultMap">     select * from worksInfo where userId = #{userId} </select> <resultMap type="com.tarena.djs.entity.User" id="UserResult">     <id column="id" property="id" />      <result column="email" property="email" />     <result column="telephone" property="telephone" />     <result  column="realName"  property="realName"/>     <collection property="worksInfos" columns="id" select="findWorksInfoByUserId" /> </resultMap> <select id="findUserByUserId" parameterType="int" resultMap="UserResult">     select * from user where id = #{id} </select>

4)測試方法忽略

注意:collention元素里的column屬性,即主表中要傳遞給副表做查詢的條件,例如本例中:

<collection property="worksInfos" columns="id" select="findWorksInfoByUserId" />

及時將user表中的id字段傳遞給findWorksInfoByUserId方法做參數使用的,對應worksInfo表中的userId字段。除此之外,嵌套select語句會導致N+1的問題。首先,主查詢將會執(zhí)行(1 次) ,對于主

查詢返回的每一行,另外一個查詢將會被執(zhí)行(主查詢 N 行,則此查詢 N 次) 。對于

大型數據庫而言,這會導致很差的性能問題。

另外有需要云服務器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。

當前題目:mybatis的一對多映射-創(chuàng)新互聯(lián)
當前路徑:http://www.chinadenli.net/article30/docpso.html

成都網站建設公司_創(chuàng)新互聯(lián),為您提供關鍵詞優(yōu)化用戶體驗靜態(tài)網站域名注冊微信小程序ChatGPT

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

手機網站建設