首页 > 其他 > 详细

mybatis两张表关联关系映射

时间:2019-06-07 22:34:58      阅读:143      评论:0      收藏:0      [点我收藏+]

一对多

方法一

<resultMap type="Teacher" id="teacherMap">
<id column="id" property="id"/>
<result column="name" property="name"/>
<collection property="students" ofType="Student" column="id">
<id column="sid" property="id"/><!-- 这里的column对应的是下面查询的别名,而不是表字段名 -->
<result column="sname" property="name"/><!-- property对应JavaBean中的属性名 -->
<result column="className" property="className"/>
</collection>
</resultMap>


<!-- 查询所有的老师级各自的所有学生 -->
<select id="getTeachers" parameterType="Teacher" resultMap="teacherMap">
SELECT
t.id,
t.NAME,
t.class_Name,
s.id AS sid,
s. NAME AS sname,
s.class_name as className
FROM
teacher t
LEFT JOIN student s ON t.id = s.teacher_id
</select>

 

方法二

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

 

多对一

<resultMap type="Student" id="studentMap">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="class_name" property="className"/>
        <result column="teacher_id" property="teacherId"/>
        <association property="teacher" select="getTeacher" column="teacher_id" javaType="Teacher">
        <!-- 这里要注意的是column对应的是student中的外键,而且需是表字段名 -->
        </association>
    </resultMap>
    
    
    <select id="getStudent" resultMap="studentMap">
        SELECT
            s.id,
            s.name,
            s.class_name,
            s.teacher_id
        FROM
            student s
    </select>
    
    <select id="getTeacher" resultType="Teacher" parameterType="int">
        SELECT
            t.id,
            t.name,
            t.class_name as className 
        FROM teacher t 
        where id = #{teacher_id}
    </select>

多对多查询

 <resultMap id="courseMapper" type="Course">

        <id property="id" column="cid"/>
        <result property="name" column="cname"/>
        <collection property="students" ofType="Student">
            <id property="id" column="sid"/>
            <result property="name" column="sname"/>
        </collection>
    </resultMap>


    <select id="selectCourseStudent" resultMap="courseMapper">
        SELECT
            c.id cid, c.name cname, s.id sid, s.name sname
        FROM
            t_course c,
            t_student s,
            t_student_course sc
        WHERE
            c.id = #{id}
            AND s.id = sc.sid
            AND c.id = sc.cid;
    </select>

 

mybatis两张表关联关系映射

原文:https://www.cnblogs.com/liuna369-4369/p/10989208.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!