原文地址:http://blog.csdn.net/gao36951/article/details/44258217
- spring中@param
<code class="hljs java has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"> <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; background-color: rgb(238, 238, 238); 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;"><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>
- mybatis中的param
<code class="hljs java has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"> <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; background-color: rgb(238, 238, 238); 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;"><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; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">select</span> id=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"selectRoleCount"</span> resultType=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"java.lang.Integer"</span> > <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">select</span> count(tbm.id) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">from</span> t_business_member_relation tbm <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">where</span> tbm.business_id = <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#{0,jdbcType=INTEGER}</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">and</span> tbm.member_id = <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#{1,jdbcType=INTEGER}</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">and</span> tbm.role_business_id <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">is</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">not</span> null </<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">select</span>></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); 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;"><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中则是如下这样引用变量的
<code class="hljs vbnet has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">select</span> id=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"selectRoleCount"</span> resultType=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"java.lang.Integer"</span> > <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">select</span> count(tbm.id) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">from</span> t_business_member_relation tbm <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">where</span> tbm.business_id = <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#{businessId,jdbcType=INTEGER}</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">and</span> tbm.member_id = <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#{memberId,jdbcType=INTEGER}</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">and</span> tbm.role_business_id <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">is</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">not</span> null </<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">select</span>></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); 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;"><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; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">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; background-color: rgb(238, 238, 238); 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;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
但是Mapper.xml中使用的是mybatis 的用法,那么就会如下的错误
<code class="hljs oxygene has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">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; background-color: rgb(238, 238, 238); 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;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
所以在使用的时候一定要注意@param引用和使用的一致性