dao传递类参数 mybatis_MyBatis DAO层传递参数到mapping.xml 几种方式
Dao層傳遞參數到mapping.xml文件的幾種方式:(Mybatis傳值總結)
第一種:傳遞單個參數
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
count(1)
from?table_img?img
where?img.company_id?=?#{companyid}
and?img.id?=?#{id}
此時不需要寫parameterType,但是注意“#{}”內的參數名必須跟你在Dao層中注解@Param("")內定義的名稱一致。
2,直接傳遞參數
Dao層Code片段:
/**
*?查詢companyId是否存在.
*
*?@param?companyId
*?@param?imgId
*?@return?int
*/
public?int?queryCompanyIdAndImgIdIsExist(?Long?companyId,??Long?imgId);
Mapping片段:
select
count(1)
from?table_img?img
where?img.company_id?=?#{0}
and?img.id?=?#{1}
#{0}與#{1}是你在Dao里的參數順序
3,以Map傳遞參數
實現類Code片段:
Map?searchCondition?=?new?HashMap<>();
searchCondition.put("categoryId",?categoryId);
searchCondition.put("status",?status);
List?cmsProductArticles?=?cmsProdcutArticleDao.getCmsProductArticles(searchCondition);
Dao層Code片段:
/**
*?根據搜索條件查詢產品模板集.
*
*?@param?searchCondition
*?@return?List
*/
public?List?getCmsProductArticles(Map?searchCondition);
Mapping片段:
SELECT
*
FROM
table?a,?table?b
WHERE
a.article_id?=?b.article_id
and?a.del_flag?!=?2
and?a.category_id?=?#{categoryId}
and?a.status?=?#{status}
#{categoryId}、#{status}對應你在Map里面的Key
第三種:以實體類傳遞
Dao層Code片段:
/**
*?更新.
*
*?@param?cmsProductArticle
*?@return
*/
public?void?updateCmsProductArticle(CmsProductArticle?cmsProductArticle);
Mapping片段:
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
#{categoryId}等對應實體類中屬性。
天津雍博科技有限責任公司
歡迎大家訪問www.yongbosoft.com ,崇尚技術,為您提供完美產品,優質服務!
總結
以上是生活随笔為你收集整理的dao传递类参数 mybatis_MyBatis DAO层传递参数到mapping.xml 几种方式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: pdf导入ps颜色太浅_分享五个免费的p
- 下一篇: linux windows文件 编码_L