shop--7.店铺信息编辑--Dao层

根据ShopId获取店铺信息

店铺编辑

首先需要获取店铺的信息,然后在此基础上更改

编写店铺查询Shop queryByShopId(long shopId);

但是呢,查询店铺的时候也会返回一些别的信息如ShopCategory、Area等信息

所以resultType就不能封装了,使用的是联合查询

在进行联合查询时,接收联合查询的返回值不能这样单纯的定义为Shop,而要将信息封装为一个ResultMap 

使用association定义关联的单个对象的封装规则;

JavaEE--Mybatis学习笔记(五)--关联关系查询

 

ResultMap的定义

 1     <resultMap type="com.ryanxu.o2o.entity.Shop" id="shopMap">
 2         <id column="shop_id" property="shopId" />
 3         <result column="shop_name" property="shopName" />
 4         <result column="shop_desc" property="shopDesc" />
 5         <result column="shop_addr" property="shopAddr" />
 6         <result column="phone" property="phone" />
 7         <result column="shop_img" property="shopImg" />
 8         <result column="priority" property="priority" />
 9         <result column="create_time" property="createTime" />
10         <result column="last_edit_time" property="lastEditTime" />
11         <result column="enable_status" property="enableStatus" />
12         <result column="advice" property="advice" />
13         <association property="area" column="area_id"
14             javaType="com.ryanxu.o2o.entity.Area">
15             <id column="area_id" property="areaId" />
16             <result column="area_name" property="areaName" />
17         </association>
18         <association property="shopCategory"
19             column="shop_category_id"
20             javaType="com.ryanxu.o2o.entity.ShopCategory">
21             <id column="shop_category_id" property="shopCategoryId" />
22             <result column="shop_category_name"
23                 property="shopCategoryName" />
24         </association>
25         <association property="owner" column="user_id"
26             javaType="com.ryanxu.o2o.entity.PersonInfo">
27             <id column="user_id" property="userId" />
28             <result column="name" property="name" />
29         </association>
30     </resultMap>
其中association就是关联到其他表的

 

SQL语句

    <select id="queryByShopId" resultMap="shopMap"
        parameterType="Long">
        SELECT
        s.shop_id,
        s.shop_name,
        s.shop_desc,
        s.shop_addr,
        s.phone,
        s.shop_img,
        s.priority,
        s.create_time,
        s.last_edit_time,
        s.enable_status,
        s.advice,
        a.area_id,
        a.area_name,
        sc.shop_category_id,
        sc.shop_category_name
        FROM
        tb_shop s,
        tb_area a,
        tb_shop_category sc
        WHERE
        s.area_id = a.area_id
        AND
        s.shop_category_id = sc.shop_category_id
        AND
        s.shop_id = #{shopId}
    </select>

 

posted @ 2018-07-29 14:23  windbag7  阅读(201)  评论(0编辑  收藏  举报