Mybatis自定义resultMap

2021-09-06 14:29:21 浏览数 (1)

根据门店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>

0 人点赞