国产探花免费观看_亚洲丰满少妇自慰呻吟_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教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 武山县| 和田市| 康定县| 大安市| 卢湾区| 加查县| 文登市| 麟游县| 西吉县| 河间市| 乌兰察布市| 九江县| 呼图壁县| 高淳县| 扎兰屯市| 平江县| 修水县| 湟源县| 云霄县| 渭源县| 汝阳县| 呼图壁县| 江陵县| 墨玉县| 苏尼特左旗| 建昌县| 赣州市| 尼勒克县| 合肥市| 霞浦县| 怀宁县| 四平市| 苍梧县| 琼海市| 光泽县| 浦县| 石棉县| 延庆县| 绍兴县| 洪江市| 绩溪县|