1、spring中@Param ( import org.springframework.data.repository.query.Param; )

int selectRoleCount(@Param("businessId") Integer businessId,@Param("memberId") Long memberId);

2、mybatis中param,( import org.apache.ibatis.annotations.Param )

int selectRoleCount(@Param("businessId") Integer businessId,@Param("memberId") Long memberId);

从表面上看,两种并没有区别,但是在xml文件中使用的时候是有区别的

Spring中的@param在xml需要如下这样引用变量:

<select id="selectRoleCount" resultType="java.lang.Integer" >
select 
    count(tbm.id)
    from t_business_member_relation tbm
    where tbm.business_id = #{0,jdbcType=INTEGER}
    and tbm.member_id = #{1,jdbcType=INTEGER}
    and tbm.role_business_id is not null
</select>

是根据参数的顺序来取值的,并且从0开始。

 

而在mybatis @param在xml中则是如下这样引用变量的

<select id="selectRoleCount" resultType="java.lang.Integer" >
    select 
    count(tbm.id)
    from t_business_member_relation tbm
    where tbm.business_id = #{businessId,jdbcType=INTEGER}
    and tbm.member_id = #{memberId,jdbcType=INTEGER}
    and tbm.role_business_id is not null
</select>

是通过参数名来引用的。

 

注:如果Mapper.java文件中引用的是Spring的

org.springframework.data.repository.query.Param;

但是Mapper.xml中使用的是mybatis 的用法,那么就会如下的错误

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter 'businessId' not found. Available parameters are [1, 0, param1, param2]

 

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