一对多和多对多是一样的.
而多对多是指 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-11-10 14:59:33