-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path14689923975384.html
More file actions
559 lines (373 loc) · 18 KB
/
14689923975384.html
File metadata and controls
559 lines (373 loc) · 18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
<!doctype html>
<html class="no-js" lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>
Java康复性训练——MyBatis3(3) - Zhan's Blog
</title>
<link href="atom.xml" rel="alternate" title="Zhan's Blog" type="application/atom+xml">
<link rel="stylesheet" href="asset/css/foundation.min.css" />
<link rel="stylesheet" href="asset/css/docs.css" />
<script src="asset/js/vendor/modernizr.js"></script>
<script src="asset/js/vendor/jquery.js"></script>
<script src="asset/highlightjs/highlight.pack.js"></script>
<link href="asset/highlightjs/styles/github.css" media="screen, projection" rel="stylesheet" type="text/css">
<script>hljs.initHighlightingOnLoad();</script>
<script type="text/javascript">
function before_search(){
var searchVal = 'site:swuzjb.github.io ' + document.getElementById('search_input').value;
document.getElementById('search_q').value = searchVal;
return true;
}
</script>
</head>
<body class="antialiased hide-extras">
<div class="marketing off-canvas-wrap" data-offcanvas>
<div class="inner-wrap">
<nav class="top-bar docs-bar hide-for-small" data-topbar>
<section class="top-bar-section">
<div class="row">
<div style="position: relative;width:100%;"><div style="position: absolute; width:100%;">
<ul id="main-menu" class="left">
<li id=""><a target="_self" href="index.html">Home</a></li>
<li id=""><a target="_self" href="archives.html">Archives</a></li>
<li id=""><a target="_blank" href="aboutMe.html">RESUME</a></li>
</ul>
<ul class="right" id="search-wrap">
<li>
<form target="_blank" onsubmit="return before_search();" action="http://google.com/search" method="get">
<input type="hidden" id="search_q" name="q" value="" />
<input tabindex="1" type="search" id="search_input" placeholder="Search"/>
</form>
</li>
</ul>
</div></div>
</div>
</section>
</nav>
<nav class="tab-bar show-for-small">
<a href="javascript:void(0)" class="left-off-canvas-toggle menu-icon">
<span> Zhan's Blog</span>
</a>
</nav>
<aside class="left-off-canvas-menu">
<ul class="off-canvas-list">
<li><a href="index.html">HOME</a></li>
<li><a href="archives.html">Archives</a></li>
<li><a href="about.html">ABOUT</a></li>
<li><label>Categories</label></li>
<li><a href="iOS%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0.html">iOS学习笔记</a></li>
<li><a href="Java.html">Java</a></li>
<li><a href="%E8%AF%BE%E7%A8%8B%E7%9B%B8%E5%85%B3.html">课程相关</a></li>
<li><a href="%E7%AE%97%E6%B3%95-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84.html">算法-数据结构</a></li>
<li><a href="%E5%85%B6%E5%AE%83.html">其它</a></li>
</ul>
</aside>
<a class="exit-off-canvas" href="#"></a>
<section id="main-content" role="main" class="scroll-container">
<script type="text/javascript">
$(function(){
$('#menu_item_index').addClass('is_active');
});
</script>
<div class="row">
<div class="large-8 medium-8 columns">
<div class="markdown-body article-wrap">
<div class="article">
<h1>Java康复性训练——MyBatis3(3)</h1>
<div class="read-more clearfix">
<span class="date">2016/7/20</span>
<span>posted in </span>
<span class="posted-in"><a href='Java.html'>Java</a></span>
<span class="comments">
<a class="ds-thread-count" data-thread-key="14689923975384.html" data-count-type="comments" href="14689923975384.html#ds-thread"></a>
</span>
</div>
</div><!-- article -->
<div class="article-content">
<p>接上篇,继续对Mybatis的使用进行介绍。</p>
<blockquote>
<p>代码展示:<a href="https://github.com/swuzjb/MybatisDemo">Github地址</a></p>
</blockquote>
<h2 id="toc_0">动态SQL</h2>
<p>在实际开发过程中,静态的SQL写法并没有什么灵活性。当我们对User进行检索时,可能是依据id,role,username等等参数,如果使用静态SQL语句,我们就需要重复写大量的代码。为了解决这类问题,我们引入动态SQL。</p>
<p>Mybatis对动态SQL有很不错的支持,增加了很多扩展。</p>
<h3 id="toc_1">IF标签</h3>
<p>当我们开发过程中,遇到上述的多条件或指定条件查询时,我们可以使用IF标签来扩展。<br/>
以实现一个用户检索为例:</p>
<ol>
<li>在UserMapper接口中定义查询方法:<code>public List<User> searchUser(Map<String, Object> searchMap);</code></li>
<li><p>在UserMapper.xml中对接口进行实现:</p>
<pre><code class="language-xml"> <select id="searchUser" parameterType="Map" resultMap="UserResult">
select * from user where 1=1
<if test="username!=null">
and username like #{username}
</if>
<if test="id!=null">
and id=#{id}
</if>
<if test="role!=null">
and roleId=#{role.id}
</if>
</select>
</code></pre></li>
<li><p>调用查询,传入查询条件的Map即可。</p>
<pre><code class="language-java">public void testSearch(){
Role role = new Role();
role.setId(1);
Map<String, Object> map = new HashMap<String, Object>();
map.put("role", role);
List<User> userList = userMapper.searchUser(map);
for(User user:userList){
System.out.println(user);
}
}
</code></pre></li>
</ol>
<h3 id="toc_2">Choose、when和otherwise标签</h3>
<p>上面的IF标签类似于我们结构控制的IF语句,那么现在介绍的这个标签就类似于我们的结构控制中的Switch-Case语句。<br/>
当我们需要对条件进行限制的时候,我们就可以使用这个标签。标签的结构如下:</p>
<pre><code class="language-xml"><choose>
<when test=""></when>
<when test=""></when>
<otherwise></otherwise>
</choose>
</code></pre>
<h3 id="toc_3">where标签</h3>
<p>where条件是对Where语句的一个优化,可以自动给SQL语句加上Where。如果Where子句以and或者or开头时,则自动删除第一个and或or。</p>
<pre><code class="language-xml"> <select id="searchUser" parameterType="Map" resultMap="UserResult">
select * from user
<where>
<if test="username!=null">
and username like #{username}
</if>
<if test="id!=null">
and id=#{id}
</if>
<if test="role!=null">
and roleId=#{role.id}
</if>
</where>
</select>
</code></pre>
<h3 id="toc_4">trim标签</h3>
<p>与where语句类似,给语句增加前后缀。</p>
<h3 id="toc_5">Foreach标签</h3>
<p>用于拼接SQL语句的in语句,对传入的集合进行遍历。集合名称collectionName,元素名称itemName,拼接时以<code>(</code>开始,以<code>)</code>结束,用<code>,</code>分割。</p>
<pre><code class="language-xml"><foreach collection="collectionName" item="itemName" open="(" close=")" separator=",">
#{itemName}
</foreach>
</code></pre>
<h3 id="toc_6">set标签</h3>
<p>和where子句类似,用于update语句。可以自动加上set,并自动删除最后一个<code>,</code>。</p>
<pre><code class="language-xml"><update id="updateUser" parameterType="User">
update user
<set>
<if test="username!=null">
username=#{username},
</if>
<if test="password!=null">
password=#{password},
</if>
<if test="role!=null">
roleId=#{role.id},
</if>
</set>
where id=#{id}
</update>
</code></pre>
<h3 id="toc_7">处理CLOB、BLOB类型数据</h3>
<ul>
<li>BLOB映射为<code>byte[]</code>,CLOB映射为<code>String</code>。</li>
<li>数据插入和读取时,参照对应的映射类型进行处理即可。</li>
</ul>
<h3 id="toc_8">传入多个输入参数</h3>
<ul>
<li>使用<code><key,value></code>键值对的Map进行传递</li>
<li>在SQL拼接时,直接使用#{param1}、#{param2}...来获取</li>
</ul>
<h3 id="toc_9">MyBatis分页</h3>
<ul>
<li>逻辑分页,即把所有的数据加入内存,每次取出若干条。通过<code>RowBounds</code>对象即可实现(需要指定offset,limit两个值)</li>
<li>物理分页,使用limit子句来实现分页</li>
</ul>
<h3 id="toc_10">MyBatis缓存</h3>
<p>MyBatis默认情况下,启用一级缓存,即同一个SqlSession接口对象调用了相同的select语句,则直接会从缓存中返回结果,而不再查询一次数据库。<br/>
开发者可以自己配置二级缓存,二级缓存是全局的。默认情况下,select使用缓存,insert、update、delete不使用缓存。</p>
<h3 id="toc_11">使用注解配置SQL映射器</h3>
<h4 id="toc_12">基本映射语句</h4>
<ol>
<li>@Insert:声明在插入的接口上方,参数为sql语句,和XML配置时相同;</li>
<li>@Update:同上;</li>
<li>@delete:同上;</li>
<li>@select:简单的返回单个对象时,和在查询方法上配置sql语句即可。</li>
<li><p>@select:当需要返回集合时:</p>
<pre><code class="language-java">@Select("select * from user where id=#{id}")
@Results({
@Result(id=true,column="id",property="id"),
@Result(column="username",property="username"),
@Result(column="password",property="password"),
})
public User getUserById(Integer id);
</code></pre></li>
</ol>
<h4 id="toc_13">关系映射</h4>
<ol>
<li><p>一对一映射:<code>@Result(column="roleId",property="role" one=@One(select="site.zhanjingbo.mapper.RoleMapper.getRoleById"))</code>;</p></li>
<li><p>一对多: <code>@Result(column="id",property="userList" many=@Many(select="site.zhanjingbo.mapper.StudentMapper.getUserbyRole"))</code></p></li>
</ol>
<h4 id="toc_14">动态SQL</h4>
<p>实现比较复杂,建议使用XML实现。使用注解效果不是很好。</p>
</div>
<div class="row">
<div class="large-6 columns">
<p class="text-left" style="padding:15px 0px;">
<a href="14690817459253.html"
title="Previous Post: Java康复性训练——Spring(1)">« Java康复性训练——Spring(1)</a>
</p>
</div>
<div class="large-6 columns">
<p class="text-right" style="padding:15px 0px;">
<a href="14689148951216.html"
title="Next Post: Java康复性训练——MyBatis3(2)">Java康复性训练——MyBatis3(2) »</a>
</p>
</div>
</div>
<div class="comments-wrap">
<div class="share-comments">
<div class="ds-thread" data-thread-key="14689923975384.html" data-url="http://swuzjb.github.io/14689923975384.html" data-title="Java康复性训练——MyBatis3(3)"></div>
</div>
</div>
</div><!-- article-wrap -->
</div><!-- large 8 -->
<div class="large-4 medium-4 columns">
<div class="hide-for-small">
<div id="sidebar" class="sidebar">
<div id="site-info" class="site-info">
<div class="site-a-logo"><img src="asset/img/icon.jpg" /></div>
<h1>Zhan's Blog</h1>
<div class="site-des">展镜博的个人博客</div>
<div class="social">
<a class="github" target="_blank" href="https://github.com/swuzjb" title="GitHub">GitHub</a>
<a class="email" href="mailto:412008196@qq.com" title="Email">Email</a>
<a class="rss" href="atom.xml" title="RSS">RSS</a>
</div>
</div>
<div id="site-categories" class="side-item ">
<div class="side-header">
<h2>Categories</h2>
</div>
<div class="side-content">
<p class="cat-list">
<a href="iOS%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0.html"><strong>iOS学习笔记</strong></a>
<a href="Java.html"><strong>Java</strong></a>
<a href="%E8%AF%BE%E7%A8%8B%E7%9B%B8%E5%85%B3.html"><strong>课程相关</strong></a>
<a href="%E7%AE%97%E6%B3%95-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84.html"><strong>算法-数据结构</strong></a>
<a href="%E5%85%B6%E5%AE%83.html"><strong>其它</strong></a>
</p>
</div>
</div>
<div id="site-categories" class="side-item ">
<div class="side-header">
<h2>BookList</h2>
</div>
<div class="side-content">
<p class="cat-list">
<a href="bookList.html"><strong>待读书单</strong></a>
</p>
</div>
</div>
<div id="site-categories" class="side-item">
<div class="side-header">
<h2>Recent Posts</h2>
</div>
<div class="side-content">
<ul class="posts-list">
<li class="post">
<a href="15090149503358.html">Jupyterhub安装流程</a>
</li>
<li class="post">
<a href="15090148978006.html"></a>
</li>
<li class="post">
<a href="15037351230027.html">Java研发工程师的面试宝典</a>
</li>
<li class="post">
<a href="14896338034345.html">设计模式-适配器模式</a>
</li>
<li class="post">
<a href="14894720983586.html">设计模式-命令模式</a>
</li>
</ul>
</div>
</div>
<div id="site-categories" class="side-item">
<div class="side-header">
<h2>FRIEND LINKS</h2>
</div>
<div class="side-content">
<ul class="posts-list">
<li class="post">
<a href="https://guxinyan.github.io">顾鑫燕的博客</a>
</li>
<li class="post">
<a href="http://ranchu.win">CD'S Blog</a>
</li>
<li class="post">
<a href="https://hran.me">Hran's Blog</a>
</li>
</ul>
</div>
</div>
</div><!-- sidebar -->
</div><!-- hide for small -->
</div><!-- large 4 -->
</div><!-- row -->
<div class="page-bottom clearfix">
<div class="row">
<p class="copyright">Copyright © 2016 swuzjb
</div>
</div>
</section>
</div>
</div>
<script type="text/javascript">
var duoshuoQuery = {short_name:'zhanjingbo'};
(function() {
var ds = document.createElement('script');
ds.type = 'text/javascript';ds.async = true;
ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
ds.charset = 'UTF-8';
(document.getElementsByTagName('head')[0]
|| document.getElementsByTagName('body')[0]).appendChild(ds);
})();
</script>
<script src="asset/js/foundation.min.js"></script>
<script>
$(document).foundation();
function fixSidebarHeight(){
var w1 = $('.markdown-body').height();
var w2 = $('#sidebar').height();
if (w1 > w2) { $('#sidebar').height(w1); };
}
$(function(){
fixSidebarHeight();
})
$(window).load(function(){
fixSidebarHeight();
});
</script>
<script src="asset/chart/all-min.js"></script><script type="text/javascript">$(function(){ var mwebii=0; var mwebChartEleId = 'mweb-chart-ele-'; $('pre>code').each(function(){ mwebii++; var eleiid = mwebChartEleId+mwebii; if($(this).hasClass('language-sequence')){ var ele = $(this).addClass('nohighlight').parent(); $('<div id="'+eleiid+'"></div>').insertAfter(ele); ele.hide(); var diagram = Diagram.parse($(this).text()); diagram.drawSVG(eleiid,{theme: 'simple'}); }else if($(this).hasClass('language-flow')){ var ele = $(this).addClass('nohighlight').parent(); $('<div id="'+eleiid+'"></div>').insertAfter(ele); ele.hide(); var diagram = flowchart.parse($(this).text()); diagram.drawSVG(eleiid); } });});</script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script><script type="text/x-mathjax-config">MathJax.Hub.Config({TeX: { equationNumbers: { autoNumber: "AMS" } }});</script>
<script>
var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "//hm.baidu.com/hm.js?0fb9ecff8c9a2c820ce81c18ff77c1e8";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
</script>
</body>
</html>