標籤:too 多個參數 log list map void mod xxxxx get
總結我所用到的MyBatis,Dao層傳遞參數到mapping.xml檔案的幾種方式:
第一種:傳遞單個參數
Dao層Code片段:
[java] view plain copy
- /**
- * 根據articleId查詢XXXX詳情.
- *
- * @param articleId
- * @return {@link CmsProductArticle}
- */
- public CmsProductArticle getCmsProductArticleByArticleId(Long articleId);
Mapping片段:
[sql] view plain copy
- <select id="getCmsProductArticleByArticleId" parameterType="Long" resultType="xxx.xxxxx.xxx.xxxxx.xxx.CmsProductArticle">
- SELECT
- *
- FROM
- tableA a, tableB b
- WHERE
- a.article_id = b.article_id
- and a.del_flag != 2
- and b.article_id = #{articleId}
- </select>
傳遞單個參數時直接將parameterType設定成你傳入的參數類型(Long),直接用“#{}”獲得參數,參數名必須與Dao層參數名一致。
resultType是SQL查詢結果返回的類型,Dao層介面返回是實體類,所以這裡的resultType是實體類的路徑(按住ctrl鍵,滑鼠點擊路徑時可以直接進入實體類時路徑正確)
第二種:傳遞多個參數
1,以註解標記
Dao層Code片段:
[java] view plain copy
- /**
- * 查詢companyId是否存在.
- *
- * @param companyId
- * @param imgId
- * @return int
- */
- public int queryCompanyIdAndImgIdIsExist(@Param("companyid") Long companyId, @Param("id") Long imgId);
Mapping片段:
[java] view plain copy
- <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片段:
[java] view plain copy
- /**
- * 查詢companyId是否存在.
- *
- * @param companyId
- * @param imgId
- * @return int
- */
- public int queryCompanyIdAndImgIdIsExist( Long companyId, Long imgId);
Mapping片段:
[java] view plain copy
- <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片段:
[java] view plain copy
- Map<String,Object> searchCondition = new HashMap<>();
- searchCondition.put("categoryId", categoryId);
- searchCondition.put("status", status);
- List<CmsProductArticle> cmsProductArticles = cmsProdcutArticleDao.getCmsProductArticles(searchCondition);
Dao層Code片段:
[java] view plain copy
- /**
- * 根據搜尋條件查詢產品樣板集.
- *
- * @param searchCondition
- * @return List<CmsProductArticle>
- */
- public List<CmsProductArticle> getCmsProductArticles(Map<String, Object> searchCondition);
Mapping片段:
[sql] view plain copy
- <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片段:
[java] view plain copy
- /**
- * 更新.
- *
- * @param cmsProductArticle
- * @return
- */
- public void updateCmsProductArticle(CmsProductArticle cmsProductArticle);
Mapping片段:
[sql] view plain copy
- <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}等對應實體類中屬性。
MyBatis DAO層傳遞參數到mapping.xml