Difference between @ param in spring and @ param in mybatis, @ parammybatis

Source: Internet
Author: User

Difference between @ param in spring and @ param in mybatis, @ parammybatis

/*** Query whether the specified user and enterprise are associated with a configured role * @ param businessId memberId * @ return */int selectRoleCount (@ Param ("businessId") Integer businessId, @ Param ("memberId") Long memberId );
/*** Query whether the specified user and enterprise are associated with a configured role * @ param businessId memberId * @ return */int selectRoleCount (@ Param ("businessId") Integer businessId, @ Param ("memberId") Long memberId );

On the surface, there is no difference between the two types, but they are different when used in xml files. @ param in Spring needs to reference variables in xml as follows:

<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>

The value is based on the order of parameters, and starts from 0. In mybatis @ param, variables are referenced as follows in 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>

It is referenced by the parameter name.
Note: If the Mapper. java file references Spring

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

However, if mybatis is used in Mapper. xml, the following error occurs:

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

As follows:

Therefore, you must pay attention to the consistency between @ param reference and usage.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.