其實在小能就已經用過了,只不過很久沒用過這種查詢都快忘記了,從網上找了大部分都是讓人無語的方法。html
@Data @EqualsAndHashCode(callSuper = false) @NoArgsConstructor @AllArgsConstructor public class ShopGoodsEntity { private String goodsNumber; //商品編號 private String goodsName; //商品名稱 private String goodsTitle; //商品標題 private String productOne; //商品規格 private String goodsImageTopList; //輪播圖 private String goodsImageBottom; //介紹圖 private int goodsStatus; //0上架 1下架 private Date createTime; //商品建立時間 private int sellerId; //賣家id private String sellerPhone; //商家提供的客服電話 private Date activityStartTime; //活動開始時間 private Date activityEndTime; //活動結束時間 private int isActivity; //是否活動 private String activityName; //活動名稱 private List<ShopProductEntity> products; }
@Data @EqualsAndHashCode(callSuper = false) @NoArgsConstructor @AllArgsConstructor public class ShopProductEntity { private int id; private String productOne; private String name; //規格名稱 private int parameterId; //參數id private double originalPrice; //原價 private double activityPrice; //活動價 private int weight; //重量 private int repertoryNum;//庫存 private int sellNum; //已售數量 private int fakeNum; //作假數量 private ShopParameterEntity parameter; }
@Data @EqualsAndHashCode(callSuper = false) @NoArgsConstructor @AllArgsConstructor public class ShopParameterEntity { private int id; private int parameterType; //產品類型 private int productWeight;//單瓶產品重量 private int blisterWeight;//吸塑重量 private String size; //標籤尺寸 private String productName; //規格名稱 private int productId; //t_shop_product主鍵id }
從這三個實體類中能夠看到,分別對應的是1對多,一對一,用一個sql查詢出全部的數據,以下:java
collection 一對多 裏邊property 屬性值對應實體類的參數值,不能隨便寫sql
association 實體一對一 裏邊property 屬性值對應實體類的參數值,不能隨便寫spa
<resultMap id="allData" type="com.dtb.shop.model.ShopGoodsEntity"> <id column="goods_number" jdbcType="VARCHAR" property="goodsNumber" /> <result column="goods_name" jdbcType="VARCHAR" property="goodsName" /> <result column="goods_title" jdbcType="VARCHAR" property="goodsTitle" /> <result column="product_one" jdbcType="VARCHAR" property="productOne" /> <result column="goods_image_top_list" jdbcType="VARCHAR" property="goodsImageTopList" /> <result column="goods_image_bottom" jdbcType="VARCHAR" property="goodsImageBottom" /> <result column="goods_status" jdbcType="INTEGER" property="goodsStatus" /> <result column="create_time" jdbcType="TIMESTAMP" property="createTime" /> <result column="seller_id" jdbcType="INTEGER" property="sellerId" /> <result column="seller_phone" jdbcType="VARCHAR" property="sellerPhone" /> <result column="activity_start_time" jdbcType="TIMESTAMP" property="activityStartTime" /> <result column="activity_end_time" jdbcType="TIMESTAMP" property="activityEndTime" /> <result column="is_activity" jdbcType="INTEGER" property="isActivity" /> <result column="activity_name" jdbcType="VARCHAR" property="activityName" /> <collection property="products" javaType="java.util.ArrayList" ofType="com.dtb.shop.model.ShopProductEntity"> <id column="id" jdbcType="INTEGER" property="id" /> <result column="product_one" jdbcType="VARCHAR" property="productOne" /> <result column="name" jdbcType="VARCHAR" property="name" /> <result column="parameter_id" jdbcType="INTEGER" property="parameterId" /> <result column="original_price" jdbcType="DOUBLE" property="originalPrice" /> <result column="activity_price" jdbcType="DOUBLE" property="activityPrice" /> <result column="weight" jdbcType="INTEGER" property="weight" /> <result column="repertory_num" jdbcType="INTEGER" property="repertoryNum" /> <result column="sell_num" jdbcType="INTEGER" property="sellNum" /> <result column="fake_num" jdbcType="INTEGER" property="fakeNum" /> <association property="parameter" javaType="com.dtb.shop.model.ShopParameterEntity"> <id column="id" jdbcType="INTEGER" property="id" /> <result column="t_parameter_type" jdbcType="INTEGER" property="parameterType" /> <result column="t_product_weight" jdbcType="INTEGER" property="productWeight" /> <result column="t_blister_weight" jdbcType="INTEGER" property="blisterWeight" /> <result column="t_size" jdbcType="VARCHAR" property="size" /> <result column="t_product_name" jdbcType="VARCHAR" property="productName" /> <result column="t_product_id" jdbcType="INTEGER" property="productId" /> </association> </collection> </resultMap>
<select id="getDetailInfo" parameterType="java.lang.String" resultMap="allData"> select g.goods_number,g.goods_name,g.goods_title,g.product_one,g.goods_image_top_list,g.goods_image_bottom,g.goods_status, g.create_time,g.seller_id,g.seller_phone,g.activity_start_time,g.activity_end_time,g.is_activity,g.activity_name,p.id, p.product_one,p.name,p.parameter_id,p.original_price,p.activity_price,p.weight,p.repertory_num,p.sell_num,p.fake_num, m.id,m.t_parameter_type,m.t_product_weight,m.t_blister_weight,m.t_size,m.t_product_name,m.t_product_id from t_shop_goods g left join t_shop_product p on p.product_one = g.product_one left join t_shop_parameter m on p.parameter_id = m.id where g.goods_number = #{goodsNumber} </select>
結果就出來了,爲何還有那麼多讓人無語的博客呢?以下:.net
https://blog.csdn.net/qq_38157516/article/details/79712721htm
https://www.cnblogs.com/yansum/p/5819973.htmlblog
不知道這些sql還不是多個sql拼成的嘛ci