zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    (十三)子查询
    (十二)多表查询
    MFC读写配置ini文件
    (十一)分组函数(多行函数)
    Django(二十一)组合搜索
    Django(二十)model中的 class Meta
    (十)单行函数
    (九)逻辑运算,order by,desc
    类作为成员变量
    内部类——匿名内部类
  • 原文地址:https://www.cnblogs.com/windbag7/p/9385202.html
Copyright © 2011-2022 走看看