Mybatis框架-17:多表操作之一对多——分布查询

第一步:建立mapper

客户的mapper

List<Customer> getAllCustomer();

订单的mapper

因为这里是根据客户的id进行的查询,所以形参为integer类型

Order getOrderWithCustId(Integer custId);

第二步:编写mapper.xml中的sql语句

客户的mapper.xml

这里同样是查询完客户以后,再讲客户的id作为参数,去订单的mapper.xml中寻找相应的语句进行查询

<resultMap id="allCustomerMap2" type="com.ctbu.domain.Customer">
        <id column="cust_id" property="cust_id"/>
        <result column="cust_name" property="cust_name"/>
        <result column="cust_phone" property="cust_phone"/>
        <result column="cust_profession" property="cust_profession"/>
        <result column="email" property="email"/>
        <collection property="orders" ofType="com.ctbu.domain.Order" javaType="list" select="com.ctbu.mapper.OrderMapper.getOrderWithCustId" column="cust_id"/>
    </resultMap>

    <select id="getAllCustomer" resultMap="allCustomerMap2">
        select * from `customer`;
    </select>

订单的mapper.xml

<select id="getOrderWithCustId" parameterType="integer" resultType="com.ctbu.domain.Order">
        select * from  `order` where orderCustId=#{id};
    </select>

第三步:编写测试类

@Test
    public void test7(){
        SqlSession session = MyUtils.openSession();
        CustomerMapper mapper = session.getMapper(CustomerMapper.class);
        List<Customer> allCustomer = mapper.getAllCustomer();
        for (Customer customer : allCustomer) {
            System.out.println(customer);
        }
        session.close();
    }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值