dao层传参到mapper接收不到的问题

当在DAO层传参到Mapper时遇到参数接收不到的问题,本文介绍了四种解决方法:1) 单个参数,确保parameterType和#{}内的参数名匹配;2) 多个参数,可通过注解或直接传参,保持参数名一致性;3) 使用Map传参,保证Map Key与#{}内参数一致;4) 通过实体类传参,#{}对应实体类属性。同时提醒注意,mapper.xml中的SQL不应以分号结尾。

dao层传参到mapper接收不到的问题

传参方式有多种,如果从dao层传参到mapper接收不到,首先,排除单词写错的可能,如果你有习惯从sql工具里面写完了复制到mapper文件,那你就要注意一个问题了,就是mapper.xml文件里面写sql是不能用;(分号)结尾的,下面介绍下几种传参方式

每天开发很累吧!小弟专门做了个公众号叫“冲浪蝌蚪”,专门发布搞笑段子,让你每天一乐,帮你解压在这里插入图片描述

第一种:传递单个参数

Dao层Code片段:

/**
 * 根据articleId查询XXXX详情.
 * 
 * @param articleId
 * @return {@link CmsProductArticle}
 */
public CmsProductArticle getCmsProductArticleByArticleId(Long articleId);

Mapping片段:

SELECT
*
FROM
tableA a, tableB b
WHERE
a.article_id = b.article_id
and a.del_flag != 2
and b.article_id = #{articleId}

传递单个参数时直接将parameterType设置成你传入的参数类型(Long),直接用“#{}”获得参数,参数名必须与Dao层参数名一致。

resultType是SQL查询结果返回的类型,Dao层接口返回是实体类,所以这里的resultType是实体类的路径(按住ctrl键,鼠标点击路径时可以直接进入实体类时路径正确)

第二种:传递多个参数

1,以注解标记
Dao层Code片段:

/**
 * 查询companyId是否存在.
 * 
 * @param companyId
 * @param imgId
 * @return int
 */
public int queryCompanyIdAndImgIdIsExist(@Param("companyid") Long companyId, @Param("id") Long imgId);

Mapping片段:

<select id="queryCompanyIdAndImgIdIsExist" resultType="Integer">
    select 
        count(1)
     from table_img img
    where img.company_id = #{companyid}
      and img.id = #{id}
</select>

此时不需要写 parameterType,但是注意“#{}”内的参数名必须跟你在Dao层中注解@Param("")内定义的名称一致。

2,直接传递参数

Dao层Code片段:

/**
 * 查询companyId是否存在.
 * 
 * @param companyId
 * @param imgId
 * @return int
 */
public int queryCompanyIdAndImgIdIsExist( Long companyId,  Long imgId);

Mapping片段:

<select id="queryCompanyIdAndImgIdIsExist" resultType="Integer">
    select 
        count(1)
     from table_img img
    where img.company_id = #{0}
      and img.id = #{1}
</select>

#{0}与#{1}是你在Dao里的参数顺序

3,以Map传递参数

实现类Code片段:

   Map<String,Object> searchCondition = new HashMap<>();
   searchCondition.put("categoryId", categoryId);
   searchCondition.put("status", status);
   List<CmsProductArticle> cmsProductArticles = cmsProdcutArticleDao.getCmsProductArticles(searchCondition);

Dao层Code片段:

/**
 * 根据搜索条件查询产品模板集.
 * 
 * @param searchCondition
 * @return List<CmsProductArticle>
 */
public List<CmsProductArticle> getCmsProductArticles(Map<String, Object> searchCondition);

Mapping片段:

   <select id="getCmsProductArticles" parameterType="java.util.Map" resultType="xxx.xxxxxxx.xxx.xxxxx.xxxxxxx.CmsProductArticle">
    SELECT 
        *
    FROM 
         table a, table b
    WHERE 
         a.article_id = b.article_id
     and a.del_flag != 2 
     <if test="categoryId != null">
             and a.category_id = #{categoryId}
     </if>
     <if test="status != null">
             and a.status = #{status}
     </if>
    </select>

{ categoryId}、#{status}对应你在Map里面的Key

第三种:以实体类传递

Dao层Code片段:

/**
 * 更新.
 * 
 * @param cmsProductArticle
 * @return 
 */
public void updateCmsProductArticle(CmsProductArticle cmsProductArticle);

Mapping片段:

<update id="updateCmsProductArticleBase" parameterType="xxx.xxxxxxx.xxx.xxxxx.xxxxxxx.CmsProductArticle">
    UPDATE table 
    SET 
        category_id = #{categoryId}, supply_type = #{supplyType}, pay_type = #{payType}, pay_value = #{payValue}, status = #{status}
    WHERE 
          article_id = #{articleId}
      and del_flag != 2
</update>

#{categoryId}等对应实体类中属性。

每天开发很累吧!小弟专门做了个公众号叫“冲浪蝌蚪”,专门发布搞笑段子,让你每天一乐,帮你解压

在这里插入图片描述扫码关注,给个支持,谢谢啦

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值