Mybatis学习第19节 -- 嵌套查询一对多的配置

一对多和多对多是一样的.

而多对多是指 A对于B来说是一对多的关系, 同时B对于A来说也是一对多的关系, 互为一对多,即为多对多.

比如说一个标签下面有多篇文章,一篇文章也可能有多个标签

Shop实体类设计

List<Product> productList;

Product实体类设计

public class Product implements Serializable{

    Integer id;    String name;    String desc;    String imgAddr;    String normalPrice;    String promotionPrice;    Integer priority;    Date createTime;    Date lastEditTime;    Integer enableStatus;

ProductMapper接口

List<Product> getProductListByShopID(Integer id);

Product映射文件

<?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="io.github.coinsjack.dao.ProductMapper">    <cache/>    <resultMap id="productResultMap" type="Product">        <result column="product_id" property="id"/>        <result column="product_name" property="name"/>        <result column="product_desc" property="desc"/>        <result column="img_addr" property="imgAddr"/>        <result column="normal_price" property="normalPrice"/>        <result column="promotion_price" property="promotionPrice"/>        <result column="create_time" property="createTime"/>        <result column="last_edit_time" property="lastEditTime"/>        <result column="enable_status" property="enableStatus"/>        <result column="product_category_id" property="categoryID"/>        <result column="shop_id" property="shopID"/>    </resultMap>

    <select id="getProductListByShopID" resultMap="productResultMap">        select * from tb_product        WHERE `shop_id` = #{id};    </select></mapper>

测试使用Product

public class ProductMapperTest {    @Test    public void test_getProductByID() {        SqlSession sqlSession = MyBatisUtil.getSqlSession();        ProductMapper productMapper= sqlSession.getMapper(ProductMapper.class);

