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上

猜你喜欢

转载自www.cnblogs.com/amiezhang/p/9610443.html