zoukankan      html  css  js  c++  java
  • mybatis文件映射之利用association进行关联查询(二)

    与初探中大部分一样,只是修改resultMap中的相关信息。

        <resultMap type="com.gong.mybatis.bean.Employee" id="MySimpleMap2">
            <id column="id" property="id"/>
            <result column="last_name" property="lastName"/>
            <result column="gender" property="gender"/>
            <result column="email" property="email"/>
            <association property="dept" javaType="com.gong.mybatis.bean.Department">
                <id column="did" property="id"/>
                <result column="dept_name" property="deptName"/>
            </association>
        </resultMap>
        <select id="getEmpAndDept" resultMap="MySimpleMap2">
            SELECT e.id,e.last_name,e.gender,e.email,d.dept_name,d.id did 
            FROM tbl_employee e,tbl_department d
            WHERE e.d_id = d.id and e.id=#{id};
        </select>

    说明:在resultMap中使用association标签,property属性对应着Employee中的成员属性的名称,javaType对应着该成员属性的类型,子标签id标识tbl_department中的主键,因为我们在select标签中将d.id取别名did,所以将did映射给Department中的id属性,然后通过result标签将普通的字段映射给Department中的deptName属性。

    结果:

    DEBUG 01-20 12:42:43,808 ==> Preparing: SELECT e.id,e.last_name,e.gender,e.email,d.dept_name,d.id did FROM tbl_employee e,tbl_department d WHERE e.d_id = d.id and e.id=?; (BaseJdbcLogger.java:145)
    DEBUG 01-20 12:42:43,887 ==> Parameters: 3(Integer) (BaseJdbcLogger.java:145)
    DEBUG 01-20 12:42:43,926 <== Total: 1 (BaseJdbcLogger.java:145)
    Employee [id=3, lastName=小红, gender=0, email=xiaohong@qq.com, dept=Department [id=1, deptName=开发部]]
    Department [id=1, deptName=开发部]

    说明这种方法也是可以的。

  • 相关阅读:
    【UOJ#77】A+B Problem
    【AGC048B】Bracket Score
    ubuntu 下python opengl编程(2)
    网站建设的营销途径
    python脚本初探---新手写的QQ邮箱发送脚本
    Cstyle的C语言笔记 ---UEFI当中的面向对象模式
    date得到当前日期
    简单几步让SecureCRT更舒服【图文并茂】
    苹果的airplayer推荐
    【Cocos2d-X开发学习笔记】第22期:事件处理机制之触屏事件
  • 原文地址:https://www.cnblogs.com/xiximayou/p/12217526.html
Copyright © 2011-2022 走看看