zoukankan      html  css  js  c++  java
  • mybatis中的多表查询

    1)无延迟加载的一对一关联


    <resultMap type="Userbean" id="baseMap">
    <id column="userid" property="userid"/>
    <result column="username" property="username"/>
    <collection property="dep" ofType="Dept">
    <id column="did" property="did"/>
    <result column="dname" property="dname"/>
    </collection>

    </resultMap>

    <select id="queAll" resultMap="baseMap">
    select userid,username,t2.dname dname from t_user t1 inner join t_dept t2 on t1.did = t2.did
    </select>

    2)无延迟加载的一对多关联

    <resultMap type="Userbean" id="baseMap">
    <id column="userid" property="userid"/>
    <result column="username" property="username"/>
    <association property="dep" javaType="Dept">
    <id column="did" property="did"/>
    <result column="dname" property="dname"/>
    </association>
    </resultMap>

    <select id="queAll1" resultMap="baseMap">
    select userid,username,t2.dname dname from t_user t1 inner join t_dept t2 on t1.did = t2.did
    </select>

    3)有延迟加载的一对多(一对一和一对多差不多)

    <resultMap type="Userbean" id="baseMap">
    <id column="userid" property="userid"/>
    <result column="username" property="username"/>
    <association property="dep" column="did" select="findDeptByDid" javaType="Dept">
    <id column="did" property="did"/>
    <result column="dname" property="dname"/>
    </association>


    <!-- <collection property="dep" column="did" select="findDeptByDid" javaType="Dept">
    <id column="did" property="did"/>
    <result column="dname" property="dname"/>
    </collection> -->
    </resultMap>

    <select id="findDeptByDid" resultType="Dept" parameterType="int">
    select dname,did from t_dept where did=#{did}
    </select>


    <select id="queAll" resultMap="baseMap">
    select userid,username,did from t_user t1

    </select>

  • 相关阅读:
    10.2 处理大集合
    观察者模式——出版者与订阅者
    phonegap(cordova) 自己定义插件代码篇(五)----android ,iOS 集成微信登陆
    另一鲜为人知的单例写法-ThreadLocal
    Spring MVC中Controller如何将数据返回给页面
    IntelliJ IDEA安装主题详细步骤
    oracle导出dmp文件的2种方法
    Oracle导出表(即DMP文件)的两种方法
    Spring MVC 实现文件的上传和下载
    压力测试 JMeter3.3
  • 原文地址:https://www.cnblogs.com/joyous-day/p/6143608.html
Copyright © 2011-2022 走看看