Java自学之mybatis:一对多查询

学习目的:使用mybatis,进行一对多查询。

Part 1

清空category_和product_表中的数据,修改product_表:

create table vaefun_mybatis.product_
(
    id    int auto_increment
        primary key,
    name  varchar(30)     null,
    price float default 0 null,
    cid   int             null
)
    charset = utf8;

在表中添加数据:

INSERT INTO vaefun_mybatis.product_ (id, name, price, cid) VALUES (1, 'product a', 88.88, 1);
INSERT INTO vaefun_mybatis.product_ (id, name, price, cid) VALUES (2, 'product b', 88.88, 1);
INSERT INTO vaefun_mybatis.product_ (id, name, price, cid) VALUES (3, 'product c', 88.88, 1);
INSERT INTO vaefun_mybatis.product_ (id, name, price, cid) VALUES (4, 'product x', 88.88, 2);
INSERT INTO vaefun_mybatis.product_ (id, name, price, cid) VALUES (5, 'product y', 88.88, 2);
INSERT INTO vaefun_mybatis.product_ (id, name, price, cid) VALUES (6, 'product z', 88.88, 2);
INSERT INTO vaefun_mybatis.category_ (id, name) VALUES (1, 'category1');
INSERT INTO vaefun_mybatis.category_ (id, name) VALUES (2, 'category2');

Part 2

pojo类

Product

package cn.vaefun.pojo;

public class Product {
    private int id;
    private String name;
    private float price;

    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 float getPrice() {
        return price;
    }

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

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

Category

package cn.vaefun.pojo;

import java.util.List;

public class Category {
    private int id;
    private String name;
    List<Product> products;

    public List<Product> getProducts() {
        return products;
    }

    public void setProducts(List<Product> products) {
        this.products = products;
    }

    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;
    }

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

Part 3

配置pojo.xml,通过left join关联查询,对Category和Product表进行关联查询。

与前面学习的有所区别,这里不是用的resultType, 而是resultMap,通过resultMap把数据取出来放在对应的对象属性里。

: Category的id 字段 和Product的id字段同名,Mybatis不知道谁是谁的,所以需要通过取别名cid,pid来区分。name字段同理。

新增以下配置:

    <resultMap type="Category" id="categoryBean">
        <id column="cid" property="id" />
        <result column="cname" property="name" />

        <!-- 一对多的关系 -->
        <!-- property: 指的是集合属性的值, ofType:指的是集合中元素的类型 -->
        <collection property="products" ofType="Product">
            <id column="pid" property="id" />
            <result column="pname" property="name" />
            <result column="price" property="price" />
        </collection>
    </resultMap>

    <!-- 关联查询分类和产品表 -->
    <select id="listCategoryP" resultMap="categoryBean">
            select c.*, p.*, c.id 'cid', p.id 'pid', c.name 'cname', p.name 'pname' from category_ c left join product_ p on c.id = p.cid
    </select>

Part 4

测试代码块

    /**
     * 一对多
     */
    public static void listCategory(SqlSession session){
        List<Category> cs = session.selectList("listCategoryP");
        for (Category c : cs) {
            System.out.println(c);
            List<Product> ps = c.getProducts();
            for (Product p : ps) {
                System.out.println("\t"+p);
            }
        }
    }

测试结果

v2-8d2d973b23ddb2857611c13e23b5bb20_b.jpg

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值