国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Java > 正文

mybatis 一對一、一對多和多對多查詢實例代碼

2024-07-14 08:41:12
字體:
來源:轉載
供稿:網友

關鍵字:association 一對一映射(一個班級只有一個班主任)

  <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>  

關鍵字:collection 一對多映射(一個老師有多個學生)

<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>    <!-- 查詢所有的老師級各自的所有學生 -->    <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> 

    關鍵字:association 多對一映射(多個人屬于一個國家)

             多對一相當于一對多,也可以使用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>  

總結

以上所述是小編給大家介紹的mybatis 一對一、一對多和多對多查詢,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對VeVb武林網網站的支持!


注:相關教程知識閱讀請移步到JAVA教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 沭阳县| 扶风县| 岳普湖县| 洛阳市| 比如县| 通州区| 贵溪市| 沅江市| 玛多县| 都安| 涞源县| 永州市| 河源市| 莎车县| 威宁| 酒泉市| 开江县| 广汉市| 库伦旗| 宁陕县| 天长市| 万全县| 磐安县| 辽阳县| 盘山县| 杭州市| 乳山市| 元谋县| 克拉玛依市| 那曲县| 安乡县| 泰宁县| 铁岭市| 定日县| 微山县| 嘉义市| 永新县| 白玉县| 冷水江市| 华宁县| 东兴市|