zoukankan      html  css  js  c++  java
  • mybatis两张表关联关系映射

    一对多

    方法一

    <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>
  • 相关阅读:
    MyEclipse 常用快捷键
    javaEE基础08
    MySql卸载重新安装出现Start service没有响应的解决办法(64位)
    javaSE基础07
    为WAMP中的mysql设置密码(默认为空)
    javaSE基础06
    javaSE基础05
    vue框架构建项目流程
    阿里云或本地部署服务器(一)---nginx本地和服务器代理
    修改vue element Transfer 穿梭框里内容区的宽度
  • 原文地址:https://www.cnblogs.com/liuna369-4369/p/10989208.html
Copyright © 2011-2022 走看看