mybatis一對(duì)一、一對(duì)多和多對(duì)多查詢實(shí)例代碼

關(guān)鍵字:association 一對(duì)一映射(一個(gè)班級(jí)只有一個(gè)班主任)

為易縣等地區(qū)用戶提供了全套網(wǎng)頁(yè)設(shè)計(jì)制作服務(wù),及易縣網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為網(wǎng)站設(shè)計(jì)制作、做網(wǎng)站、易縣網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!

  <select id="getClass" parameterType="int" resultMap="ClassesResultMap">  
    select * from class c,teacher t where c.teacher_id=t.t_id and c.c_id=#{id}  
  </select>  
  <resultMap type="com.lcb.user.Classes" id="ClassesResultMap">  
    <id property="id" column="c_id"/>  
    <result property="name" column="c_name"/>  
    <association property="teacher" javaType="com.lcb.user.Teacher">  
      <id property="id" column="t_id"/>  
      <result property="name" column="t_name"/>  
    </association>  
  </resultMap>  

關(guān)鍵字:collection 一對(duì)多映射(一個(gè)老師有多個(gè)學(xué)生)

<resultMap type="Teacher" id="teacherMaps">  
    <id column="id" property="id"/>  
    <result column="name" property="name"/>  
    <result column="class_name" property="className"/>  
    <collection property="students" ofType="Student" select="getStudents" column="id">  
    </collection>  
  </resultMap>  
  <!-- 查詢所有的老師級(jí)各自的所有學(xué)生 -->  
  <select id="getAllTeacher" parameterType="Teacher" resultMap="teacherMaps">  
    SELECT  
      t.id,  
      t.NAME,  
      t.class_name  
    FROM  
      teacher t  
  </select>  
  <select id="getStudents" parameterType="int" resultType="Student">  
    select   
      s.id,  
      s. NAME,  
      s.class_name as className  
    from student s  
    where teacher_id = #{id}  
  </select> 

    關(guān)鍵字:association 多對(duì)一映射(多個(gè)人屬于一個(gè)國(guó)家)

             多對(duì)一相當(dāng)于一對(duì)多,也可以使用collection

<select id="selectCountry" resultType="Country">  
  select cid,cname from country where cid=#{ooo}  
</select>  
<resultMap type="People" id="peopleMapper2">  
  <id column="pid" property="pid"/>  
  <result column="pname" property="pname"/>  
  <association property="country"   
         javaType="Country"  
         select="selectCountry"  
         column="countryId" />  
</resultMap>  
  <select id="selectById2" resultMap="peopleMapper2">  
  select pid,pname,countryId from people where pid = #{xxx}  
</select>  

總結(jié)

以上所述是小編給大家介紹的mybatis 一對(duì)一、一對(duì)多和多對(duì)多查詢,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)創(chuàng)新互聯(lián)網(wǎng)站的支持!

分享名稱:mybatis一對(duì)一、一對(duì)多和多對(duì)多查詢實(shí)例代碼
瀏覽地址:http://bm7419.com/article34/igeise.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、做網(wǎng)站網(wǎng)站建設(shè)、服務(wù)器托管、外貿(mào)建站品牌網(wǎng)站建設(shè)

廣告

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

h5響應(yīng)式網(wǎng)站建設(shè)