码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2020-01-09 01:09:43      阅读:97      评论:0      收藏:0      [点我收藏+]

标签:integer   price   utils   over   单表   apache   inf   名称   pac   

 

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<Goods> 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<Goods> getGoodsList() {
        return goodsList;
    }

    public void setGoodsList(List<Goods> 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);
}

 

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper  PUBLIC
        "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.chy.mapper.OrderMapper">
    <select id="queryOrderByOrderNo" parameterType="integer" resultMap="orderResultWithGoods">
        SELECT order_tb.*,goods_tb.*,order_item_tb.purchase_amount
        FROM order_tb,goods_tb,order_item_tb
        WHERE order_tb.no=#{no} AND order_item_tb.order_no=order_tb.no AND goods_tb.id=order_item_tb.goods_id
    </select>
    <resultMap id="orderResultWithGoods" type="order">
        <id property="no" column="no"/>
        <result property="userId" column="user_id"/>
        <collection property="goodsList" ofType="goods">
            <id property="id" column="id"/>
            <result property="goodsName" column="goods_name"/>
            <result property="goodsPrice" column="goods_price"/>
            <result property="purchaseAmount" column="purchase_amount"/>
        </collection>
    </resultMap>
</mapper>

三表联合查询,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 关联查询的实现:多对多

标签:integer   price   utils   over   单表   apache   inf   名称   pac   

原文地址:https://www.cnblogs.com/chy18883701161/p/12167005.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!