mybatis之多对一映射关系处理

实例:多个商品对应一个工厂

有两种方法实现多对一映射,查询每个商品对应的工厂
方法一、按照查询嵌套处理
先查出所有商品,然后根据结果集的商品的factoryid查出对应的product

  1. 实体类:

在商品的实体类中加入一个工厂的实体属性,完成映射

package com.wei.entity;

public class Product {
    private int id;
    private String name;
    private  double price;
//这里需要一个工厂的实体属性,对应一个工厂
    private Factory factory;

    public Product() {
    }

    public Product(int id, String name, double price, Factory factory) {
        this.id = id;
        this.name = name;
        this.price = price;
        this.factory = factory;
    }

    @Override
    public String toString() {
        return "Product{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", price=" + price +
                ", factory=" + factory +
                '}';
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public Factory getFactory() {
        return factory;
    }

    public void setFactory(Factory factory) {
        this.factory = factory;
    }
}

package com.wei.entity;

public class Factory {
    private int factoryid;
    private  String name;

    public Factory() {
    }

    public Factory(int factoryid, String name) {
        this.factoryid = factoryid;
        this.name = name;
    }

    @Override
    public String toString() {
        return "Factory{" +
                "factoryid=" + factoryid +
                ", name='" + name + '\'' +
                '}';
    }

    public int getFactoryid() {
        return factoryid;
    }

    public void setFactoryid(int factoryid) {
        this.factoryid = factoryid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

2.Mapper映射文件
这里主要是使用了resultMap标签的association属性,通过association将工厂表中包含的factoryid列和工厂实体类的factory属性关联起来,javaType代表返回的类型,association中select属性其实就是在getProductList的查询里面嵌套了一个子查询getFactory。

<!--namespace:指向sql是属于那个Mapper的接口-->
<mapper namespace="com.wei.Mapper.ProductMapper">
   <resultMap id="producttMap" type="com.wei.entity.Product">
       <result column="id" property="id"></result>
       <result column="name" property="name"></result>
       <result column="price" property="price"></result>
<!--        对于复杂的属性,我们需要单独处理-->
<!--        属性是对象使用association,是集合使用collection-->
       <association property="factory" column="factoryid" javaType="factory" select="getFactory"></association>
<!--        <collection property=""></collection>-->
   </resultMap>

   <select id="getProductList" resultMap="producttMap">
       select * from tab_product
</select>
   <select id="getFactory" resultType="factory">
   <!--       这里的#{id}只是一个占位符,没有特定的意思-->
       select * from tab_factory where factoryid=#{id}
   </select>

  1. 测试
//工具类
public class MybatisUtil {
    private static SqlSessionFactory sqlSessionFactory;
    static {
        try {
            InputStream inputStream = MybatisUtil.class.getResourceAsStream("/mybatis-config.xml");
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public static SqlSession getSqlSession(){
        return sqlSessionFactory.openSession();
    }
}
//测试
public class ProductTest {

    @Test
    public  void  getfactoryListtest(){
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
        List<Product> productList = mapper.getProductList();
        for(Product product:productList){
            System.out.println(product);
        }
        sqlSession.close();
    }
}

方法二、按照结果嵌套处理

其他代码同上,只改变了mapper.xml文件内容
sql查询语句的改变。
在association 标签中的javaType为返回类型,association中的result标签对应factory类的属性

<!--    方法二、按照结果嵌套处理:-->
    <select id="getProductList2" resultMap="producttMap2">
        select *
        from tab_factory f,tab_product p
        where f.factoryid =p.factoryid
    </select>
    <resultMap id="producttMap2" type="com.wei.entity.Product">
        <result column="id" property="id"></result>
        <result column="name" property="name"></result>
        <result column="price" property="price"></result>
        <!--        对于复杂的属性,我们需要单独处理-->
        <!--        属性是对象使用association,是集合使用collection-->
        <association property="factory" javaType="factory">
            <result column="factoryid" property="factoryid"></result>
            <result column="name" property="name"></result>
        </association>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值