spring中@param和mybatis中@param使用區別,@parammybatis

來源:互聯網
上載者:User

spring中@param和mybatis中@param使用區別,@parammybatis

    /**      * 查詢指定使用者和企業關聯有沒有配置角色      * @param businessId  memberId      * @return      */     int selectRoleCount(@Param("businessId") Integer businessId,@Param("memberId") Long memberId); 
    /**      * 查詢指定使用者和企業關聯有沒有配置角色      * @param businessId memberId      * @return      */     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]

如下

所以在使用的時候一定要注意@param引用和使用的一致性

相關文章

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.