MyBatis 3.3.1版本新功能示例
MyBatis3.3.1更新日志:
https://github.com/mybatis/mybatis-3/issues?q=milestone%3A3.3.1
这里不对更新做翻译或者其他详细介绍。
这个更新除了一些bug修复,还有两个新增的功能:
- 增加了对批量插入回写自增主键的功能
- 增加了注解引用
@Results
的功能
下面通过简单例子来介绍这两个功能,为了例子的简洁,这里都使用注解实现的,没有用XML,批量插入的例子很容易就能变成XML形式的,大家自己尝试。
先看基础的表和对应的POJO。
city表:
<code class="language-sql hljs 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-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-operator" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">SET</span> FOREIGN_KEY_CHECKS=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>;</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">-- ----------------------------</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">-- Table structure for city</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">-- ----------------------------</span> <span class="hljs-operator" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">DROP</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">TABLE</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">IF</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">EXISTS</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">`city`</span>;</span> <span class="hljs-operator" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">CREATE</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">TABLE</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">`city`</span> ( <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">`id`</span> bigint(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">20</span>) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">NOT</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">NULL</span> AUTO_INCREMENT, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">`name`</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">varchar</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">255</span>) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">CHARACTER</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">SET</span> utf8 <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">DEFAULT</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">NULL</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">`state`</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">varchar</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">255</span>) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">CHARACTER</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">SET</span> utf8 <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">DEFAULT</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">NULL</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">PRIMARY</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">KEY</span> (<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">`id`</span>) ) ENGINE=InnoDB AUTO_INCREMENT=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">DEFAULT</span> CHARSET=latin1;</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">-- ----------------------------</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">-- Records of city</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">-- ----------------------------</span> <span class="hljs-operator" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">INSERT</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">INTO</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">`city`</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">VALUES</span> (<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'1'</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'石家庄'</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'河北'</span>);</span> <span class="hljs-operator" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">INSERT</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">INTO</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">`city`</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">VALUES</span> (<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'2'</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'邯郸'</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'河北'</span>);</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><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li></ul>
city对象:
<code class="language-java hljs 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-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">City2</span> {</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> Integer id; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> String cityName; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> String cityState; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-title" style="box-sizing: border-box;">City2</span>() { } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-title" style="box-sizing: border-box;">City2</span>(String cityName, String cityState) { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>.cityName = cityName; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>.cityState = cityState; } <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//省略setter,getter</span> <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Override</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> String <span class="hljs-title" style="box-sizing: border-box;">toString</span>() { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"City2{"</span> + <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"id="</span> + id + <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">", cityName='"</span> + cityName + <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'\''</span> + <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">", cityState='"</span> + cityState + <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'\''</span> + <span class="hljs-string" style="color: rgb(0, 136, 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><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><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li></ul>
定义如下MyBatis331Mapper
接口
<code class="language-java hljs 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-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/** * mybatis3.3.1版本新增功能测试 * *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @author</span> liuzh *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @since</span> 2016-03-06 17:22 */</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">interface</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">MyBatis331Mapper</span> {</span> <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> cities *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @return</span> */</span> <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Insert</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"<script>"</span> + <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"insert into city (id, name, state) values "</span> + <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"<foreach collection=\"list\" item=\"city\" separator=\",\" >"</span> + <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"(#{city.id}, #{city.cityName}, #{city.cityState})"</span> + <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"</foreach>"</span> + <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"</script>"</span>) <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Options</span>(useGeneratedKeys = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">true</span>, keyProperty = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"id"</span>) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> insertCities(List<City2> cities); <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> id *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @return</span> */</span> <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Results</span>(id = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"cityResult"</span>, value = { <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Result</span>(property = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"id"</span>, column = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"id"</span>, id = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">true</span>), <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Result</span>(property = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"cityName"</span>, column = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"name"</span>, id = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">true</span>), <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Result</span>(property = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"cityState"</span>, column = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"state"</span>, id = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">true</span>) }) <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Select</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"select id, name, state from city where id = #{id}"</span>) City2 selectByCityId(Integer id); <span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/** * 查询全部,引用上面的Results * *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @return</span> */</span> <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@ResultMap</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"cityResult"</span>) <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Select</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"select id, name, state from city"</span>) List<City2> selectAll(); }</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><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li><li style="box-sizing: border-box; padding: 0px 5px;">40</li><li style="box-sizing: border-box; padding: 0px 5px;">41</li><li style="box-sizing: border-box; padding: 0px 5px;">42</li><li style="box-sizing: border-box; padding: 0px 5px;">43</li><li style="box-sizing: border-box; padding: 0px 5px;">44</li><li style="box-sizing: border-box; padding: 0px 5px;">45</li><li style="box-sizing: border-box; padding: 0px 5px;">46</li></ul>
这里详细说一下这两个新功能的用法。
先看批量插入的例子
<code class="language-java hljs 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-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Insert</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"<script>"</span> + <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"insert into city (id, name, state) values "</span> + <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"<foreach collection=\"list\" item=\"city\" separator=\",\" >"</span> + <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"(#{city.id}, #{city.cityName}, #{city.cityState})"</span> + <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"</foreach>"</span> + <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"</script>"</span>) <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Options</span>(useGeneratedKeys = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">true</span>, keyProperty = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"id"</span>) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> insertCities(List<City2> cities);</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>
首先接口参数只能有一个(默认情况下),如果你参数有多个,那么要返回主键的那个List
必须加注解@Param("list")
或者在参数Map
中对应的key
为"list"
。这一点很重要,只有看源码才能了解(当然除了"list"
还有另外的名字,例如支持数组的"array"
),参考Jdbc3KeyGenerator
类中的这段代码:
<code class="language-java hljs 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-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (parameterMap.containsKey(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"collection"</span>)) { parameters = (Collection) parameterMap.get(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"collection"</span>); } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (parameterMap.containsKey(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"list"</span>)) { parameters = (List) parameterMap.get(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"list"</span>); } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (parameterMap.containsKey(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"array"</span>)) { parameters = Arrays.asList((Object[]) parameterMap.get(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"array"</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></ul>
然后就是必须使用useGeneratedKeys
的方式,注解使用下面的方式:
<code class="language-java hljs 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-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Options</span>(useGeneratedKeys = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">true</span>, keyProperty = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"id"</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>
XML使用类似下面的方式:
<code class="language-xml hljs 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-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">insert</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">id</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"insertList"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">useGeneratedKeys</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"true"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">keyProperty</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"id"</span>></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>
只要注意上面这几点,批量插入应该就能返回自增的值了。
注意:大家应该能理解,自增的不一定是主键,而且一个表中可能有多个自增的值。这些情况下都能获取到,keyProperty
需要设置多个属性值,逗号隔开即可。
再看引用@Results
(此功能是否为新增功能,我并不确定,因为我平时不用注解)
用MyBatis的人中,使用注解的是少数,但是有些企业由于领导或者别的原因,会限制必须用注解。
这对一些复杂的情况来说,使用起来不如XML的方便,但是不得不用。
以前如果返回一个对象的属性需要配置映射,那么每个对象上都需要这段重复的代码,看起来很乱很麻烦。
在上面的例子中,在selectByCityId
上定义了Results
,在下面的方法selectAll
上通过@ResultMap("cityResult")
直接引用的上面的Results
。这个功能在使用的时候没有特别注意的地方。
测试
写个简单的测试,代码如下:
<code class="language-java hljs 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-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@RunWith</span>(SpringJUnit4ClassRunner.class) <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@WebAppConfiguration</span> <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Transactional</span> <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@SpringApplicationConfiguration</span>(Application.class) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">MyBatis331Test</span> {</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> Logger logger = LoggerFactory.getLogger(getClass()); <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Autowired</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> MyBatis331Mapper mapper; <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Test</span> <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Rollback</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">testInsertList</span>() { List<City2> city2List = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> ArrayList<City2>(); city2List.add(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> City2(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"石家庄"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"河北"</span>)); city2List.add(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> City2(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"邯郸"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"河北"</span>)); city2List.add(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> City2(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"秦皇岛"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"河北"</span>)); Assert.assertEquals(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>, mapper.insertCities(city2List)); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> (City2 c2 : city2List) { logger.info(c2.toString()); Assert.assertNotNull(c2.getId()); } } <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Test</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">testSelectById</span>(){ City2 city2 = mapper.selectByCityId(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>); logger.info(city2.toString()); Assert.assertNotNull(city2); Assert.assertNotNull(city2.getCityName()); Assert.assertNotNull(city2.getCityState()); } <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Test</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">testSelectAll</span>(){ List<City2> city2List = mapper.selectAll(); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span>(City2 c2 : city2List){ logger.info(c2.toString()); Assert.assertNotNull(c2); Assert.assertNotNull(c2.getCityName()); Assert.assertNotNull(c2.getCityState()); } } }</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><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li><li style="box-sizing: border-box; padding: 0px 5px;">40</li><li style="box-sizing: border-box; padding: 0px 5px;">41</li><li style="box-sizing: border-box; padding: 0px 5px;">42</li><li style="box-sizing: border-box; padding: 0px 5px;">43</li><li style="box-sizing: border-box; padding: 0px 5px;">44</li><li style="box-sizing: border-box; padding: 0px 5px;">45</li></ul>
第一个测试方法输出的部分日志如下:
<code class="language-sql hljs 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-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;">==> Preparing: <span class="hljs-operator" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">insert</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">into</span> city (id, name, state) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">values</span> (?, ?, ?) , (?, ?, ?) , (?, ?, ?) ==> Parameters: <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null</span>, 石家庄(String), 河北(String), <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null</span>, 邯郸(String), 河北(String), <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null</span>, 秦皇岛(String), 河北(String) <== Updates: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span> City2{id=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6</span>, cityName=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'石家庄'</span>, cityState=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'河北'</span>} City2{id=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">7</span>, cityName=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'邯郸'</span>, cityState=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'河北'</span>} City2{id=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8</span>, cityName=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'秦皇岛'</span>, cityState=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'河北'</span>}</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></ul>
后两个方法输出的部分日志如下:
<code class="language-sql hljs 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-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;">==> Preparing: <span class="hljs-operator" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">select</span> id, name, state <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">from</span> city <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">where</span> id = ? ==> Parameters: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">Integer</span>) <== Total: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> City2{id=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>, cityName=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'石家庄'</span>, cityState=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'河北'</span>} ==> Preparing: <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">select</span> id, name, state <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">from</span> city ==> Parameters: <== Total: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> City2{id=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>, cityName=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'石家庄'</span>, cityState=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'河北'</span>} City2{id=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>, cityName=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'邯郸'</span>, cityState=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'河北'</span>}</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><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li></ul>
注:由于批量插入事务并没有提交,因此这里查询出来的结果就是表中原有的两条数据。
最后
为了方便尝试上面的代码,可以直接查看下面项目的src/test
:
MyBatis-Spring-Boot: https://github.com/abel533/MyBatis-Spring-Boot
另外mybatis-spring项目也同时更新到了1.2.4
,这个版本对于使用SpringBoot的开发人员非常有用,这个版本解决了mybatis的循环依赖异常,如果你在使用SpringBoot,赶紧升级到最新的版本试试吧。