mybatis的resultMap最多可以完成多少层的关联查询

清风无恙 发布于 2016/12/18 22:10
阅读 389
收藏 1

二级缓存关闭情况下测试,日志中没有发出查询item的语句,配置都是正确的为什么查不出来值?resultMap最多能有几层嵌套关联查询?


mapper配置

<resultMap type="Order" id="orderMap" autoMapping="true">

        <id column="id" property="id" javaType="Integer" />
        <result column="user_id" property="userId" javaType="Long"/>
        <result column="order_number" property="orderNumber" javaType="String"/>
        <association column="user_id" property="user" javaType="User" select="queryUserById"></association>
        <!-- 通过order的id来查找orderdetail -->
        <collection autoMapping="true" select="queryOrderdetailByOrderId" column="id" property="orderdetails" javaType="java.util.List" ofType="Orderdetail">
            <id column="id" property="id" javaType="Integer"/>
            <result column="total_price" property="totalPrice" javaType="Double"/>
            <association  property="item"   javaType="Item" autoMapping="true"
             select="queryItemById" column="item_id" fetchType="eager">
            </association>
        </collection>

    </resultMap>

    <select id="queryUserById" resultType="User">
        select <include refid="com.vskar.dao.UserMapper.baseUserSql"/> from tb_user where id=#{id}
    </select>
    <select id="queryOrderdetailByOrderId" resultType="Orderdetail">
        select <include refid="baseOrderdetailSql"/> from tb_orderdetail where order_id=#{id}
    </select>
    <select id="queryItemById" resultType="Item">
        select <include refid="baseItemSql"/> from tb_item where id=#{id}
    </select>


测试代码:

public void orderTest(){
        List<Order> orders=orderService.queryAllOrder();
        for (Order order : orders) {
            System.out.println(order);
            System.out.println(order.getUser());
            List<Orderdetail> orderdetails = order.getOrderdetails();
            for (Orderdetail orderdetail : orderdetails) {
                System.out.println(orderdetail);
                Item item = orderdetail.getItem();
                System.out.println(item);
            }
        }
    }

测试结果:

Order [id=1, userId=1, orderNumber=20140921001]
User [id=1, userName=zhangsan, password=123456, name=张三, age=30, sex=1, birthday=Wed Aug 08 00:00:00 CST 1984, created=Fri Sep 19 16:56:04 CST 2014, updated=Sun Sep 21 11:24:59 CST 2014]
Orderdetail [id=1, totalPrice=5288.0, status=1]
null       item值未查出
Orderdetail [id=2, totalPrice=6288.0, status=1]
null           item值未查出
Order [id=2, userId=2, orderNumber=20140921002]
User [id=2, userName=lisi, password=123456, name=李四, age=21, sex=2, birthday=Tue Jan 01 00:00:00 CST 1991, created=Fri Sep 19 16:56:04 CST 2014, updated=Fri Sep 19 16:56:04 CST 2014]
Orderdetail [id=3, totalPrice=6288.0, status=1]
null           item值未查出
Order [id=3, userId=1, orderNumber=20140921003]
User [id=1, userName=zhangsan, password=123456, name=张三, age=30, sex=1, birthday=Wed Aug 08 00:00:00 CST 1984, created=Fri Sep 19 16:56:04 CST 2014, updated=Sun Sep 21 11:24:59 CST 2014]
Orderdetail [id=4, totalPrice=5288.0, status=1]
null              item值未查出


加载中
返回顶部
顶部