-
Notifications
You must be signed in to change notification settings - Fork 0
/
atom.xml
648 lines (513 loc) · 165 KB
/
atom.xml
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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>makerwei blog</title>
<subtitle>Java and Python Developer</subtitle>
<link href="/atom.xml" rel="self"/>
<link href="http://blog.makerwei.me/"/>
<updated>2017-03-23T08:13:21.000Z</updated>
<id>http://blog.makerwei.me/</id>
<author>
<name>小小小maker_wei</name>
</author>
<generator uri="http://hexo.io/">Hexo</generator>
<entry>
<title>MyBatis 使用指南(1)</title>
<link href="http://blog.makerwei.me/posts/33582/"/>
<id>http://blog.makerwei.me/posts/33582/</id>
<published>2017-02-03T05:43:29.000Z</published>
<updated>2017-03-23T08:13:21.000Z</updated>
<content type="html"><![CDATA[<h2 id="什么是-MyBatis"><a href="#什么是-MyBatis" class="headerlink" title="什么是 MyBatis"></a>什么是 MyBatis</h2><blockquote>
<p>MyBatis 是支持定制化 SQL、存储过程以及高级映射的优秀的持久层框架。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以对配置和原生Map使用简单的 XML 或注解,将接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。</p>
</blockquote>
<a id="more"></a>
<h2 id="SpringBoot-中使用"><a href="#SpringBoot-中使用" class="headerlink" title="SpringBoot 中使用"></a>SpringBoot 中使用</h2><h3 id="使用-Maven"><a href="#使用-Maven" class="headerlink" title="使用 Maven"></a>使用 Maven</h3><p>在你的<code>pom.xml</code>中:</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="tag"><<span class="name">dependency</span>></span></div><div class="line"> <span class="tag"><<span class="name">groupId</span>></span>org.mybatis.spring.boot<span class="tag"></<span class="name">groupId</span>></span></div><div class="line"> <span class="tag"><<span class="name">artifactId</span>></span>mybatis-spring-boot-starter<span class="tag"></<span class="name">artifactId</span>></span></div><div class="line"> <span class="tag"><<span class="name">version</span>></span>1.2.1-SNAPSHOT<span class="tag"></<span class="name">version</span>></span></div><div class="line"><span class="tag"></<span class="name">dependency</span>></span></div></pre></td></tr></table></figure>
<h3 id="使用-Gradle"><a href="#使用-Gradle" class="headerlink" title="使用 Gradle"></a>使用 Gradle</h3><p>在你的 <code>build.gradle</code>中:</p>
<figure class="highlight groovy"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">dependencies {</div><div class="line"> compile(<span class="string">"org.mybatis.spring.boot:mybatis-spring-boot-starter:1.2.1-SNAPSHOT"</span>)</div><div class="line">}</div></pre></td></tr></table></figure>
<h2 id="使用"><a href="#使用" class="headerlink" title="使用"></a>使用</h2><p>目录结构</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line">--src </div><div class="line"> --main</div><div class="line"> --java</div><div class="line"> --com.example</div><div class="line"> --dao</div><div class="line"> --UserDao.java</div><div class="line"> --entity</div><div class="line"> --User.java</div><div class="line"> --DemoApplication.java</div><div class="line"> --UserSex.java</div><div class="line"> --resources</div><div class="line"> --mapper</div><div class="line"> --UserMapper.xml</div><div class="line"> --application.properties</div></pre></td></tr></table></figure>
<h3 id="基于注解"><a href="#基于注解" class="headerlink" title="基于注解"></a>基于注解</h3><p>使用MyBatis-Spring-Boot-Starter will:</p>
<ul>
<li>将自动检测存在的数据源</li>
<li>将数据源输入然后创建注册一个<code>SqlSessionFactoryBean</code>实例</li>
<li>从<code>SqlSessionFactoryBean</code>中创建注册一个<code>SqlSessionTemplate</code>实例</li>
<li>自动扫描你的 <code>mappers</code>,将它们连接到<code>SqlSessionTemplate</code>然后将它们注册到 Spring 的上下文</li>
</ul>
<p>我们新建一个实体类<code>User.java</code>,在这里我们使用 <code>Lombok</code> <a href="http://blog.makerwei.me/2016/11/03/springboot集成lombok%20/">(使用说明见这篇文章)</a>来简化代码</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="meta">@Data</span></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">User</span> </span>{</div><div class="line"> <span class="keyword">private</span> Long id;</div><div class="line"> <span class="keyword">private</span> String userName;</div><div class="line"> <span class="keyword">private</span> String password;</div><div class="line"> <span class="keyword">private</span> UserSex userSex;</div><div class="line"> <span class="keyword">private</span> String nickName;</div><div class="line">}</div></pre></td></tr></table></figure>
<p>接着我们创建一张叫 <code>user</code>的表</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">DROP</span> <span class="keyword">TABLE</span> <span class="keyword">IF</span> <span class="keyword">EXISTS</span> <span class="keyword">user</span>;</div><div class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> <span class="keyword">user</span>(</div><div class="line"> <span class="keyword">id</span> <span class="built_in">BIGINT</span>(<span class="number">20</span>) <span class="keyword">NOT</span> <span class="literal">NULL</span> AUTO_INCREMENT <span class="keyword">COMMENT</span> <span class="string">'主键id'</span>,</div><div class="line"> user_name <span class="built_in">VARCHAR</span>(<span class="number">32</span>) <span class="keyword">DEFAULT</span> <span class="literal">NULL</span> <span class="keyword">COMMENT</span> <span class="string">'用户名'</span>,</div><div class="line"> <span class="keyword">password</span> <span class="built_in">VARCHAR</span>(<span class="number">32</span>) <span class="keyword">DEFAULT</span> <span class="literal">NULL</span> <span class="keyword">COMMENT</span> <span class="string">'密码'</span>,</div><div class="line"> user_sex <span class="built_in">VARCHAR</span>(<span class="number">32</span>) <span class="keyword">DEFAULT</span> <span class="literal">NULL</span> ,</div><div class="line"> nick_name <span class="built_in">VARCHAR</span>(<span class="number">32</span>) <span class="keyword">DEFAULT</span> <span class="literal">NULL</span> ,</div><div class="line"> PRIMARY <span class="keyword">KEY</span> (<span class="keyword">id</span>)</div><div class="line">) <span class="keyword">ENGINE</span> = <span class="keyword">InnoDB</span> <span class="keyword">DEFAULT</span> <span class="keyword">CHARSET</span> = utf8;</div></pre></td></tr></table></figure>
<p>然后创建一个 <code>Dao</code> 文件<code>UserDao.java</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div></pre></td><td class="code"><pre><div class="line"><span class="meta">@Mapper</span></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">UserDao</span> </span>{</div><div class="line"> <span class="meta">@Select</span>(<span class="string">"SELECT * FROM user WHERE id = #{id}"</span>)</div><div class="line"> <span class="meta">@Results</span>({</div><div class="line"> <span class="meta">@Result</span>(property = <span class="string">"userName"</span>, column = <span class="string">"user_name"</span>),</div><div class="line"> <span class="meta">@Result</span>(property = <span class="string">"password"</span>, column = <span class="string">"password"</span>),</div><div class="line"> <span class="meta">@Result</span>(property = <span class="string">"userSex"</span>, column = <span class="string">"user_sex"</span>, javaType = UserSex.class),</div><div class="line"> <span class="meta">@Result</span>(property = <span class="string">"nickName"</span>,column = <span class="string">"nick_name"</span>)</div><div class="line"> })</div><div class="line"> <span class="function">User <span class="title">findById</span><span class="params">(@Param(<span class="string">"id"</span>)</span> Long id)</span>;</div><div class="line"></div><div class="line"> <span class="meta">@Insert</span>(<span class="string">"INSERT INTO user (user_name,password,user_sex ) VALUES (#{userName},#{password},#{userSex})"</span>)</div><div class="line"> <span class="function"><span class="keyword">void</span> <span class="title">save</span><span class="params">(User user)</span></span>;</div><div class="line"></div><div class="line"> <span class="meta">@Update</span>(<span class="string">"UPDATE user set user_name = #{userName}, nick_name = #{nickName} WHERE id = #{id}"</span>)</div><div class="line"> <span class="function"><span class="keyword">void</span> <span class="title">update</span><span class="params">(User user)</span></span>;</div><div class="line"> </div><div class="line"> <span class="meta">@Delete</span>(<span class="string">"DELETE FROM user where id = #{id}"</span>)</div><div class="line"> <span class="function"><span class="keyword">void</span> <span class="title">remove</span><span class="params">(Long id)</span></span>;</div><div class="line">}</div></pre></td></tr></table></figure>
<p>每个方法都对应的对应的 sql 语句,其中<code>@Select</code>注解用于查询,<code>@Result</code>注解将实体类属性跟数据库的字段一一对应,<code>@Insert</code>注解用于插入数据,<code>@Update</code>注解用于修改数据,<code>@Delete</code>注解用于删除</p>
<p>接下来我们简单的测试下</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div></pre></td><td class="code"><pre><div class="line"><span class="meta">@RunWith</span>(SpringRunner.class)</div><div class="line"><span class="meta">@SpringBootTest</span></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserMapperTest</span> </span>{</div><div class="line"></div><div class="line"> <span class="meta">@Autowired</span></div><div class="line"> UserDao userDao;</div><div class="line"></div><div class="line"> <span class="meta">@Test</span></div><div class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test</span><span class="params">()</span></span>{</div><div class="line"> User one = <span class="keyword">new</span> User();</div><div class="line"> one.setUserName(<span class="string">"张三"</span>);</div><div class="line"> one.setPassword(<span class="string">"123456"</span>);</div><div class="line"> one.setUserSex(UserSex.MAN);</div><div class="line"> userDao.save(one);</div><div class="line"> User two = <span class="keyword">new</span> User();</div><div class="line"> two.setUserName(<span class="string">"李四"</span>);</div><div class="line"> two.setPassword(<span class="string">"123456"</span>);</div><div class="line"> two.setUserSex(UserSex. WOMAN);</div><div class="line"> userDao.save(two);</div><div class="line"> }</div><div class="line">}</div></pre></td></tr></table></figure>
<p>结果如下<br><img src="http://7xojnx.com1.z0.glb.clouddn.com/result.png" alt="结果"></p>
<h3 id="基于-XML-配置"><a href="#基于-XML-配置" class="headerlink" title="基于 XML 配置"></a>基于 XML 配置</h3><p>在<code>application.properties</code>配置文件中新增以下配置</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">mybatis.type-aliases-package= com.example.entity</div><div class="line">mybatis.mapper-locations=classpath:mapper/*.xml</div></pre></td></tr></table></figure>
<p>然后在<code>resources</code>文件夹中创建<code>mapper</code>文件夹,然后新建一个<code>userMapper.xml</code></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div></pre></td><td class="code"><pre><div class="line"><?xml version="1.0" encoding="UTF-8" ?></div><div class="line"><span class="meta"><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" ></span></div><div class="line"><span class="tag"><<span class="name">mapper</span> <span class="attr">namespace</span>=<span class="string">"com.example.dao.UserDao"</span>></span></div><div class="line"> <span class="tag"><<span class="name">resultMap</span> <span class="attr">id</span>=<span class="string">"userMap"</span> <span class="attr">type</span>=<span class="string">"com.example.entity.User"</span>></span></div><div class="line"> <span class="tag"><<span class="name">result</span> <span class="attr">column</span>=<span class="string">"id"</span> <span class="attr">property</span>=<span class="string">"id"</span>/></span></div><div class="line"> <span class="tag"><<span class="name">result</span> <span class="attr">column</span>=<span class="string">"user_name"</span> <span class="attr">property</span>=<span class="string">"userName"</span>/></span></div><div class="line"> <span class="tag"><<span class="name">result</span> <span class="attr">column</span>=<span class="string">"password"</span> <span class="attr">property</span>=<span class="string">"password"</span>/></span></div><div class="line"> <span class="tag"><<span class="name">result</span> <span class="attr">column</span>=<span class="string">"user_sex"</span> <span class="attr">property</span>=<span class="string">"userSex"</span> <span class="attr">javaType</span>=<span class="string">"com.example.UserSex"</span>/></span></div><div class="line"> <span class="tag"><<span class="name">result</span> <span class="attr">column</span>=<span class="string">"nick_name"</span> <span class="attr">property</span>=<span class="string">"nickName"</span>/></span></div><div class="line"> <span class="tag"></<span class="name">resultMap</span>></span></div><div class="line"> </div><div class="line"> <span class="tag"><<span class="name">insert</span> <span class="attr">id</span>=<span class="string">"save"</span> <span class="attr">parameterType</span>=<span class="string">"com.example.entity.User"</span>></span></div><div class="line"> INSERT INTO user (user_name,password,user_sex ) VALUES (#{userName},#{password},#{userSex})</div><div class="line"> <span class="tag"></<span class="name">insert</span>></span></div><div class="line"> </div><div class="line"> <span class="tag"><<span class="name">delete</span> <span class="attr">id</span>=<span class="string">"remove"</span> <span class="attr">parameterType</span>=<span class="string">"java.lang.Long"</span>></span></div><div class="line"> DELETE FROM user where id = #{id}</div><div class="line"> <span class="tag"></<span class="name">delete</span>></span></div><div class="line"> </div><div class="line"> <span class="tag"><<span class="name">update</span> <span class="attr">id</span>=<span class="string">"update"</span> <span class="attr">parameterType</span>=<span class="string">"com.example.entity.User"</span>></span></div><div class="line"> UPDATE user SET user_name = #{userName}, nick_name =#{nickName} WHERE id = #{id}</div><div class="line"> <span class="tag"></<span class="name">update</span>></span></div><div class="line"> </div><div class="line"> <span class="tag"><<span class="name">select</span> <span class="attr">id</span>=<span class="string">"findById"</span> <span class="attr">parameterType</span>=<span class="string">"java.lang.Long"</span> <span class="attr">resultMap</span>=<span class="string">"userMap"</span>></span></div><div class="line"> SELECT * FROM user WHERE id = #{id}</div><div class="line"> <span class="tag"></<span class="name">select</span>></span></div><div class="line"> </div><div class="line"><span class="tag"></<span class="name">mapper</span>></span></div></pre></td></tr></table></figure>
<p>然后将<code>UserDao.java</code>文件修改如下</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">UserDao</span> </span>{</div><div class="line"> </div><div class="line"> <span class="function">User <span class="title">findById</span><span class="params">(Long id)</span></span>;</div><div class="line"></div><div class="line"> </div><div class="line"> <span class="function"><span class="keyword">void</span> <span class="title">save</span><span class="params">(User user)</span></span>;</div><div class="line"></div><div class="line"> </div><div class="line"> <span class="function"><span class="keyword">void</span> <span class="title">update</span><span class="params">(User user)</span></span>;</div><div class="line"></div><div class="line"> </div><div class="line"> <span class="function"><span class="keyword">void</span> <span class="title">remove</span><span class="params">(Long id)</span></span>;</div><div class="line">}</div></pre></td></tr></table></figure>
<p>最后在SpringBoot的主程序上加上<code>@MapperScan</code>的注解将 dao 文件跟 sql 语句对应起来</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="meta">@SpringBootApplication</span></div><div class="line"><span class="meta">@MapperScan</span>(<span class="string">"com.example.dao"</span>)</div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">DemoApplication</span> </span>{</div><div class="line"></div><div class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>{</div><div class="line"> SpringApplication.run(DemoApplication.class, args);</div><div class="line"> }</div><div class="line">}</div></pre></td></tr></table></figure>
<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><p><code>mybatis-spring-boot-starter</code>可以快速的通过注解来开发,十分方便,可以灵活的使用 SQL 和调整 SQL,具体用注解还是基于 XML,我觉得还是看自己的喜好</p>
]]></content>
<summary type="html">
<h2 id="什么是-MyBatis"><a href="#什么是-MyBatis" class="headerlink" title="什么是 MyBatis"></a>什么是 MyBatis</h2><blockquote>
<p>MyBatis 是支持定制化 SQL、存储过程以及高级映射的优秀的持久层框架。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以对配置和原生Map使用简单的 XML 或注解,将接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。</p>
</blockquote>
</summary>
<category term="MyBatis使用指南" scheme="http://blog.makerwei.me/categories/MyBatis%E4%BD%BF%E7%94%A8%E6%8C%87%E5%8D%97/"/>
<category term="Java" scheme="http://blog.makerwei.me/tags/Java/"/>
<category term="Spring Boot" scheme="http://blog.makerwei.me/tags/Spring-Boot/"/>
<category term="MyBatis" scheme="http://blog.makerwei.me/tags/MyBatis/"/>
<category term="IDEA" scheme="http://blog.makerwei.me/tags/IDEA/"/>
</entry>
<entry>
<title>Java容器 - Vector 源码分析</title>
<link href="http://blog.makerwei.me/posts/6582/"/>
<id>http://blog.makerwei.me/posts/6582/</id>
<published>2017-01-05T03:31:26.000Z</published>
<updated>2017-03-23T08:13:32.000Z</updated>
<content type="html"><![CDATA[<p>Vector 是 ArrayList 的线程安全版本,依靠<code>synchronized</code>实现,其他大多一样,本文主要说明两者不同的地方</p>
<a id="more"></a>
<h2 id="成员变量不同"><a href="#成员变量不同" class="headerlink" title="成员变量不同"></a>成员变量不同</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Vector</span><<span class="title">E</span>></span></div><div class="line"> <span class="keyword">extends</span> <span class="title">AbstractList</span><<span class="title">E</span>></div><div class="line"> <span class="keyword">implements</span> <span class="title">List</span><<span class="title">E</span>>, <span class="title">RandomAccess</span>, <span class="title">Cloneable</span>, <span class="title">java</span>.<span class="title">io</span>.<span class="title">Serializable</span></div><div class="line">{</div><div class="line"> <span class="comment">/**</span></div><div class="line"> * The array buffer into which the components of the vector are</div><div class="line"> * stored. The capacity of the vector is the length of this array buffer,</div><div class="line"> * and is at least large enough to contain all the vector's elements.</div><div class="line"> *</div><div class="line"> * <p>Any array elements following the last element in the Vector are null.</div><div class="line"> *</div><div class="line"> * <span class="doctag">@serial</span></div><div class="line"> */</div><div class="line"> <span class="keyword">protected</span> Object[] elementData;</div><div class="line"></div><div class="line"> <span class="comment">/**</span></div><div class="line"> * The number of valid components in this {<span class="doctag">@code</span> Vector} object.</div><div class="line"> * Components {<span class="doctag">@code</span> elementData[0]} through</div><div class="line"> * {<span class="doctag">@code</span> elementData[elementCount-1]} are the actual items.</div><div class="line"> *</div><div class="line"> * <span class="doctag">@serial</span></div><div class="line"> */</div><div class="line"> <span class="keyword">protected</span> <span class="keyword">int</span> elementCount;</div><div class="line"></div><div class="line"> <span class="comment">/**</span></div><div class="line"> * The amount by which the capacity of the vector is automatically</div><div class="line"> * incremented when its size becomes greater than its capacity. If</div><div class="line"> * the capacity increment is less than or equal to zero, the capacity</div><div class="line"> * of the vector is doubled each time it needs to grow.</div><div class="line"> *</div><div class="line"> * <span class="doctag">@serial</span></div><div class="line"> */</div><div class="line"> <span class="keyword">protected</span> <span class="keyword">int</span> capacityIncrement;</div></pre></td></tr></table></figure>
<p>可以看出 Vector 多了一个<code>capacityIncrement</code>变量,暂且翻译为<code>容量增量</code></p>
<h2 id="构造方法不同"><a href="#构造方法不同" class="headerlink" title="构造方法不同"></a>构造方法不同</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">public</span> <span class="title">Vector</span><span class="params">(<span class="keyword">int</span> initialCapacity, <span class="keyword">int</span> capacityIncrement)</span> </span>{</div><div class="line"> <span class="keyword">super</span>();</div><div class="line"> <span class="keyword">if</span> (initialCapacity < <span class="number">0</span>)</div><div class="line"> <span class="keyword">throw</span> <span class="keyword">new</span> IllegalArgumentException(<span class="string">"Illegal Capacity: "</span>+</div><div class="line"> initialCapacity);</div><div class="line"> <span class="keyword">this</span>.elementData = <span class="keyword">new</span> Object[initialCapacity];</div><div class="line"> <span class="keyword">this</span>.capacityIncrement = capacityIncrement;</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="comment">/**</span></div><div class="line"> * Constructs an empty vector with the specified initial capacity and</div><div class="line"> * with its capacity increment equal to zero.</div><div class="line"> *</div><div class="line"> * <span class="doctag">@param</span> initialCapacity the initial capacity of the vector</div><div class="line"> * <span class="doctag">@throws</span> IllegalArgumentException if the specified initial capacity</div><div class="line"> * is negative</div><div class="line"> */</div><div class="line"> <span class="function"><span class="keyword">public</span> <span class="title">Vector</span><span class="params">(<span class="keyword">int</span> initialCapacity)</span> </span>{</div><div class="line"> <span class="keyword">this</span>(initialCapacity, <span class="number">0</span>);</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="comment">/**</span></div><div class="line"> * Constructs an empty vector so that its internal data array</div><div class="line"> * has size {<span class="doctag">@code</span> 10} and its standard capacity increment is</div><div class="line"> * zero.</div><div class="line"> */</div><div class="line"> <span class="function"><span class="keyword">public</span> <span class="title">Vector</span><span class="params">()</span> </span>{</div><div class="line"> <span class="keyword">this</span>(<span class="number">10</span>);</div><div class="line"> }</div></pre></td></tr></table></figure>
<p>初始化调用默认构造方法时,其会默认容量为<code>10</code>然后调用一个参数的构造方法,当调用一个参数的构造方法时默认容量增量为<code>0</code></p>
<h2 id="添加方法"><a href="#添加方法" class="headerlink" title="添加方法"></a>添加方法</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">synchronized</span> <span class="keyword">boolean</span> <span class="title">add</span><span class="params">(E e)</span> </span>{</div><div class="line"> modCount++;</div><div class="line"> ensureCapacityHelper(elementCount + <span class="number">1</span>);</div><div class="line"> elementData[elementCount++] = e;</div><div class="line"> <span class="keyword">return</span> <span class="keyword">true</span>;</div><div class="line"> }</div></pre></td></tr></table></figure>
<p>除了多了个<code>synchronized</code>关键字其它基本一样</p>
<h2 id="扩容方法有差异"><a href="#扩容方法有差异" class="headerlink" title="扩容方法有差异"></a>扩容方法有差异</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">synchronized</span> <span class="keyword">void</span> <span class="title">ensureCapacity</span><span class="params">(<span class="keyword">int</span> minCapacity)</span> </span>{</div><div class="line"> <span class="keyword">if</span> (minCapacity > <span class="number">0</span>) {</div><div class="line"> modCount++;</div><div class="line"> ensureCapacityHelper(minCapacity);</div><div class="line"> }</div><div class="line"> }</div><div class="line"> <span class="comment">/*这里跟 ArrayList 一样*/</span></div><div class="line"> <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">ensureCapacityHelper</span><span class="params">(<span class="keyword">int</span> minCapacity)</span> </span>{</div><div class="line"> <span class="comment">// overflow-conscious code</span></div><div class="line"> <span class="keyword">if</span> (minCapacity - elementData.length > <span class="number">0</span>)</div><div class="line"> grow(minCapacity);</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="comment">/*数组最大容量*/</span></div><div class="line"> <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> MAX_ARRAY_SIZE = Integer.MAX_VALUE - <span class="number">8</span>;</div><div class="line"></div><div class="line"> <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">grow</span><span class="params">(<span class="keyword">int</span> minCapacity)</span> </span>{</div><div class="line"> <span class="comment">// overflow-conscious code</span></div><div class="line"> <span class="comment">/*原来数组的容量*/</span></div><div class="line"> <span class="keyword">int</span> oldCapacity = elementData.length;</div><div class="line"> <span class="comment">/*若容量增量大于0,则新容量为旧容量加上容量增量,否则新容量为旧容量的2倍*/</span></div><div class="line"> <span class="keyword">int</span> newCapacity = oldCapacity + ((capacityIncrement > <span class="number">0</span>) ?</div><div class="line"> capacityIncrement : oldCapacity);</div><div class="line"> <span class="comment">/*其它判断跟 ArrayList 一致*/</span> </div><div class="line"> <span class="keyword">if</span> (newCapacity - minCapacity < <span class="number">0</span>)</div><div class="line"> newCapacity = minCapacity;</div><div class="line"> <span class="keyword">if</span> (newCapacity - MAX_ARRAY_SIZE > <span class="number">0</span>)</div><div class="line"> newCapacity = hugeCapacity(minCapacity);</div><div class="line"> elementData = Arrays.copyOf(elementData, newCapacity);</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">int</span> <span class="title">hugeCapacity</span><span class="params">(<span class="keyword">int</span> minCapacity)</span> </span>{</div><div class="line"> <span class="keyword">if</span> (minCapacity < <span class="number">0</span>) <span class="comment">// overflow</span></div><div class="line"> <span class="keyword">throw</span> <span class="keyword">new</span> OutOfMemoryError();</div><div class="line"> <span class="keyword">return</span> (minCapacity > MAX_ARRAY_SIZE) ?</div><div class="line"> Integer.MAX_VALUE :</div><div class="line"> MAX_ARRAY_SIZE;</div><div class="line"> }</div></pre></td></tr></table></figure>
<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><p>Vector依靠<code>synchronized</code>关键字实现线程安全的 ArrayList,但是这样会造成效率比较低。默认容量就为10,容量增量为0.数组扩容时,容量增量大于0则新容量为旧容量与容量增量的和,否则为旧容量大小的两倍。</p>
]]></content>
<summary type="html">
<p>Vector 是 ArrayList 的线程安全版本,依靠<code>synchronized</code>实现,其他大多一样,本文主要说明两者不同的地方</p>
</summary>
<category term="Java源码分析" scheme="http://blog.makerwei.me/categories/Java%E6%BA%90%E7%A0%81%E5%88%86%E6%9E%90/"/>
<category term="Java" scheme="http://blog.makerwei.me/tags/Java/"/>
<category term="源码分析" scheme="http://blog.makerwei.me/tags/%E6%BA%90%E7%A0%81%E5%88%86%E6%9E%90/"/>
<category term="Vector" scheme="http://blog.makerwei.me/tags/Vector/"/>
</entry>
<entry>
<title>Java容器 - ArrayList 源码分析</title>
<link href="http://blog.makerwei.me/posts/63081/"/>
<id>http://blog.makerwei.me/posts/63081/</id>
<published>2017-01-03T04:41:13.000Z</published>
<updated>2017-03-23T08:11:42.000Z</updated>
<content type="html"><![CDATA[<p>Java 集合中的 ArrayList 是基于数组实现的<br><a id="more"></a></p>
<h2 id="开头"><a href="#开头" class="headerlink" title="开头"></a>开头</h2><p>首先我们看初始化一个 ArrayList 的结构</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ArrayList</span><<span class="title">E</span>> <span class="keyword">extends</span> <span class="title">AbstractList</span><<span class="title">E</span>></span></div><div class="line"> <span class="keyword">implements</span> <span class="title">List</span><<span class="title">E</span>>, <span class="title">RandomAccess</span>, <span class="title">Cloneable</span>, <span class="title">java</span>.<span class="title">io</span>.<span class="title">Serializable</span></div><div class="line">{</div><div class="line"> <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">long</span> serialVersionUID = <span class="number">8683452581122892189L</span>;</div><div class="line"></div><div class="line"> <span class="comment">/**</span></div><div class="line"> * 默认大小</div><div class="line"> */</div><div class="line"> <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> DEFAULT_CAPACITY = <span class="number">10</span>;</div><div class="line"></div><div class="line"> <span class="comment">/**</span></div><div class="line"> * 共用的数组实例用于空的实例</div><div class="line"> */</div><div class="line"> <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> Object[] EMPTY_ELEMENTDATA = {};</div><div class="line"></div><div class="line"> <span class="comment">/**</span></div><div class="line"> * 默认大小的空实例所用的共用的数组实例。我们判断当一个元素被加入时数组实例该怎样扩大*/</div><div class="line"> <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};</div><div class="line"></div><div class="line"> <span class="comment">/**</span></div><div class="line"> * The array buffer into which the elements of the ArrayList are stored.</div><div class="line"> * The capacity of the ArrayList is the length of this array buffer. Any</div><div class="line"> * empty ArrayList with elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA</div><div class="line"> * will be expanded to DEFAULT_CAPACITY when the first element is added.</div><div class="line"> */</div><div class="line"> <span class="keyword">transient</span> Object[] elementData; <span class="comment">// non-private to simplify nested class access</span></div><div class="line"></div><div class="line"> <span class="comment">/**</span></div><div class="line"> * The size of the ArrayList (the number of elements it contains).</div><div class="line"> *</div><div class="line"> * <span class="doctag">@serial</span></div><div class="line"> */</div><div class="line"> <span class="keyword">private</span> <span class="keyword">int</span> size;</div></pre></td></tr></table></figure>
<p>大家可以发现ArrayList 继承于 AbstractList,还有 List、Clone、Serializable接口,说明它可以被克隆和序列化。<br><code>Any empty ArrayList with elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA will be expanded to DEFAULT_CAPACITY when the first element is added.</code>说明只是<code>DEFAULTCAPACITY_EMPTY_ELEMENTDATA</code>类型的 ArrayList 在加入一个元素时将会被初始化为10</p>
<h2 id="构造方法"><a href="#构造方法" class="headerlink" title="构造方法"></a>构造方法</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div></pre></td><td class="code"><pre><div class="line"><span class="comment">/**</span></div><div class="line"> *构造一个有初始大小的 list</div><div class="line"> */</div><div class="line"><span class="function"><span class="keyword">public</span> <span class="title">ArrayList</span><span class="params">(<span class="keyword">int</span> initialCapacity)</span> </span>{</div><div class="line"> <span class="comment">/*判断输入的容量是否大于0*/</span></div><div class="line"> <span class="keyword">if</span> (initialCapacity > <span class="number">0</span>) {</div><div class="line"> <span class="comment">/*大于0初始化一个大小为输入的容量的数组*/</span></div><div class="line"> <span class="keyword">this</span>.elementData = <span class="keyword">new</span> Object[initialCapacity];</div><div class="line"> } <span class="keyword">else</span> <span class="keyword">if</span> (initialCapacity == <span class="number">0</span>) {</div><div class="line"> <span class="comment">/*如果输入的大小为0就初始化一个空数组*/</span></div><div class="line"> <span class="keyword">this</span>.elementData = EMPTY_ELEMENTDATA;</div><div class="line"> } <span class="keyword">else</span> {</div><div class="line"> <span class="comment">/*否则抛出异常*/</span></div><div class="line"> <span class="keyword">throw</span> <span class="keyword">new</span> IllegalArgumentException(<span class="string">"Illegal Capacity: "</span>+</div><div class="line"> initialCapacity);</div><div class="line"> }</div><div class="line">}</div><div class="line"></div><div class="line"><span class="comment">/*初始化一个默认的空数组*/</span></div><div class="line"><span class="function"><span class="keyword">public</span> <span class="title">ArrayList</span><span class="params">()</span> </span>{</div><div class="line"> <span class="keyword">this</span>.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;</div><div class="line">}</div><div class="line"></div><div class="line"></div><div class="line"><span class="function"><span class="keyword">public</span> <span class="title">ArrayList</span><span class="params">(Collection<? extends E> c)</span> </span>{</div><div class="line"> elementData = c.toArray();</div><div class="line"> <span class="keyword">if</span> ((size = elementData.length) != <span class="number">0</span>) {</div><div class="line"> <span class="comment">// c.toArray might (incorrectly) not return Object[] (see 6260652)</span></div><div class="line"> <span class="keyword">if</span> (elementData.getClass() != Object[].class)</div><div class="line"> elementData = Arrays.copyOf(elementData, size, Object[].class);</div><div class="line"> } <span class="keyword">else</span> {</div><div class="line"> <span class="comment">// replace with empty array.</span></div><div class="line"> <span class="keyword">this</span>.elementData = EMPTY_ELEMENTDATA;</div><div class="line"> }</div><div class="line">}</div></pre></td></tr></table></figure>
<h2 id="添加方法"><a href="#添加方法" class="headerlink" title="添加方法"></a>添加方法</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">public boolean add(E e) {</div><div class="line"> ensureCapacityInternal(size + 1); // Increments modCount!!</div><div class="line"> elementData[size++] = e;</div><div class="line"> return true;</div><div class="line"> }</div></pre></td></tr></table></figure>
<p>当添加的时候,首先会调用<code>ensureCapacityInternal(int minCapacity)</code>这个方法来扩充数组的容量,然后再把值加进数组。</p>
<h2 id="扩容方法"><a href="#扩容方法" class="headerlink" title="扩容方法"></a>扩容方法</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">ensureCapacityInternal</span><span class="params">(<span class="keyword">int</span> minCapacity)</span> </span>{</div><div class="line"> <span class="comment">/*先判断数组时是否为空数组*/</span></div><div class="line"> <span class="keyword">if</span> (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {</div><div class="line"> <span class="comment">/*选择 minCapacity 和10中最大的那个值*/</span></div><div class="line"> minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);</div><div class="line"> }</div><div class="line"> </div><div class="line"> ensureExplicitCapacity(minCapacity);</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">ensureExplicitCapacity</span><span class="params">(<span class="keyword">int</span> minCapacity)</span> </span>{</div><div class="line"> modCount++;</div><div class="line"></div><div class="line"> <span class="comment">// 判断minCapacity时候大于数组的容量</span></div><div class="line"> <span class="keyword">if</span> (minCapacity - elementData.length > <span class="number">0</span>)</div><div class="line"> grow(minCapacity);</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="comment">//数组的最大容量</span></div><div class="line"> <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> MAX_ARRAY_SIZE = Integer.MAX_VALUE - <span class="number">8</span>;</div><div class="line"></div><div class="line"> </div><div class="line"> <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">grow</span><span class="params">(<span class="keyword">int</span> minCapacity)</span> </span>{</div><div class="line"> <span class="comment">// 原来数组的容量</span></div><div class="line"> <span class="keyword">int</span> oldCapacity = elementData.length;</div><div class="line"> <span class="comment">/*新数组的容量为oldCapacity + oldCapacity/2 (右移一位为原来的数除以2),也就是1.5倍*/</span></div><div class="line"> <span class="keyword">int</span> newCapacity = oldCapacity + (oldCapacity >> <span class="number">1</span>);</div><div class="line"> <span class="comment">/*比较新数组的大小是否小于传进来的参数,是的话新数组的大小为传进来的参数*/</span></div><div class="line"> <span class="keyword">if</span> (newCapacity - minCapacity < <span class="number">0</span>)</div><div class="line"> newCapacity = minCapacity;</div><div class="line"> <span class="comment">/*判断新数组的大小是否大于数组的最大容量,是的话调用函数*/</span></div><div class="line"> <span class="keyword">if</span> (newCapacity - MAX_ARRAY_SIZE > <span class="number">0</span>)</div><div class="line"> newCapacity = hugeCapacity(minCapacity);</div><div class="line"> <span class="comment">// 将旧数组按照新容量复制一遍得到新数组</span></div><div class="line"> elementData = Arrays.copyOf(elementData, newCapacity);</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">int</span> <span class="title">hugeCapacity</span><span class="params">(<span class="keyword">int</span> minCapacity)</span> </span>{</div><div class="line"> <span class="comment">/*传进来的参数为负数抛出异常,如果大于数组最大容量,返回 Integer 的最大值*/</span></div><div class="line"> <span class="keyword">if</span> (minCapacity < <span class="number">0</span>) <span class="comment">// overflow</span></div><div class="line"> <span class="keyword">throw</span> <span class="keyword">new</span> OutOfMemoryError();</div><div class="line"> <span class="keyword">return</span> (minCapacity > MAX_ARRAY_SIZE) ?</div><div class="line"> Integer.MAX_VALUE :</div><div class="line"> MAX_ARRAY_SIZE;</div><div class="line"> }</div></pre></td></tr></table></figure>
<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><p>ArrayList 初始化时底层为空数组且大小为0,当有第一个元素加进去后,数组的容量为10。扩容后新容量是原来容量的1.5倍。</p>
]]></content>
<summary type="html">
<p>Java 集合中的 ArrayList 是基于数组实现的<br>
</summary>
<category term="Java源码分析" scheme="http://blog.makerwei.me/categories/Java%E6%BA%90%E7%A0%81%E5%88%86%E6%9E%90/"/>
<category term="Java" scheme="http://blog.makerwei.me/tags/Java/"/>
<category term="源码分析" scheme="http://blog.makerwei.me/tags/%E6%BA%90%E7%A0%81%E5%88%86%E6%9E%90/"/>
<category term="ArrayList" scheme="http://blog.makerwei.me/tags/ArrayList/"/>
</entry>
<entry>
<title>Java文件中多个类的使用及初始化顺序</title>
<link href="http://blog.makerwei.me/posts/28106/"/>
<id>http://blog.makerwei.me/posts/28106/</id>
<published>2017-01-02T07:41:13.000Z</published>
<updated>2017-03-23T08:13:53.000Z</updated>
<content type="html"><![CDATA[<blockquote>
<p>“一个 Java 文件中可以写多个类吗?”</p>
</blockquote>
<p>“可以,不过只允许有一个 public 修饰的类,且修饰的类的名字与文件名相同”</p>
<a id="more"></a>
<h2 id="代码验证"><a href="#代码验证" class="headerlink" title="代码验证"></a>代码验证</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div></pre></td><td class="code"><pre><div class="line">public class One {</div><div class="line"> public static void main(String[] args){</div><div class="line"> new Three(3);</div><div class="line"> }</div><div class="line">}</div><div class="line"></div><div class="line">class Two{</div><div class="line"> static Four four = new Four(4);</div><div class="line"> static {</div><div class="line"> System.out.println("This is Two static");</div><div class="line"> }</div><div class="line"> </div><div class="line"> </div><div class="line"> Two(int i){</div><div class="line"> System.out.println("This is Tow" + i);</div><div class="line"> }</div><div class="line"></div><div class="line">}</div><div class="line"></div><div class="line">class Three extends Two{</div><div class="line"> static Five five = new Five();</div><div class="line"></div><div class="line"> {</div><div class="line"> System.out.println("This is three code block");</div><div class="line"> }</div><div class="line"></div><div class="line"> Three(int i) {</div><div class="line"> super(i);</div><div class="line"> System.out.println("This is three" + i);</div><div class="line"> }</div><div class="line">}</div><div class="line"></div><div class="line">class Four{</div><div class="line"> Six six = new Six();</div><div class="line"> Four(int i){</div><div class="line"> System.out.println("This is four" +i);</div><div class="line"> }</div><div class="line">}</div><div class="line">class Five{</div><div class="line"> Five(){</div><div class="line"> System.out.println("THis is five");</div><div class="line"> }</div><div class="line">}</div><div class="line">class Six{</div><div class="line"> Six(){</div><div class="line"> System.out.println("This is Six" );</div><div class="line"> }</div><div class="line">}</div></pre></td></tr></table></figure>
<p>运行结果:</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">This is Tow2</div><div class="line">This is four4</div><div class="line">This is Two <span class="keyword">static</span></div><div class="line">THis is five</div><div class="line">This is Tow3</div><div class="line">This is three code block</div><div class="line">This is three3</div></pre></td></tr></table></figure>
<p>由此可见,在同一个 Java 文件里面允许有多个类,但只能有一个与文件名相同的 public 修饰的类,其他用 default。除此之外,我们还可以看出 代码各部分的初始化顺序,顺序是<code>父类的静态成员变量和静态代码块</code>-><code>子类的静态成员变量和静态代码块</code>-><code>父类的普通成员变量和代码块</code>-><code>父类的构造方法</code>-><code>子类的普通成员变量和代码块</code>-><code>子类的构造方法</code>。<br>另外,若子类会调用父类的构造方法,若父类构造方法中有参数且没有实现默认构造器,子类的构造方法中必须使用 <code>super(parameter)</code>方法调用父类的有参数的构造方法,且初始化在子类的普通成员变量和代码块前。</p>
]]></content>
<summary type="html">
<blockquote>
<p>“一个 Java 文件中可以写多个类吗?”</p>
</blockquote>
<p>“可以,不过只允许有一个 public 修饰的类,且修饰的类的名字与文件名相同”</p>
</summary>
<category term="java学习笔记" scheme="http://blog.makerwei.me/categories/java%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/"/>
<category term="java" scheme="http://blog.makerwei.me/tags/java/"/>
<category term="学习笔记" scheme="http://blog.makerwei.me/tags/%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/"/>
</entry>
<entry>
<title>Spring Boot 使用 Mysql</title>
<link href="http://blog.makerwei.me/posts/61345/"/>
<id>http://blog.makerwei.me/posts/61345/</id>
<published>2016-11-26T14:00:28.000Z</published>
<updated>2017-03-23T08:12:39.000Z</updated>
<content type="html"><![CDATA[<h3 id="知识点"><a href="#知识点" class="headerlink" title="知识点"></a>知识点</h3><ul>
<li>Spring Data JPA</li>
<li>Mysql</li>
</ul>
<p>本文将会教大家如何将 mysql 和 Spring Boot 结合起来,且本教程使用 <strong>Gradle</strong> 构建整个项目。</p>
<h2 id=""><a href="#" class="headerlink" title=""></a><a id="more"></a></h2><h3 id="Spring-Boot-配置"><a href="#Spring-Boot-配置" class="headerlink" title="Spring Boot 配置"></a>Spring Boot 配置</h3><ul>
<li>在IDEA中新建一个 Spring 项目,在<code>build.gradle</code>中添加依赖如下</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">dependencies {</div><div class="line"> compile(<span class="string">'org.springframework.boot:spring-boot-starter-data-jpa'</span>)</div><div class="line"> compile(<span class="string">'org.springframework.boot:spring-boot-starter-web'</span>)</div><div class="line"></div><div class="line"> <span class="comment">/* 数据库依赖 */</span></div><div class="line"> runtime group: <span class="string">'mysql'</span>, name: <span class="string">'mysql-connector-java'</span>, version: <span class="string">'5.1.35'</span></div><div class="line"></div><div class="line"> testCompile(<span class="string">'org.springframework.boot:spring-boot-starter-test'</span>)</div><div class="line">}</div></pre></td></tr></table></figure>
<ul>
<li><p>接下来在<code>src/main/resources</code>中的<code>application.properties</code>文件中配置 mysql 如下:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">spring.datasource.driverClassName=com.mysql.jdbc.Driver</div><div class="line">spring.datasource.url=jdbc:mysql://localhost:3306/myblog</div><div class="line">spring.datasource.username=你的数据库用户名</div><div class="line">spring.datasource.password=你的数据库密码</div><div class="line">spring.jpa.database= mysql</div><div class="line">spring.jpa.hibernate.ddl-auto= create-drop</div></pre></td></tr></table></figure>
<ul>
<li>url需要写 mysql 的端口和 database 名</li>
<li>username 和 password 分别写mysql 的用户名和密码</li>
<li>ddl-auto 常用的有<code>create</code>, <code>create-drop</code>, <code>update</code><ul>
<li>create:每次加载 hibernate 会删除上一次生成的表,然后根据 Entity 类 重新生成表。就算两次数据没有改变也会执行</li>
<li>create-drop:每次加载 hibernate 时生成表,退出时删除表</li>
<li>update:第一次加载 hibernate 生成表,以后加载时根据 Entity 自动更新表</li>
<li>需要自己创建数据库,通过终端进入 mysql 输入<code>create database myblog</code></li>
</ul>
</li>
</ul>
</li>
</ul>
<h3 id="创建-repository"><a href="#创建-repository" class="headerlink" title="创建 repository"></a>创建 repository</h3><p>示例是一个 blog 管理系统,所以代码中应该有 article 对象如下:</p>
<ul>
<li>Article.java<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div></pre></td><td class="code"><pre><div class="line"><span class="comment">/**</span></div><div class="line"> * Created by weixianbin on 16/10/12.</div><div class="line"> */</div><div class="line"><span class="meta">@Entity</span></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Article</span> </span>{</div><div class="line"> <span class="meta">@Id</span></div><div class="line"> <span class="meta">@GeneratedValue</span>(strategy=GenerationType.AUTO)</div><div class="line"> <span class="comment">/* 顺序号 */</span></div><div class="line"> <span class="keyword">private</span> Long id;</div><div class="line"> <span class="meta">@Column</span>(name = <span class="string">"title"</span>)</div><div class="line"> <span class="comment">/* 文章名 */</span></div><div class="line"> <span class="keyword">private</span> String title;</div><div class="line"> <span class="meta">@Column</span>(name = <span class="string">"time"</span>)</div><div class="line"> <span class="comment">/* 写作时间*/</span></div><div class="line"> <span class="keyword">private</span> String time;</div><div class="line"> <span class="meta">@Column</span>(name = <span class="string">"tags"</span>)</div><div class="line"> <span class="comment">/* 标签 */</span></div><div class="line"> <span class="keyword">private</span> String tags;</div><div class="line"> <span class="meta">@Column</span>(name = <span class="string">"info"</span>)</div><div class="line"> <span class="comment">/* 内容 */</span></div><div class="line"> <span class="keyword">private</span> String info;</div><div class="line"></div><div class="line"> <span class="function"><span class="keyword">protected</span> <span class="title">Article</span><span class="params">()</span> </span>{ }</div><div class="line"> <span class="function"><span class="keyword">public</span> Long <span class="title">getId</span><span class="params">()</span> </span>{</div><div class="line"> <span class="keyword">return</span> id;</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setId</span><span class="params">(Long id)</span> </span>{</div><div class="line"> <span class="keyword">this</span>.id = id;</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="function"><span class="keyword">public</span> String <span class="title">getTitle</span><span class="params">()</span> </span>{</div><div class="line"> <span class="keyword">return</span> title;</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setTitle</span><span class="params">(String title)</span> </span>{</div><div class="line"> <span class="keyword">this</span>.title = title;</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="function"><span class="keyword">public</span> String <span class="title">getTime</span><span class="params">()</span> </span>{</div><div class="line"> <span class="keyword">return</span> time;</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setTime</span><span class="params">(String time)</span> </span>{</div><div class="line"> <span class="keyword">this</span>.time = time;</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="function"><span class="keyword">public</span> String <span class="title">getInfo</span><span class="params">()</span> </span>{</div><div class="line"> <span class="keyword">return</span> info;</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setInfo</span><span class="params">(String info)</span> </span>{</div><div class="line"> <span class="keyword">this</span>.info = info;</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="function"><span class="keyword">public</span> String <span class="title">getTags</span><span class="params">()</span> </span>{</div><div class="line"> <span class="keyword">return</span> tags;</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setTags</span><span class="params">(String tags)</span> </span>{</div><div class="line"> <span class="keyword">this</span>.tags = tags;</div><div class="line"> }</div><div class="line">}</div></pre></td></tr></table></figure>
</li>
</ul>
<p>创建完实体类后还需要创建ArticleRepo接口,该接口继承自JpaRepository,具体代码如下:</p>
<ul>
<li>ArticleRepo.java<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="comment">/**</span></div><div class="line"> * Created by weixianbin on 16/10/12.</div><div class="line"> */</div><div class="line"><span class="meta">@Repository</span></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">ArticleRepo</span> <span class="keyword">extends</span> <span class="title">JpaRepository</span><<span class="title">Article</span>,<span class="title">Long</span>> </span>{</div><div class="line"> <span class="function">List<Article> <span class="title">findAll</span><span class="params">()</span></span>;</div><div class="line"> <span class="function">Article <span class="title">save</span><span class="params">(Article newArticle)</span></span>;</div><div class="line"></div><div class="line">}</div></pre></td></tr></table></figure>
</li>
</ul>
<p>以上代码中的注解将会帮我们自动与数据库做交互,我们可以不用写一句 SQL 语句。spring 将会自动帮我们完成建立 table 和对 database 的 insert,delete,update,find等操作。注解说明如下:</p>
<ul>
<li>@Entity:这个注解修饰的类与数据库的表对应,表的名字可由类名推断,也可以用@Table(name = “article”)具体说明。</li>
<li>@Id 和 @GeneratedValue:@Id 注解说明这是主键,@GeneratedValue(strategy=GenerationType.AUTO) 说明这个是由数据库自动生成</li>
<li>@Repository: 表示访问数据库并操作的数据库的接口,根据相关规则声明方法可以自动完成对数据库的操作,在 Controller 中使用@Autowired注解</li>
</ul>
]]></content>
<summary type="html">
<h3 id="知识点"><a href="#知识点" class="headerlink" title="知识点"></a>知识点</h3><ul>
<li>Spring Data JPA</li>
<li>Mysql</li>
</ul>
<p>本文将会教大家如何将 mysql 和 Spring Boot 结合起来,且本教程使用 <strong>Gradle</strong> 构建整个项目。</p>
<h2 id=""><a href="#" class="headerlink" title=""></a>
</summary>
<category term="spring 学习笔记" scheme="http://blog.makerwei.me/categories/spring-%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/"/>
<category term="spring boot" scheme="http://blog.makerwei.me/tags/spring-boot/"/>
<category term="mysql" scheme="http://blog.makerwei.me/tags/mysql/"/>
<category term="教程" scheme="http://blog.makerwei.me/tags/%E6%95%99%E7%A8%8B/"/>
</entry>
<entry>
<title>Spring Boot 在 IDEA 中使用 Lombok</title>
<link href="http://blog.makerwei.me/posts/56381/"/>
<id>http://blog.makerwei.me/posts/56381/</id>
<published>2016-11-03T01:23:13.000Z</published>
<updated>2017-03-23T08:12:38.000Z</updated>
<content type="html"><![CDATA[<p>Lombok 是一个帮助程序员简化代码的一个库。当你对 <code>JavaBean</code> 中个某个属性进行改动时,你需要频繁的去改动 <code>Getter</code> 和 <code>Setter</code> 或 <code>toString</code> 或<code>equals</code>等方法,<br>当你使用 Lombok,只需一个<code>@Data</code>就能帮你自动生成以上的方法。详情点击<a href="https://projectlombok.org" target="_blank" rel="external">官方网站</a>,github 地址为<a href="https://github.com/rzwitserloot/lombok" target="_blank" rel="external">https://github.com/rzwitserloot/lombok</a><br><a id="more"></a></p>
<h2 id="安装插件"><a href="#安装插件" class="headerlink" title="安装插件"></a>安装插件</h2><p>在 IDEA 中使用Lombok需要安装插件</p>
<ul>
<li><p>Windows 操作系统:</p>
<ul>
<li><kbd>File</kbd> > <kbd>Settings</kbd> > <kbd>Plugins</kbd> > <kbd>Browse repositories…</kbd> > <kbd>Search for “lombok”</kbd> > <kbd>Install Plugin</kbd></li>
</ul>
</li>
<li><p>MacOs 操作系统:</p>
<ul>
<li><kbd>Preferences</kbd> > <kbd>Settings</kbd> > <kbd>Plugins</kbd> > <kbd>Browse repositories…</kbd> > <kbd>Search for “lombok”</kbd> > <kbd>Install Plugin</kbd></li>
</ul>
</li>
<li>通常的方法:<ul>
<li>下载 <a href="https://github.com/mplushnikov/lombok-intellij-plugin/releases/latest" target="_blank" rel="external">latest release</a> 然后安装它 <kbd>Preferences</kbd> > <kbd>Plugins</kbd> > <kbd>Install plugin from disk…</kbd></li>
</ul>
</li>
</ul>
<p>安装完后重启 IDEA</p>
<h2 id="IntelliJ-IDEA-的设置"><a href="#IntelliJ-IDEA-的设置" class="headerlink" title="IntelliJ IDEA 的设置"></a>IntelliJ IDEA 的设置</h2><p>点击 <code>Preferences</code> -> <code>"Build, Execution, Deployment</code> -> <code>"Compiler, Annotation Processors"</code>. 点击 <code>Enable Annotation Processing</code></p>
<p>然后你可能需要全部重新编译你的项目通过<code>"Rebuild Project"</code>.</p>
<h2 id="添加Lombok-依赖"><a href="#添加Lombok-依赖" class="headerlink" title="添加Lombok 依赖"></a>添加Lombok 依赖</h2><p>请确认你已经将 Lombok 依赖添加到你的项目,这个插件<strong>不会自动</strong>为你添加</p>
<h3 id="使用-Gradle"><a href="#使用-Gradle" class="headerlink" title="使用 Gradle"></a>使用 Gradle</h3><p>在你的<code>build.gradle</code>里:</p>
<figure class="highlight groovy"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="comment">//'compile' can be changed to 'compileOnly' for Gradle 2.12+</span></div><div class="line"><span class="comment">// or 'provided' if using 'propdeps' plugin from SpringSource</span></div><div class="line">compile <span class="string">"org.projectlombok:lombok:1.16.10"</span></div></pre></td></tr></table></figure>
<h3 id="使用-Maven"><a href="#使用-Maven" class="headerlink" title="使用 Maven"></a>使用 Maven</h3><p>在你的 <code>pom.xml</code>里:</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="tag"><<span class="name">dependencies</span>></span></div><div class="line"> <span class="tag"><<span class="name">dependency</span>></span></div><div class="line"> <span class="tag"><<span class="name">groupId</span>></span>org.projectlombok<span class="tag"></<span class="name">groupId</span>></span></div><div class="line"> <span class="tag"><<span class="name">artifactId</span>></span>lombok<span class="tag"></<span class="name">artifactId</span>></span></div><div class="line"> <span class="tag"><<span class="name">version</span>></span>1.16.10<span class="tag"></<span class="name">version</span>></span></div><div class="line"> <span class="tag"><<span class="name">scope</span>></span>provided<span class="tag"></<span class="name">scope</span>></span></div><div class="line"> <span class="tag"></<span class="name">dependency</span>></span></div><div class="line"><span class="tag"></<span class="name">dependencies</span>></span></div></pre></td></tr></table></figure>
<p>##注解介绍<br>下面介绍几个常用的注解,更多请参考<a href="https://projectlombok.org/features/index.html" target="_blank" rel="external">https://projectlombok.org/features/index.html</a></p>
<h3 id="Getter-Setter"><a href="#Getter-Setter" class="headerlink" title="@Getter/@Setter"></a>@Getter/@Setter</h3><p>不用再写<code>public int getFoo() {return foo;}</code>类似的代码</p>
<h4 id="Java-代码"><a href="#Java-代码" class="headerlink" title="Java 代码"></a>Java 代码</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">GetterSetterExample</span> </span>{</div><div class="line"> <span class="comment">/**</span></div><div class="line"> * Age of the person. Water is wet.</div><div class="line"> */</div><div class="line"> <span class="keyword">private</span> <span class="keyword">int</span> age = <span class="number">10</span>;</div><div class="line"></div><div class="line"> <span class="comment">/**</span></div><div class="line"> * Name of the person.</div><div class="line"> */</div><div class="line"> <span class="keyword">private</span> String name;</div><div class="line"> </div><div class="line"> <span class="meta">@Override</span> <span class="function"><span class="keyword">public</span> String <span class="title">toString</span><span class="params">()</span> </span>{</div><div class="line"> <span class="keyword">return</span> String.format(<span class="string">"%s (age: %d)"</span>, name, age);</div><div class="line"> }</div><div class="line"> </div><div class="line"> <span class="comment">/**</span></div><div class="line"> * Age of the person. Water is wet.</div><div class="line"> *</div><div class="line"> * <span class="doctag">@return</span> The current value of this person's age. Circles are round.</div><div class="line"> */</div><div class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getAge</span><span class="params">()</span> </span>{</div><div class="line"> <span class="keyword">return</span> age;</div><div class="line"> }</div><div class="line"> </div><div class="line"> <span class="comment">/**</span></div><div class="line"> * Age of the person. Water is wet.</div><div class="line"> *</div><div class="line"> * <span class="doctag">@param</span> age New value for this person's age. Sky is blue.</div><div class="line"> */</div><div class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setAge</span><span class="params">(<span class="keyword">int</span> age)</span> </span>{</div><div class="line"> <span class="keyword">this</span>.age = age;</div><div class="line"> }</div><div class="line"> </div><div class="line"> <span class="comment">/**</span></div><div class="line"> * Changes the name of this person.</div><div class="line"> *</div><div class="line"> * <span class="doctag">@param</span> name The new value.</div><div class="line"> */</div><div class="line"> <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">setName</span><span class="params">(String name)</span> </span>{</div><div class="line"> <span class="keyword">this</span>.name = name;</div><div class="line"> }</div><div class="line">}</div></pre></td></tr></table></figure>
<h4 id="使用-Lombok"><a href="#使用-Lombok" class="headerlink" title="使用 Lombok"></a>使用 Lombok</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">import</span> lombok.AccessLevel;</div><div class="line"><span class="keyword">import</span> lombok.Getter;</div><div class="line"><span class="keyword">import</span> lombok.Setter;</div><div class="line"></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">GetterSetterExample</span> </span>{</div><div class="line"> <span class="comment">/**</span></div><div class="line"> * Age of the person. Water is wet.</div><div class="line"> * </div><div class="line"> * <span class="doctag">@param</span> age New value for this person's age. Sky is blue.</div><div class="line"> * <span class="doctag">@return</span> The current value of this person's age. Circles are round.</div><div class="line"> */</div><div class="line"> <span class="meta">@Getter</span> <span class="meta">@Setter</span> <span class="keyword">private</span> <span class="keyword">int</span> age = <span class="number">10</span>;</div><div class="line"> </div><div class="line"> <span class="comment">/**</span></div><div class="line"> * Name of the person.</div><div class="line"> * -- SETTER --</div><div class="line"> * Changes the name of this person.</div><div class="line"> * </div><div class="line"> * <span class="doctag">@param</span> name The new value.</div><div class="line"> */</div><div class="line"> <span class="meta">@Setter</span>(AccessLevel.PROTECTED) <span class="keyword">private</span> String name;</div><div class="line"> </div><div class="line"> <span class="meta">@Override</span> <span class="function"><span class="keyword">public</span> String <span class="title">toString</span><span class="params">()</span> </span>{</div><div class="line"> <span class="keyword">return</span> String.format(<span class="string">"%s (age: %d)"</span>, name, age);</div><div class="line"> }</div><div class="line">}</div></pre></td></tr></table></figure>
<h3 id="Data"><a href="#Data" class="headerlink" title="@Data"></a>@Data</h3><p>所有的注解方法,包括@Getter 和@Setter,通常用这个就够了</p>
<h4 id="Java-代码-1"><a href="#Java-代码-1" class="headerlink" title="Java 代码"></a>Java 代码</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div><div class="line">99</div><div class="line">100</div><div class="line">101</div><div class="line">102</div><div class="line">103</div><div class="line">104</div><div class="line">105</div><div class="line">106</div><div class="line">107</div><div class="line">108</div><div class="line">109</div><div class="line">110</div><div class="line">111</div><div class="line">112</div><div class="line">113</div><div class="line">114</div><div class="line">115</div><div class="line">116</div><div class="line">117</div><div class="line">118</div><div class="line">119</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">import</span> java.util.Arrays;</div><div class="line"> </div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">DataExample</span> </span>{</div><div class="line"> <span class="keyword">private</span> <span class="keyword">final</span> String name;</div><div class="line"> <span class="keyword">private</span> <span class="keyword">int</span> age;</div><div class="line"> <span class="keyword">private</span> <span class="keyword">double</span> score;</div><div class="line"> <span class="keyword">private</span> String[] tags;</div><div class="line"> </div><div class="line"> <span class="function"><span class="keyword">public</span> <span class="title">DataExample</span><span class="params">(String name)</span> </span>{</div><div class="line"> <span class="keyword">this</span>.name = name;</div><div class="line"> }</div><div class="line"> </div><div class="line"> <span class="function"><span class="keyword">public</span> String <span class="title">getName</span><span class="params">()</span> </span>{</div><div class="line"> <span class="keyword">return</span> <span class="keyword">this</span>.name;</div><div class="line"> }</div><div class="line"> </div><div class="line"> <span class="function"><span class="keyword">void</span> <span class="title">setAge</span><span class="params">(<span class="keyword">int</span> age)</span> </span>{</div><div class="line"> <span class="keyword">this</span>.age = age;</div><div class="line"> }</div><div class="line"> </div><div class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getAge</span><span class="params">()</span> </span>{</div><div class="line"> <span class="keyword">return</span> <span class="keyword">this</span>.age;</div><div class="line"> }</div><div class="line"> </div><div class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setScore</span><span class="params">(<span class="keyword">double</span> score)</span> </span>{</div><div class="line"> <span class="keyword">this</span>.score = score;</div><div class="line"> }</div><div class="line"> </div><div class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">double</span> <span class="title">getScore</span><span class="params">()</span> </span>{</div><div class="line"> <span class="keyword">return</span> <span class="keyword">this</span>.score;</div><div class="line"> }</div><div class="line"> </div><div class="line"> <span class="keyword">public</span> String[] getTags() {</div><div class="line"> <span class="keyword">return</span> <span class="keyword">this</span>.tags;</div><div class="line"> }</div><div class="line"> </div><div class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setTags</span><span class="params">(String[] tags)</span> </span>{</div><div class="line"> <span class="keyword">this</span>.tags = tags;</div><div class="line"> }</div><div class="line"> </div><div class="line"> <span class="meta">@Override</span> <span class="function"><span class="keyword">public</span> String <span class="title">toString</span><span class="params">()</span> </span>{</div><div class="line"> <span class="keyword">return</span> <span class="string">"DataExample("</span> + <span class="keyword">this</span>.getName() + <span class="string">", "</span> + <span class="keyword">this</span>.getAge() + <span class="string">", "</span> + <span class="keyword">this</span>.getScore() + <span class="string">", "</span> + Arrays.deepToString(<span class="keyword">this</span>.getTags()) + <span class="string">")"</span>;</div><div class="line"> }</div><div class="line"> </div><div class="line"> <span class="function"><span class="keyword">protected</span> <span class="keyword">boolean</span> <span class="title">canEqual</span><span class="params">(Object other)</span> </span>{</div><div class="line"> <span class="keyword">return</span> other <span class="keyword">instanceof</span> DataExample;</div><div class="line"> }</div><div class="line"> </div><div class="line"> <span class="meta">@Override</span> <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">equals</span><span class="params">(Object o)</span> </span>{</div><div class="line"> <span class="keyword">if</span> (o == <span class="keyword">this</span>) <span class="keyword">return</span> <span class="keyword">true</span>;</div><div class="line"> <span class="keyword">if</span> (!(o <span class="keyword">instanceof</span> DataExample)) <span class="keyword">return</span> <span class="keyword">false</span>;</div><div class="line"> DataExample other = (DataExample) o;</div><div class="line"> <span class="keyword">if</span> (!other.canEqual((Object)<span class="keyword">this</span>)) <span class="keyword">return</span> <span class="keyword">false</span>;</div><div class="line"> <span class="keyword">if</span> (<span class="keyword">this</span>.getName() == <span class="keyword">null</span> ? other.getName() != <span class="keyword">null</span> : !<span class="keyword">this</span>.getName().equals(other.getName())) <span class="keyword">return</span> <span class="keyword">false</span>;</div><div class="line"> <span class="keyword">if</span> (<span class="keyword">this</span>.getAge() != other.getAge()) <span class="keyword">return</span> <span class="keyword">false</span>;</div><div class="line"> <span class="keyword">if</span> (Double.compare(<span class="keyword">this</span>.getScore(), other.getScore()) != <span class="number">0</span>) <span class="keyword">return</span> <span class="keyword">false</span>;</div><div class="line"> <span class="keyword">if</span> (!Arrays.deepEquals(<span class="keyword">this</span>.getTags(), other.getTags())) <span class="keyword">return</span> <span class="keyword">false</span>;</div><div class="line"> <span class="keyword">return</span> <span class="keyword">true</span>;</div><div class="line"> }</div><div class="line"> </div><div class="line"> <span class="meta">@Override</span> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">hashCode</span><span class="params">()</span> </span>{</div><div class="line"> <span class="keyword">final</span> <span class="keyword">int</span> PRIME = <span class="number">59</span>;</div><div class="line"> <span class="keyword">int</span> result = <span class="number">1</span>;</div><div class="line"> <span class="keyword">final</span> <span class="keyword">long</span> temp1 = Double.doubleToLongBits(<span class="keyword">this</span>.getScore());</div><div class="line"> result = (result*PRIME) + (<span class="keyword">this</span>.getName() == <span class="keyword">null</span> ? <span class="number">43</span> : <span class="keyword">this</span>.getName().hashCode());</div><div class="line"> result = (result*PRIME) + <span class="keyword">this</span>.getAge();</div><div class="line"> result = (result*PRIME) + (<span class="keyword">int</span>)(temp1 ^ (temp1 >>> <span class="number">32</span>));</div><div class="line"> result = (result*PRIME) + Arrays.deepHashCode(<span class="keyword">this</span>.getTags());</div><div class="line"> <span class="keyword">return</span> result;</div><div class="line"> }</div><div class="line"> </div><div class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="class"><span class="keyword">class</span> <span class="title">Exercise</span><<span class="title">T</span>> </span>{</div><div class="line"> <span class="keyword">private</span> <span class="keyword">final</span> String name;</div><div class="line"> <span class="keyword">private</span> <span class="keyword">final</span> T value;</div><div class="line"> </div><div class="line"> <span class="function"><span class="keyword">private</span> <span class="title">Exercise</span><span class="params">(String name, T value)</span> </span>{</div><div class="line"> <span class="keyword">this</span>.name = name;</div><div class="line"> <span class="keyword">this</span>.value = value;</div><div class="line"> }</div><div class="line"> </div><div class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <T> <span class="function">Exercise<T> <span class="title">of</span><span class="params">(String name, T value)</span> </span>{</div><div class="line"> <span class="keyword">return</span> <span class="keyword">new</span> Exercise<T>(name, value);</div><div class="line"> }</div><div class="line"> </div><div class="line"> <span class="function"><span class="keyword">public</span> String <span class="title">getName</span><span class="params">()</span> </span>{</div><div class="line"> <span class="keyword">return</span> <span class="keyword">this</span>.name;</div><div class="line"> }</div><div class="line"> </div><div class="line"> <span class="function"><span class="keyword">public</span> T <span class="title">getValue</span><span class="params">()</span> </span>{</div><div class="line"> <span class="keyword">return</span> <span class="keyword">this</span>.value;</div><div class="line"> }</div><div class="line"> </div><div class="line"> <span class="meta">@Override</span> <span class="function"><span class="keyword">public</span> String <span class="title">toString</span><span class="params">()</span> </span>{</div><div class="line"> <span class="keyword">return</span> <span class="string">"Exercise(name="</span> + <span class="keyword">this</span>.getName() + <span class="string">", value="</span> + <span class="keyword">this</span>.getValue() + <span class="string">")"</span>;</div><div class="line"> }</div><div class="line"> </div><div class="line"> <span class="function"><span class="keyword">protected</span> <span class="keyword">boolean</span> <span class="title">canEqual</span><span class="params">(Object other)</span> </span>{</div><div class="line"> <span class="keyword">return</span> other <span class="keyword">instanceof</span> Exercise;</div><div class="line"> }</div><div class="line"> </div><div class="line"> <span class="meta">@Override</span> <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">equals</span><span class="params">(Object o)</span> </span>{</div><div class="line"> <span class="keyword">if</span> (o == <span class="keyword">this</span>) <span class="keyword">return</span> <span class="keyword">true</span>;</div><div class="line"> <span class="keyword">if</span> (!(o <span class="keyword">instanceof</span> Exercise)) <span class="keyword">return</span> <span class="keyword">false</span>;</div><div class="line"> Exercise<?> other = (Exercise<?>) o;</div><div class="line"> <span class="keyword">if</span> (!other.canEqual((Object)<span class="keyword">this</span>)) <span class="keyword">return</span> <span class="keyword">false</span>;</div><div class="line"> <span class="keyword">if</span> (<span class="keyword">this</span>.getName() == <span class="keyword">null</span> ? other.getValue() != <span class="keyword">null</span> : !<span class="keyword">this</span>.getName().equals(other.getName())) <span class="keyword">return</span> <span class="keyword">false</span>;</div><div class="line"> <span class="keyword">if</span> (<span class="keyword">this</span>.getValue() == <span class="keyword">null</span> ? other.getValue() != <span class="keyword">null</span> : !<span class="keyword">this</span>.getValue().equals(other.getValue())) <span class="keyword">return</span> <span class="keyword">false</span>;</div><div class="line"> <span class="keyword">return</span> <span class="keyword">true</span>;</div><div class="line"> }</div><div class="line"> </div><div class="line"> <span class="meta">@Override</span> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">hashCode</span><span class="params">()</span> </span>{</div><div class="line"> <span class="keyword">final</span> <span class="keyword">int</span> PRIME = <span class="number">59</span>;</div><div class="line"> <span class="keyword">int</span> result = <span class="number">1</span>;</div><div class="line"> result = (result*PRIME) + (<span class="keyword">this</span>.getName() == <span class="keyword">null</span> ? <span class="number">43</span> : <span class="keyword">this</span>.getName().hashCode());</div><div class="line"> result = (result*PRIME) + (<span class="keyword">this</span>.getValue() == <span class="keyword">null</span> ? <span class="number">43</span> : <span class="keyword">this</span>.getValue().hashCode());</div><div class="line"> <span class="keyword">return</span> result;</div><div class="line"> }</div><div class="line"> }</div><div class="line"> }</div></pre></td></tr></table></figure>
<h4 id="使用-Lombok-1"><a href="#使用-Lombok-1" class="headerlink" title="使用 Lombok"></a>使用 Lombok</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">import</span> lombok.AccessLevel;</div><div class="line"><span class="keyword">import</span> lombok.Setter;</div><div class="line"><span class="keyword">import</span> lombok.Data;</div><div class="line"><span class="keyword">import</span> lombok.ToString;</div><div class="line"></div><div class="line"><span class="meta">@Data</span> </div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">DataExample</span> </span>{</div><div class="line"> <span class="keyword">private</span> <span class="keyword">final</span> String name;</div><div class="line"> <span class="meta">@Setter</span>(AccessLevel.PACKAGE) <span class="keyword">private</span> <span class="keyword">int</span> age;</div><div class="line"> <span class="keyword">private</span> <span class="keyword">double</span> score;</div><div class="line"> <span class="keyword">private</span> String[] tags;</div><div class="line"> </div><div class="line"> <span class="meta">@ToString</span>(includeFieldNames=<span class="keyword">true</span>)</div><div class="line"> <span class="meta">@Data</span>(staticConstructor=<span class="string">"of"</span>)</div><div class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="class"><span class="keyword">class</span> <span class="title">Exercise</span><<span class="title">T</span>> </span>{</div><div class="line"> <span class="keyword">private</span> <span class="keyword">final</span> String name;</div><div class="line"> <span class="keyword">private</span> <span class="keyword">final</span> T value; </div><div class="line"> }</div><div class="line"> }</div></pre></td></tr></table></figure>
]]></content>
<summary type="html">
<p>Lombok 是一个帮助程序员简化代码的一个库。当你对 <code>JavaBean</code> 中个某个属性进行改动时,你需要频繁的去改动 <code>Getter</code> 和 <code>Setter</code> 或 <code>toString</code> 或<code>equals</code>等方法,<br>当你使用 Lombok,只需一个<code>@Data</code>就能帮你自动生成以上的方法。详情点击<a href="https://projectlombok.org">官方网站</a>,github 地址为<a href="https://github.com/rzwitserloot/lombok">https://github.com/rzwitserloot/lombok</a><br>
</summary>
<category term="Java" scheme="http://blog.makerwei.me/categories/Java/"/>
<category term="Java" scheme="http://blog.makerwei.me/tags/Java/"/>
<category term="IDEA" scheme="http://blog.makerwei.me/tags/IDEA/"/>
<category term="Spring boot" scheme="http://blog.makerwei.me/tags/Spring-boot/"/>
<category term="Lombok" scheme="http://blog.makerwei.me/tags/Lombok/"/>
</entry>
<entry>
<title>Android AsyncTask 使用总结</title>
<link href="http://blog.makerwei.me/posts/18614/"/>
<id>http://blog.makerwei.me/posts/18614/</id>
<published>2016-02-26T10:39:19.000Z</published>
<updated>2017-03-23T08:12:37.000Z</updated>
<content type="html"><![CDATA[<h2 id="使用原因"><a href="#使用原因" class="headerlink" title="使用原因"></a>使用原因</h2><p>由于android UI主线程是不安全的,如果在子线程中进行更新UI的操作,就会抛出<code>android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views</code>的异常。而且UI主线程不能处理耗时的操作,例如访问网络或者查询数据库;一旦UI线程超过5s没有响应用户请求,就会出现ANR(“Application Not Responding”)错误。所以,在一些应用场景我们不得不采用android的异步消息处理机制。<br><a id="more"></a></p>
<h2 id="AsyncTask(异步任务)"><a href="#AsyncTask(异步任务)" class="headerlink" title="AsyncTask(异步任务)"></a>AsyncTask(异步任务)</h2><p>AsyncTask是一个抽象类,我们使用它就要定义一个类来继承AsyncTask这个抽象类,并实现其中doInBackgroud方法。</p>
<p>使用时我们为AsyncTask类指定3个泛型参数。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">public class XXXX extends AsyncTask<Params, Progress, Result>{}</div></pre></td></tr></table></figure>
<ul>
<li><code>Params</code> 启动任务时输入的参数类型</li>
<li><code>Progress</code> 后台任务执行中返回给UI线程的执行进度值的类型</li>
<li><code>Result</code> 后台任务完成后返回给UI线程的结果的类型</li>
</ul>
<p>若不指定的话,则都将写成Void,如下:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">AsyncTask <Void, Void, Void></div></pre></td></tr></table></figure>
<p>此外,当我们执行一个异步任务时,其会按照下面4个方法分别执行:</p>
<ul>
<li><code>onPreExecute()</code>:执行后台耗时操作前被调用,并且在UI线程中执行,通常我们在这个方法中完成一些初始化操作,例如弹出ProgressDialog。</li>
<li><code>doInBackground(Params... params)</code>: 这个方法必须重写,在onPreExecute()方法执行完后执行,在这里面实现我们获取数据等一些耗时操作。</li>
<li><code>onProgressUpdate(Progess... values)</code>: 这个方法是在UI线程中执行,在异步任务中,有时候我们需要将执行的进度返回给UI线程,如我们要显示下载一张图片的进度,就可以使用这个方法。在这个方法调用之前,我们需要在doInBackground方法中调用publishProgress(Progress)方法来将进度传递给onProgressUpdate方法来更新。</li>
<li><code>onPostExecute(Result... result)</code>:当doInBackground完成后会将结果返回给这个方法,调用此方法可以更新UI</li>
</ul>
<p>以上方法<code>doInBackground</code>方法必须<code>实现</code>!</p>
<p>在UI线程中调用执行:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">new XXXX().execute();</div></pre></td></tr></table></figure>
<p>注意:</p>
<ul>
<li>必须在UI线程中创建AsyncTask的实例</li>
<li>必须在UI线程中调用AsyncTask的execute()方法</li>
<li>每个AsyncTask只能被执行一次,多次调用将会引发异常</li>
<li>不要手动的调用onPreExecute(), onPostExecute(Result),doInBackground(Params…), onProgressUpdate(Progress…)这几个方法</li>
</ul>
<h2 id="AsyncTask问题"><a href="#AsyncTask问题" class="headerlink" title="AsyncTask问题"></a>AsyncTask问题</h2><ul>
<li><p>Activity中创建AsyncTask,当Activity的销毁而AsyncTask会一直执行, 直到doInBackground()方法执行完毕。然后,如果 cancel(boolean)被调用, 那么onCancelled(Result result) 方法会被执行;否则,执行onPostExecute(Result result) 方法。如果我们的Activity销毁之前,没有取消AsyncTask,这有可能让我们的AsyncTask崩溃。因为它想要处理的view已经不存在了。所以,我们总是必须确保在销毁活动之前取消任务</p>
</li>
<li><p>如果AsyncTask被声明为Activity的非静态的内部类,那么AsyncTask会保留一个对创建了AsyncTask的Activity的引用。如果Activity已经被销毁,AsyncTask的后台线程还在执行,它将继续在内存里保留这个引用,导致Activity无法被回收,引起内存泄露</p>
</li>
<li><p>屏幕旋转或Activity在后台被系统杀掉等情况会导致Activity的重新创建,之前运行的AsyncTask会持有一个之前Activity的引用,这个引用已经无效,这时调用onPostExecute()再去更新界面将不再生效</p>
</li>
</ul>
]]></content>
<summary type="html">
<h2 id="使用原因"><a href="#使用原因" class="headerlink" title="使用原因"></a>使用原因</h2><p>由于android UI主线程是不安全的,如果在子线程中进行更新UI的操作,就会抛出<code>android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views</code>的异常。而且UI主线程不能处理耗时的操作,例如访问网络或者查询数据库;一旦UI线程超过5s没有响应用户请求,就会出现ANR(“Application Not Responding”)错误。所以,在一些应用场景我们不得不采用android的异步消息处理机制。<br>
</summary>
<category term="android学习笔记" scheme="http://blog.makerwei.me/categories/android%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/"/>
<category term="android" scheme="http://blog.makerwei.me/tags/android/"/>
<category term="AsyncTask" scheme="http://blog.makerwei.me/tags/AsyncTask/"/>
<category term="异步" scheme="http://blog.makerwei.me/tags/%E5%BC%82%E6%AD%A5/"/>
</entry>
<entry>
<title>OkHttp基本使用</title>
<link href="http://blog.makerwei.me/posts/23698/"/>
<id>http://blog.makerwei.me/posts/23698/</id>
<published>2016-02-26T08:28:19.000Z</published>
<updated>2017-03-23T08:11:36.000Z</updated>
<content type="html"><![CDATA[<blockquote>
<p>HTTP is the way modern applications network. It’s how we exchange data & media. Doing HTTP efficiently makes your stuff load faster and saves bandwidth.</p>
</blockquote>
<a id="more"></a>
<h2 id="HTTP-GET"><a href="#HTTP-GET" class="headerlink" title="HTTP GET"></a>HTTP GET</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">OkHttpClient client = new OkHttpClient();</div><div class="line"> Request request = new Request.Builder().url("http://baidu.com").build();</div><div class="line"> client.newCall(request).enqueue(new Callback() {</div><div class="line"> @Override</div><div class="line"> public void onFailure(Request request, IOException e) {</div><div class="line"></div><div class="line"> }</div><div class="line"></div><div class="line"> @Override</div><div class="line"> public void onResponse(Response response) throws IOException {</div><div class="line"> //Log.e(TAG, response.body().string());</div><div class="line"> }</div><div class="line"> });</div></pre></td></tr></table></figure>
<h3 id="注意"><a href="#注意" class="headerlink" title="注意"></a>注意</h3><ol>
<li>Request是OkHttp中访问的请求,<code>Builder</code>是辅助类。Response即OkHttp中的响应。</li>
<li>构造一个<code>Request</code>对象,设置好URL,还可以通过<code>Request.Builder</code>设置更多的参数。</li>
<li><code>enqueue</code>是异步的请求,还有execute()方法直接返回Response。</li>
<li><code>response.body()</code>返回ResponseBody类,可以通过string方法得到string类型数据。</li>
<li>onResponse执行的线程<code>并不是UI线程</code>,所以要注意线程间通信问题。</li>
</ol>
<h2 id="HTTP-POST"><a href="#HTTP-POST" class="headerlink" title="HTTP POST"></a>HTTP POST</h2><h3 id="post提交键值对"><a href="#post提交键值对" class="headerlink" title="post提交键值对"></a>post提交键值对</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div></pre></td><td class="code"><pre><div class="line">OkHttpClient client = new OkHttpClient();</div><div class="line"> RequestBody requestBody = new FormEncodingBuilder()</div><div class="line"> .add("name","xxx")</div><div class="line"> .add("ID","xxx")</div><div class="line"> .build();</div><div class="line"> Request request = new Request.Builder().url("http://xxxxxxx.com")</div><div class="line"> .post(requestBody)</div><div class="line"> .build();</div><div class="line"> client.newCall(request).enqueue(new Callback() {</div><div class="line"> @Override</div><div class="line"> public void onFailure(Request request, IOException e) {</div><div class="line"></div><div class="line"> }</div><div class="line"></div><div class="line"> @Override</div><div class="line"> public void onResponse(Response response) throws IOException {</div><div class="line"> Log.e(TAG, response.body().string());</div><div class="line"> }</div><div class="line"> });</div></pre></td></tr></table></figure>
<ol>
<li>先构造RequestBody,通过FormEncodingBuilder添加多个String键值对。</li>
</ol>
<h3 id="post提交Json数据"><a href="#post提交Json数据" class="headerlink" title="post提交Json数据"></a>post提交Json数据</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line">final MediaType JSON = MediaType.parse("application/json; charset=utf-8");</div><div class="line"> OkHttpClient client = new OkHttpClient();</div><div class="line"> RequestBody requestBody = RequestBody.create(JSON,json);</div><div class="line"> Request request = new Request.Builder().url("http://xxxxxxx.com")</div><div class="line"> .post(requestBody)</div><div class="line"> .build();</div><div class="line"> client.newCall(request).enqueue(new Callback() {</div><div class="line"> @Override</div><div class="line"> public void onFailure(Request request, IOException e) {</div><div class="line"></div><div class="line"> }</div><div class="line"></div><div class="line"> @Override</div><div class="line"> public void onResponse(Response response) throws IOException {</div><div class="line"> Log.e(TAG, response.body().string());</div><div class="line"> }</div><div class="line"> });</div></pre></td></tr></table></figure>
<p>具体参考<a href="http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0106/2275.html" target="_blank" rel="external">泡网OkHttp使用教程</a>,<a href="http://www.cnblogs.com/ct2011/p/3997368.html" target="_blank" rel="external">OkHttp使用进阶 译自OkHttp Github官方教程</a></p>
]]></content>
<summary type="html">
<blockquote>
<p>HTTP is the way modern applications network. It’s how we exchange data &amp; media. Doing HTTP efficiently makes your stuff load faster and saves bandwidth.</p>
</blockquote>
</summary>
<category term="android学习笔记" scheme="http://blog.makerwei.me/categories/android%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/"/>
<category term="android" scheme="http://blog.makerwei.me/tags/android/"/>
<category term="OkHttp" scheme="http://blog.makerwei.me/tags/OkHttp/"/>
</entry>
<entry>
<title>Android中Gson解析json数据</title>
<link href="http://blog.makerwei.me/posts/35060/"/>
<id>http://blog.makerwei.me/posts/35060/</id>
<published>2016-02-16T12:27:37.000Z</published>
<updated>2017-03-23T08:11:44.000Z</updated>
<content type="html"><![CDATA[<blockquote>
<p>JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。 </p>
<p>JSON对象:对象在js中表示为“{}”括起来的内容,数据结构为 {key:value,key:value,…}的键值对的结构,在面向对象的语言中,key为对象的属性,value为对应的属性值,所以很容易理解,取值方法为 对象.key 获取属性值,这个属性值的类型可以是 数字、字符串、数组、对象几种。</p>
<p>JSON数组:数组在js中是中括号“[]”括起来的内容,数据结构为 [“java”,”javascript”,”vb”,…],取值方式和所有语言中一样,使用索引获取,字段值的类型可以是 数字、字符串、数组、对象几种。<br> ————百度百科</p>
</blockquote>
<hr>
<a id="more"></a>
<p>解析JSON首先要创建JSON数据对应的一个对象,例如假设我们获得数据如下:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">{</div><div class="line"> "status": "error",</div><div class="line"> "UUID": 123</div><div class="line">}</div></pre></td></tr></table></figure>
<p>则要创建的类<code>Data.java</code>如下:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div></pre></td><td class="code"><pre><div class="line">public class Data {</div><div class="line"> private String status;</div><div class="line"> private String UUID;</div><div class="line"></div><div class="line"> public String getStatus() {</div><div class="line"> return status;</div><div class="line"> }</div><div class="line"></div><div class="line"> public void setStatus(String status) {</div><div class="line"> this.status = status;</div><div class="line"> }</div><div class="line"></div><div class="line"> public String getUUID() {</div><div class="line"> return UUID;</div><div class="line"> }</div><div class="line"></div><div class="line"> public void setUUID(String UUID) {</div><div class="line"> this.UUID = UUID;</div><div class="line"> }</div><div class="line">}</div></pre></td></tr></table></figure>
<p>解析代码如下:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">String data = {"status": "error","UUID": 123}</div><div class="line">Gson gon = new Gson();</div><div class="line">Data data = gson.fronJson(data,Data.class);</div><div class="line">System.out.println(getStatus());</div></pre></td></tr></table></figure>
<p>解析多层嵌套JSON,例如<code>Gank.io</code>提供的数据:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div></pre></td><td class="code"><pre><div class="line">{</div><div class="line"> "error": false, </div><div class="line"> "results": [</div><div class="line"> {</div><div class="line"> "_id": "56d7b07f6776595ac3e5cf92", </div><div class="line"> "_ns": "ganhuo", </div><div class="line"> "createdAt": "2016-03-03T11:33:19.706Z", </div><div class="line"> "desc": "3.3", </div><div class="line"> "publishedAt": "2016-03-03T12:12:56.684Z", </div><div class="line"> "source": "chrome", </div><div class="line"> "type": "\u798f\u5229", </div><div class="line"> "url": "http://ww4.sinaimg.cn/large/7a8aed7bjw1f1jionqvz6j20hs0qoq7p.jpg", </div><div class="line"> "used": true, </div><div class="line"> "who": "\u5f20\u6db5\u5b87"</div><div class="line"> }, </div><div class="line"> {</div><div class="line"> "_id": "56d6481e6776592a03e624a4", </div><div class="line"> "_ns": "ganhuo", </div><div class="line"> "createdAt": "2016-03-02T09:55:42.63Z", </div><div class="line"> "desc": "3.2", </div><div class="line"> "publishedAt": "2016-03-02T12:06:37.242Z", </div><div class="line"> "source": "chrome", </div><div class="line"> "type": "\u798f\u5229", </div><div class="line"> "url": "http://ww3.sinaimg.cn/large/7a8aed7bjw1f1ia8qj5qbj20nd0zkmzp.jpg", </div><div class="line"> "used": true, </div><div class="line"> "who": "\u5f20\u6db5\u5b87"</div><div class="line"> }</div><div class="line"> ]</div><div class="line">}</div></pre></td></tr></table></figure>
<p>创建的实体类Data.java为:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div><div class="line">99</div><div class="line">100</div><div class="line">101</div><div class="line">102</div><div class="line">103</div><div class="line">104</div><div class="line">105</div><div class="line">106</div><div class="line">107</div><div class="line">108</div><div class="line">109</div><div class="line">110</div><div class="line">111</div><div class="line">112</div><div class="line">113</div><div class="line">114</div><div class="line">115</div><div class="line">116</div><div class="line">117</div><div class="line">118</div><div class="line">119</div><div class="line">120</div><div class="line">121</div><div class="line">122</div><div class="line">123</div><div class="line">124</div><div class="line">125</div><div class="line">126</div><div class="line">127</div><div class="line">128</div><div class="line">129</div><div class="line">130</div><div class="line">131</div><div class="line">132</div><div class="line">133</div><div class="line">134</div><div class="line">135</div><div class="line">136</div><div class="line">137</div><div class="line">138</div><div class="line">139</div><div class="line">140</div><div class="line">141</div><div class="line">142</div><div class="line">143</div><div class="line">144</div><div class="line">145</div><div class="line">146</div><div class="line">147</div><div class="line">148</div><div class="line">149</div><div class="line">150</div><div class="line">151</div><div class="line">152</div><div class="line">153</div><div class="line">154</div><div class="line">155</div><div class="line">156</div><div class="line">157</div><div class="line">158</div><div class="line">159</div><div class="line">160</div><div class="line">161</div><div class="line">162</div><div class="line">163</div><div class="line">164</div><div class="line">165</div><div class="line">166</div><div class="line">167</div><div class="line">168</div><div class="line">169</div><div class="line">170</div><div class="line">171</div><div class="line">172</div><div class="line">173</div><div class="line">174</div><div class="line">175</div><div class="line">176</div><div class="line">177</div><div class="line">178</div><div class="line">179</div><div class="line">180</div><div class="line">181</div><div class="line">182</div><div class="line">183</div><div class="line">184</div><div class="line">185</div><div class="line">186</div><div class="line">187</div><div class="line">188</div><div class="line">189</div><div class="line">190</div><div class="line">191</div><div class="line">192</div><div class="line">193</div><div class="line">194</div><div class="line">195</div><div class="line">196</div><div class="line">197</div><div class="line">198</div><div class="line">199</div><div class="line">200</div><div class="line">201</div><div class="line">202</div><div class="line">203</div><div class="line">204</div><div class="line">205</div><div class="line">206</div><div class="line">207</div><div class="line">208</div><div class="line">209</div><div class="line">210</div><div class="line">211</div><div class="line">212</div><div class="line">213</div><div class="line">214</div><div class="line">215</div><div class="line">216</div><div class="line">217</div><div class="line">218</div><div class="line">219</div></pre></td><td class="code"><pre><div class="line">import java.util.ArrayList;</div><div class="line">import java.util.List;</div><div class="line"></div><div class="line">import com.google.gson.annotations.Expose;</div><div class="line">import com.google.gson.annotations.SerializedName;</div><div class="line"></div><div class="line">public class Data {</div><div class="line"></div><div class="line"> @SerializedName("error")</div><div class="line"> @Expose</div><div class="line"> private boolean error;</div><div class="line"> @SerializedName("results")</div><div class="line"> @Expose</div><div class="line"> private List<Result> results = new ArrayList<Result>();</div><div class="line"></div><div class="line"> /**</div><div class="line"> * @return The error</div><div class="line"> */</div><div class="line"> public boolean isError() {</div><div class="line"> return error;</div><div class="line"> }</div><div class="line"></div><div class="line"> /**</div><div class="line"> * @param error The error</div><div class="line"> */</div><div class="line"> public void setError(boolean error) {</div><div class="line"> this.error = error;</div><div class="line"> }</div><div class="line"></div><div class="line"> /**</div><div class="line"> * @return The results</div><div class="line"> */</div><div class="line"> public List<Result> getResults() {</div><div class="line"> return results;</div><div class="line"> }</div><div class="line"></div><div class="line"> /**</div><div class="line"> * @param results The results</div><div class="line"> */</div><div class="line"> public void setResults(List<Result> results) {</div><div class="line"> this.results = results;</div><div class="line"> }</div><div class="line"></div><div class="line"> public static class Result {</div><div class="line"></div><div class="line"> @SerializedName("_id")</div><div class="line"> @Expose</div><div class="line"> private String Id;</div><div class="line"> @SerializedName("_ns")</div><div class="line"> @Expose</div><div class="line"> private String Ns;</div><div class="line"> @SerializedName("createdAt")</div><div class="line"> @Expose</div><div class="line"> private String createdAt;</div><div class="line"> @SerializedName("desc")</div><div class="line"> @Expose</div><div class="line"> private String desc;</div><div class="line"> @SerializedName("publishedAt")</div><div class="line"> @Expose</div><div class="line"> private String publishedAt;</div><div class="line"> @SerializedName("source")</div><div class="line"> @Expose</div><div class="line"> private String source;</div><div class="line"> @SerializedName("type")</div><div class="line"> @Expose</div><div class="line"> private String type;</div><div class="line"> @SerializedName("url")</div><div class="line"> @Expose</div><div class="line"> private String url;</div><div class="line"> @SerializedName("used")</div><div class="line"> @Expose</div><div class="line"> private boolean used;</div><div class="line"> @SerializedName("who")</div><div class="line"> @Expose</div><div class="line"> private String who;</div><div class="line"></div><div class="line"> /**</div><div class="line"> * @return The Id</div><div class="line"> */</div><div class="line"> public String getId() {</div><div class="line"> return Id;</div><div class="line"> }</div><div class="line"></div><div class="line"> /**</div><div class="line"> * @param Id The _id</div><div class="line"> */</div><div class="line"> public void setId(String Id) {</div><div class="line"> this.Id = Id;</div><div class="line"> }</div><div class="line"></div><div class="line"> /**</div><div class="line"> * @return The Ns</div><div class="line"> */</div><div class="line"> public String getNs() {</div><div class="line"> return Ns;</div><div class="line"> }</div><div class="line"></div><div class="line"> /**</div><div class="line"> * @param Ns The _ns</div><div class="line"> */</div><div class="line"> public void setNs(String Ns) {</div><div class="line"> this.Ns = Ns;</div><div class="line"> }</div><div class="line"></div><div class="line"> /**</div><div class="line"> * @return The createdAt</div><div class="line"> */</div><div class="line"> public String getCreatedAt() {</div><div class="line"> return createdAt;</div><div class="line"> }</div><div class="line"></div><div class="line"> /**</div><div class="line"> * @param createdAt The createdAt</div><div class="line"> */</div><div class="line"> public void setCreatedAt(String createdAt) {</div><div class="line"> this.createdAt = createdAt;</div><div class="line"> }</div><div class="line"></div><div class="line"> /**</div><div class="line"> * @return The desc</div><div class="line"> */</div><div class="line"> public String getDesc() {</div><div class="line"> return desc;</div><div class="line"> }</div><div class="line"></div><div class="line"> /**</div><div class="line"> * @param desc The desc</div><div class="line"> */</div><div class="line"> public void setDesc(String desc) {</div><div class="line"> this.desc = desc;</div><div class="line"> }</div><div class="line"></div><div class="line"> /**</div><div class="line"> * @return The publishedAt</div><div class="line"> */</div><div class="line"> public String getPublishedAt() {</div><div class="line"> return publishedAt;</div><div class="line"> }</div><div class="line"></div><div class="line"> /**</div><div class="line"> * @param publishedAt The publishedAt</div><div class="line"> */</div><div class="line"> public void setPublishedAt(String publishedAt) {</div><div class="line"> this.publishedAt = publishedAt;</div><div class="line"> }</div><div class="line"></div><div class="line"> /**</div><div class="line"> * @return The source</div><div class="line"> */</div><div class="line"> public String getSource() {</div><div class="line"> return source;</div><div class="line"> }</div><div class="line"></div><div class="line"> /**</div><div class="line"> * @param source The source</div><div class="line"> */</div><div class="line"> public void setSource(String source) {</div><div class="line"> this.source = source;</div><div class="line"> }</div><div class="line"></div><div class="line"> /**</div><div class="line"> * @return The type</div><div class="line"> */</div><div class="line"> public String getType() {</div><div class="line"> return type;</div><div class="line"> }</div><div class="line"></div><div class="line"> /**</div><div class="line"> * @param type The type</div><div class="line"> */</div><div class="line"> public void setType(String type) {</div><div class="line"> this.type = type;</div><div class="line"> }</div><div class="line"></div><div class="line"> /**</div><div class="line"> * @return The url</div><div class="line"> */</div><div class="line"> public String getUrl() {</div><div class="line"> return url;</div><div class="line"> }</div><div class="line"></div><div class="line"> /**</div><div class="line"> * @param url The url</div><div class="line"> */</div><div class="line"> public void setUrl(String url) {</div><div class="line"> this.url = url;</div><div class="line"> }</div><div class="line"></div><div class="line"> /**</div><div class="line"> * @return The used</div><div class="line"> */</div><div class="line"> public boolean isUsed() {</div><div class="line"> return used;</div><div class="line"> }</div><div class="line"></div><div class="line"> /**</div><div class="line"> * @param used The used</div><div class="line"> */</div><div class="line"> public void setUsed(boolean used) {</div><div class="line"> this.used = used;</div><div class="line"> }</div><div class="line"></div><div class="line"> /**</div><div class="line"> * @return The who</div><div class="line"> */</div><div class="line"> public String getWho() {</div><div class="line"> return who;</div><div class="line"> }</div><div class="line"></div><div class="line"> /**</div><div class="line"> * @param who The who</div><div class="line"> */</div><div class="line"> public void setWho(String who) {</div><div class="line"> this.who = who;</div><div class="line"> }</div><div class="line"></div><div class="line"> }</div><div class="line"></div><div class="line">}</div></pre></td></tr></table></figure>
<p>总结:</p>
<p>如果是用[]括起来的部分就是一个List,只用{}就定义为<code>public 类型 名称</code>,对照着数据一层一层的写就是了。</p>
<p><code>http://www.jsonschema2pojo.org/</code>这个网站可以帮你自动生成Gson解析需要的实体类,不过需要翻墙</p>
<p>参考:<a href="http://www.cnblogs.com/jxgxy/p/3677256.html" target="_blank" rel="external">http://www.cnblogs.com/jxgxy/p/3677256.html</a></p>
]]></content>
<summary type="html">
<blockquote>
<p>JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。 </p>
<p>JSON对象:对象在js中表示为“{}”括起来的内容,数据结构为 {key:value,key:value,…}的键值对的结构,在面向对象的语言中,key为对象的属性,value为对应的属性值,所以很容易理解,取值方法为 对象.key 获取属性值,这个属性值的类型可以是 数字、字符串、数组、对象几种。</p>
<p>JSON数组:数组在js中是中括号“[]”括起来的内容,数据结构为 [“java”,”javascript”,”vb”,…],取值方式和所有语言中一样,使用索引获取,字段值的类型可以是 数字、字符串、数组、对象几种。<br> ————百度百科</p>
</blockquote>
<hr>
</summary>
<category term="android学习笔记" scheme="http://blog.makerwei.me/categories/android%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/"/>
<category term="android" scheme="http://blog.makerwei.me/tags/android/"/>
<category term="json" scheme="http://blog.makerwei.me/tags/json/"/>
<category term="Gson" scheme="http://blog.makerwei.me/tags/Gson/"/>
</entry>
<entry>
<title>Mac上 Sublime Text 打造 Latex 中文环境</title>
<link href="http://blog.makerwei.me/posts/29855/"/>
<id>http://blog.makerwei.me/posts/29855/</id>
<published>2016-02-05T07:41:13.000Z</published>
<updated>2017-03-23T08:11:38.000Z</updated>
<content type="html"><![CDATA[<blockquote>
<p>工欲善其事必先利其器</p>
</blockquote>
<p>这次参加数模美赛,发现写论文时用word时,不是那么得心应手,最后半个小时操作失误出现了一些小问题,不过总算弄好了。所以为了下次不在那么窘迫,同时也是本着瞎折腾的精神,弄起了Latex。</p>
<p>Sublime text 是一款很优秀的编辑器,它不仅体积较小而且运行速度快,把它作为Latex的编辑器最好不过了<br><a id="more"></a></p>
<h2 id="安装步骤"><a href="#安装步骤" class="headerlink" title="安装步骤"></a>安装步骤</h2><ol>
<li><p>下载<a href="http://www.tug.org/mactex/" target="_blank" rel="external">MacTex</a>和<a href="http://skim-app.sourceforge.net" target="_blank" rel="external">skim</a>并安装</p>
</li>
<li><p>首先安装Package Control<br> 从菜单 <code>View - Show Console</code> 或者 <code>^ + ~</code> 快捷键,调出 console。在里面输入以下代码</p>
<p> <em>Sublime Text 2</em></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">import urllib2,os,hashlib; h = '2915d1851351e5ee549c20394736b442' + '8bc59f460fa1548d1514676163dafc88'; pf = 'Package Control.sublime-package'; ipp = sublime.installed_packages_path(); os.makedirs( ipp ) if not os.path.exists(ipp) else None; urllib2.install_opener( urllib2.build_opener( urllib2.ProxyHandler()) ); by = urllib2.urlopen( 'http://packagecontrol.io/' + pf.replace(' ', '%20')).read(); dh = hashlib.sha256(by).hexdigest(); open( os.path.join( ipp, pf), 'wb' ).write(by) if dh == h else None; print('Error validating download (got %s instead of %s), please try manual install' % (dh, h) if dh != h else 'Please restart Sublime Text to finish installation')</div></pre></td></tr></table></figure>
<p> <em>Sublime Text 3</em></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">import urllib.request,os,hashlib; h = '2915d1851351e5ee549c20394736b442' + '8bc59f460fa1548d1514676163dafc88'; pf = 'Package Control.sublime-package'; ipp = sublime.installed_packages_path(); urllib.request.install_opener( urllib.request.build_opener( urllib.request.ProxyHandler()) ); by = urllib.request.urlopen( 'http://packagecontrol.io/' + pf.replace(' ', '%20')).read(); dh = hashlib.sha256(by).hexdigest(); print('Error validating download (got %s instead of %s), please try manual install' % (dh, h)) if dh != h else open(os.path.join( ipp, pf), 'wb' ).write(by)</div></pre></td></tr></table></figure>
</li>
<li><p>在Sublime Text里用快捷键<code>Command+Shift+P</code>调出命令窗口,输入<code>Install Package</code>,之后选择<code>LaTexTools</code>安装</p>
</li>
<li><p>运行一下Skim,进入<code>选项——同步</code>,在<code>PDF-Tex</code>同步支持那里选择<code>自定义</code>,输入<code>/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl</code>,第二行不变。这样,当你在Sublime Text里修改tex文件时,Skim预览也会相应变更。</p>
</li>
<li><p>中文环境</p>
<p> 打开终端,输入</p>
<pre><code>sudo tlmgr update --self
sudo tlmgr install latexm
</code></pre><p> 然后在Sublime Text的<code>Preferences -- Package Settings —— LaTeXTools —— settings-User</code>重建配置文件,在<code>builder-settings下</code>面新增两项配置:</p>
<pre><code>"program" : "xelatex",
"command" : ["latexmk", "-cd", "-e", "$pdflatex = 'xelatex -interaction=nonstopmode -synctex=1 %S %O'", "-f", "-pdf"],
</code></pre></li>
</ol>
<p>另外注意之前应该有<code>"builder": "default"</code>(或直接设置为空或”traditional”)。</p>
<p>最后新建一个test.tex文件,输入以下测试代码:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></td><td class="code"><pre><div class="line">\documentclass{article}</div><div class="line">\usepackage{fontspec, xunicode, xltxtra} </div><div class="line">\setmainfont{Hiragino Sans GB} </div><div class="line"></div><div class="line">\title{Title}</div><div class="line">\author{}</div><div class="line"></div><div class="line">\begin{document}</div><div class="line"></div><div class="line">\maketitle{}</div><div class="line"></div><div class="line">\section{Introduction}</div><div class="line"></div><div class="line">This is where you will write your content. 在这里写上内容。</div><div class="line"></div><div class="line">\end{document}</div></pre></td></tr></table></figure>
<p>然后<code>command+B</code>编译,若没出错,就会出现pdf文档</p>
]]></content>
<summary type="html">
<blockquote>
<p>工欲善其事必先利其器</p>
</blockquote>
<p>这次参加数模美赛,发现写论文时用word时,不是那么得心应手,最后半个小时操作失误出现了一些小问题,不过总算弄好了。所以为了下次不在那么窘迫,同时也是本着瞎折腾的精神,弄起了Latex。</p>
<p>Sublime text 是一款很优秀的编辑器,它不仅体积较小而且运行速度快,把它作为Latex的编辑器最好不过了<br>
</summary>
<category term="鼓捣" scheme="http://blog.makerwei.me/categories/%E9%BC%93%E6%8D%A3/"/>
<category term="Latex" scheme="http://blog.makerwei.me/tags/Latex/"/>
<category term="mac" scheme="http://blog.makerwei.me/tags/mac/"/>
</entry>
<entry>
<title>Hexo+github pages/Coding pages搭建自己的blog</title>
<link href="http://blog.makerwei.me/posts/22949/"/>
<id>http://blog.makerwei.me/posts/22949/</id>
<published>2016-02-03T13:13:25.000Z</published>
<updated>2017-03-23T08:11:44.000Z</updated>
<content type="html"><![CDATA[<h2 id="什么是Hexo?"><a href="#什么是Hexo?" class="headerlink" title="什么是Hexo?"></a>什么是Hexo?</h2><p><code>Hexo</code> 是一个快速、简洁且高效的博客框架。通过它,我们可以快速的搭建自己的bolg并托管到github上。踩了几个坑,我成功的将blog同时部署到github和gitcafe上。</p>
<h2 id="安装Hexo"><a href="#安装Hexo" class="headerlink" title="安装Hexo"></a>安装Hexo</h2><p>检查电脑中是否有以下程序:</p>
<ul>
<li>Node.js</li>
<li>Git</li>
</ul>
<a id="more"></a>
<p>安装完以上程序,然后通过<code>npm install -g hexo</code>即安装好Hexo,然后执行以下命令,Hexo会在指定文件夹中新建所需要的文件</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">hexo init <folder></div><div class="line">cd <folder></div><div class="line">npm install</div></pre></td></tr></table></figure>
<h2 id="本地预览"><a href="#本地预览" class="headerlink" title="本地预览"></a>本地预览</h2><p>切换到目标文件夹,执行命令<code>hexo generate</code>,则会生成静态文件。输入<code>hexo server</code> 开启本地服务。在浏览器中输入以下网址<code>http://localhost:4000/</code> 则可以在本地预览。</p>
<h2 id="配置github和Coding"><a href="#配置github和Coding" class="headerlink" title="配置github和Coding"></a>配置github和Coding</h2><p>两者用法基本一样,都是新建一个仓库,github上仓库名必须为<code>你的用户名.github.io</code>,Coding上仓库名为你的用户名就行了,但git的分支为<code>coding-pages</code>,然后编辑目标文件里的<code>_config.yml</code>文件如下</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">deploy:</div><div class="line"> - type: git</div><div class="line"> repository: https://github.com/Username/Username.github.io.git</div><div class="line"> branch: master</div><div class="line"> - type: git</div><div class="line"> repository: https://git.coding.net/Username/Username.git</div><div class="line"> branch: coding-pages</div></pre></td></tr></table></figure>
<p>Hexo可以使用多个deployerHe,Hexo会依照顺序执行每个deployer。记住每个冒号后面都有个空格,要不然部署时会报错。执行命令<code>hexo deploy</code>,你就可以在浏览器中输入<code>http://Username.coding.me/Username</code>或者<code>http://Username.github.io</code>就行了。</p>
<p>如果报错为<br><code>ERROR Deployer not found: git</code>,我的情况是执行命令<code>npm install hexo-deployer-git --save</code> 就解决了。</p>
<p>于2016年11月更新</p>
<p>更多资料请参考<a href="https://hexo.io/zh-cn/" target="_blank" rel="external">Hexo官方文档</a></p>
]]></content>
<summary type="html">
<h2 id="什么是Hexo?"><a href="#什么是Hexo?" class="headerlink" title="什么是Hexo?"></a>什么是Hexo?</h2><p><code>Hexo</code> 是一个快速、简洁且高效的博客框架。通过它,我们可以快速的搭建自己的bolg并托管到github上。踩了几个坑,我成功的将blog同时部署到github和gitcafe上。</p>
<h2 id="安装Hexo"><a href="#安装Hexo" class="headerlink" title="安装Hexo"></a>安装Hexo</h2><p>检查电脑中是否有以下程序:</p>
<ul>
<li>Node.js</li>
<li>Git</li>
</ul>
</summary>
<category term="鼓捣" scheme="http://blog.makerwei.me/categories/%E9%BC%93%E6%8D%A3/"/>
<category term="hexo" scheme="http://blog.makerwei.me/tags/hexo/"/>
<category term="github pages" scheme="http://blog.makerwei.me/tags/github-pages/"/>
</entry>
<entry>
<title>Pelican + Github Pages 搭建自己的博客</title>
<link href="http://blog.makerwei.me/posts/47021/"/>
<id>http://blog.makerwei.me/posts/47021/</id>
<published>2016-01-02T08:13:08.000Z</published>
<updated>2017-03-23T08:12:39.000Z</updated>
<content type="html"><![CDATA[<h2 id="pelican"><a href="#pelican" class="headerlink" title="pelican"></a>pelican</h2><p>Pelican是用python写的一套开源的静态网页生成项目,与当前很火的Jekyll很相似。不过作为一个python爱好者,Pelican更加让我喜欢。它评论使用Disqus,支持Markdown格式,支持代码高亮等功能。之前有使用Wordpress搭过blog。<br><a id="more"></a></p>
<h2 id="Installation"><a href="#Installation" class="headerlink" title="Installation"></a>Installation</h2><p><code>virtualenv</code> 可用来用来建立一个虚拟的python环境,一个专属于项目的python环境。</p>
<h3 id="激活环境"><a href="#激活环境" class="headerlink" title="激活环境"></a>激活环境</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">pip install virtualenv</div><div class="line">virtualenv myblog</div><div class="line">cd myblog</div><div class="line">source bin/activate</div></pre></td></tr></table></figure>
<h3 id="安装Pelican"><a href="#安装Pelican" class="headerlink" title="安装Pelican"></a>安装Pelican</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">git clone https://github.com/getpelican/pelican.git</div><div class="line">cd pelican</div><div class="line">python setup.py install</div></pre></td></tr></table></figure>
<h3 id="创建blog"><a href="#创建blog" class="headerlink" title="创建blog"></a>创建blog</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">cd path(your blog's path)</div><div class="line">pelican-quickstart</div></pre></td></tr></table></figure>
<p>在回答一系列的问题后就会自动生成你的blog,主要文件如下:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">path/</div><div class="line">|-- content</div><div class="line">|-- output</div><div class="line">|-- Makefile</div><div class="line">|-- develop_server.sh</div><div class="line">|-- pelicanconf.py</div><div class="line">|-- publishconf.py</div></pre></td></tr></table></figure>
<h3 id="写一篇文章"><a href="#写一篇文章" class="headerlink" title="写一篇文章"></a>写一篇文章</h3><p>为了支持<code>Markdown</code>,你首先还要通过<code>pip install Markdown</code> 安装相关的包,然后在content目录下新建一个test.md文件,示例内容如下:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">Title: My super title</div><div class="line">Date: 2010-12-03 10:20</div><div class="line">Modified: 2010-12-05 19:30</div><div class="line">Category: Python</div><div class="line">Tags: pelican, publishing</div><div class="line">Slug: my-super-post</div><div class="line">Authors: Alexis Metaireau, Conan Doyle</div><div class="line">Summary: Short version for index and feeds</div><div class="line"></div><div class="line">This is the content of my super blog post.</div></pre></td></tr></table></figure>
<p>然后执行</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">make html</div><div class="line">./develop_server.sh start</div></pre></td></tr></table></figure>
<p>这将开启一个测试服务器,可以通过<code>http://localhost:8000</code>来查看blog的效果</p>
<h3 id="使用Disqus第三方评论"><a href="#使用Disqus第三方评论" class="headerlink" title="使用Disqus第三方评论"></a>使用Disqus第三方评论</h3><p>在<a href="https://disqus.com/" target="_blank" rel="external">Disqus</a>上申请一个站点,然后在<code>pelicanconf.py</code>里添加或修改<code>DISQUS_SITENAME</code>:</p>
<pre><code>DISQUS_SITENAME = u"你的站点ID"
</code></pre><p>然后执行</p>
<pre><code>make html
</code></pre><p>然后刷新<code>http://localhost:8000</code>可以查看效果</p>
<h3 id="主题"><a href="#主题" class="headerlink" title="主题"></a>主题</h3><p>Pelican也有一些主题可以下载,比如使用<a href="https://github.com/alexandrevicenzi/Flex" target="_blank" rel="external">Flex</a>这个主题。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">git clone git://github.com/getpelican/pelican-themes.git</div></pre></td></tr></table></figure>
<p>然后将你想要的主题拷贝进你博客项目的目录,然后在<code>pelicanconf.py</code>文件中修改THEME为Flex</p>
<pre><code>THEME = "Flex"
</code></pre><p>然后执行</p>
<pre><code>make html
</code></pre><p>再打开<code>http://localhost:8000</code>查看效果</p>
<h3 id="使用插件"><a href="#使用插件" class="headerlink" title="使用插件"></a>使用插件</h3><p>在博客的目录中执行:</p>
<pre><code>git clone git://github.com/getpelican/pelican-plugins.git
</code></pre><p>以配置<code>sitemap</code>为例,在<code>pelicanconf.py</code>的配置文件里添加以下配置:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">SITEMAP = {</div><div class="line"> 'format': 'xml',</div><div class="line"> 'priorities': {</div><div class="line"> 'articles': 1,</div><div class="line"> 'pages': 0.9,</div><div class="line"> 'indexes': 0.8,</div><div class="line"> },</div><div class="line"> 'changefreqs': {</div><div class="line"> 'indexes': 'daily',</div><div class="line"> 'articles': 'daily',</div><div class="line"> 'pages': 'weekly'</div><div class="line"> }</div><div class="line">}</div></pre></td></tr></table></figure>
<p>然后再执行<code>make html</code> </p>
<p>打开<code>http://localhost:8000/sitemap.xml</code>就可看到生成的Sitemap了。</p>
<h2 id="Github-Pages"><a href="#Github-Pages" class="headerlink" title="Github Pages"></a>Github Pages</h2><p>创建一个新的repository,并将其命名为<code>username.github.io</code></p>
<h3 id="配置"><a href="#配置" class="headerlink" title="配置"></a>配置</h3><p>进入output目录,将生成好的静态HTML文件上传到你的<code>username.github.io</code>中。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">cd output</div><div class="line">git init</div><div class="line">git remote add origin 远端地址</div><div class="line">git add .</div><div class="line">git commit -m "first commit"</div><div class="line">git push -u origin master</div></pre></td></tr></table></figure>
<p>等待一段时间你就可以访问<code>username.github.io</code>看到网站效果。</p>
<p>若要使用独立域名,则要output当前目录新建一个<code>CNAME</code>文件,里面写入你的域名(域名的指向等设置请自行查阅资料),然后上传到你的repository里就成功了。</p>
<h2 id="参考"><a href="#参考" class="headerlink" title="参考"></a>参考</h2><p>更多的配置请参考<a href="http://docs.getpelican.com/en/3.6.3/" target="_blank" rel="external">官方文档</a></p>
]]></content>
<summary type="html">
<h2 id="pelican"><a href="#pelican" class="headerlink" title="pelican"></a>pelican</h2><p>Pelican是用python写的一套开源的静态网页生成项目,与当前很火的Jekyll很相似。不过作为一个python爱好者,Pelican更加让我喜欢。它评论使用Disqus,支持Markdown格式,支持代码高亮等功能。之前有使用Wordpress搭过blog。<br>
</summary>
<category term="鼓捣" scheme="http://blog.makerwei.me/categories/%E9%BC%93%E6%8D%A3/"/>
<category term="Python" scheme="http://blog.makerwei.me/tags/Python/"/>
<category term="pelican" scheme="http://blog.makerwei.me/tags/pelican/"/>
<category term="blog" scheme="http://blog.makerwei.me/tags/blog/"/>
</entry>
</feed>