前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Mybatis自定义resultMap

Mybatis自定义resultMap

作者头像
chenchenchen
发布2021-09-06 14:29:21
7310
发布2021-09-06 14:29:21
举报
文章被收录于专栏:chenchenchenchenchenchen

根据门店ID,查询门店和服务列表,一个门店对应多个服务

代码语言:javascript
复制
<!-- 门店和服务列表查询映射结果 -->
    <resultMap id="storeAndServices"
               type="com.mall.vo.response.StoreDetailRes">
        <result property="storeId" column="store_id" />
        <result property="storeImg" column="store_img" />
        <result property="name" column="storename" />
        <result property="address" column="address" />
        <result property="phone" column="phone" />
        <result property="lon" column="longitude" />
        <result property="lat" column="latitude" />
        <collection property="servicelist" javaType="list"
                    ofType="com.mall.vo.response.ServiceDetailRes">
            <result property="serviceCode" column="service_code" />
            <result property="serviceName" column="service_name" />
            <result property="price" column="actually_amount" />
            <result property="serviceType" column="service_type" />
        </collection>
    </resultMap>

    <select id="getStoreDetail" resultMap="storeAndServices">
        SELECT
            t.store_id,
            t.store_img,
            t.storename,
            t.address,
            t.phone,
            t.longitude,
            t.latitude,
            t1.service_code,
            t1.service_name,
            t1.actually_amount,
            t1.service_type
        FROM
            t_store t,
            t_service t1
        WHERE
            t.store_id = t1.store_id
        <if test="storeId !=null and storeId !='' ">
            AND t.store_id = #{storeId}
        </if>

    </select>

通用查询映射结果,避免使用select *

代码语言:javascript
复制
<!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.mall.entity.Service">
        <id column="id" property="id" />
        <result column="store_id" property="storeId"/>
        <result column="service_code" property="serviceCode"/>
        <result column="service_name" property="serviceName"/>
        <result column="service_type" property="serviceType"/>
        <result column="actually_amount" property="actuallyAmount"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        ID, STORE_ID, SERVICE_CODE, SERVICE_NAME, SERVICE_TYPE, ACTUALLY_AMOUNT
    </sql>

<select id="getServiceList" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/>
        FROM
            t_service t
        <trim prefix="WHERE" prefixOverrides="AND |OR ">
            <if test="storeId !=null and storeId !='' ">
                AND t.store_id = #{storeId}
            </if>
        </trim>
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021-06-17 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 根据门店ID,查询门店和服务列表,一个门店对应多个服务
  • 通用查询映射结果,避免使用select *
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档