Java自学之mybatis:注解方式实现多对多查询

学习目的:在xml方式实现多对多查询的基础上,学习使用注解方式多对多查询。

Part 1

Mapper

ProductMapper,提供get方法。

package cn.vaefun.mapper;

import cn.vaefun.pojo.Product;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface ProductMapper {
    @Select("select * from product_ where cid=#{cid}")
    public List<Product> listByCategory(int cid);
    @Select("select * from product_")
    @Results({
            @Result(property = "category",column = "cid",one = @One(select = "cn.vaefun.mapper.CategoryMapper.get"))
    })
    public List<Product> productList();
    @Select("select * from product_ where id = #{id}")
    public Product get(int id);
}

OrderItemMapper,提供listByOrder方法。与product建立多对一关系。

package cn.vaefun.mapper;

import java.util.List;

import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import cn.vaefun.pojo.OrderItem;

public interface OrderItemMapper {

    @Select(" select * from order_item_ where oid = #{oid}")
    @Results({
            @Result(property="product",column="pid",one=@One(select="cn.vaefun.mapper.ProductMapper.get"))
    })
    public List<OrderItem> listByOrder(int oid);
}

OrderMapper,提供list方法,与OrderItem建立一对多的关系。

package cn.vaefun.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import cn.vaefun.pojo.Order;

public interface OrderMapper {
    @Select("select * from order_")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "orderItems", javaType = List.class, column = "id",
                    many = @Many(select = "cn.vaefun.mapper.OrderItemMapper.listByOrder"))
    })
    public List<Order> list();

}

Part 2

在mybatis-config.xml中配置所需的映射:

        <mapper class="cn.vaefun.mapper.ProductMapper"/>
        <mapper class="cn.vaefun.mapper.OrderMapper"/>
        <mapper class="cn.vaefun.mapper.OrderItemMapper"/>

Part 3

测试代码块:

private static void listOrderByzhujie(SqlSession session) {
        OrderMapper mapper =session.getMapper(OrderMapper.class);
        List<Order> os = mapper.list();
        for (Order o : os) {
            System.out.println(o.getCode());
            List<OrderItem> ois= o.getOrderItems();
            if(null!=ois){
                for (OrderItem oi : ois) {
                    System.out.format("\t%s\t%f\t%d%n", oi.getProduct().getName(),oi.getProduct().getPrice(),oi.getNumber());
                }
            }

        }
    }

测试结果:

v2-8f695636621391257ca354e286c267e4_b.jpg

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值