MyBatis DAO层传递参数到mapping.xml

总结我所用到的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}等对应实体类中属性。

原文地址:https://www.cnblogs.com/tanzq/p/8365839.html