MyBatis 关联查询的实现:多对多

 

2个实体:订单、商品,一个订单可以包含多种商品,同时一种商品可以属于多个订单,即多对多。

 

商品表goods_tb:

 

 

订单表order_tb:

no是订单编号,user_id与用户表的id关联。

 

 

需要新建一张中间表order_item_tb,引入2个“多”的主键作为外键,把这2个“多”联系起来:

purchase_amount是该种商品的购买数量。

 

 


 

 

使用嵌套结果实现一对多

(一)编写pojo类

package com.chy.pojo;

public class Goods {
    private Integer id;  //商品id
    private String goodsName;  //商品名称
    private float goodsPrice;  //商品单价
    private Integer purchaseAmount;  //购买数量

    public Integer getId() {
        return id;
    }

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

    public String getGoodsName() {
        return goodsName;
    }

    public void setGoodsName(String goodsName) {
        this.goodsName = goodsName;
    }

    public float getGoodsPrice() {
        return goodsPrice;
    }

    public void setGoodsPrice(float goodsPrice) {
        this.goodsPrice = goodsPrice;
    }

    public Integer getPurchaseAmount() {
        return purchaseAmount;
    }

    public void setPurchaseAmount(Integer purchaseAmount) {
        this.purchaseAmount = purchaseAmount;
    }

    @Override
    public String toString() {
        return "Goods{" +
                "id=" + id +
                ", goodsName='" + goodsName + '\'' +
                ", goodsPrice=" + goodsPrice +
                ", purchaseAmount=" + purchaseAmount +
                '}';
    }
}

 

package com.chy.pojo;

import java.util.List;

public class Order {
    private Integer no;
    private Integer userId;
    private List goodsList;  //包含的商品

    public Integer getNo() {
        return no;
    }

    public void setNo(Integer no) {
        this.no = no;
    }

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public List getGoodsList() {
        return goodsList;
    }

    public void setGoodsList(List goodsList) {
        this.goodsList = goodsList;
    }

    @Override
    public String toString() {
        return "Order{" +
                "no=" + no +
                ", userId=" + userId +
                ", goodsList=" + goodsList +
                '}';
    }
}

在哪个pojo中使用List来关联其他实体,需要根据业务需求来确定。

 

 

(二)编写Mapper接口、映射文件

package com.chy.mapper;

import com.chy.pojo.Order;

public interface OrderMapper {
    //根据orderId查询订单信息
    public Order queryOrderByOrderNo(Integer orderNo);
}

 


DOCTYPE mapper  PUBLIC
        "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

    
    
        
        
        
            
            
            
            
        
    

三表联合查询,sql语句很长,如果觉得表名、字段名不好写,可以用as设置表名、字段名的别名。

不是每个pojo类都要写对应的Mapper接口、映射文件,写哪些,看业务需求。

 

 

(三)使用

package com.chy.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;

public class MyBatisUtils {
    private static SqlSessionFactory sqlSessionFactory;

    static {
        try {
            InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static SqlSession getSqlSession(){
        return sqlSessionFactory.openSession();
    }
}

 

package com.chy.test;

import com.chy.mapper.OrderMapper;
import com.chy.pojo.Order;
import com.chy.utils.MyBatisUtils;
import org.apache.ibatis.session.*;

public class Test {
    public static void main(String[] args) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
        Order order = mapper.queryOrderByOrderNo(1);
        System.out.println(order);
        sqlSession.close();
    }
}

 

 

结果:

Order{no=1, userId=1, goodsList=[Goods{id=1, goodsName='抽纸', goodsPrice=6.5, purchaseAmount=1}, Goods{id=2, goodsName='中华', goodsPrice=80.0, purchaseAmount=2}]}

 

 

也可以使用嵌套查询来实现多对多,但嵌套查询缺点较多,不推荐。

mybatis让开发人员专注于数据库的设计、sql语句的编写,而不需要花费过多精力在jdbc的底层操作上。

你可能感兴趣的:(MyBatis 关联查询的实现:多对多)