美文网首页
关联表查询

关联表查询

作者: 提笔忘字_波 | 来源:发表于2019-05-15 08:57 被阅读0次

    一对一关联

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <!-- 为这个mapper指定一个唯一的namespace,namespace的值习惯上设置成包名+sql映射文件名,这样就能够保证namespace的值是唯一的
    例如namespace="me.gacl.mapping.classMapper"就是me.gacl.mapping(包名)+classMapper(classMapper.xml文件去除后缀)
     -->
    <mapper namespace="me.gacl.mapping.classMapper">
    
        <!-- 
            根据班级id查询班级信息(带老师的信息)
            ##1. 联表查询
            SELECT * FROM class c,teacher t WHERE c.teacher_id=t.t_id AND c.c_id=1;
            
            ##2. 执行两次查询
            SELECT * FROM class WHERE c_id=1;  //teacher_id=1
            SELECT * FROM teacher WHERE t_id=1;//使用上面得到的teacher_id
         -->
    
        <!-- 
        方式一:嵌套结果:使用嵌套结果映射来处理重复的联合结果的子集
                 封装联表查询的数据(去除重复的数据)
            select * from class c, teacher t where c.teacher_id=t.t_id and c.c_id=1
        -->
        <select id="getClass" parameterType="int" resultMap="ClassResultMap">
            select * from class c, teacher t where c.teacher_id=t.t_id and c.c_id=#{id}
        </select>
        <!-- 使用resultMap映射实体类和字段之间的一一对应关系 -->
        <resultMap type="me.gacl.domain.Classes" id="ClassResultMap">
            <id property="id" column="c_id"/>
            <result property="name" column="c_name"/>
            <association property="teacher" javaType="me.gacl.domain.Teacher">
                <id property="id" column="t_id"/>
                <result property="name" column="t_name"/>
            </association>
        </resultMap>
        
        <!-- 
        方式二:嵌套查询:通过执行另外一个SQL映射语句来返回预期的复杂类型
            SELECT * FROM class WHERE c_id=1;
            SELECT * FROM teacher WHERE t_id=1   //1 是上一个查询得到的teacher_id的值
        -->
         <select id="getClass2" parameterType="int" resultMap="ClassResultMap2">
            select * from class where c_id=#{id}
         </select>
         <!-- 使用resultMap映射实体类和字段之间的一一对应关系 -->
         <resultMap type="me.gacl.domain.Classes" id="ClassResultMap2">
            <id property="id" column="c_id"/>
            <result property="name" column="c_name"/>
            <association property="teacher" column="teacher_id" select="getTeacher"/>
         </resultMap>
         
         <select id="getTeacher" parameterType="int" resultType="me.gacl.domain.Teacher">
            SELECT t_id id, t_name name FROM teacher WHERE t_id=#{id}
         </select>
    
    </mapper>
    

    一对多关联

    <!-- 
            根据classId查询对应的班级信息,包括学生,老师
         -->
        <!-- 
        方式一: 嵌套结果: 使用嵌套结果映射来处理重复的联合结果的子集
        SELECT * FROM class c, teacher t,student s WHERE c.teacher_id=t.t_id AND c.C_id=s.class_id AND  c.c_id=1
         -->
        <select id="getClass3" parameterType="int" resultMap="ClassResultMap3">
            select * from class c, teacher t,student s where c.teacher_id=t.t_id and c.C_id=s.class_id and  c.c_id=#{id}
        </select>
        <resultMap type="me.gacl.domain.Classes" id="ClassResultMap3">
            <id property="id" column="c_id"/>
            <result property="name" column="c_name"/>
            <association property="teacher" column="teacher_id" javaType="me.gacl.domain.Teacher">
                <id property="id" column="t_id"/>
                <result property="name" column="t_name"/>
            </association>
            <!-- ofType指定students集合中的对象类型 -->
            <collection property="students" ofType="me.gacl.domain.Student">
                <id property="id" column="s_id"/>
                <result property="name" column="s_name"/>
            </collection>
        </resultMap>
        
        <!-- 
            方式二:嵌套查询:通过执行另外一个SQL映射语句来返回预期的复杂类型
                SELECT * FROM class WHERE c_id=1;
                SELECT * FROM teacher WHERE t_id=1   //1 是上一个查询得到的teacher_id的值
                SELECT * FROM student WHERE class_id=1  //1是第一个查询得到的c_id字段的值
         -->
         <select id="getClass4" parameterType="int" resultMap="ClassResultMap4">
            select * from class where c_id=#{id}
         </select>
         <resultMap type="me.gacl.domain.Classes" id="ClassResultMap4">
            <id property="id" column="c_id"/>
            <result property="name" column="c_name"/>
            <association property="teacher" column="teacher_id" javaType="me.gacl.domain.Teacher" select="getTeacher2"></association>
            <collection property="students" ofType="me.gacl.domain.Student" column="c_id" select="getStudent"></collection>
         </resultMap>
         
         <select id="getTeacher2" parameterType="int" resultType="me.gacl.domain.Teacher">
            SELECT t_id id, t_name name FROM teacher WHERE t_id=#{id}
         </select>
         
         <select id="getStudent" parameterType="int" resultType="me.gacl.domain.Student">
            SELECT s_id id, s_name name FROM student WHERE class_id=#{id}
         </select>
    

    相关文章

      网友评论

          本文标题:关联表查询

          本文链接:https://www.haomeiwen.com/subject/wcjuaqtx.html