zoukankan      html  css  js  c++  java
  • Java_myBatis_一对多映射

    例如我们有需求需要实现以下查询  "一个用户对多条订单编号":

    select user.*,o.number,o.createtime from user left JOIN orders o on o.user_id = user.id 

    这时候,我们需要在映射配置文件中使用resultMap

    <resultMap type="com.mavenTest.mybatis_test.po.User" id="SelectList">
        <id column="id" property="id"/>
        <result column="username" property="username"/>
        <result column="birthday" property="birthday"/>
        <result column="sex" property="sex"/>
        <result column="address" property="address"/>
        <!-- 映射嵌套对象:orders信息  (一对多) -->
        <!-- association的类型需要使用javaType来指定 -->
        <collection property="orderList" ofType="com.mavenTest.mybatis_test.po.Order">
            <result column="number" property="number"/>
            <result column="createtime" property="createtime"/>
        </collection>
    </resultMap>
    <select id="SelectList" resultMap="SelectList">
        select user.*,o.number from user left JOIN orders o on o.user_id = user.id 
    </select>

    就是说,当我们的POJO中存在一个List属性的时候,我们需要把这个属性放在collection标签,然后把这个list对应的POJO卸载ofType上

  • 相关阅读:
    ZOJ
    CodeForces
    模板
    前门
    错误记录
    2021/1/10例会 academy of management journal 2014vol 57 No.2,484-514
    Day7下
    Day7上
    Day6 下(
    Day6上 括号匹配专项
  • 原文地址:https://www.cnblogs.com/amiezhang/p/9610443.html
Copyright © 2011-2022 走看看