mybatis @param
总结我所用到的MyBatis,Dao层传递参数到mapping.xml文件的几种方式:
第一种:传递单个参数
Dao层Code片段:
/**
* 根据articleId查询XXXX详情.
*
* @param articleId
* @return {@link CmsProductArticle}
*/
public CmsProductArticle getCmsProductArticleByArticleId(Long articleId); Mapping片段:
/**
* 查询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片段:
/** * 更新. * * @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}等对应实体类中属性。
相关推荐
xiuyangsong 2020-11-16
Nishinoshou 2020-11-09
jimgreatly 2020-09-01
dongxurr 2020-08-18
Dullonjiang 2020-08-15
Dullonjiang 2020-08-11
Dullonjiang 2020-08-09
dongxurr 2020-08-08
yunzhonmghe 2020-08-07
jimgreatly 2020-08-03
Dullonjiang 2020-07-30
jimgreatly 2020-07-27
liqiancao 2020-07-26
xiuyangsong 2020-07-26
dongxurr 2020-07-26
mcvsyy 2020-07-26
helloxusir 2020-07-25
牧场SZShepherd 2020-07-20