Spring中@Param和Mybatis中的@Param

  1. spring中@param
<code class="hljs java has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">    <span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/**
      * 查询指定用户和企业关联有没有配置角色
      *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @param</span> businessId  memberId
      *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @return</span>
      */</span>
     <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> selectRoleCount(<span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Param</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"businessId"</span>) Integer businessId,<span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Param</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"memberId"</span>) Long memberId); </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul>
  1. mybatis中的param
<code class="hljs java has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">    <span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/**
      * 查询指定用户和企业关联有没有配置角色
      *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @param</span> businessId memberId
      *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @return</span>
      */</span>
     <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> selectRoleCount(<span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Param</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"businessId"</span>) Integer businessId,<span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Param</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"memberId"</span>) Long memberId); </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul>

从表面上看,两种并没有区别,但是在xml文件中使用的时候是有区别的,Spring中的@param在xml需要如下这样引用变量

<code class="hljs vbnet has-numbering" style="display: block; padding: 0px; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><span style="color:rgba(0, 0, 0, 0);background-color: transparent;"><</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; background-color: transparent;">select</span><span style="color:rgba(0, 0, 0, 0);background-color: transparent;"> id=</span><span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box; background-color: transparent;">"selectRoleCount"</span><span style="color:rgba(0, 0, 0, 0);background-color: transparent;"> resultType=</span><span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box; background-color: transparent;">"java.lang.Integer"</span><span style="color:rgba(0, 0, 0, 0);background-color: transparent;"> >
</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; background-color: transparent;">select</span><span style="color:rgba(0, 0, 0, 0);background-color: transparent;"> 
    count(tbm.id)
    </span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; background-color: transparent;">from</span><span style="color:rgba(0, 0, 0, 0);background-color: transparent;"> t_business_member_relation tbm
    </span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; background-color: transparent;">where</span><span style="color:rgba(0, 0, 0, 0);background-color: transparent;"> tbm.business_id = </span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; background-color: transparent;">#{</span><span class="hljs-preprocessor" style="box-sizing: border-box; background-color: rgb(255, 204, 204);">0</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; background-color: transparent;">,jdbcType=INTEGER}</span><span style="color:rgba(0, 0, 0, 0);background-color: transparent;">
    </span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; background-color: transparent;">and</span><span style="color:rgba(0, 0, 0, 0);background-color: transparent;"> tbm.member_id = </span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; background-color: transparent;">#{</span><span class="hljs-preprocessor" style="box-sizing: border-box; background-color: rgb(255, 204, 204);">1</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; background-color: transparent;">,jdbcType=INTEGER}</span><span style="color:rgba(0, 0, 0, 0);background-color: transparent;">
    </span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; background-color: transparent;">and</span><span style="color:rgba(0, 0, 0, 0);background-color: transparent;"> tbm.role_business_id </span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; background-color: transparent;">is</span><span style="color:rgba(0, 0, 0, 0);background-color: transparent;"> </span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; background-color: transparent;">not</span><span style="color:rgba(0, 0, 0, 0);background-color: transparent;"> null
</</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; background-color: transparent;">select</span><span style="color:rgba(0, 0, 0, 0);background-color: transparent;">></span></code><ul class="pre-numbering" style="color: rgb(51, 51, 51); box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li></ul><ul class="pre-numbering" style="color: rgb(51, 51, 51); box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li></ul>

是根据参数的顺序来取值的,并且从0开始。而在mybatis @param在xml中则是如下这样引用变量的(注意这里的参数,要和@Param括号里面的一致)

<code class="hljs vbnet has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; background-color: transparent;">select</span> id=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box; background-color: transparent;">"selectRoleCount"</span> resultType=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box; background-color: transparent;">"java.lang.Integer"</span> >
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; background-color: transparent;">select</span> 
    count(tbm.id)
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; background-color: transparent;">from</span> t_business_member_relation tbm
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; background-color: transparent;">where</span> tbm.business_id = <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#{<span style="background-color: rgb(255, 204, 204);">businessId</span>,jdbcType=INTEGER}</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; background-color: transparent;">and</span> tbm.member_id = <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; background-color: transparent;">#{</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; background-color: rgb(255, 204, 204);">memberId</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box; background-color: transparent;">,jdbcType=INTEGER}</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; background-color: transparent;">and</span> tbm.role_business_id <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; background-color: transparent;">is</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; background-color: transparent;">not</span> null
 </<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box; background-color: transparent;">select</span>></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li></ul>

是通过参数名来引用的 
注:如果Mapper.Java文件中引用的是Spring的

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">org<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.springframework</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.data</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.repository</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.query</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.Param</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

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

mybatis的需要引入的是:import org.apache.ibatis.annotations.Param;

<code class="hljs oxygene has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">org.mybatis.spring.MyBatisSystemException: <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">nested</span> exception <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">is</span> org.apache.ibatis.binding.BindingException: Parameter <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'businessId'</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">not</span> found. Available parameters are [<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, param1, param2]</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

截图如下 
这里写图片描述 
所以在使用的时候一定要注意@param引用和使用的一致性

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值