        System.out.printf("查询结果: %s%n", productMapper.getProductListByShopID(29));;    }}

测试Product

ShopMapper配置文件

<resultMap id="simpleResultMap" type="Shop">    <id column="shop_id" property="id" ></id>    <result column="owner_id" property="ownerId" ></result>    <result column="shop_category_id" property="categoryId" ></result>    <result column="shop_name" property="name"></result>    <result column="shop_desc" property="desc"></result>    <result column="shop_addr" property="addr"></result>    <result column="phone" property="phone"></result>    <result column="shop_img" property="image"></result>    <result column="priority" property="priority"></result>    <result column="create_time" property="createTime"></result>    <result column="last_edit_time" property="lastEditTime"></result>    <result column="enable_status" property="enableStatus"></result>    <association property="area" column="area_id" javaType="Area"                 select="io.github.coinsjack.dao.AreaMapper.getAreaById"/>    <collection property="productList" column="shop_id"                select="io.github.coinsjack.dao.ProductMapper.getProductListByShopID" javaType="ArrayList" ofType="Product" >    </collection></resultMap>

id, result, association, collection的顺序不能错, 因为有Mapper对应的构造器的原因, association的column属性用来给select指定的方法进行参数传入, collection也是一样

测试查询Shop中的所有商品

@Testpublic void testGetShopById() {    SqlSession session = MyBatisUtil.getSqlSession();    ShopMapper mapper = session.getMapper(ShopMapper.class);    System.out.println(mapper.getShopById(29));    session.close();}

测试结果

Shop{id=29, ownerId=1, area=Area{id=3, name=‘长治学院‘, priority=2, createTime=null, lastEditTime=null}, categoryId=22, name=‘暴漫奶茶店‘, desc=‘过来喝喝就知道啦,你是我的奶茶‘, addr=‘西苑1号‘, phone=‘1211334565‘, image=‘/upload/images/item/shop/29/2017092601054939287.jpg‘, priority=40, createTime=2017-09-26, lastEditTime=2017-09-26, enableStatus=1, advice=‘null‘, productList=[Product{id=1, name=‘大黄人‘, desc=‘我是大黄人‘, imgAddr=‘upload/images/item/shop/29/2017092601204036435.jpg‘, normalPrice=‘2‘, promotionPrice=‘1‘, priority=100, createTime=Tue Sep 26 09:20:40 CST 2017, lastEditTime=Tue Sep 26 09:20:40 CST 2017, enableStatus=1, CategoryID=3, shopID=29}
, Product{id=2, name=‘小黄人‘, desc=‘我是小黄人‘, imgAddr=‘upload/images/item/shop/29/2017092601212211185.jpg‘, normalPrice=‘3‘, promotionPrice=‘2‘, priority=90, createTime=Tue Sep 26 09:21:22 CST 2017, lastEditTime=Tue Sep 26 09:21:22 CST 2017, enableStatus=1, CategoryID=2, shopID=29}
, Product{id=3, name=‘暴漫人‘, desc=‘开心了‘, imgAddr=‘upload/images/item/shop/29/2017092601220059819.jpg‘, normalPrice=‘3‘, promotionPrice=‘2‘, priority=80, createTime=Tue Sep 26 09:22:00 CST 2017, lastEditTime=Tue Sep 26 09:22:00 CST 2017, enableStatus=1, CategoryID=3, shopID=29}
, Product{id=4, name=‘宇宙第一‘, desc=‘宇宙无敌‘, imgAddr=‘upload/images/item/shop/29/2017092601224389939.jpg‘, normalPrice=‘5‘, promotionPrice=‘2‘, priority=70, createTime=Tue Sep 26 09:22:43 CST 2017, lastEditTime=Tue Sep 26 09:22:43 CST 2017, enableStatus=1, CategoryID=3, shopID=29}
, Product{id=5, name=‘眼凸凸‘, desc=‘宇宙无敌‘, imgAddr=‘upload/images/item/shop/29/2017092601231570458.jpg‘, normalPrice=‘3‘, promotionPrice=‘2‘, priority=60, createTime=Tue Sep 26 09:23:15 CST 2017, lastEditTime=Tue Sep 26 09:23:15 CST 2017, enableStatus=1, CategoryID=3, shopID=29}
, Product{id=6, name=‘笑眯眯‘, desc=‘笑眯眯 甜蜜蜜‘, imgAddr=‘upload/images/item/shop/29/2017092601234922140.jpg‘, normalPrice=‘2‘, promotionPrice=‘2‘, priority=50, createTime=Tue Sep 26 09:23:49 CST 2017, lastEditTime=Tue Sep 26 09:23:49 CST 2017, enableStatus=1, CategoryID=3, shopID=29}
]}

原文地址:https://www.cnblogs.com/litran/p/10546245.html

时间: 2024-08-30 15:43:26

Mybatis学习第19节 -- 嵌套查询一对多的配置的相关文章

Mybatis学习第2节 -- 模糊查询之#和$的区别

先说结论, #是占位符,而$的行为是字符串拼接, 在参数是java的基本类型且只有一个参数的情况下,使用$时,只能用value作为参数传递 需求决定设计, 先在interface里面添加相应方法 public interface ShopMapper { Shop getShopById(Integer id); Shop getShopByIdAlias(Integer id); Shop getShopByTitleContainDollar(String value); Shop getS

mybatis学习笔记(11)-多对多查询

mybatis学习笔记(11)-多对多查询 mybatis学习笔记11-多对多查询 示例 多对多查询总结 resultMap总结 本文实现多对多查询,查询用户及用户购买商品信息. 示例 查询主表是:用户表 关联表:由于用户和商品没有直接关联,通过订单和订单明细进行关联,所以关联表:orders.orderdetail.items sql SELECT orders.*, user.username, user.sex, user.address, orderdetail.id orderdeta

MyBatis学习总结——实现关联表查询(转)

原文链接:孤傲苍狼 一.一对一关联 1.1.提出需求 根据班级id查询班级信息(带老师的信息) 1.2.创建表和数据 创建一张教师表和班级表,这里我们假设一个老师只负责教一个班,那么老师和班级之间的关系就是一种一对一的关系. 1 CREATE TABLE teacher( 2 t_id INT PRIMARY KEY AUTO_INCREMENT, 3 t_name VARCHAR(20) 4 ); 5 CREATE TABLE class( 6 c_id INT PRIMARY KEY AUT

Mybatis学习总结四(关联查询)

一.一对一查询 实例:查询所有订单信息,关联查询下单用户信息. Method1:使用resultType,定义订单信息po类,此po类中包括了订单信息和用户信息. 1 public class OrdersCustom extends Orders { 2 3 private String username;// 用户名称 4 private String address;// 用户地址 5 get/set.... Mapper.xml <!-- 查询所有订单信息 --> <select

Mybatis学习第25节 -- 懒加载 积极与不积极

积极懒加载是指如果你访问一个对象的属性,Mybatis就会帮你把需要进步一查询的该属性或者其他属性在数据库中查询出来. 不积极懒加载是指,有这种必要的时候,采取进行必要的数据库检索 我看的教程中的Mybatis版本默认是积极的lazy加载, 而我实际用的Mybatis3.4.6实际上默认不积极的lazy加载. mybatis-config.xml文件 <settings> <!-- 打印查询语句 --> <setting name="logImpl" va

Mybatis学习总结(九)——查询缓存

一.什么是查询缓存 mybatis提供查询缓存,用于减轻数据压力,提高数据库性能.mybaits提供一级缓存和二级缓存. 1.一级缓存是sqlSession级别的缓存.在操作数据库时需要构造sqlSession对象,在对象中有一个数据结构(HashMap),用于存储缓存数据.不同的sqlSession之间的缓存区域(HashMap)是互不影响的. 2.二级缓存是mapper级别的缓存,多个sqlSession去操作同一个Mapper的sql语句,多个SqlSession可以公用二级缓存,二级缓存

Mybatis学习第23节 -- 鉴别器 discriminator

discriminator或者叫做分类器 Vehicle类   package io.github.coinsjack.pojo; import java.util.Date; public class Vehicle { protected Integer id; protected String vin; protected Date year; protected String make; protected String model; protected String color; pu

MyBatis学习之一对多关联查询

<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <typeAliases> <ty

Mybatis学习第5节 -- 插入并获取ID

插入过程 接口 int insertShop(Shop shop); 映射 <insert id="insertShop" parameterType="Shop" useGeneratedKeys="true" keyProperty="id"> INSERT INTO `oto`.`tb_shop` ( `owner_id`, `area_id`, `shop_category_id`, `shop_name`