ssm @Param

部分内容转载自:https://blog.csdn.net/DJ_coder/article/details/81050817

1.接口方法只有一个参数
如果只有一个参数的情况下框架是可以自动识别的 用不用 @Param效果一样

public interface PaperDao {
    Paper queryById(long id);
} 

此时相应的xml文件中,#{}中可以填写任意名称

<select id="queryById" parameterType="long" resultMap="resultMap1">
    SELECT paper_id,name,number,detail
    FROM paper
    WHERE paper_id=#{id}
</select>

但是如果sql语句需要传入多个参数 框架自己不知道怎么匹配 就需要使用@Param
注解

    /**
     * 分页查询店铺,可输入的条件有:店铺名(模糊查询)、店铺状态、店铺类别,区域id、owner
     *  @Param 注解需要 是因为接口中有多个参数  不注解配置文件不知道哪一个参数对应那一个参数
     *
     * @param shopCondition
     * @param rowIndex
     *            从第几行开始取:rowIndex
     * @param pageSize
     *            返回的条数:pageSize
     * @return
     */
    List<Shop> queryShopList(@Param("shopCondition") Shop shopCondition, @Param("rowIndex") int rowIndex,
                             @Param("pageSize") int pageSize);


需要精确的匹配传入的三个参数:

<!--  通过shopId获取店铺的信息-->
    <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>
    <!--  查询每个商户的全部商铺-->
    <select id="queryShopList" resultMap="shopMap">
        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>
            <!--   根据商店的分类查询所有的商店-->
            <!--  联系另外一张表查询相对应的信息-->
            <!--  查询所有的二级目录分类Id-->
            <!--  根据商店分类的二级目录的父类即上一级目录的分类Id查询二级分类目录下的分类Id-->
            <!--  即查询的是某个一级分类下所有的店铺-->
             <!--  多表关联-->
             <!--  分页条件-->
            <if
                    test="shopCondition.shopCategory != null and
                    shopCondition.shopCategory.shopCategoryId != null">
                AND
                s.shop_category_id=#{shopCondition.shopCategory.shopCategoryId}
            </if>

            <if
                    test="shopCondition.shopCategory != null and
                    shopCondition.shopCategory.parent != null and
                    shopCondition.shopCategory.parent.shopCategoryId != null">
                AND

                s.shop_category_id in
                (select shop_category_id from
                tb_shop_category
                WHERE

                parent_id=#{shopCondition.shopCategory.parent.shopCategoryId})
            </if>
            <if
                    test="shopCondition.area != null and
                    shopCondition.area.areaId != null">
                AND
                s.area_id=#{shopCondition.area.areaId}
            </if>
            <if test="shopCondition.shopName != null">
                AND
                s.shop_name like '%${shopCondition.shopName}%'
            </if>
            <if test="shopCondition.enableStatus != null">
                AND
                s.enable_status=#{shopCondition.enableStatus}
            </if>
            <if
                    test="shopCondition.owner != null
                    and shopCondition.owner.userId != null">
                AND
                s.owner_id=#{shopCondition.owner.userId}
            </if>
            AND

            s.area_id=a.area_id
            AND
            s.shop_category_id=sc.shop_category_id
        </where>
        ORDER BY
        s.priority DESC

        LIMIT #{rowIndex},#{pageSize}
    </select>

猜你喜欢

转载自blog.csdn.net/qq_42664961/article/details/86553669
ssm
今日推荐