MyBatis DAO層傳遞參數到mapping.xml

來源:互聯網
上載者:User

標籤:too   多個參數   log   list   map   void   mod   xxxxx   get   

總結我所用到的MyBatis,Dao層傳遞參數到mapping.xml檔案的幾種方式:

第一種:傳遞單個參數

Dao層Code片段:

 

[java] view plain copy 
  1. /** 
  2.  * 根據articleId查詢XXXX詳情. 
  3.  *  
  4.  * @param articleId 
  5.  * @return {@link CmsProductArticle} 
  6.  */  
  7. public CmsProductArticle getCmsProductArticleByArticleId(Long articleId);  

Mapping片段:

 

 

[sql] view plain copy 
  1. <select id="getCmsProductArticleByArticleId" parameterType="Long" resultType="xxx.xxxxx.xxx.xxxxx.xxx.CmsProductArticle">  
  2.     SELECT   
  3.         *  
  4.     FROM   
  5.          tableA a, tableB b  
  6.     WHERE   
  7.          a.article_id = b.article_id  
  8.      and a.del_flag != 2      
  9.      and b.article_id = #{articleId}    
  10. </select>  

 

傳遞單個參數時直接將parameterType設定成你傳入的參數類型(Long),直接用“#{}”獲得參數,參數名必須與Dao層參數名一致。

resultType是SQL查詢結果返回的類型,Dao層介面返回是實體類,所以這裡的resultType是實體類的路徑(按住ctrl鍵,滑鼠點擊路徑時可以直接進入實體類時路徑正確)

 

第二種:傳遞多個參數

1,以註解標記

Dao層Code片段:

 

[java] view plain copy 
  1. /** 
  2.  * 查詢companyId是否存在. 
  3.  *  
  4.  * @param companyId 
  5.  * @param imgId 
  6.  * @return int 
  7.  */  
  8. public int queryCompanyIdAndImgIdIsExist(@Param("companyid") Long companyId, @Param("id") Long imgId);  

Mapping片段:

 

 

[java] view plain copy 
  1. <select id="queryCompanyIdAndImgIdIsExist" resultType="Integer">  
  2.     select   
  3.         count(1)  
  4.      from table_img img  
  5.     where img.company_id = #{companyid}  
  6.       and img.id = #{id}  
  7. </select>  

此時不需要寫parameterType,但是注意“#{}”內的參數名必須跟你在Dao層中註解@Param("")內定義的名稱一致。

 

 

2,直接傳遞參數

Dao層Code片段:

 

[java] view plain copy 
  1. /** 
  2.  * 查詢companyId是否存在. 
  3.  *  
  4.  * @param companyId 
  5.  * @param imgId 
  6.  * @return int 
  7.  */  
  8. public int queryCompanyIdAndImgIdIsExist( Long companyId,  Long imgId);  

Mapping片段:

 

 

[java] view plain copy 
  1. <select id="queryCompanyIdAndImgIdIsExist" resultType="Integer">  
  2.     select   
  3.         count(1)  
  4.      from table_img img  
  5.     where img.company_id = #{0}  
  6.       and img.id = #{1}  
  7. </select>  

#{0}與#{1}是你在Dao裡的參數順序

 

 

3,以Map傳遞參數

實作類別Code片段:

 

[java] view plain copy 
  1. Map<String,Object> searchCondition = new HashMap<>();  
  2. searchCondition.put("categoryId", categoryId);  
  3. searchCondition.put("status", status);  
  4. List<CmsProductArticle> cmsProductArticles = cmsProdcutArticleDao.getCmsProductArticles(searchCondition);  

 

Dao層Code片段:

 

[java] view plain copy 
  1. /** 
  2.  * 根據搜尋條件查詢產品樣板集. 
  3.  *  
  4.  * @param searchCondition 
  5.  * @return List<CmsProductArticle> 
  6.  */  
  7. public List<CmsProductArticle> getCmsProductArticles(Map<String, Object> searchCondition);  

 

Mapping片段:

 

[sql] view plain copy 
  1. <select id="getCmsProductArticles" parameterType="java.util.Map" resultType="xxx.xxxxxxx.xxx.xxxxx.xxxxxxx.CmsProductArticle">  
  2.  SELECT   
  3.      *  
  4.  FROM   
  5.       table a, table b  
  6.  WHERE   
  7.       a.article_id = b.article_id  
  8.   and a.del_flag != 2   
  9.   <if test="categoryId != null">  
  10.           and a.category_id = #{categoryId}  
  11.   </if>  
  12.   <if test="status != null">  
  13.           and a.status = #{status}  
  14.   </if>  
  15.  </select>  

#{categoryId}、#{status}對應你在Map裡面的Key

 

 

第三種:以實體類傳遞

Dao層Code片段:

 

[java] view plain copy 
  1. /** 
  2.  * 更新. 
  3.  *  
  4.  * @param cmsProductArticle 
  5.  * @return  
  6.  */  
  7. public void updateCmsProductArticle(CmsProductArticle cmsProductArticle);  

Mapping片段:

 

 

[sql] view plain copy 
  1. <update id="updateCmsProductArticleBase" parameterType="xxx.xxxxxxx.xxx.xxxxx.xxxxxxx.CmsProductArticle">  
  2.     UPDATE table   
  3.     SET   
  4.         category_id = #{categoryId}, supply_type = #{supplyType}, pay_type = #{payType}, pay_value = #{payValue}, status = #{status}  
  5.     WHERE   
  6.           article_id = #{articleId}  
  7.       and del_flag != 2  
  8. </update>  

#{categoryId}等對應實體類中屬性。

MyBatis DAO層傳遞參數到mapping.xml

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.