-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.xml
1062 lines (1062 loc) · 262 KB
/
search.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
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="utf-8"?>
<search>
<entry>
<title>立下18年最后的目标</title>
<url>/life/hello-target-20181119/</url>
<content><![CDATA[<h1 id="立下18年最后的目标"><a href="#立下18年最后的目标" class="headerlink" title="立下18年最后的目标"></a>立下18年最后的目标</h1><h2 id="开言"><a href="#开言" class="headerlink" title="开言"></a>开言</h2><ul>
<li>18年还有一个半月就结束,今年一直在为公司向前冲,忘记了沉下心来多想想和总结,一直忙到快年底,除了努力工作还需要勤奋学习,不管多忙都要在18年完成对JDK源码的阅读,顺便在农历过年前完成对Spring源码的阅读。</li>
<li>一切都从基础做起,现在还不晚,自已必须要强大。</li>
</ul>
]]></content>
<categories>
<category>life</category>
</categories>
<tags>
<tag>target</tag>
</tags>
</entry>
<entry>
<title>Hello World</title>
<url>/life/hello-world/</url>
<content><![CDATA[<h1 id="欢迎访问我的站点"><a href="#欢迎访问我的站点" class="headerlink" title="欢迎访问我的站点"></a>欢迎访问我的站点</h1><h2 id="测试JAVA代码"><a href="#测试JAVA代码" class="headerlink" title="测试JAVA代码"></a>测试JAVA代码</h2><figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.regex.Matcher;</span><br><span class="line"><span class="keyword">import</span> java.util.regex.Pattern;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.apache.commons.lang3.StringUtils;</span><br><span class="line"><span class="keyword">import</span> org.apache.log4j.Logger;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Created by IDEA</span></span><br><span class="line"><span class="comment"> * Desc: URL相关工具类</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">final</span> <span class="class"><span class="keyword">class</span> <span class="title">UrlUtil</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">private</span> <span class="title">UrlUtil</span><span class="params">()</span> </span>{</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">int</span> <span class="title">getId</span><span class="params">(String url)</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h2 id="测试JS"><a href="#测试JS" class="headerlink" title="测试JS"></a>测试JS</h2><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getValue</span>(<span class="params"></span>)</span>{</span><br><span class="line"> <span class="keyword">return</span> <span class="string">"123"</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h2 id="测试sql"><a href="#测试sql" class="headerlink" title="测试sql"></a>测试sql</h2><figure class="highlight sql"><table><tr><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> * <span class="keyword">FROM</span> <span class="keyword">table</span> <span class="keyword">AS</span> t <span class="keyword">WHERE</span> t.id=<span class="number">1</span></span><br></pre></td></tr></table></figure>
]]></content>
<categories>
<category>java</category>
</categories>
<tags>
<tag>testTags</tag>
</tags>
</entry>
<entry>
<title>Gulp搭建web服务器并自动刷新</title>
<url>/front/nodejs/win-10-node-js-gulp-web-server-livereload/</url>
<content><![CDATA[<h2 id="需求"><a href="#需求" class="headerlink" title="需求"></a>需求</h2><ul>
<li>现在应用angularjs或VUE等框架可以实现前后端完全分离式开发,开发过程中也可以用到轻量级的文本编辑器来实现,需要一个简单的本地开发调试环境,想到了IIS、Gulp等,在非Windows下Gulp是很好的应用,还可以打包和压缩js、css等,每改一次去手动刷新的情况也可以避免,只要改动完保存下就可以实现自动刷新</li>
</ul>
<h2 id="环境搭建"><a href="#环境搭建" class="headerlink" title="环境搭建"></a>环境搭建</h2><ul>
<li><p>首先要安装nodeJs,这个就不介绍了,接下来我们安装gulp 和 gulp-connect</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line">npm install --save-dev gulp gulp-connect</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line">npm install --save-dev gulp</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line">npm install --global livereload</span><br></pre></td></tr></table></figure>
<p>这三个命令执行完后,安装就完成了</p>
</li>
</ul>
<h2 id="WebServer实现"><a href="#WebServer实现" class="headerlink" title="WebServer实现"></a>WebServer实现</h2><ul>
<li>安装完成后我们需要在需要在开发目录中加入配置文件 gulpfile.js</li>
<li><p>代码如下:</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line">var gulp = require(<span class="string">'gulp'</span>);</span><br><span class="line">var connect = require(<span class="string">'gulp-connect'</span>);</span><br><span class="line"></span><br><span class="line">gulp.task(<span class="string">'watch'</span>, function() {</span><br><span class="line"> gulp.watch(<span class="string">'./*.html'</span>, [<span class="string">'html'</span>]);</span><br><span class="line"> gulp.watch(<span class="string">'./css/**/*.css'</span>, [<span class="string">'css'</span>]);</span><br><span class="line"> gulp.watch(<span class="string">'./img/**/*.{png,jpg,gif}'</span>, [<span class="string">'images'</span>]);</span><br><span class="line">});</span><br><span class="line"></span><br><span class="line">gulp.task(<span class="string">'webserver'</span>, function() {</span><br><span class="line"> connect.server({</span><br><span class="line"> livereload: <span class="keyword">true</span>,</span><br><span class="line"> port: <span class="number">8066</span></span><br><span class="line"> });</span><br><span class="line">});</span><br><span class="line"></span><br><span class="line">gulp.task(<span class="string">'html'</span>, function() {</span><br><span class="line"> gulp.src(<span class="string">'./*.html'</span>)</span><br><span class="line"> .pipe(connect.reload());</span><br><span class="line">});</span><br><span class="line"></span><br><span class="line">gulp.task(<span class="string">'css'</span>, function() {</span><br><span class="line"> gulp.src(<span class="string">'./css/**/*.css'</span>)</span><br><span class="line"> .pipe(connect.reload());</span><br><span class="line">});</span><br><span class="line"></span><br><span class="line">gulp.task(<span class="string">'images'</span>, function() {</span><br><span class="line"> gulp.src(<span class="string">'./img/**/*.{png,jpg,gif}'</span>)</span><br><span class="line"> .pipe(connect.reload());</span><br><span class="line">});</span><br><span class="line"></span><br><span class="line">gulp.task(<span class="string">'default'</span>, [<span class="string">'webserver'</span>, <span class="string">'watch'</span>]);</span><br></pre></td></tr></table></figure>
</li>
<li><p>到此配置完毕,我们可以运行 gulp 来体验一下,用Atom打开html编辑下看看,修改自动变化</p>
</li>
</ul>
]]></content>
<categories>
<category>front</category>
<category>nodejs</category>
</categories>
<tags>
<tag>nodejs</tag>
<tag>gulp</tag>
<tag>livereload</tag>
</tags>
</entry>
<entry>
<title>APP中H5调试</title>
<url>/front/test/app-h5-vconsole-js/</url>
<content><![CDATA[<h2 id="需求"><a href="#需求" class="headerlink" title="需求"></a>需求</h2><ul>
<li>在APP中开发H5,由于环境的问题我们可能只能在APP中调试,这时就有了问题,我们在调试时抓包不好实现,谷歌提供的插件需要翻墙且不稳定,要是可以在页面中输出所有的日志就好了</li>
</ul>
<h2 id="实现"><a href="#实现" class="headerlink" title="实现"></a>实现</h2><ul>
<li>总有大牛想到了这些并且实现了,这里我们用到的是腾讯团队提供的vConsole</li>
<li>只需要在页面中引用</li>
</ul>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line">src="//wechatfe.github.io/vconsole/lib/vconsole.min.js?v=3.0.0.0"><span class="tag"></<span class="name">script</span>></span></span><br></pre></td></tr></table></figure>
<ul>
<li>接下来在页面中任意位置初使化一下就可以</li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> vConsole = <span class="keyword">new</span> VConsole();</span><br></pre></td></tr></table></figure>
<ul>
<li>在APP中打开页面会发现了什么不同吗?</li>
<li>自已尝试呀</li>
</ul>
]]></content>
<categories>
<category>front</category>
<category>test</category>
</categories>
<tags>
<tag>app</tag>
<tag>h5</tag>
<tag>js</tag>
</tags>
</entry>
<entry>
<title>Java中遍历Map的几种方法</title>
<url>/java/basic/java-map-foreach-lambda/</url>
<content><![CDATA[<h1 id="Java中遍历Map的几种方法"><a href="#Java中遍历Map的几种方法" class="headerlink" title="Java中遍历Map的几种方法"></a>Java中遍历Map的几种方法</h1><h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><pre><code>java中的map遍历有多种方法,从最早的Iterator,到java5支持的foreach,再到java8 Lambda,让我们一起来看下具体的用法以及各自的优缺点
先初始化一个map
</code></pre><figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> Map<Integer,Integer> map = <span class="keyword">new</span> HashMap<Integer, Integer>();</span><br></pre></td></tr></table></figure>
<h2 id="keySet-values"><a href="#keySet-values" class="headerlink" title="keySet values"></a>keySet values</h2><pre><code>如果只需要map的key或者value,用map的keySet或values方法无疑是最方便的
</code></pre><figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">//获以Key</span></span><br><span class="line"><span class="function"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">testKeyset</span><span class="params">()</span></span>{</span><br><span class="line"> <span class="keyword">for</span>(Integer key :map.keySet()){</span><br><span class="line"> System.out.println(key);</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">//获以Value</span></span><br><span class="line"><span class="function"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">testValue</span><span class="params">()</span></span>{</span><br><span class="line"> <span class="keyword">for</span>(Integer value :map.values()){</span><br><span class="line"> System.out.println(value);</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h2 id="keySet-get-key"><a href="#keySet-get-key" class="headerlink" title="keySet get(key)"></a>keySet get(key)</h2><p> 如果需要同时获取key和value,可以先获取key,然后再通过map的get(key)获取value<br> 需要说明的是,该方法不是最优选择,一般不推荐使用<br><figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">//获以Key And key</span></span><br><span class="line"><span class="function"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">testKeysetAndKey</span><span class="params">()</span></span>{</span><br><span class="line"> <span class="keyword">for</span>(Integer key :map.keySet()){</span><br><span class="line"> System.out.println(key+<span class="string">":"</span>+map.get(key));</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure></p>
<h2 id="entrySet"><a href="#entrySet" class="headerlink" title="entrySet"></a>entrySet</h2><p> 通过对map entrySet的遍历,也可以同时拿到key和value,一般情况下,性能上要优于上一种,这一种也是最常用的遍历方法<br><figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">//获以Key And key</span></span><br><span class="line"><span class="function"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">testEntry</span><span class="params">()</span></span>{</span><br><span class="line"> <span class="keyword">for</span>(Map.Entry<Integer,Integer> entry:map.entrySet()){</span><br><span class="line"> System.out.println(entry.getKey()+<span class="string">":"</span>+entry.getValue());</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure></p>
<h2 id="Iterator"><a href="#Iterator" class="headerlink" title="Iterator"></a>Iterator</h2><p> 对于上面的几种foreach都可以用Iterator代替,其实foreach在java5中才被支持,foreach的写法看起来更简洁</p>
<p> 但Iterator也有其优势:在用foreach遍历map时,如果改变其大小,会报错,但如果只是删除元素,可以使用Iterator的remove方法删除元素<br> <figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">//获以Key And key</span></span><br><span class="line"><span class="function"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">testIterator</span><span class="params">()</span></span>{</span><br><span class="line"> Iterator<Map.Entry<Integer,Integer>> it = map.entrySet().iterator();</span><br><span class="line"> <span class="keyword">while</span> (it.hasNext()){</span><br><span class="line"> Map.Entry<Integer,Integer> entry = it.next();</span><br><span class="line"> System.out.println(entry.getKey()+<span class="string">":"</span>+entry.getValue());</span><br><span class="line"> it.remove();</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure></p>
<h2 id="Lambda"><a href="#Lambda" class="headerlink" title="Lambda"></a>Lambda</h2><pre><code>java8提供了Lambda表达式支持,语法看起来更简洁,可以同时拿到key和value,不过,经测试,性能低于entrySet,所以更推荐用entrySet的方式
</code></pre> <figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">//获以Key And key</span></span><br><span class="line"><span class="function"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">testLambda</span><span class="params">()</span> </span>{</span><br><span class="line"> map.forEach((key, value) -> {</span><br><span class="line"> System.out.println(key + <span class="string">":"</span> + value);</span><br><span class="line"> });</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
]]></content>
<categories>
<category>java</category>
<category>basic</category>
</categories>
<tags>
<tag>java</tag>
<tag>map</tag>
<tag>lambda</tag>
</tags>
</entry>
<entry>
<title>Cron表达式未来N次运行时间</title>
<url>/java/basic/java-cron-calc-execute-times/</url>
<content><![CDATA[<h1 id="Cron表达式未来N次运行时间"><a href="#Cron表达式未来N次运行时间" class="headerlink" title="Cron表达式未来N次运行时间"></a>Cron表达式未来N次运行时间</h1><h2 id="实现"><a href="#实现" class="headerlink" title="实现"></a>实现</h2> <figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 计算表达式近N次时间</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> cron 表达式</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> times 次数</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span> 列表</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> List<String> <span class="title">calcExecuteTimes</span><span class="params">(String cron, <span class="keyword">int</span> times)</span> </span>{</span><br><span class="line"> <span class="keyword">if</span> (StringUtils.isEmpty(cron)) {</span><br><span class="line"> <span class="keyword">throw</span> <span class="keyword">new</span> IllegalArgumentException(<span class="string">"参数不能为空"</span>);</span><br><span class="line"> }</span><br><span class="line"> CronSequenceGenerator cronSequenceGenerator = <span class="keyword">new</span> CronSequenceGenerator(cron);</span><br><span class="line"> List<String> list = <span class="keyword">new</span> ArrayList<>(times);</span><br><span class="line"> Date nextTimePoint = <span class="keyword">new</span> Date();</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < times; i++) {</span><br><span class="line"> nextTimePoint = cronSequenceGenerator.next(nextTimePoint);</span><br><span class="line"> list.add(DateFormatUtils.format(nextTimePoint, <span class="string">""</span>));</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> list;</span><br><span class="line">}</span><br><span class="line"></span><br><span 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 class="keyword">throws</span> Exception </span>{</span><br><span class="line"> List<String> list = calcExecuteTimes(<span class="string">"5 0/1 * * * ?"</span>, <span class="number">10</span>);</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < list.size(); i++) {</span><br><span class="line"> System.out.println(list.get(i));</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<ul>
<li>Github Gist:<a href="https://gist.github.com/hantianwei/893484100ce681e52790b1334fee3ab2" target="_blank" rel="noopener">https://gist.github.com/hantianwei/893484100ce681e52790b1334fee3ab2</a></li>
</ul>
]]></content>
<categories>
<category>java</category>
<category>basic</category>
</categories>
<tags>
<tag>cron</tag>
<tag>quartz</tag>
</tags>
</entry>
<entry>
<title>Idea Maven 建本地仓库-导入本地JAR包</title>
<url>/java/tool/idea-maven-local-package-jar/</url>
<content><![CDATA[<h1 id="Idea-Maven-建本地仓库-导入本地JAR包"><a href="#Idea-Maven-建本地仓库-导入本地JAR包" class="headerlink" title="Idea Maven 建本地仓库-导入本地JAR包"></a>Idea Maven 建本地仓库-导入本地JAR包</h1><h2 id="需求"><a href="#需求" class="headerlink" title="需求"></a>需求</h2><pre><code>IDEA 很方便集成了Maven,但是也有相应的问题,比如使用Maven仓没有包的时候不方便,这时我们需要建立自已的本地仓库来实现
</code></pre><h2 id="实现"><a href="#实现" class="headerlink" title="实现"></a>实现</h2><pre><code>找到Idea的安装目录下面的Maven,我的在C:\Program Files (x86)\JetBrains\IntelliJ IDEA 2016.3\plugins\maven\lib\maven3
打开conf 目录中的 settings.xml
找到
</code></pre><figure class="highlight xml"><table><tr><td class="code"><pre><span class="line"><span class="comment"><!-- localRepository</span></span><br><span class="line"><span class="comment"> | The path to the local repository maven will use to store artifacts.</span></span><br><span class="line"><span class="comment"> |</span></span><br><span class="line"><span class="comment"> | Default: ${user.home}/.m2/repository</span></span><br><span class="line"><span class="comment"><localRepository>/path/to/local/repo</localRepository></span></span><br><span class="line"><span class="comment">--></span></span><br></pre></td></tr></table></figure>
<p>修改为<br><figure class="highlight xml"><table><tr><td class="code"><pre><span class="line"><span class="tag"><<span class="name">localRepository</span>></span>D:/Work/maven-dependcies<span class="tag"></<span class="name">localRepository</span>></span></span><br></pre></td></tr></table></figure></p>
<p>本地仓就建完了,这时需要加入我们自已的JAR<br><figure class="highlight xml"><table><tr><td class="code"><pre><span class="line"><span class="tag"><<span class="name">dependency</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">groupId</span>></span>taobao-sdk<span class="tag"></<span class="name">groupId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">artifactId</span>></span>taobao-sdk-java-auto<span class="tag"></<span class="name">artifactId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">version</span>></span>1.0<span class="tag"></<span class="name">version</span>></span></span><br><span class="line"><span class="tag"></<span class="name">dependency</span>></span></span><br></pre></td></tr></table></figure></p>
<p>需要在本地仓目录(D:/Work/maven-dependcies)下新建 taobao-sdk/taobao-sdk-java-auto/1.0/taobao-sdk-java-auto-1.0.jar</p>
<p>到些建完了,项目可以正常运行</p>
]]></content>
<categories>
<category>java</category>
<category>tool</category>
</categories>
<tags>
<tag>springMvc</tag>
<tag>maven</tag>
</tags>
</entry>
<entry>
<title>Tomcat 的优化</title>
<url>/java/tomcat/tomcat-optimization/</url>
<content><![CDATA[<h1 id="Tomcat-的优化"><a href="#Tomcat-的优化" class="headerlink" title="Tomcat 的优化"></a>Tomcat 的优化</h1><p>参数分析:</p>
<p>(1)、maxHttpHeaderSize=”8192”</p>
<p>此选项用于配置:来自于客户端请求的Request和Response的HTTP header</p>
<p>的最大长度,以字节计算。如果不设置,该属性为4096(4K)。</p>
<p>(2)、maxThreads=”512”</p>
<p>Connector创建的请求处理线程的最大值,它决定了能够同时处理并发请求的最</p>
<p>大值。如果不指定,该属性值为200。Tomcat的线程数量太多会导致切换过多,性能</p>
<p>下降严重。这个数量应该是你单个机器的承载能力,压力测试下得出的结果。不可任意</p>
<p>加大。一般情况下,256-512个已经非常高的数值了。</p>
<p>(3)、minSpareThreads=”100”</p>
<p>tomcat初始化时创建的线程数,即最小工作线程数目。</p>
<p>(4)、maxSpareThreads=”300”</p>
<p>tomcat所允许的最大线程数,一旦创建的线程超过这个数,Tomcat就将关闭不</p>
<p>再需要的Socket线程。</p>
<p>(5)、enableLookups=”false”</p>
<p>使用允许DNS查询,通常情况下设置为false。如果为true则tomcat会损耗一</p>
<p>部分系统性能。</p>
<p>(6)、redirectPort=”8443”</p>
<p>这里系统默认的,它指定转发端口,如果当前只支持non-SSL请求,在需要安全</p>
<p>通信的场所,将把客户请求转发至SSL的redirectPort端口。</p>
<p>(7)、acceptCount=“200”</p>
<p>当所有请求处理线程都被占用时,允许接收请求队列的最大长度。当队列已满时,</p>
<p>任何收到的请求都将被拒绝。缺省值为10。</p>
<p>(8)、connectionTimeout=”50000”</p>
<p>Connector接收连接后,等待提供请求URI的时间,缺省值为60000(60秒)。</p>
<p>(9)、disableUploadTimeout=”true”</p>
<p>该标志位表明当执行servlet时,是否允许servlet容器使用一个不同的、更长的</p>
<p>连接超时。启用该标志位将导致在上传数据时,要么使用更长的时间完成上传,要么</p>
<p>出现更长的超时。如果不指定,该属性为“false”。</p>
<p>(10)、有时不光要优化tomcat,同时还需要更改Linux的一些系统参数。如果WEB</p>
<p>服务器进行压力测试时报socket不足,则些可以对/etc/security/limits.conf进行相</p>
<p>应修改:</p>
<ul>
<li><p>soft nofile 57766</p>
</li>
<li><p>hard nofile 65535</p>
</li>
</ul>
<p>二、优化tomcat启动的初始内存设置($TOMCAT_HOME/bin/catalina.sh)</p>
<p>JAVA_OPTS=”-server -Xms2048M -Xmx2048M -Xss128k -XX:+AggressiveOpts -XX:+UseParallelGC -XX:+UseBiasedLocking”</p>
<p>参数详解:</p>
<p>(1)、-Xms2048M</p>
<p>Java虚拟机初始化时使用的内存大小,为了减少tomcat频繁的申请内存,此参数</p>
<p>应该和Xmx设置的参数一样大。</p>
<p>(2)、-Xmx2048M</p>
<p>JAVA虚拟机可以使用的最大内存,此参数的最大设置不允许超过物理内存的90%。</p>
<p>(3)、-XX:+AggressiveOpts</p>
<p>加快编译</p>
<p>(4)、-Xss128k</p>
<p>设置tomcat每增加一个线程(Thread)就会立即消耗128K内存,最佳值应该是128,</p>
<p>默认值是512K。见意256K大小,这个对性能影响较大,需要多次测试。减少这个值可</p>
<p>以增加一个进程内的线程数量。</p>
<p>(5)、-XX:+UseParallelGC</p>
<p>一些测试表明当非64位系统,并且非高性能服务器时,这个选项会造成垃圾回收</p>
<p>变慢。(此选项决定对垃圾回收是否采用多线程)</p>
<p>(6)、-XX:+UseBiasedLocking”</p>
<p>非竞争性的同步选项,锁机制的性能改善。</p>
<p>(7)、-XX:PermSize=256m</p>
<p>最小堆大小,一般把内存不足时就是把这个空间不足。默认为物理内存的1/4。</p>
<p>(8)、-XX:+DisableExplicitGC</p>
<p>禁止System.gc(),免得程序员误调用gc方法影响性能。</p>
<p>下面给出见意设置:<br><figure class="highlight xml"><table><tr><td class="code"><pre><span class="line">JAVA_OPTS="-server -Xms512M -Xmx512M -Xss256k -XX:+AggressiveOpts -XX:+UseBiasedLocking"</span><br><span class="line"></span><br><span class="line"><span class="tag"><<span class="name">Connector</span> <span class="attr">port</span>=<span class="string">"8080"</span> <span class="attr">protocol</span>=<span class="string">"org.apache.coyote.http11.Http11NioProtocol"</span></span></span><br><span class="line"><span class="tag"></span></span><br><span class="line"><span class="tag"><span class="attr">maxHttpHeaderSize</span>=<span class="string">"8192"</span></span></span><br><span class="line"><span class="tag"></span></span><br><span class="line"><span class="tag"><span class="attr">minProcessors</span>=<span class="string">"100"</span></span></span><br><span class="line"><span class="tag"></span></span><br><span class="line"><span class="tag"><span class="attr">maxProcessors</span>=<span class="string">"5000"</span></span></span><br><span class="line"><span class="tag"></span></span><br><span class="line"><span class="tag"><span class="attr">maxThreads</span>=<span class="string">"5000"</span></span></span><br><span class="line"><span class="tag"></span></span><br><span class="line"><span class="tag"><span class="attr">minSpareThreads</span>=<span class="string">"1000"</span></span></span><br><span class="line"><span class="tag"></span></span><br><span class="line"><span class="tag"><span class="attr">maxSpareThreads</span>=<span class="string">"4000"</span></span></span><br><span class="line"><span class="tag"></span></span><br><span class="line"><span class="tag"><span class="attr">enableLookups</span>=<span class="string">"false"</span></span></span><br><span class="line"><span class="tag"></span></span><br><span class="line"><span class="tag"><span class="attr">acceptCount</span>=<span class="string">"3500"</span></span></span><br><span class="line"><span class="tag"></span></span><br><span class="line"><span class="tag"><span class="attr">disableUploadTimeout</span>=<span class="string">"true"</span></span></span><br><span class="line"><span class="tag"></span></span><br><span class="line"><span class="tag"><span class="attr">connectionTimeout</span>=<span class="string">"20000"</span></span></span><br><span class="line"><span class="tag"></span></span><br><span class="line"><span class="tag"><span class="attr">debug</span>=<span class="string">"0"</span></span></span><br><span class="line"><span class="tag"></span></span><br><span class="line"><span class="tag"><span class="attr">redirectPort</span>=<span class="string">"8443"</span> /></span></span><br></pre></td></tr></table></figure></p>
<p>maxThreads:Tomcat可创建的最大的线程数,每一个线程处理一个请求;</p>
<p>minSpareThreads:最小备用线程数,tomcat启动时的初始化的线程数;</p>
<p>maxSpareThreads:最大备用线程数,一旦创建的线程超过这个值,Tomcat就会关闭(应该有个超时时间吧,超时后关闭)不再需要的socket线程;</p>
<p>所以maxThreads决定了tomcat的最大线程阀值,需要设置的大一些</p>
<p><img src="/images/post/2017/06/201706240001.jpg"></p>
]]></content>
<categories>
<category>java</category>
<category>tomcat</category>
</categories>
<tags>
<tag>tomcat</tag>
</tags>
</entry>
<entry>
<title>Spring Mvc Url和参数忽略大小写</title>
<url>/java/springmvc/spring-mvc-url-ingore-case/</url>
<content><![CDATA[<h1 id="Spring-Mvc-Url和参数名称忽略大小写"><a href="#Spring-Mvc-Url和参数名称忽略大小写" class="headerlink" title="Spring Mvc Url和参数名称忽略大小写"></a>Spring Mvc Url和参数名称忽略大小写</h1><h3 id="在开发过程中Spring-Mvc-默认-Url和参数名称都是区分大小写的"><a href="#在开发过程中Spring-Mvc-默认-Url和参数名称都是区分大小写的" class="headerlink" title="在开发过程中Spring Mvc 默认 Url和参数名称都是区分大小写的"></a>在开发过程中Spring Mvc 默认 Url和参数名称都是区分大小写的</h3><pre><code>例如:
www.a.com/user/getUserInfo?userId=1
www.a.com/user/getuserInfo?userId=1
www.a.com/user/getUserInfo?userid=1
www.a.com/user/getuserinfo?userid=1
</code></pre><p><em>这些都认为不同的地址和参数,在实际中用户根本不区分这些,所以我们要忽略大小写</em></p>
<h3 id="URL忽略大小写"><a href="#URL忽略大小写" class="headerlink" title="URL忽略大小写"></a>URL忽略大小写</h3><figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> org.springframework.context.annotation.Configuration;</span><br><span class="line"><span class="keyword">import</span> org.springframework.util.AntPathMatcher;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.servlet.config.annotation.PathMatchConfigurer;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Created by tianwei on 2017/6/22.</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Configuration</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">SpringWebConfig</span> <span class="keyword">extends</span> <span class="title">WebMvcConfigurationSupport</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="meta">@Override</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">configurePathMatch</span><span class="params">(PathMatchConfigurer configurer)</span> </span>{</span><br><span class="line"> AntPathMatcher pathMatcher = <span class="keyword">new</span> AntPathMatcher();</span><br><span class="line"> pathMatcher.setCaseSensitive(<span class="keyword">false</span>);</span><br><span class="line"> configurer.setPathMatcher(pathMatcher);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h3 id="参数名称忽略大小写"><a href="#参数名称忽略大小写" class="headerlink" title="参数名称忽略大小写"></a>参数名称忽略大小写</h3><figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> java.io.IOException;</span><br><span class="line"><span class="keyword">import</span> java.util.Collections;</span><br><span class="line"><span class="keyword">import</span> java.util.Enumeration;</span><br><span class="line"><span class="keyword">import</span> java.util.Map;</span><br><span class="line"><span class="keyword">import</span> javax.servlet.FilterChain;</span><br><span class="line"><span class="keyword">import</span> javax.servlet.ServletException;</span><br><span class="line"><span class="keyword">import</span> javax.servlet.http.HttpServletRequest;</span><br><span class="line"><span class="keyword">import</span> javax.servlet.http.HttpServletRequestWrapper;</span><br><span class="line"><span class="keyword">import</span> javax.servlet.http.HttpServletResponse;</span><br><span class="line"><span class="keyword">import</span> org.springframework.util.LinkedCaseInsensitiveMap;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.filter.OncePerRequestFilter;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">CaseInsensitiveRequestParameterNameFilter</span> <span class="keyword">extends</span> <span class="title">OncePerRequestFilter</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="meta">@Override</span></span><br><span class="line"> <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">doFilterInternal</span><span class="params">(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)</span></span></span><br><span class="line"><span class="function"> <span class="keyword">throws</span> ServletException, IOException </span>{</span><br><span class="line"> filterChain.doFilter(<span class="keyword">new</span> CaseInsensitiveParameterNameHttpServletRequest(request), response);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="class"><span class="keyword">class</span> <span class="title">CaseInsensitiveParameterNameHttpServletRequest</span> <span class="keyword">extends</span> <span class="title">HttpServletRequestWrapper</span> </span>{</span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">final</span> LinkedCaseInsensitiveMap<String[]> map = <span class="keyword">new</span> LinkedCaseInsensitiveMap<>();</span><br><span class="line"></span><br><span class="line"> <span class="meta">@SuppressWarnings</span>(<span class="string">"unchecked"</span>)</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="title">CaseInsensitiveParameterNameHttpServletRequest</span><span class="params">(HttpServletRequest request)</span> </span>{</span><br><span class="line"> <span class="keyword">super</span>(request);</span><br><span class="line"> map.putAll(request.getParameterMap());</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="meta">@Override</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> String <span class="title">getParameter</span><span class="params">(String name)</span> </span>{</span><br><span class="line"></span><br><span class="line"> String[] array = <span class="keyword">this</span>.map.get(name);</span><br><span class="line"> <span class="keyword">if</span> (array != <span class="keyword">null</span> && array.length > <span class="number">0</span>)</span><br><span class="line"> <span class="keyword">return</span> array[<span class="number">0</span>];</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="meta">@Override</span></span><br><span class="line"> <span class="keyword">public</span> Map<String, String[]> getParameterMap() {</span><br><span class="line"> <span class="keyword">return</span> Collections.unmodifiableMap(<span class="keyword">this</span>.map);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="meta">@Override</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> Enumeration<String> <span class="title">getParameterNames</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> Collections.enumeration(<span class="keyword">this</span>.map.keySet());</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="meta">@Override</span></span><br><span class="line"> <span class="keyword">public</span> String[] getParameterValues(String name) {</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">this</span>.map.get(name);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h3 id="定义Bean"><a href="#定义Bean" class="headerlink" title="定义Bean"></a>定义Bean</h3><figure class="highlight xml"><table><tr><td class="code"><pre><span class="line"><span class="comment"><!--输入参数忽略大小写--></span></span><br><span class="line"> <span class="tag"><<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"caseInsensitiveRequestFilterProxy"</span> <span class="attr">class</span>=<span class="string">"org.springframework.web.filter.DelegatingFilterProxy"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"targetBeanName"</span> <span class="attr">value</span>=<span class="string">"caseInsensitiveRequestFilter"</span>/></span></span><br><span class="line"> <span class="tag"></<span class="name">bean</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"caseInsensitiveRequestFilter"</span></span></span><br><span class="line"><span class="tag"> <span class="attr">class</span>=<span class="string">"com.hantianwei.util.CaseInsensitiveRequestParameterNameFilter"</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">bean</span>></span></span><br></pre></td></tr></table></figure>
<h3 id="web-xml-增加Filter"><a href="#web-xml-增加Filter" class="headerlink" title="web.xml 增加Filter"></a>web.xml 增加Filter</h3><figure class="highlight xml"><table><tr><td class="code"><pre><span class="line"><span class="tag"><<span class="name">filter</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">filter-name</span>></span>caseInsensitiveRequestFilterProxy<span class="tag"></<span class="name">filter-name</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">filter-class</span>></span>org.springframework.web.filter.DelegatingFilterProxy<span class="tag"></<span class="name">filter-class</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">filter</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">filter-mapping</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">filter-name</span>></span>caseInsensitiveRequestFilterProxy<span class="tag"></<span class="name">filter-name</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">url-pattern</span>></span>/*<span class="tag"></<span class="name">url-pattern</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">filter-mapping</span>></span></span><br></pre></td></tr></table></figure>
<font color="red">到此再次运行项目就可以了,最上面的URL访问的是同一页面了</font>
]]></content>
<categories>
<category>java</category>
<category>springMvc</category>
</categories>
<tags>
<tag>springMvc</tag>
</tags>
</entry>
<entry>
<title>Idea 设置Artifacts的BUG</title>
<url>/java/tool/idea-setting-artifacts-bugs/</url>
<content><![CDATA[<h1 id="Idea-设置Artifacts的BUG"><a href="#Idea-设置Artifacts的BUG" class="headerlink" title="Idea 设置Artifacts的BUG"></a>Idea 设置Artifacts的BUG</h1><h2 id="原因"><a href="#原因" class="headerlink" title="原因"></a>原因</h2><pre><code>IDEA 用起来很方便,但是在设置Artifacts时遇到的问题
</code></pre><h2 id="实现"><a href="#实现" class="headerlink" title="实现"></a>实现</h2><pre><code>在File->Project Structure->Project Setting->Artifacts 中
</code></pre><p><img src="/images/post/2017/06/20170627001.png"></p>
<pre><code>中的java修改为resources就可以
</code></pre><p><img src="/images/post/2017/06/20170627002.png"></p>
<pre><code>不知道算不算BUG,有时好有时不好,但经常会遇到,留个备份,谁有更好的方法请一定要通知我,谢谢!
</code></pre>]]></content>
<categories>
<category>java</category>
<category>tool</category>
</categories>
<tags>
<tag>idea</tag>
</tags>
</entry>
<entry>
<title>Mac OS X下Maven的安装与配置</title>
<url>/java/tool/mac-os-maven-setting/</url>
<content><![CDATA[<h2 id="Mac-OS-X-安装Maven"><a href="#Mac-OS-X-安装Maven" class="headerlink" title="Mac OS X 安装Maven:"></a>Mac OS X 安装Maven:</h2><ul>
<li><p>下载 <a href="http://maven.apache.org/download.cgi" target="_blank" rel="noopener">Maven</a>, 并解压到某个目录。例如/Users/hantianwei/apache-maven-3.5.0</p>
</li>
<li><p>打开Terminal,输入以下命令,设置Maven classpath</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line">$ vi ~/.bash_profile</span><br></pre></td></tr></table></figure>
</li>
<li><p>添加下列两行代码,之后保存并退出Vi:</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line">export M3_HOME=/Users/hantianwei/apache-maven-<span class="number">3.5</span>.0</span><br><span class="line">export PATH=$PATH:$M3_HOME/bin</span><br></pre></td></tr></table></figure>
<p><img src="/images/post/2017/08/20170804001.png"></p>
</li>
<li><p>输入命令以使bash_profile生效</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line">$ source ~/.bash_profile</span><br></pre></td></tr></table></figure>
</li>
<li><p>输入mvn -v查看Maven是否安装成功</p>
<p><img src="/images/post/2017/08/20170804002.png"></p>
</li>
</ul>
]]></content>
<categories>
<category>java</category>
<category>tool</category>
</categories>
<tags>
<tag>maven</tag>
<tag>macOs</tag>
</tags>
</entry>
<entry>
<title>Maven私服Nexus安装和配置</title>
<url>/java/tool/maven-nexus-install-setting/</url>
<content><![CDATA[<h3 id="需求"><a href="#需求" class="headerlink" title="需求"></a>需求</h3><ul>
<li>在用Maven的开发过程中,我们经常会遇到一些自有包的依赖,但又不能都在一个项目中(可能多个团队在维护),也有可能是我们用到了第三方的包,例如前段时间所有的阿里的包一样,我们把项目交给Maven管理后自已单独引进来的包在项目打包时是没办法打入包里面的,这时我们有两个解决办法一是前几天所说到的建立本地仓,这个方法简单但不易维护,接下来讲另一种方法就是建立基于Nexus的私服</li>
</ul>
<h3 id="安装Nexus"><a href="#安装Nexus" class="headerlink" title="安装Nexus"></a>安装Nexus</h3><ul>
<li>我用的是Nexus2.9版本,3.3版本没有2.9方便可能是我没有弄明白吧。</li>
<li>如果有需要的请网上搜,找不到的也可以发邮件给我,我提供给大家网盘共享</li>
<li>下载下来后我们解压到任一目录,我是放在了D:\soft\nexus目录</li>
<li>我们需要为Nexus建立环境变量,就是把BIN目录加入Path就可以</li>
<li>做完上面几步后,我们可以在DOS窗口试一下命令 nexus 只是不是不认这个命令就是成功了</li>
<li>安装服务,以管理员模式启动DOS后输入 nexus install 提示安装成功 再 nexus start 启动服务,只有第一次这样以后开机自启的</li>
<li>默认端口号为 8081 如果修改,请修改 D:\Soft\nexus\nexus-2.9.0-04\conf\nexus.properties 中的application-port=9002 修改后记得重启服务</li>
<li><p>运行 loaclhost:9002 可以看到如下图界面<br><img src="/images/post/2017/06/20170628001.png"></p>
</li>
<li><p>安装成功了,管理账号 admin 默认密码 admin123</p>
</li>
</ul>
<h2 id="Maven-设置"><a href="#Maven-设置" class="headerlink" title="Maven 设置"></a>Maven 设置</h2><ul>
<li>在Pom文件中增加</li>
</ul>
<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line"><span class="tag"><<span class="name">repositories</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">repository</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">id</span>></span>nexus_public<span class="tag"></<span class="name">id</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">url</span>></span>http://nexus.hantianwei.com:9002/nexus/content/groups/public/<span class="tag"></<span class="name">url</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">releases</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">enabled</span>></span>true<span class="tag"></<span class="name">enabled</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">releases</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">snapshots</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">enabled</span>></span>true<span class="tag"></<span class="name">enabled</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">snapshots</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">repository</span>></span></span><br><span class="line"><span class="tag"></<span class="name">repositories</span>></span></span><br><span class="line"><span class="tag"><<span class="name">pluginRepositories</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">pluginRepository</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">id</span>></span>nexus_public<span class="tag"></<span class="name">id</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">url</span>></span>http://nexus.hantianwei.com:9002/nexus/content/groups/public/<span class="tag"></<span class="name">url</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">releases</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">enabled</span>></span>true<span class="tag"></<span class="name">enabled</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">releases</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">snapshots</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">enabled</span>></span>true<span class="tag"></<span class="name">enabled</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">snapshots</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">pluginRepository</span>></span></span><br><span class="line"><span class="tag"></<span class="name">pluginRepositories</span>></span></span><br><span class="line"><span class="tag"><<span class="name">distributionManagement</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">repository</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">id</span>></span>nexus_public<span class="tag"></<span class="name">id</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">name</span>></span>Nexus Release Repository<span class="tag"></<span class="name">name</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">url</span>></span>http://nexus.hantianwei.com:9002/nexus/content/repositories/releases/<span class="tag"></<span class="name">url</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">repository</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">snapshotRepository</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">id</span>></span>nexus_public<span class="tag"></<span class="name">id</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">name</span>></span>Nexus Snapshot Repository<span class="tag"></<span class="name">name</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">url</span>></span>http://nexus.hantianwei.com:9002/nexus/content/repositories/snapshots/<span class="tag"></<span class="name">url</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">snapshotRepository</span>></span></span><br><span class="line"><span class="tag"></<span class="name">distributionManagement</span>></span></span><br></pre></td></tr></table></figure>
<ul>
<li><p>在Maven的Setting中增加</p>
<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line"><span class="tag"><<span class="name">servers</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">server</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">id</span>></span>nexus_public<span class="tag"></<span class="name">id</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">username</span>></span>admin<span class="tag"></<span class="name">username</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">password</span>></span>admin123<span class="tag"></<span class="name">password</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">server</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">servers</span>></span></span><br></pre></td></tr></table></figure>
</li>
<li><p>到此设置完毕,这样是对每个项目单独的设置,也可以全在Setting中设置为全局性</p>
<h2 id="验证"><a href="#验证" class="headerlink" title="验证"></a>验证</h2></li>
<li>在项目中执行 Deploy 提示成功后,可以到Nexus中搜一下包名,就可以看到如下</li>
</ul>
<p><img src="/images/post/2017/06/20170628002.png"></p>
<ul>
<li>这样就和其它包一样填加引用就可</li>
</ul>
<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line"><span class="tag"><<span class="name">dependency</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">groupId</span>></span>com.hantianwei<span class="tag"></<span class="name">groupId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">artifactId</span>></span>jgenerator<span class="tag"></<span class="name">artifactId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">version</span>></span>1.0-SNAPSHOT<span class="tag"></<span class="name">version</span>></span></span><br><span class="line"><span class="tag"></<span class="name">dependency</span>></span></span><br></pre></td></tr></table></figure>
<ul>
<li>是不是很方便,还可以为远程设置 开源中国 或 阿里云 的镜像</li>
</ul>
]]></content>
<categories>
<category>java</category>
<category>tool</category>
</categories>
<tags>
<tag>maven</tag>
<tag>nexus</tag>
</tags>
</entry>
<entry>
<title>sonar排除指定的目录</title>
<url>/java/tool/sonar-exclusions-files/</url>
<content><![CDATA[<h3 id="在开发中我们会有很多是不需要Sonar扫描的,这时就要对这部分文件夹或文件进行排除"><a href="#在开发中我们会有很多是不需要Sonar扫描的,这时就要对这部分文件夹或文件进行排除" class="headerlink" title="在开发中我们会有很多是不需要Sonar扫描的,这时就要对这部分文件夹或文件进行排除"></a>在开发中我们会有很多是不需要Sonar扫描的,这时就要对这部分文件夹或文件进行排除</h3><ul>
<li>这个很简单我们只需要在pom.xml中进行配置就可</li>
</ul>
<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line"><span class="tag"><<span class="name">properties</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">sonar.exclusions</span>></span></span><br><span class="line"> **/node_modules/**/*,**/html/**/src/**/*</span><br><span class="line"> <span class="tag"></<span class="name">sonar.exclusions</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">properties</span>></span></span><br></pre></td></tr></table></figure>
<ul>
<li>官方文档 docs.sonarqube.org</li>
<li>注意使用maven项目时匹配的文件夹基础是project base dir</li>
<li>? 匹配单个字符</li>
<li>** 匹配0个或多个文件夹 </li>
<li>匹配0个或多个字符</li>
</ul>
]]></content>
<categories>
<category>java</category>
<category>tool</category>
</categories>
<tags>
<tag>sonar</tag>
</tags>
</entry>
<entry>
<title>sonar-runner配置文件 sonar-project.properties</title>
<url>/java/tool/sonar-project-properties/</url>
<content><![CDATA[<h3 id="sonar-project-properties"><a href="#sonar-project-properties" class="headerlink" title="sonar-project.properties"></a>sonar-project.properties</h3><figure class="highlight xml"><table><tr><td class="code"><pre><span class="line">#required metadata</span><br><span class="line">#projectKey项目的唯一标识,不能重复</span><br><span class="line">sonar.projectKey=xjglxt</span><br><span class="line">sonar.projectName=xjglxt</span><br><span class="line">sonar.projectVersion=1.0</span><br><span class="line">sonar.sourceEncoding=UTF-8</span><br><span class="line">sonar.modules=java-module,javascript-module,html-module</span><br><span class="line"></span><br><span class="line"># Java module</span><br><span class="line">java-module.sonar.projectName=Java Module</span><br><span class="line">java-module.sonar.language=java</span><br><span class="line"># .表示projectBaseDir指定的目录</span><br><span class="line">java-module.sonar.sources=.</span><br><span class="line">java-module.sonar.projectBaseDir=src</span><br><span class="line">sonar.binaries=classes</span><br><span class="line"></span><br><span class="line"># JavaScript module</span><br><span class="line">javascript-module.sonar.projectName=JavaScript Module</span><br><span class="line">javascript-module.sonar.language=js</span><br><span class="line">javascript-module.sonar.sources=js</span><br><span class="line">javascript-module.sonar.projectBaseDir=webRoot</span><br><span class="line"></span><br><span class="line"># Html module</span><br><span class="line">html-module.sonar.projectName=Html Module</span><br><span class="line">html-module.sonar.language=web</span><br><span class="line">html-module.sonar.sources=pages</span><br><span class="line">html-module.sonar.projectBaseDir=webRoot</span><br><span class="line"></span><br><span class="line">sonar.projectKey=org.codehaus.sonar:php-sonar-runner-unit-tests</span><br><span class="line">sonar.projectName=PHP project analyzed with the SonarQube Runner reusing PHPUnit reports</span><br><span class="line">sonar.projectVersion=1.0</span><br><span class="line">sonar.sources=src</span><br><span class="line">sonar.tests=tests</span><br><span class="line">sonar.language=php</span><br><span class="line">sonar.sourceEncoding=UTF-8</span><br><span class="line"># Reusing PHPUnit reports</span><br><span class="line">sonar.php.coverage.reportPath=reports/phpunit.coverage.xml</span><br><span class="line">sonar.php.tests.reportPath=reports/phpunit.xml</span><br></pre></td></tr></table></figure>
]]></content>
<categories>
<category>java</category>
<category>tool</category>
</categories>
<tags>
<tag>sonar</tag>
<tag>sonar-runner</tag>
</tags>
</entry>
<entry>
<title>Sonar在Windows下批处理</title>
<url>/java/tool/sonar-windows-cmd-bat/</url>
<content><![CDATA[<h3 id="在开发中用Sonar进行代码扫描,我们在测试环境中经常要进行检测,下面提供个BAT处理文档每次执行下就OK"><a href="#在开发中用Sonar进行代码扫描,我们在测试环境中经常要进行检测,下面提供个BAT处理文档每次执行下就OK" class="headerlink" title="在开发中用Sonar进行代码扫描,我们在测试环境中经常要进行检测,下面提供个BAT处理文档每次执行下就OK"></a>在开发中用Sonar进行代码扫描,我们在测试环境中经常要进行检测,下面提供个BAT处理文档每次执行下就OK</h3><ul>
<li>我们需要在pom.xml中配置<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line"><span class="tag"><<span class="name">build</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">plugins</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">plugin</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">groupId</span>></span>org.jacoco<span class="tag"></<span class="name">groupId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">artifactId</span>></span>jacoco-maven-plugin<span class="tag"></<span class="name">artifactId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">executions</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">execution</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">id</span>></span>pre-test<span class="tag"></<span class="name">id</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">goals</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">goal</span>></span>prepare-agent<span class="tag"></<span class="name">goal</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">goals</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">execution</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">execution</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">id</span>></span>post-test<span class="tag"></<span class="name">id</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">phase</span>></span>test<span class="tag"></<span class="name">phase</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">goals</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">goal</span>></span>report<span class="tag"></<span class="name">goal</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">goals</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">execution</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">executions</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">plugin</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">plugins</span>></span></span><br><span class="line"><span class="tag"></<span class="name">build</span>></span></span><br></pre></td></tr></table></figure>
</li>
</ul>
<ul>
<li>然后我们在项目根目录新建一个文件sonar.bat 内容如下:</li>
</ul>
<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line">call mvn clean verify org.sonarsource.scanner.maven:sonar-maven-plugin:3.0.1:sonar -Dsonar.host.url=http://127.0.0.1:9002 -P dev -e -Dsonar.projectName="projectName"</span><br><span class="line">pause</span><br></pre></td></tr></table></figure>
<ul>
<li>这样每次需要检查时,只需要执行一下这个文件就可以</li>
</ul>
]]></content>
<categories>
<category>java</category>
<category>tool</category>
</categories>
<tags>
<tag>sonar</tag>
</tags>
</entry>
<entry>
<title>基于Freemarker的适用于Mybatis的代码生成器-(三)数据篇</title>
<url>/module/code-generator-freemarker/code-generator-freemarker-mybatis-database/</url>
<content><![CDATA[<h2 id="需求"><a href="#需求" class="headerlink" title="需求"></a>需求</h2><ul>
<li>有了配置后,接下来我们需要把数据库中的表信息和表的字段信息取出来,然后进行循环生成每个表相关代码</li>
</ul>
<h2 id="实现"><a href="#实现" class="headerlink" title="实现"></a>实现</h2><ul>
<li><p>先来定义一下用到的实体,表 Table 字段Field</p>
</li>
<li><p>Table</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">package</span> com.hantianwei.generator.model;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Created by tianwei on 2017/6/26.</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Table</span> </span>{</span><br><span class="line"> <span class="keyword">private</span> String name;</span><br><span class="line"> <span class="keyword">private</span> String proName;</span><br><span class="line"> <span class="keyword">private</span> String description;</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> String <span class="title">getName</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> name;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setName</span><span class="params">(String name)</span> </span>{</span><br><span class="line"> <span class="keyword">this</span>.name = name;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> String <span class="title">getDescription</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> description;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setDescription</span><span class="params">(String description)</span> </span>{</span><br><span class="line"> <span class="keyword">this</span>.description = description;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> String <span class="title">getProName</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> proName;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setProName</span><span class="params">(String proName)</span> </span>{</span><br><span class="line"> <span class="keyword">this</span>.proName = proName;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
</li>
<li><p>Field</p>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">package</span> com.hantianwei.generator.model;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Created by tianwei on 2017/6/28.</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Field</span> </span>{</span><br><span class="line"> <span class="keyword">private</span> String name;</span><br><span class="line"> <span class="keyword">private</span> String proName;</span><br><span class="line"> <span class="keyword">private</span> String type;</span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">int</span> maxLength;</span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">int</span> isNullable;</span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">int</span> isIdentity;</span><br><span class="line"> <span class="keyword">private</span> String description;</span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">int</span> isPk;</span><br><span class="line"> <span class="keyword">private</span> String jdbcType;</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> String <span class="title">getName</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> name;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setName</span><span class="params">(String name)</span> </span>{</span><br><span class="line"> <span class="keyword">this</span>.name = name;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> String <span class="title">getType</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> type;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setType</span><span class="params">(String type)</span> </span>{</span><br><span class="line"> <span class="keyword">this</span>.type = type;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getMaxLength</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> maxLength;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setMaxLength</span><span class="params">(<span class="keyword">int</span> maxLength)</span> </span>{</span><br><span class="line"> <span class="keyword">this</span>.maxLength = maxLength;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getIsNullable</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> isNullable;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setIsNullable</span><span class="params">(<span class="keyword">int</span> isNullable)</span> </span>{</span><br><span class="line"> <span class="keyword">this</span>.isNullable = isNullable;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getIsIdentity</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> isIdentity;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setIsIdentity</span><span class="params">(<span class="keyword">int</span> isIdentity)</span> </span>{</span><br><span class="line"> <span class="keyword">this</span>.isIdentity = isIdentity;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> String <span class="title">getDescription</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> description;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setDescription</span><span class="params">(String description)</span> </span>{</span><br><span class="line"> <span class="keyword">this</span>.description = description;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> String <span class="title">getProName</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> proName;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setProName</span><span class="params">(String proName)</span> </span>{</span><br><span class="line"> <span class="keyword">this</span>.proName = proName;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> String <span class="title">getJdbcType</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> jdbcType;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setJdbcType</span><span class="params">(String jdbcType)</span> </span>{</span><br><span class="line"> <span class="keyword">this</span>.jdbcType = jdbcType;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getIsPk</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> isPk;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setIsPk</span><span class="params">(<span class="keyword">int</span> isPk)</span> </span>{</span><br><span class="line"> <span class="keyword">this</span>.isPk = isPk;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<ul>
<li>需要封装对数据操作类 JdbcUtil</li>
</ul>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">package</span> com.hantianwei.generator.util;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.hantianwei.generator.model.Config;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.sql.*;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Created by tianwei on 2017/6/28.</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">JdbcUtil</span> </span>{</span><br><span class="line"> <span class="comment">//定义一个连接对象</span></span><br><span class="line"> <span class="keyword">private</span> Connection conn = <span class="keyword">null</span>;</span><br><span class="line"> <span class="comment">//定义连接数据库的url资源</span></span><br><span class="line"> <span class="keyword">private</span> String url = String.format(<span class="string">"jdbc:mysql://%s:%d/%s?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useUnicode=true&&autoReconnect=true&useSSL=false"</span>, Config.DB_SERVER, Config.DB_PORT, Config.DB_NAME);</span><br><span class="line"></span><br><span class="line"> <span class="comment">//定义连接数据库的用户名和密码</span></span><br><span class="line"> <span class="keyword">private</span> String userName = Config.DB_USER;</span><br><span class="line"> <span class="keyword">private</span> String passWord = Config.DB_PASSWORD;</span><br><span class="line"></span><br><span class="line"> <span class="comment">//加载数据库连接驱动</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> Connection <span class="title">getConnection</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">try</span> {</span><br><span class="line"> Class.forName(<span class="string">"com.mysql.jdbc.Driver"</span>);</span><br><span class="line"> conn = DriverManager.getConnection(url, userName, passWord);</span><br><span class="line"> } <span class="keyword">catch</span> (Exception e) {</span><br><span class="line"> e.printStackTrace();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> conn;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">//关闭数据库</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">closeALL</span><span class="params">(Connection conn, Statement st, ResultSet rs, PreparedStatement pst)</span> </span>{</span><br><span class="line"> <span class="keyword">try</span> {</span><br><span class="line"> <span class="keyword">if</span> (rs != <span class="keyword">null</span>) {</span><br><span class="line"> rs.close();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span> (st != <span class="keyword">null</span>) {</span><br><span class="line"> st.close();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span> (conn != <span class="keyword">null</span>) {</span><br><span class="line"> conn.close();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span> (pst != <span class="keyword">null</span>)</span><br><span class="line"> pst.close();</span><br><span class="line"> } <span class="keyword">catch</span> (SQLException e) {</span><br><span class="line"> e.printStackTrace();</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<ul>
<li>我们需要对查询的字段或表名进行驼峰式转换</li>
</ul>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">package</span> com.hantianwei.generator.util;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.hantianwei.generator.model.Field;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.HashMap;</span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"><span class="keyword">import</span> java.util.Map;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Created by tianwei on 2017/6/26.</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">StringUtil</span> </span>{</span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 把数据库中的字段转换为变量类型</span></span><br><span class="line"><span class="comment"> * 如(user_id ----> userId)</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> field</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">convertField</span><span class="params">(String field)</span> </span>{</span><br><span class="line"> <span class="comment">//分隔符</span></span><br><span class="line"> <span class="keyword">char</span> separator = <span class="string">'_'</span>;</span><br><span class="line"> <span class="comment">//转化为小写</span></span><br><span class="line"> String variable = field.toLowerCase();</span><br><span class="line"></span><br><span class="line"> <span class="keyword">if</span> (variable.indexOf(separator) > -<span class="number">1</span>) {</span><br><span class="line"> <span class="keyword">char</span>[] varArray = variable.toCharArray();</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < varArray.length; i++) {</span><br><span class="line"> <span class="keyword">if</span> (varArray[i] == separator && i < varArray.length - <span class="number">1</span>) {</span><br><span class="line"> varArray[i + <span class="number">1</span>] = Character.toUpperCase(varArray[i + <span class="number">1</span>]);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> variable = <span class="keyword">new</span> String(varArray).replaceAll(<span class="string">"_"</span>, <span class="string">""</span>);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> variable;</span><br><span class="line"></span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 获取字符串型的类型名</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> type</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">convertToJavaType</span><span class="params">(String type)</span> </span>{</span><br><span class="line"> Map<String,String> map = getTypesJava();</span><br><span class="line"> <span class="keyword">if</span>(map.containsKey(type)){</span><br><span class="line"> <span class="keyword">return</span> map.get(type);</span><br><span class="line"> }<span class="keyword">else</span> {</span><br><span class="line"> <span class="keyword">return</span> <span class="string">"String"</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> Map<String, String> <span class="title">getTypesJava</span><span class="params">()</span> </span>{</span><br><span class="line"> Map<String, String> map = <span class="keyword">new</span> HashMap<String, String>();</span><br><span class="line"> map.put(<span class="string">"VARCHAR"</span>, <span class="string">"String"</span>);</span><br><span class="line"> map.put(<span class="string">"CHAR"</span>, <span class="string">"String"</span>);</span><br><span class="line"> map.put(<span class="string">"BLOB"</span>, <span class="string">"byte[]"</span>);</span><br><span class="line"> map.put(<span class="string">"TEXT"</span>, <span class="string">"String"</span>);</span><br><span class="line"> map.put(<span class="string">"INTEGER"</span>, <span class="string">"Integer"</span>);</span><br><span class="line"> map.put(<span class="string">"INT"</span>, <span class="string">"Integer"</span>);</span><br><span class="line"> map.put(<span class="string">"SMALLINT"</span>, <span class="string">"Short"</span>);</span><br><span class="line"> map.put(<span class="string">"MEDIUMINT"</span>, <span class="string">"Integer"</span>);</span><br><span class="line"> map.put(<span class="string">"BIT"</span>, <span class="string">"Boolean"</span>);</span><br><span class="line"> map.put(<span class="string">"BIGINT"</span>, <span class="string">"Long"</span>);</span><br><span class="line"> map.put(<span class="string">"FLOAT"</span>, <span class="string">"Float"</span>);</span><br><span class="line"> map.put(<span class="string">"DOUBLE"</span>, <span class="string">"Double"</span>);</span><br><span class="line"> map.put(<span class="string">"DECIMAL"</span>, <span class="string">"BigDecimal"</span>);</span><br><span class="line"> map.put(<span class="string">"BOOLEAN"</span>, <span class="string">"Integer"</span>);</span><br><span class="line"> map.put(<span class="string">"TINYINT"</span>, <span class="string">"Byte"</span>);</span><br><span class="line"> map.put(<span class="string">"DATE"</span>, <span class="string">"Date"</span>);</span><br><span class="line"> map.put(<span class="string">"TIME"</span>, <span class="string">"Time"</span>);</span><br><span class="line"> map.put(<span class="string">"DATETIME"</span>, <span class="string">"Date"</span>);</span><br><span class="line"> map.put(<span class="string">"TIMESTAMP"</span>, <span class="string">"Timestamp"</span>);</span><br><span class="line"> map.put(<span class="string">"YEAR"</span>, <span class="string">"Date"</span>);</span><br><span class="line"> <span class="keyword">return</span> map;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">convertToJdbcType</span><span class="params">(String type)</span> </span>{</span><br><span class="line"> Map<String,String> map = getTypesJdbc();</span><br><span class="line"> <span class="keyword">if</span>(map.containsKey(type)){</span><br><span class="line"> <span class="keyword">return</span> map.get(type);</span><br><span class="line"> }<span class="keyword">else</span> {</span><br><span class="line"> <span class="keyword">return</span> <span class="string">"String"</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> Map<String, String> <span class="title">getTypesJdbc</span><span class="params">()</span> </span>{</span><br><span class="line"> Map<String, String> map = <span class="keyword">new</span> HashMap<String, String>();</span><br><span class="line"> map.put(<span class="string">"VARCHAR"</span>, <span class="string">"VARCHAR"</span>);</span><br><span class="line"> map.put(<span class="string">"CHAR"</span>, <span class="string">"CHAR"</span>);</span><br><span class="line"> map.put(<span class="string">"BLOB"</span>, <span class="string">"BINARY"</span>);</span><br><span class="line"> map.put(<span class="string">"TEXT"</span>, <span class="string">"LONGVARCHAR"</span>);</span><br><span class="line"> map.put(<span class="string">"INTEGER"</span>, <span class="string">"INTEGER"</span>);</span><br><span class="line"> map.put(<span class="string">"INT"</span>, <span class="string">"INTEGER"</span>);</span><br><span class="line"> map.put(<span class="string">"SMALLINT"</span>, <span class="string">"SMALLINT"</span>);</span><br><span class="line"> map.put(<span class="string">"MEDIUMINT"</span>, <span class="string">"INTEGER"</span>);</span><br><span class="line"> map.put(<span class="string">"BIT"</span>, <span class="string">"BIT"</span>);</span><br><span class="line"> map.put(<span class="string">"BIGINT"</span>, <span class="string">"BIGINT"</span>);</span><br><span class="line"> map.put(<span class="string">"FLOAT"</span>, <span class="string">"REAL"</span>);</span><br><span class="line"> map.put(<span class="string">"DOUBLE"</span>, <span class="string">"DOUBLE"</span>);</span><br><span class="line"> map.put(<span class="string">"DECIMAL"</span>, <span class="string">"DECIMAL"</span>);</span><br><span class="line"> map.put(<span class="string">"TINYINT"</span>, <span class="string">"TINYINT"</span>);</span><br><span class="line"> map.put(<span class="string">"DATE"</span>, <span class="string">"DATE"</span>);</span><br><span class="line"> map.put(<span class="string">"TIME"</span>, <span class="string">"TIME"</span>);</span><br><span class="line"> map.put(<span class="string">"DATETIME"</span>, <span class="string">"TIMESTAMP"</span>);</span><br><span class="line"> map.put(<span class="string">"TIMESTAMP"</span>, <span class="string">"TIMESTAMP"</span>);</span><br><span class="line"> map.put(<span class="string">"YEAR"</span>, <span class="string">"DATE"</span>);</span><br><span class="line"> <span class="keyword">return</span> map;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> Field <span class="title">getFieldPk</span><span class="params">(List<Field> fieldList)</span></span>{</span><br><span class="line"> Field field = <span class="keyword">new</span> Field();</span><br><span class="line"> <span class="keyword">for</span> (Field item:fieldList) {</span><br><span class="line"> <span class="keyword">if</span>(item.getIsPk()==<span class="number">1</span>){</span><br><span class="line"> field=item;</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> field;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">//首字母转小写</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">toLowerCaseFirstOne</span><span class="params">(String s)</span></span></span><br><span class="line"><span class="function"> </span>{</span><br><span class="line"> <span class="keyword">if</span>(Character.isLowerCase(s.charAt(<span class="number">0</span>)))</span><br><span class="line"> <span class="keyword">return</span> s;</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> <span class="keyword">return</span> (<span class="keyword">new</span> StringBuilder()).append(Character.toLowerCase(s.charAt(<span class="number">0</span>))).append(s.substring(<span class="number">1</span>)).toString();</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">//首字母转大写</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">toUpperCaseFirstOne</span><span class="params">(String s)</span></span></span><br><span class="line"><span class="function"> </span>{</span><br><span class="line"> <span class="keyword">if</span>(Character.isUpperCase(s.charAt(<span class="number">0</span>)))</span><br><span class="line"> <span class="keyword">return</span> s;</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> <span class="keyword">return</span> (<span class="keyword">new</span> StringBuilder()).append(Character.toUpperCase(s.charAt(<span class="number">0</span>))).append(s.substring(<span class="number">1</span>)).toString();</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<ul>
<li>最后是对数据库的查询</li>
</ul>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">package</span> com.hantianwei.generator.dao;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.hantianwei.generator.model.Field;</span><br><span class="line"><span class="keyword">import</span> com.hantianwei.generator.model.Table;</span><br><span class="line"><span class="keyword">import</span> com.hantianwei.generator.util.JdbcUtil;</span><br><span class="line"><span class="keyword">import</span> com.hantianwei.generator.util.StringUtil;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.sql.*;</span><br><span class="line"><span class="keyword">import</span> java.util.ArrayList;</span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Created by tianwei on 2017/6/26.</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">DataDao</span> </span>{</span><br><span class="line"> Connection conn = <span class="keyword">null</span>;</span><br><span class="line"> Statement st = <span class="keyword">null</span>;</span><br><span class="line"> ResultSet rs = <span class="keyword">null</span>;</span><br><span class="line"> PreparedStatement pst = <span class="keyword">null</span>;</span><br><span class="line"> String sqlstr = <span class="keyword">null</span>;</span><br><span class="line"></span><br><span class="line"> String tableSql = <span class="string">"SELECT \n"</span> +</span><br><span class="line"> <span class="string">"\t TABLE_NAME as name,\n"</span> +</span><br><span class="line"> <span class="string">"\t TABLE_COMMENT as description \n"</span> +</span><br><span class="line"> <span class="string">" FROM \n"</span> +</span><br><span class="line"> <span class="string">"\t information_schema.TABLES \n"</span> +</span><br><span class="line"> <span class="string">" WHERE \n"</span> +</span><br><span class="line"> <span class="string">"\t TABLE_SCHEMA='%s'"</span>;</span><br><span class="line"></span><br><span class="line"> String fildSql = <span class="string">"SELECT \n"</span> +</span><br><span class="line"> <span class="string">"\t c.COLUMN_NAME as name,\n"</span> +</span><br><span class="line"> <span class="string">"\t c.DATA_TYPE as type,\n"</span> +</span><br><span class="line"> <span class="string">"\t IFNULL(c.CHARACTER_MAXIMUM_LENGTH,0) as maxLength,\n"</span> +</span><br><span class="line"> <span class="string">"\t (CASE c.IS_NULLABLE WHEN 'NO' THEN 0 ELSE 1 END) as isNullable,\n"</span> +</span><br><span class="line"> <span class="string">"\t (CASE WHEN t.AUTO_INCREMENT>0 AND k.COLUMN_NAME IS NOT NULL THEN 1 ELSE 0 END) as isIdentity,\n"</span> +</span><br><span class="line"> <span class="string">"\t c.COLUMN_COMMENT as description,\n"</span> +</span><br><span class="line"> <span class="string">"\t (CASE c.COLUMN_KEY WHEN 'PRI' THEN 1 ELSE 0 END) as isPk\n"</span> +</span><br><span class="line"> <span class="string">" FROM \n"</span> +</span><br><span class="line"> <span class="string">"\t information_schema.COLUMNS AS c\n"</span> +</span><br><span class="line"> <span class="string">"\t INNER JOIN information_schema.`TABLES` AS t ON t.TABLE_SCHEMA=c.TABLE_SCHEMA AND t.TABLE_NAME=c.TABLE_NAME\n"</span> +</span><br><span class="line"> <span class="string">"\t LEFT JOIN information_schema.KEY_COLUMN_USAGE k ON k.COLUMN_NAME=c.COLUMN_NAME AND c.TABLE_NAME=k.TABLE_NAME AND c.TABLE_SCHEMA=k.TABLE_SCHEMA\n"</span> +</span><br><span class="line"> <span class="string">" WHERE \n"</span> +</span><br><span class="line"> <span class="string">"\t c.TABLE_SCHEMA='%s'"</span> +</span><br><span class="line"> <span class="string">"\t AND c.TABLE_NAME='%s'"</span> +</span><br><span class="line"> <span class="string">" ORDER BY\n"</span> +</span><br><span class="line"> <span class="string">"\t c.ORDINAL_POSITION ASC"</span>;</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 根据表名获取所有的列信息</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> tableSchema</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> tableName</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> List<Field> <span class="title">getAllColums</span><span class="params">(String tableSchema, String tableName)</span> </span>{</span><br><span class="line"></span><br><span class="line"> List<Field> fieldList = <span class="keyword">new</span> ArrayList<Field>();</span><br><span class="line"> JdbcUtil dbConn = <span class="keyword">new</span> JdbcUtil();</span><br><span class="line"></span><br><span class="line"> <span class="keyword">try</span> {</span><br><span class="line"> <span class="keyword">this</span>.conn = dbConn.getConnection();</span><br><span class="line"> <span class="keyword">this</span>.sqlstr = String.format(fildSql, tableSchema, tableName);</span><br><span class="line"> <span class="keyword">this</span>.st = conn.createStatement();</span><br><span class="line"> <span class="keyword">this</span>.rs = <span class="keyword">this</span>.st.executeQuery(sqlstr);</span><br><span class="line"> <span class="keyword">while</span> (rs.next()) {</span><br><span class="line"> Field field = <span class="keyword">new</span> Field();</span><br><span class="line"> String fieldName = rs.getString(<span class="string">"name"</span>);</span><br><span class="line"> field.setName(fieldName);</span><br><span class="line"> field.setProName(StringUtil.convertField(fieldName));</span><br><span class="line"> String fieldType = rs.getString(<span class="string">"type"</span>).toUpperCase();</span><br><span class="line"> String javaType = StringUtil.convertToJavaType(fieldType);</span><br><span class="line"> field.setType(javaType);</span><br><span class="line"> field.setJdbcType(StringUtil.convertToJdbcType(fieldType));</span><br><span class="line"> field.setMaxLength(rs.getInt(<span class="string">"maxLength"</span>));</span><br><span class="line"> field.setIsNullable(rs.getInt(<span class="string">"isNullable"</span>));</span><br><span class="line"> field.setIsIdentity(rs.getInt(<span class="string">"isIdentity"</span>));</span><br><span class="line"> String desc = rs.getString(<span class="string">"description"</span>);</span><br><span class="line"> field.setDescription(desc == <span class="keyword">null</span> ? <span class="string">""</span> : desc);</span><br><span class="line"> field.setIsPk(rs.getInt(<span class="string">"isPk"</span>));</span><br><span class="line"> fieldList.add(field);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> } <span class="keyword">catch</span> (SQLException e) {</span><br><span class="line"> e.printStackTrace();</span><br><span class="line"> } <span class="keyword">finally</span> {</span><br><span class="line"> dbConn.closeALL(conn, st, rs, pst);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> fieldList;</span><br><span class="line"></span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 根据库名聂所有表</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> tableSchema</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> List<Table> <span class="title">getAllTables</span><span class="params">(String tableSchema)</span> </span>{</span><br><span class="line"> List<Table> fieldList = <span class="keyword">new</span> ArrayList<Table>();</span><br><span class="line"> JdbcUtil dbConn = <span class="keyword">new</span> JdbcUtil();</span><br><span class="line"></span><br><span class="line"> <span class="keyword">try</span> {</span><br><span class="line"> <span class="keyword">this</span>.conn = dbConn.getConnection();</span><br><span class="line"> <span class="keyword">this</span>.sqlstr = String.format(tableSql, tableSchema);</span><br><span class="line"> <span class="keyword">this</span>.st = conn.createStatement();</span><br><span class="line"> <span class="keyword">this</span>.rs = <span class="keyword">this</span>.st.executeQuery(sqlstr);</span><br><span class="line"> <span class="keyword">while</span> (rs.next()) {</span><br><span class="line"> Table table = <span class="keyword">new</span> Table();</span><br><span class="line"> String tableName = rs.getString(<span class="string">"name"</span>);</span><br><span class="line"> table.setName(tableName);</span><br><span class="line"> table.setProName(StringUtil.convertField(tableName));</span><br><span class="line"> String desc = rs.getString(<span class="string">"description"</span>);</span><br><span class="line"> table.setDescription(desc == <span class="keyword">null</span> ? <span class="string">""</span> : desc);</span><br><span class="line"> fieldList.add(table);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> } <span class="keyword">catch</span> (SQLException e) {</span><br><span class="line"> e.printStackTrace();</span><br><span class="line"> } <span class="keyword">finally</span> {</span><br><span class="line"> dbConn.closeALL(conn, st, rs, pst);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> fieldList;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<ul>
<li>下一节我们要进行文件生成封装</li>
</ul>
<ul>
<li>开源地址:<a href="https://github.com/hantianwei/jgenerator" target="_blank" rel="noopener">https://github.com/hantianwei/jgenerator</a></li>
</ul>
]]></content>
<categories>
<category>module</category>
<category>code-generator-freemarker</category>
</categories>
<tags>
<tag>freemarker</tag>
<tag>codeGenerator</tag>
<tag>mybatis</tag>
</tags>
</entry>
<entry>
<title>基于Freemarker的适用于Mybatis的代码生成器-(二)配置篇</title>
<url>/module/code-generator-freemarker/code-generator-freemarker-mybatis-config/</url>
<content><![CDATA[<h2 id="需求"><a href="#需求" class="headerlink" title="需求"></a>需求</h2><ul>
<li>需求、想法和框架都有了,接下来我们就是实干阶段,首先们要考虑的是这是一个通用的代码生成器,这时我们想到的就是通过配轩置文件来实现对数据库及生成目录名称等的配置,这里需求就是要有配置</li>
</ul>
<h2 id="实现"><a href="#实现" class="headerlink" title="实现"></a>实现</h2><ul>
<li>在程序起动时就要加载我们自定义的配置,这时就需要配置文件config.properties,还需要注意的是我们打完包后是走包外的配置</li>
</ul>
<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line">DB_SERVER = 127.0.0.1</span><br><span class="line">DB_PORT = 3306</span><br><span class="line">DB_USER = root</span><br><span class="line">DB_PASSWORD = abc@123</span><br><span class="line">DB_NAME = testCode</span><br><span class="line">PACKAGE_ROOT_NAME=com.hantianwei.testcode</span><br><span class="line">PACKAGE_ROOT_PATH = com.hantianwei.testcode</span><br><span class="line">PACKAGE_MODEL_NAME = domian</span><br><span class="line">PACKAGE_MODEL_PATH = \\testcode\\testcode-domain\\src/main/java/com/hantianwei/testcode/domain/</span><br><span class="line">PACKAGE_DAO_NAME = dao</span><br><span class="line">PACKAGE_DAO_PATH = /testcode/testcode-dao/src/main/java/com/hantianwei/testcode/dao/</span><br><span class="line">PACKAGE_SERVICE_NAME = service</span><br><span class="line">PACKAGE_SERVICE_PATH = /testcode/testcode-service/src/main/java/com/hantianwei/testcode/service/impl/</span><br><span class="line">PACKAGE_ISERVICE_NAME = service</span><br><span class="line">PACKAGE_ISERVICE_PATH = /testcode/testcode-service/src/main/java/com/hantianwei/testcode/service/</span><br><span class="line">PACKAGE_MAPPER_PATH = /testcode/testcode-dao/src/main/resources/mapping/</span><br><span class="line">TRIM_STRINGS = true</span><br></pre></td></tr></table></figure>
<ul>
<li>需要在model里面建一个Config</li>
</ul>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">package</span> com.hantianwei.generator.model;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Created by tianwei on 2017/6/27.</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Config</span> </span>{</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> String DB_SERVER = <span class="string">""</span>;</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">int</span> DB_PORT = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> String DB_USER = <span class="string">""</span>;</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> String DB_PASSWORD = <span class="string">""</span>;</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> String DB_NAME = <span class="string">""</span>;</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> String PACKAGE_ROOT_NAME=<span class="string">""</span>;</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> String PACKAGE_ROOT_PATH = <span class="string">""</span>;</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> String PACKAGE_MODEL_NAME = <span class="string">""</span>;</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> String PACKAGE_MODEL_PATH = <span class="string">""</span>;</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> String PACKAGE_DAO_NAME = <span class="string">""</span>;</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> String PACKAGE_DAO_PATH = <span class="string">""</span>;</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> String PACKAGE_SERVICE_NAME = <span class="string">""</span>;</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> String PACKAGE_SERVICE_PATH = <span class="string">""</span>;</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> String PACKAGE_ISERVICE_NAME = <span class="string">""</span>;</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> String PACKAGE_ISERVICE_PATH = <span class="string">""</span>;</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> String PACKAGE_MAPPER_PATH = <span class="string">""</span>;</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">boolean</span> TRIM_STRINGS = <span class="keyword">false</span>;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> String CONFIG_LOCAL_PATH =<span class="string">"config.properties"</span>;</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">boolean</span> CONFIG_IS_LOCAL = <span class="keyword">true</span>;</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> String CONFIG_PATH =<span class="string">""</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<ul>
<li>在util里面新建PropUtil进行对配置的装载</li>
</ul>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">package</span> com.hantianwei.generator.util;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.hantianwei.generator.model.Config;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.io.FileInputStream;</span><br><span class="line"><span class="keyword">import</span> java.io.InputStream;</span><br><span class="line"><span class="keyword">import</span> java.util.Properties;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Created by tianwei on 2017/6/27.</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">PropUtil</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">loadConf</span><span class="params">(String[] args)</span></span>{</span><br><span class="line"> <span class="keyword">if</span> (args == <span class="keyword">null</span> || args.length == <span class="number">0</span>) {</span><br><span class="line"> Config.CONFIG_IS_LOCAL = <span class="keyword">true</span>;</span><br><span class="line"> PropUtil.loadConf(Config.CONFIG_LOCAL_PATH);</span><br><span class="line"> System.out.println(String.format(<span class="string">"加载默认配置完成:数据库[%s]"</span>,Config.DB_NAME));</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> Config.CONFIG_IS_LOCAL = <span class="keyword">false</span>;</span><br><span class="line"> Config.CONFIG_PATH = args[<span class="number">0</span>];</span><br><span class="line"> PropUtil.loadConf(Config.CONFIG_PATH);</span><br><span class="line"> System.out.println(String.format(<span class="string">"加载自定义配置完成:数据库[%s]"</span>,Config.DB_NAME));</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">loadConf</span><span class="params">(String path)</span> </span>{</span><br><span class="line"> <span class="keyword">try</span> {</span><br><span class="line"> Properties props = <span class="keyword">new</span> Properties();</span><br><span class="line"> InputStream in;</span><br><span class="line"> <span class="keyword">if</span> (Config.CONFIG_IS_LOCAL) {</span><br><span class="line"> in = PropUtil.class.getClassLoader().getResourceAsStream(path);</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> in = <span class="keyword">new</span> FileInputStream(path);</span><br><span class="line"> }</span><br><span class="line"> props.load(in);</span><br><span class="line"> Config.DB_SERVER = props.getProperty(<span class="string">"DB_SERVER"</span>);</span><br><span class="line"> Config.DB_PORT = Integer.parseInt(props.getProperty(<span class="string">"DB_PORT"</span>));</span><br><span class="line"> Config.DB_USER = props.getProperty(<span class="string">"DB_USER"</span>);</span><br><span class="line"> Config.DB_PASSWORD = props.getProperty(<span class="string">"DB_PASSWORD"</span>);</span><br><span class="line"> Config.DB_NAME = props.getProperty(<span class="string">"DB_NAME"</span>);</span><br><span class="line"> Config.PACKAGE_ROOT_NAME = props.getProperty(<span class="string">"PACKAGE_ROOT_NAME"</span>);</span><br><span class="line"> Config.PACKAGE_ROOT_PATH = props.getProperty(<span class="string">"PACKAGE_ROOT_PATH"</span>);</span><br><span class="line"> Config.PACKAGE_MODEL_NAME = props.getProperty(<span class="string">"PACKAGE_MODEL_NAME"</span>);</span><br><span class="line"> Config.PACKAGE_MODEL_PATH = props.getProperty(<span class="string">"PACKAGE_MODEL_PATH"</span>);</span><br><span class="line"> Config.PACKAGE_DAO_NAME = props.getProperty(<span class="string">"PACKAGE_DAO_NAME"</span>);</span><br><span class="line"> Config.PACKAGE_DAO_PATH = props.getProperty(<span class="string">"PACKAGE_DAO_PATH"</span>);</span><br><span class="line"> Config.PACKAGE_SERVICE_NAME = props.getProperty(<span class="string">"PACKAGE_SERVICE_NAME"</span>);</span><br><span class="line"> Config.PACKAGE_SERVICE_PATH = props.getProperty(<span class="string">"PACKAGE_SERVICE_PATH"</span>);</span><br><span class="line"> Config.PACKAGE_ISERVICE_NAME = props.getProperty(<span class="string">"PACKAGE_ISERVICE_NAME"</span>);</span><br><span class="line"> Config.PACKAGE_ISERVICE_PATH = props.getProperty(<span class="string">"PACKAGE_ISERVICE_PATH"</span>);</span><br><span class="line"> Config.PACKAGE_MAPPER_PATH = props.getProperty(<span class="string">"PACKAGE_MAPPER_PATH"</span>);</span><br><span class="line"> Config.TRIM_STRINGS = Boolean.parseBoolean(props.getProperty(<span class="string">"TRIM_STRINGS"</span>));</span><br><span class="line"></span><br><span class="line"> } <span class="keyword">catch</span> (Exception e) {</span><br><span class="line"> e.printStackTrace();</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<ul>
<li>对了忘了我们的Mian方法了,这里在main里新建RunGenerator</li>
</ul>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">package</span> com.hantianwei.generator.main;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.hantianwei.generator.util.PropUtil;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Created by tianwei on 2017/6/27.</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">RunGenerator</span> </span>{</span><br><span 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>{</span><br><span class="line"> PropUtil.loadConf(args);</span><br><span class="line"> System.out.println(<span class="string">"****** OK ******"</span>);</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<ul>
<li>到这里好奇的同学可以断电试一下,我们的配置可以加载成功了,如果想试打包后的效果,可以先打个包,再用下面命令,记得config.properties得是包外自定义的丁配置文件</li>
</ul>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line">java -jar jgenerator.jar config.properties</span><br></pre></td></tr></table></figure>
<ul>
<li>开源地址:<a href="https://github.com/hantianwei/jgenerator" target="_blank" rel="noopener">https://github.com/hantianwei/jgenerator</a></li>
</ul>
]]></content>
<categories>
<category>module</category>
<category>code-generator-freemarker</category>
</categories>
<tags>
<tag>freemarker</tag>
<tag>codeGenerator</tag>
<tag>mybatis</tag>
</tags>
</entry>
<entry>
<title>基于Freemarker的适用于Mybatis的代码生成器-想法篇</title>
<url>/module/code-generator-freemarker/code-generator-freemarker-mybatis-idea/</url>
<content><![CDATA[<h1 id="基于Freemarker的适用于Mybatis的代码生成器"><a href="#基于Freemarker的适用于Mybatis的代码生成器" class="headerlink" title="基于Freemarker的适用于Mybatis的代码生成器"></a>基于Freemarker的适用于Mybatis的代码生成器</h1><h2 id="需求"><a href="#需求" class="headerlink" title="需求"></a>需求</h2><ul>
<li>首选说明代码生成器开源的已有很多,如mybatis-generator就是用的比较多的,也很成熟,但是在配置中感觉有很多的不灵活,可能人家是为了很多人用的,各种各样的配置都可在实现<br>我们来实现的功能大体相似,只是简化配置且可全局配置,全局实现驼峰式映射</li>
</ul>
<h2 id="想法"><a href="#想法" class="headerlink" title="想法"></a>想法</h2><ul>
<li>有了需求就需要有想法,我们实现的到底是个什么样子的程序,代码生成器要适用于Mysql,所以我们需要引用进mysql-connector-java,程序上面不能太定制化,为了以后的扩展和改变生成出来的代码方便,那我们就需要用到模板机制,这样以后的改动只需要改模板,而不需要改动实现层面的程序,更想一下可以模板自定义,这里我们需要用到Freemarker。<br>简明扼要的说我们就是要实现一个生成框架,来通过模板生成想要的文件。</li>
</ul>
<h2 id="框架"><a href="#框架" class="headerlink" title="框架"></a>框架</h2><ul>
<li>有了想法,我们就需要落实,这时我们就可以规划一下项目的框架了。<br>1、因为要读取数据库的表信息和每个表的字段结构信息,这里肯定需要访问数据库,所有一定有一层Dao<br>2、我们需要一定的逻辑信息 Service 层(接口和实现)<br>3、我们需要对表名或字段名等进行驼峰式格式化 Util层<br>4、面向对像编程我们需要一些对像 Model层<br>5、运行Jar我们需要一个主函数 Mian层<br>6、在操作是我们用到了枚举 Enum层<br>7、我们需要配置信息用到了 properties 文件<br>8、最后就是我们的模板了<br>到此项目的架构是不是就清晰了,项目截图<br><img src="/images/post/2017/06/20170627003.png"></li>
</ul>
<ul>
<li>开源地址:<a href="https://github.com/hantianwei/jgenerator" target="_blank" rel="noopener">https://github.com/hantianwei/jgenerator</a></li>
</ul>
]]></content>
<categories>
<category>module</category>
<category>code-generator-freemarker</category>
</categories>
<tags>
<tag>freemarker</tag>
<tag>codeGenerator</tag>
<tag>mybatis</tag>
</tags>
</entry>
<entry>
<title>基于Freemarker的适用于Mybatis的代码生成器-(六)总结篇</title>
<url>/module/code-generator-freemarker/code-generator-freemarker-mybatis-summary/</url>
<content><![CDATA[<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><ul>
<li>全部开发完毕,我们要对这个项目进行总结,在这个项目中我们很多不足,最大的不足是对需求的调研不够,想想就做了,如果想的太多可能现在还没有开始,或是做着做着就做不下去了,先快速出来在使用中进行调整,比单独的想要好,还有就是对异常的处理</li>
</ul>
<h2 id="脑洞大开"><a href="#脑洞大开" class="headerlink" title="脑洞大开"></a>脑洞大开</h2><ul>
<li>在生成过程中我们可以用枚举进行配置,或是直接用配置控制生成和模板的调用</li>
<li>我们可以模板外置,通过配置来进行适配</li>
</ul>
<ul>
<li>开源地址:<a href="https://github.com/hantianwei/jgenerator" target="_blank" rel="noopener">https://github.com/hantianwei/jgenerator</a></li>
</ul>
]]></content>
<categories>
<category>module</category>
<category>code-generator-freemarker</category>
</categories>
<tags>
<tag>freemarker</tag>
<tag>codeGenerator</tag>
<tag>mybatis</tag>
</tags>
</entry>
<entry>
<title>基于Freemarker的适用于Mybatis的代码生成器-(四)文件篇</title>
<url>/module/code-generator-freemarker/code-generator-freemarker-mybatis-file/</url>
<content><![CDATA[<h2 id="需求"><a href="#需求" class="headerlink" title="需求"></a>需求</h2><ul>
<li>我们需要一个可以通用的且只是改变或增加模板就可以实现的文件生成器</li>
</ul>
<h2 id="实现"><a href="#实现" class="headerlink" title="实现"></a>实现</h2><ul>
<li><h3 id="文件类"><a href="#文件类" class="headerlink" title="文件类"></a>文件类</h3></li>
<li>对于文件的操作,我们需要判断文件夹是否存在,如果不存在则创建和取当前项目路径</li>
</ul>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">package</span> com.hantianwei.generator.util;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.io.File;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Created by tianwei on 2017/6/29.</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">FileUtil</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">createDirectory</span><span class="params">(String path)</span> </span>{</span><br><span class="line"> File file = <span class="keyword">new</span> File(path);</span><br><span class="line"> <span class="keyword">if</span> (!file.exists()) {</span><br><span class="line"> <span class="keyword">try</span> {</span><br><span class="line"> file.mkdirs();</span><br><span class="line"> } <span class="keyword">catch</span> (Exception e) {</span><br><span class="line"> e.printStackTrace();</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">getCurrentDirParent</span><span class="params">()</span> </span>{</span><br><span class="line"> String path = System.getProperty(<span class="string">"user.dir"</span>);</span><br><span class="line"> File file = <span class="keyword">new</span> File(path);</span><br><span class="line"> <span class="keyword">return</span> file.getParent();</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<ul>
<li>文件类有了,我们需要写一个Freemarker类来实现解析模板</li>
</ul>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">package</span> com.hantianwei.generator.util;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.io.*;</span><br><span class="line"><span class="keyword">import</span> java.util.Map;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> freemarker.template.Configuration;</span><br><span class="line"><span class="keyword">import</span> freemarker.template.Template;</span><br><span class="line"><span class="keyword">import</span> freemarker.template.TemplateException;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Created by tianwei on 2017/6/29.</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">FreeMakerUtil</span> </span>{</span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 获取模板文件</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> name</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> Template <span class="title">getTemplate</span><span class="params">(String name)</span> </span>{</span><br><span class="line"> <span class="keyword">try</span> {</span><br><span class="line"> Configuration cfg = <span class="keyword">new</span> Configuration();</span><br><span class="line"> cfg.setDefaultEncoding(<span class="string">"UTF-8"</span>);</span><br><span class="line"> cfg.setClassForTemplateLoading(<span class="keyword">this</span>.getClass(), <span class="string">"/ftl"</span>);</span><br><span class="line"> Template template = cfg.getTemplate(name);</span><br><span class="line"> <span class="keyword">return</span> template;</span><br><span class="line"> } <span class="keyword">catch</span> (IOException e) {</span><br><span class="line"> e.printStackTrace();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 生成文件</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> templateName:模板名</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> root:数据原型</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> outFilePath:输出路径(全路径名)</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">generateFile</span><span class="params">(String templateName, Map<String, Object> root, String outFilePath)</span> </span>{</span><br><span class="line"></span><br><span class="line"> Writer out = <span class="keyword">null</span>;</span><br><span class="line"> <span class="keyword">try</span> {</span><br><span class="line"> outFilePath = FileUtil.getCurrentDirParent() + outFilePath;</span><br><span class="line"> FileUtil.createDirectory(<span class="keyword">new</span> File(outFilePath).getParent());</span><br><span class="line"> <span class="comment">// 通过一个文件输出流,就可以写到相应的文件中,此处用的是绝对路径</span></span><br><span class="line"> out = <span class="keyword">new</span> BufferedWriter(<span class="keyword">new</span> OutputStreamWriter(<span class="keyword">new</span> FileOutputStream(<span class="keyword">new</span> File(outFilePath)), <span class="string">"UTF-8"</span>));</span><br><span class="line"> Template temp = <span class="keyword">this</span>.getTemplate(templateName);</span><br><span class="line"> temp.process(root, out);</span><br><span class="line"> } <span class="keyword">catch</span> (IOException e) {</span><br><span class="line"> e.printStackTrace();</span><br><span class="line"> } <span class="keyword">catch</span> (TemplateException e) {</span><br><span class="line"> e.printStackTrace();</span><br><span class="line"> } <span class="keyword">finally</span> {</span><br><span class="line"> <span class="keyword">try</span> {</span><br><span class="line"> <span class="keyword">if</span> (out != <span class="keyword">null</span>)</span><br><span class="line"> out.close();</span><br><span class="line"> } <span class="keyword">catch</span> (IOException e) {</span><br><span class="line"> e.printStackTrace();</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<ul>
<li><h3 id="生成类"><a href="#生成类" class="headerlink" title="生成类"></a>生成类</h3><ul>
<li>到这里我们只剩下Service里面的逻辑实现部分了,我们在 DataService 中增加模板赋值</li>
</ul>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">package</span> com.hantianwei.generator.service.impl;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.hantianwei.generator.model.Field;</span><br><span class="line"><span class="keyword">import</span> com.hantianwei.generator.model.Table;</span><br><span class="line"><span class="keyword">import</span> com.hantianwei.generator.service.IDataService;</span><br><span class="line"><span class="keyword">import</span> com.hantianwei.generator.util.FreeMakerUtil;</span><br><span class="line"><span class="keyword">import</span> com.hantianwei.generator.util.StringUtil;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.HashMap;</span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"><span class="keyword">import</span> java.util.Map;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Created by tianwei on 2017/6/26.</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">DataService</span> <span class="keyword">implements</span> <span class="title">IDataService</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 根据表名获取模板要用的Map型数据</span></span><br><span class="line"><span class="comment"> * (带有列信息)</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> table</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> Map<String, Object> <span class="title">getDbTemplateData</span><span class="params">(Table table, List<Field> fieldList)</span> </span>{</span><br><span class="line"> Map<String, Object> templateData = <span class="keyword">new</span> HashMap<String, Object>();</span><br><span class="line"> templateData.put(<span class="string">"tableName"</span>, table.getName());</span><br><span class="line"> templateData.put(<span class="string">"tableProName"</span>, table.getProName());</span><br><span class="line"> templateData.put(<span class="string">"fieldList"</span>, fieldList);</span><br><span class="line"> templateData.put(<span class="string">"fieldPk"</span>, StringUtil.getFieldPk(fieldList));</span><br><span class="line"> templateData.put(<span class="string">"config"</span>, FreeMakerUtil.useStaticPackage(<span class="string">"com.hantianwei.generator.model.Config"</span>));</span><br><span class="line"> <span class="keyword">return</span> templateData;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">package</span> com.hantianwei.generator.service;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.hantianwei.generator.model.Field;</span><br><span class="line"><span class="keyword">import</span> com.hantianwei.generator.model.Table;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"><span class="keyword">import</span> java.util.Map;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Created by tianwei on 2017/6/27.</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">IDataService</span> </span>{</span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 根据表名获取Vo模板要用的Map型数据</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> table</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="function">Map<String, Object> <span class="title">getDbTemplateData</span><span class="params">(Table table, List<Field> fieldList)</span></span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<ul>
<li>再增加 CodeGenerator 对查出的所有表进行循环生成</li>
</ul>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">package</span> com.hantianwei.generator.service.impl;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.hantianwei.generator.dao.DataDao;</span><br><span class="line"><span class="keyword">import</span> com.hantianwei.generator.model.Config;</span><br><span class="line"><span class="keyword">import</span> com.hantianwei.generator.model.Field;</span><br><span class="line"><span class="keyword">import</span> com.hantianwei.generator.model.Table;</span><br><span class="line"><span class="keyword">import</span> com.hantianwei.generator.service.ICodeGenerator;</span><br><span class="line"><span class="keyword">import</span> com.hantianwei.generator.util.FreeMakerUtil;</span><br><span class="line"><span class="keyword">import</span> com.hantianwei.generator.util.StringUtil;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"><span class="keyword">import</span> java.util.Map;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Created by tianwei on 2017/6/26.</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">CodeGenerator</span> <span class="keyword">implements</span> <span class="title">ICodeGenerator</span> </span>{</span><br><span class="line"></span><br><span class="line"> DataDao dataDao = <span class="keyword">new</span> DataDao();</span><br><span class="line"></span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">static</span> FreeMakerUtil freeMakerUtil = <span class="keyword">new</span> FreeMakerUtil();</span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">static</span> DataService dataService = <span class="keyword">new</span> DataService();</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">generateFile</span><span class="params">(String templateName, Table table, List<Field> fieldList, String packagePath, String fileName)</span> </span>{</span><br><span class="line"> generateFileWithDb(templateName, table, fieldList, packagePath, fileName);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">generateFileWithDb</span><span class="params">(String templateName, Table table, List<Field> fieldList, String packagePath, String fileName)</span> </span>{</span><br><span class="line"> Map<String, Object> templateData = dataService.getDbTemplateData(table, fieldList);</span><br><span class="line"> freeMakerUtil.generateFile(templateName, templateData, packagePath + fileName);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">generatorAllTables</span><span class="params">()</span> </span>{</span><br><span class="line"> List<Table> tables = dataDao.getAllTables(Config.DB_NAME);</span><br><span class="line"> <span class="keyword">for</span> (Table table : tables) {</span><br><span class="line"> <span class="keyword">long</span> startTime = System.currentTimeMillis();</span><br><span class="line"> List<Field> fieldList = dataDao.getAllColums(Config.DB_NAME, table.getName());</span><br><span class="line"> String tableName = StringUtil.toUpperCaseFirstOne(table.getProName());</span><br><span class="line"> <span class="comment">//这里可改为枚举控制</span></span><br><span class="line"> generateFile(<span class="string">"ModelTemplate.ftl"</span>, table, fieldList, Config.PACKAGE_MODEL_PATH, String.format(<span class="string">"%s.java"</span>, tableName));</span><br><span class="line"> generateFile(<span class="string">"DaoTemplate.ftl"</span>, table, fieldList, Config.PACKAGE_DAO_PATH, String.format(<span class="string">"%s%s.java"</span>, tableName, StringUtil.toUpperCaseFirstOne(Config.PACKAGE_DAO_NAME)));</span><br><span class="line"> generateFile(<span class="string">"ServiceTemplate.ftl"</span>, table, fieldList, Config.PACKAGE_SERVICE_PATH, String.format(<span class="string">"%s%s.java"</span>, tableName,StringUtil.toUpperCaseFirstOne(Config.PACKAGE_SERVICE_NAME)));</span><br><span class="line"> generateFile(<span class="string">"IServiceTemplate.ftl"</span>, table, fieldList, Config.PACKAGE_ISERVICE_PATH, String.format(<span class="string">"I%s%s.java"</span>, tableName,StringUtil.toUpperCaseFirstOne(Config.PACKAGE_SERVICE_NAME)));</span><br><span class="line"> generateFile(<span class="string">"MapperTemplate.ftl"</span>, table, fieldList, Config.PACKAGE_MAPPER_PATH, String.format(<span class="string">"%sMapper.xml"</span>, tableName));</span><br><span class="line"> <span class="keyword">long</span> endTime = System.currentTimeMillis();</span><br><span class="line"> System.out.println(String.format(<span class="string">"[%s]表生成完毕,用时[%d]ms"</span>, tableName, endTime - startTime));</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">package</span> com.hantianwei.generator.service;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.hantianwei.generator.model.Field;</span><br><span class="line"><span class="keyword">import</span> com.hantianwei.generator.model.Table;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Created by tianwei on 2017/6/29.</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">ICodeGenerator</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">void</span> <span class="title">generateFile</span><span class="params">(String templateName, Table table, List<Field> fieldList, String packagePath, String fileName)</span></span>;</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">void</span> <span class="title">generateFileWithDb</span><span class="params">(String templateName, Table table, List<Field> fieldList, String packagePath, String fileName)</span></span>;</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">void</span> <span class="title">generatorAllTables</span><span class="params">()</span></span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<ul>
<li><h3 id="主方法"><a href="#主方法" class="headerlink" title="主方法"></a>主方法</h3><ul>
<li>主要程序都完工了,我们需要在Mian里面进行调用</li>
</ul>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">package</span> com.hantianwei.generator.main;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.hantianwei.generator.service.ICodeGenerator;</span><br><span class="line"><span class="keyword">import</span> com.hantianwei.generator.service.impl.CodeGenerator;</span><br><span class="line"><span class="keyword">import</span> com.hantianwei.generator.util.PropUtil;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Created by tianwei on 2017/6/27.</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">RunGenerator</span> </span>{</span><br><span 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>{</span><br><span class="line"> PropUtil.loadConf(args);</span><br><span class="line"></span><br><span class="line"> ICodeGenerator cg = <span class="keyword">new</span> CodeGenerator();</span><br><span class="line"> cg.generatorAllTables();</span><br><span class="line"></span><br><span class="line"> System.out.println(<span class="string">"****** OK ******"</span>);</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<ul>
<li>到此所有程序都已完工,我们下章开始实现模板</li>
</ul>
<ul>
<li>开源地址:<a href="https://github.com/hantianwei/jgenerator" target="_blank" rel="noopener">https://github.com/hantianwei/jgenerator</a></li>
</ul>
]]></content>
<categories>
<category>module</category>
<category>code-generator-freemarker</category>
</categories>
<tags>
<tag>freemarker</tag>
<tag>codeGenerator</tag>
<tag>mybatis</tag>
</tags>
</entry>
<entry>
<title>基于Freemarker的适用于Mybatis的代码生成器-(五)模板篇</title>
<url>/module/code-generator-freemarker/code-generator-freemarker-mybatis-template/</url>
<content><![CDATA[<h2 id="需求"><a href="#需求" class="headerlink" title="需求"></a>需求</h2><ul>
<li>通用框架有了,最后我们要提供基于FreeMarker的模板</li>
</ul>
<h2 id="实现"><a href="#实现" class="headerlink" title="实现"></a>实现</h2><ul>
<li>这里就不一一说了,提供一下我写的几个模板</li>
<li>实体</li>
</ul>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">package</span> ${config.PACKAGE_ROOT_NAME}.${config.PACKAGE_MODEL_NAME};</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.math.BigDecimal;</span><br><span class="line"><span class="keyword">import</span> java.sql.Time;</span><br><span class="line"><span class="keyword">import</span> java.util.Date;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">* -------------------------------------------------------------------------------</span></span><br><span class="line"><span class="comment">* 功能描述: 代码处成器生成</span></span><br><span class="line"><span class="comment">*</span></span><br><span class="line"><span class="comment">* 创建人: 代码生成</span></span><br><span class="line"><span class="comment">* 创建日期: ${.now}</span></span><br><span class="line"><span class="comment">* 创建说明: ${ tableProName?cap_first }实体</span></span><br><span class="line"><span class="comment">*</span></span><br><span class="line"><span class="comment">* 修改人: </span></span><br><span class="line"><span class="comment">* 修改日期:</span></span><br><span class="line"><span class="comment">* 修改说明:</span></span><br><span class="line"><span class="comment">*</span></span><br><span class="line"><span class="comment">* -------------------------------------------------------------------------------</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> $</span>{ tableProName?cap_first } {</span><br><span class="line"></span><br><span class="line"> <#list fieldList as pro></span><br><span class="line"> <span class="keyword">private</span> ${pro.type} ${pro.proName}; <span class="comment">//${pro.description}</span></span><br><span class="line"> </#list></span><br><span class="line"></span><br><span class="line"> <#list fieldList as pro></span><br><span class="line"> <span class="comment">//${pro.description}</span></span><br><span class="line"> <span class="keyword">public</span> ${pro.type} get${pro.proName?cap_first}() {</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">this</span>.${pro.proName};</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> set${pro.proName?cap_first}(${pro.type} ${pro.proName}) {</span><br><span class="line"> <#if config.TRIM_STRINGS && pro.type=="String"></span><br><span class="line"> <span class="keyword">this</span>.${pro.proName} = ${pro.proName} == <span class="keyword">null</span> ? <span class="keyword">null</span> : ${pro.proName}.trim();</span><br><span class="line"> <#else ></span><br><span class="line"> <span class="keyword">this</span>.${pro.proName} = ${pro.proName};</span><br><span class="line"> </#if></span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> </#list></span><br><span class="line"></span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<ul>
<li>数据层</li>
</ul>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">package</span> ${config.PACKAGE_ROOT_NAME}.${config.PACKAGE_DAO_NAME};</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> ${config.PACKAGE_ROOT_NAME}.${config.PACKAGE_MODEL_NAME}.${tableProName?cap_first};</span><br><span class="line"><span class="keyword">import</span> org.apache.ibatis.annotations.Param;</span><br><span class="line"><span class="keyword">import</span> org.springframework.stereotype.Repository;</span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">* -------------------------------------------------------------------------------</span></span><br><span class="line"><span class="comment">* 功能描述: 代码处成器生成</span></span><br><span class="line"><span class="comment">*</span></span><br><span class="line"><span class="comment">* 创建人: 代码生成</span></span><br><span class="line"><span class="comment">* 创建日期: ${.now}</span></span><br><span class="line"><span class="comment">* 创建说明: ${ tableProName?cap_first }数据操作</span></span><br><span class="line"><span class="comment">*</span></span><br><span class="line"><span class="comment">* 修改人: </span></span><br><span class="line"><span class="comment">* 修改日期:</span></span><br><span class="line"><span class="comment">* 修改说明:</span></span><br><span class="line"><span class="comment">*</span></span><br><span class="line"><span class="comment">* -------------------------------------------------------------------------------</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"><span class="meta">@Repository</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> $</span>{tableProName?cap_first}Dao {</span><br><span class="line"></span><br><span class="line"> ${fieldPk.type} add(<span class="meta">@Param</span>(<span class="string">"${tableProName}"</span>) ${tableProName?cap_first} ${tableProName});</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">void</span> <span class="title">update</span><span class="params">(@Param(<span class="string">"${tableProName}"</span>)</span> $</span>{tableProName?cap_first} ${tableProName});</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">void</span> <span class="title">remove</span><span class="params">(@Param(<span class="string">"${fieldPk.proName}"</span>)</span> $</span>{fieldPk.type} ${fieldPk.proName});</span><br><span class="line"></span><br><span class="line"> ${tableProName?cap_first} getByUserId(<span class="meta">@Param</span>(<span class="string">"${fieldPk.proName}"</span>) ${fieldPk.type} ${fieldPk.proName});</span><br><span class="line"></span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<ul>
<li>service</li>
</ul>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">package</span> ${config.PACKAGE_ROOT_NAME}.${config.PACKAGE_SERVICE_NAME}.impl;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">* -------------------------------------------------------------------------------</span></span><br><span class="line"><span class="comment">* 功能描述: 代码处成器生成</span></span><br><span class="line"><span class="comment">*</span></span><br><span class="line"><span class="comment">* 创建人: 代码生成</span></span><br><span class="line"><span class="comment">* 创建日期: ${.now}</span></span><br><span class="line"><span class="comment">* 创建说明: ${ tableProName?cap_first }业务逻辑实现</span></span><br><span class="line"><span class="comment">*</span></span><br><span class="line"><span class="comment">* 修改人: </span></span><br><span class="line"><span class="comment">* 修改日期:</span></span><br><span class="line"><span class="comment">* 修改说明:</span></span><br><span class="line"><span class="comment">*</span></span><br><span class="line"><span class="comment">* -------------------------------------------------------------------------------</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> ${config.PACKAGE_ROOT_NAME}.${config.PACKAGE_MODEL_NAME}.${tableProName?cap_first};</span><br><span class="line"><span class="keyword">import</span> ${config.PACKAGE_ROOT_NAME}.${config.PACKAGE_DAO_NAME}.${tableProName?cap_first}Dao;</span><br><span class="line"><span class="keyword">import</span> ${config.PACKAGE_ROOT_NAME}.${config.PACKAGE_ISERVICE_NAME}.I${tableProName?cap_first}Service;</span><br><span class="line"><span class="keyword">import</span> org.springframework.stereotype.Service;</span><br><span class="line"><span class="keyword">import</span> org.springframework.transaction.annotation.Transactional;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> javax.annotation.Resource;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Service</span></span><br><span class="line"><span class="meta">@Transactional</span>(rollbackFor = Exception.class)</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> $</span>{tableProName?cap_first}Service implements I${tableProName?cap_first}Service {</span><br><span class="line"></span><br><span class="line"> <span class="meta">@Resource</span></span><br><span class="line"> <span class="keyword">private</span> ${tableProName?cap_first}Dao ${tableProName}Dao;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">public</span> ${tableProName?cap_first} add(${tableProName?cap_first} ${tableProName}){</span><br><span class="line"> ${fieldPk.type} ${fieldPk.proName} = ${tableProName}Dao.add(${tableProName});</span><br><span class="line"> ${tableProName}.set${fieldPk.proName?cap_first}(${fieldPk.proName});</span><br><span class="line"> <span class="keyword">return</span> ${tableProName};</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">update</span><span class="params">(${tableProName?cap_first} ${tableProName})</span></span>{</span><br><span class="line"> ${tableProName}Dao.update(${tableProName});</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">remove</span><span class="params">(${fieldPk.type} ${fieldPk.proName})</span></span>{</span><br><span class="line"> ${tableProName}Dao.remove(${fieldPk.proName});</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">public</span> User getBy${fieldPk.proName?cap_first}(${fieldPk.type} ${fieldPk.proName}){</span><br><span class="line"> <span class="keyword">return</span> ${tableProName}Dao.getBy${fieldPk.proName?cap_first}(${fieldPk.proName});</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<ul>
<li>IService</li>
</ul>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">package</span> ${config.PACKAGE_ROOT_NAME}.${config.PACKAGE_ISERVICE_NAME};</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">* -------------------------------------------------------------------------------</span></span><br><span class="line"><span class="comment">* 功能描述: 代码处成器生成</span></span><br><span class="line"><span class="comment">*</span></span><br><span class="line"><span class="comment">* 创建人: 代码生成</span></span><br><span class="line"><span class="comment">* 创建日期: ${.now}</span></span><br><span class="line"><span class="comment">* 创建说明: ${ tableProName?cap_first }业务逻辑接口</span></span><br><span class="line"><span class="comment">*</span></span><br><span class="line"><span class="comment">* 修改人: </span></span><br><span class="line"><span class="comment">* 修改日期:</span></span><br><span class="line"><span class="comment">* 修改说明:</span></span><br><span class="line"><span class="comment">*</span></span><br><span class="line"><span class="comment">* -------------------------------------------------------------------------------</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> ${config.PACKAGE_ROOT_NAME}.${config.PACKAGE_MODEL_NAME}.${tableProName?cap_first};</span><br><span class="line"><span class="keyword">import</span> org.springframework.stereotype.Repository;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Repository</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">I</span>$</span>{tableProName?cap_first}${config.PACKAGE_SERVICE_NAME?cap_first} {</span><br><span class="line"></span><br><span class="line"> ${tableProName?cap_first} add(${tableProName?cap_first} ${tableProName});</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">void</span> <span class="title">update</span><span class="params">(${tableProName?cap_first} ${tableProName})</span></span>;</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">void</span> <span class="title">remove</span><span class="params">(${fieldPk.type} ${fieldPk.proName})</span></span>;</span><br><span class="line"></span><br><span class="line"> ${tableProName?cap_first} getBy${fieldPk.proName?cap_first}(${fieldPk.type} ${fieldPk.proName});</span><br><span class="line"></span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<ul>
<li>Mapper</li>
</ul>
<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line"><?xml version="1.0" encoding="UTF-8"?></span><br><span class="line"><span class="meta"><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" ></span></span><br><span class="line"><span class="tag"><<span class="name">mapper</span> <span class="attr">namespace</span>=<span class="string">"package ${config.PACKAGE_ROOT_NAME}.${config.PACKAGE_DAO_NAME}.${tableName?cap_first}${config.PACKAGE_DAO_NAME?cap_first}"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">resultMap</span> <span class="attr">id</span>=<span class="string">"${tableName?cap_first}BaseMap"</span> <span class="attr">type</span>=<span class="string">"${config.PACKAGE_ROOT_NAME}.${config.PACKAGE_MODEL_NAME}.${tableName?cap_first}"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">#list</span> <span class="attr">fieldList</span> <span class="attr">as</span> <span class="attr">pro</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">#if</span> <span class="attr">pro.isPk</span> == <span class="string">1</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">id</span> <span class="attr">column</span>=<span class="string">"${pro.name}"</span> <span class="attr">property</span>=<span class="string">"${pro.proName}"</span> <span class="attr">jdbcType</span>=<span class="string">"${pro.jdbcType}"</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">#else</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">result</span> <span class="attr">column</span>=<span class="string">"${pro.name}"</span> <span class="attr">property</span>=<span class="string">"${pro.proName}"</span> <span class="attr">jdbcType</span>=<span class="string">"${pro.jdbcType}"</span>/></span></span><br><span class="line"> <span class="tag"></<span class="name">#if</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">#list</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">resultMap</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="tag"><<span class="name">insert</span> <span class="attr">id</span>=<span class="string">"add"</span> <span class="attr">useGeneratedKeys</span>=<span class="string">"true"</span> <span class="attr">keyProperty</span>=<span class="string">"${fieldPk.name}"</span> <span class="attr">parameterType</span>=<span class="string">"${config.PACKAGE_ROOT_NAME}.${config.PACKAGE_MODEL_NAME}.${tableName?cap_first}"</span>></span></span><br><span class="line"> INSERT INTO ${tableName}(<span class="tag"><<span class="name">#list</span> <span class="attr">fieldList</span> <span class="attr">as</span> <span class="attr">pro</span>></span><span class="tag"><<span class="name">#if</span> <span class="attr">pro.isIdentity</span>==<span class="string">0</span>></span>${pro.name}<span class="tag"><<span class="name">#if</span> <span class="attr">pro_has_next</span>></span>,<span class="tag"></<span class="name">#if</span>></span><span class="tag"></<span class="name">#if</span>></span><span class="tag"></<span class="name">#list</span>></span>) VALUES(<span class="tag"><<span class="name">#list</span> <span class="attr">fieldList</span> <span class="attr">as</span> <span class="attr">pro</span>></span><span class="tag"><<span class="name">#if</span> <span class="attr">pro.isIdentity</span>==<span class="string">0</span>></span>${r'#'}{${pro.proName}}<span class="tag"><<span class="name">#if</span> <span class="attr">pro_has_next</span>></span>,<span class="tag"></<span class="name">#if</span>></span><span class="tag"></<span class="name">#if</span>></span><span class="tag"></<span class="name">#list</span>></span>)</span><br><span class="line"> <span class="tag"></<span class="name">insert</span>></span></span><br><span class="line"></span><br><span 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">"${config.PACKAGE_ROOT_NAME}.${config.PACKAGE_MODEL_NAME}.${tableName?cap_first}"</span>></span></span><br><span class="line"> UPDATE</span><br><span class="line"> ${tableName}</span><br><span class="line"> SET</span><br><span class="line"> <span class="tag"><<span class="name">#list</span> <span class="attr">fieldList</span> <span class="attr">as</span> <span class="attr">pro</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">#if</span> <span class="attr">pro.isIdentity</span>==<span class="string">0</span>></span></span><br><span class="line"> ${pro.name} = ${r'#'}{${pro.proName}}<span class="tag"><<span class="name">#if</span> <span class="attr">pro_has_next</span>></span>,<span class="tag"></<span class="name">#if</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">#if</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">#list</span>></span></span><br><span class="line"> WHERE</span><br><span class="line"> ${fieldPk.name}=${r'#'}{${fieldPk.proName}}</span><br><span class="line"> <span class="tag"></<span class="name">update</span>></span></span><br><span class="line"></span><br><span 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">"${fieldPk.type}"</span>></span></span><br><span class="line"> DELETE FROM ${tableName} WHERE ${fieldPk.name}=${r'#'}{${fieldPk.proName}}</span><br><span class="line"> <span class="tag"></<span class="name">delete</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="tag"><<span class="name">select</span> <span class="attr">id</span>=<span class="string">"getBy${fieldPk.proName?cap_first}"</span> <span class="attr">parameterType</span>=<span class="string">"java.lang.Long"</span> <span class="attr">resultMap</span>=<span class="string">"${tableName?cap_first}BaseMap"</span>></span></span><br><span class="line"> SELECT</span><br><span class="line"> <span class="tag"><<span class="name">#list</span> <span class="attr">fieldList</span> <span class="attr">as</span> <span class="attr">pro</span>></span></span><br><span class="line"> ${pro.name}<span class="tag"><<span class="name">#if</span> <span class="attr">pro_has_next</span>></span>,<span class="tag"></<span class="name">#if</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">#list</span>></span></span><br><span class="line"> FROM</span><br><span class="line"> ${tableName}</span><br><span class="line"> WHERE</span><br><span class="line"> ${fieldPk.name}=${r'#'}{${fieldPk.proName}}</span><br><span class="line"> <span class="tag"></<span class="name">select</span>></span></span><br><span class="line"></span><br><span class="line"><span class="tag"></<span class="name">mapper</span>></span></span><br></pre></td></tr></table></figure>
<ul>
<li>可以正式试用了,也申请了提交Maven中央仓库</li>
</ul>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line">java -jar jgenerator.jar config.properties</span><br></pre></td></tr></table></figure>
<ul>
<li>开源地址:<a href="https://github.com/hantianwei/jgenerator" target="_blank" rel="noopener">https://github.com/hantianwei/jgenerator</a></li>
</ul>
]]></content>
<categories>
<category>module</category>
<category>code-generator-freemarker</category>
</categories>
<tags>
<tag>freemarker</tag>
<tag>codeGenerator</tag>
<tag>mybatis</tag>
</tags>
</entry>
<entry>
<title>这些 idea 插件让开发效率提高N倍</title>
<url>/java/tool/idea-plugin-recommend/</url>
<content><![CDATA[<h1 id="这些-idea-插件让开发效率提高N倍"><a href="#这些-idea-插件让开发效率提高N倍" class="headerlink" title="这些 idea 插件让开发效率提高N倍"></a>这些 idea 插件让开发效率提高N倍</h1><h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><blockquote>
<p>idea 有很多非常好用的插件,用好了这些插件能够极大的提高开发效率</p>
</blockquote>
<h2 id="IDEA-插件如何安装"><a href="#IDEA-插件如何安装" class="headerlink" title="IDEA 插件如何安装"></a>IDEA 插件如何安装</h2><ul>
<li><img src="../../../images/post/2020/java/tool/idea-plugin-recommend/setting.jpg" alt="setting"></li>
<li>打开 idea 的设置页面,选择 Plugins 选项即可搜索和安装插件</li>
</ul>
<h2 id="插件推荐"><a href="#插件推荐" class="headerlink" title="插件推荐"></a>插件推荐</h2><h3 id="1-JRebel-for-IntelliJ"><a href="#1-JRebel-for-IntelliJ" class="headerlink" title="1. JRebel for IntelliJ"></a>1. JRebel for IntelliJ</h3><ul>
<li><p><img src="../../../images/post/2020/java/tool/idea-plugin-recommend/JRebelforIntelliJ.jpg" alt="JRebelforIntelliJ"></p>
</li>
<li><p>日常开发中,当你修改任意一个 java 文件时,tomcat 并不能将此文件的修改实时编译并反映到运行的项目中去,所以只能重启项目。这样做非常耗时和麻烦。使用 JRebel 之后只要选择 Bulid 中的 Build Project 选项即可热部署项目,用着非常爽,<strong>强烈推荐</strong>。</p>
</li>
<li><p>具体安装和使用参考之前的文章: idea 热部署插件 jrebel,开发必备</p>
</li>
</ul>
<h3 id="2-Codota"><a href="#2-Codota" class="headerlink" title="2.Codota"></a>2.Codota</h3><blockquote>
<p>Codota 是一款智能 AI 代码补全插件,它从数百万 Java 程序中学习代码,最终能根据程序上下文提示并补全代码,帮助开发者减少失误,提升工作效率,并且它还是免费的。</p>
</blockquote>
<ul>
<li><img src="../../../images/post/2020/java/tool/idea-plugin-recommend/Codota.jpg" alt="Codota"></li>
</ul>
<h3 id="3-Translation"><a href="#3-Translation" class="headerlink" title="3. Translation"></a>3. Translation</h3><blockquote>
<p>翻译插件,有了它妈妈再也不用担心我的英语渣了</p>
</blockquote>
<ul>
<li><p><img src="../../../images/post/2020/java/tool/idea-plugin-recommend/translation.jpg" alt="translation"></p>
</li>
<li><p>常用的两个功能</p>
<blockquote>
<ul>
<li>翻译, 就是中英翻译, 可以当词典用</li>
<li>翻译替换, 就是将原文直接翻译替换, 起变量名字时非常有用: 起个中文名, 然后翻译替换就好了.</li>
</ul>
</blockquote>
</li>
</ul>
<h3 id="4-RestfulToolkit"><a href="#4-RestfulToolkit" class="headerlink" title="4. RestfulToolkit"></a>4. RestfulToolkit</h3><blockquote>
<p>这款插件可以根据 URL 直接跳转到对应的方法。</p>
</blockquote>
<ul>
<li><p><img src="../../../images/post/2020/java/tool/idea-plugin-recommend/RestfulToolkit.jpg" alt="RestfulToolkit"></p>
</li>
<li><p>快捷键</p>
<blockquote>
<ul>
<li>windows:ctrl + \</li>
<li>mac:cmd + \</li>
</ul>
</blockquote>
</li>
</ul>
<h3 id="5-Lombok"><a href="#5-Lombok" class="headerlink" title="5. Lombok"></a>5. Lombok</h3><blockquote>
<ul>
<li>在过往的 Java 项目中,充斥着太多不友好的代码:POJO 的 getter/setter/toString;异常处理;I/O 流的关闭操作等等,这些样板代码既没有技术含量,又影响着代码的美观,Lombok 应运而生。它让代码变得非常简洁。</li>
<li>这个插件也充斥着先多的争议,有人认为该写的getter/setter还是生成到代码中的好,有人认为这样代码整洁,依个人观点使用</li>
</ul>
</blockquote>
<p><img src="../../../images/post/2020/java/tool/idea-plugin-recommend/lombok.jpg" alt="lombok"></p>
<h3 id="6-GenerateAllSetter"><a href="#6-GenerateAllSetter" class="headerlink" title="6.GenerateAllSetter"></a>6.GenerateAllSetter</h3><blockquote>
<p>一键调用一个对象的所有 set 方法并且赋予默认值 在对象字段多的时候非常方便</p>
</blockquote>
<p><img src="../../../images/post/2020/java/tool/idea-plugin-recommend/generateallsetter.gif" alt="generateallsetter"></p>
<h3 id="7-Alibaba-Cloud-Toolkit"><a href="#7-Alibaba-Cloud-Toolkit" class="headerlink" title="7. Alibaba Cloud Toolkit"></a>7. Alibaba Cloud Toolkit</h3><blockquote>
<p>Cloud Toolkit 帮助开发者将本地应用程序一键部署到线下自有 VM,或阿里云 ECS、EDAS 和 Kubernetes 中去。内置终端 Terminal、文件上传、数据库 SQL Console 能功能。用它来部署项目非常方便。</p>
</blockquote>
<h3 id="8-Properties-to-YAML-Converter"><a href="#8-Properties-to-YAML-Converter" class="headerlink" title="8. Properties to YAML Converter"></a>8. Properties to YAML Converter</h3><blockquote>
<p>将 Properties 配置文件转换为 YAML 配置文件</p>
<ul>
<li>首先选择属性文件。</li>
<li>在属性文件上单击鼠标右键后,在菜单中使用“转换”操作。</li>
</ul>
</blockquote>
<h3 id="9-Free-MyBatis-plugin"><a href="#9-Free-MyBatis-plugin" class="headerlink" title="9.Free MyBatis plugin"></a>9.Free MyBatis plugin</h3><blockquote>
<p>free-idea-mybatis是一款增强idea对mybatis支持的插件,主要功能如下:</p>
<ul>
<li>生成mapper xml文件</li>
<li>快速从代码跳转到mapper及从mapper返回代码</li>
<li>mybatis自动补全及语法错误提示</li>
<li>集成mybatis generator gui界面</li>
</ul>
</blockquote>
]]></content>
<categories>
<category>java</category>
<category>tool</category>
</categories>
<tags>
<tag>idea</tag>
<tag>plugin</tag>
</tags>
</entry>
<entry>
<title>git命令大全</title>
<url>/life/git-command-collect/</url>
<content><![CDATA[<h1 id="git命令大全"><a href="#git命令大全" class="headerlink" title="git命令大全"></a>git命令大全</h1><h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><blockquote>
<p>做为开发者,目前拉触是多的版本管理系统就是GIT了,使用过程中也有很多的界面化工具,但为了提高效率等也会经常用到GIT的命令,收藏以下常用命令供自已和大家查阅</p>
</blockquote>
<ul>
<li><p><img src="../../images/post/2020/life/git-command-collect/git-flow-chart.jpg" alt="git-flow-chart"></p>
</li>
<li><p><img src="../../images/post/2020/life/git-command-collect/git-command-all.jpg" alt="git-command-all"></p>
</li>
</ul>
<h2 id="专用名词"><a href="#专用名词" class="headerlink" title="专用名词"></a>专用名词</h2><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">Workspace:工作区</span><br><span class="line">Index / Stage:暂存区</span><br><span class="line">Repository:仓库区(或本地仓库)</span><br><span class="line">Remote:远程仓库</span><br></pre></td></tr></table></figure>
<h2 id="常用命令"><a href="#常用命令" class="headerlink" title="常用命令"></a>常用命令</h2><h3 id="一、新建代码库"><a href="#一、新建代码库" class="headerlink" title="一、新建代码库"></a>一、新建代码库</h3><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line"># 在当前目录新建一个Git代码库</span><br><span class="line">$ git init</span><br><span class="line"># 新建一个目录,将其初始化为Git代码库</span><br><span class="line">$ git init [project-name]</span><br><span class="line"># 下载一个项目和它的整个代码历史</span><br><span class="line">$ git clone [url]</span><br></pre></td></tr></table></figure>
<h3 id="二、配置"><a href="#二、配置" class="headerlink" title="二、配置"></a>二、配置</h3><blockquote>
<p>Git的设置文件为.gitconfig,它可以在用户主目录下(全局配置),也可以在项目目录下(项目配置)。</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line"># 显示当前的Git配置</span><br><span class="line">$ git config --list</span><br><span class="line"># 编辑Git配置文件</span><br><span class="line">$ git config -e [--global]</span><br><span class="line"># 设置提交代码时的用户信息</span><br><span class="line">$ git config [--global] user.name "[name]"</span><br><span class="line">$ git config [--global] user.email "[email address]”</span><br><span class="line"></span><br><span class="line">git 修改当前的project的用户名的命令为:</span><br><span class="line">> git config user.name 你的目标用户名;</span><br><span class="line">git修改当前的project提交邮箱的命令为:</span><br><span class="line">> git config user.email 你的目标邮箱名;</span><br><span class="line">如果你要修改当前全局的用户名和邮箱时,需要在上面的两条命令中添加一个参数,–global,代表的是全局。</span><br><span class="line">命令分别为:</span><br><span class="line">> git config --global user.name 你的目标用户名;</span><br><span class="line">> git config --global user.email 你的目标邮箱名;</span><br></pre></td></tr></table></figure>
<h3 id="三、增加-删除文件"><a href="#三、增加-删除文件" class="headerlink" title="三、增加/删除文件"></a>三、增加/删除文件</h3><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line"># 添加指定文件到暂存区</span><br><span class="line">$ git add [file1] [file2] ...</span><br><span class="line"># 添加指定目录到暂存区,包括子目录</span><br><span class="line">$ git add [dir]</span><br><span class="line"># 添加当前目录的所有文件到暂存区</span><br><span class="line">$ git add .</span><br><span class="line"># 添加每个变化前,都会要求确认</span><br><span class="line"># 对于同一个文件的多处变化,可以实现分次提交</span><br><span class="line">$ git add -p</span><br><span class="line"># 删除工作区文件,并且将这次删除放入暂存区</span><br><span class="line">$ git rm [file1] [file2] ...</span><br><span class="line"># 停止追踪指定文件,但该文件会保留在工作区</span><br><span class="line">$ git rm --cached [file]</span><br><span class="line"># 改名文件,并且将这个改名放入暂存区</span><br><span class="line">$ git mv [file-original] [file-renamed]</span><br></pre></td></tr></table></figure>
<h3 id="四、代码提交"><a href="#四、代码提交" class="headerlink" title="四、代码提交"></a>四、代码提交</h3><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line"># 提交暂存区到仓库区</span><br><span class="line">$ git commit -m [message]</span><br><span class="line"># 提交暂存区的指定文件到仓库区</span><br><span class="line">$ git commit [file1] [file2] ... -m [message]</span><br><span class="line"># 提交工作区自上次commit之后的变化,直接到仓库区</span><br><span class="line">$ git commit -a</span><br><span class="line"># 提交时显示所有diff信息</span><br><span class="line">$ git commit -v</span><br><span class="line"># 使用一次新的commit,替代上一次提交</span><br><span class="line"># 如果代码没有任何新变化,则用来改写上一次commit的提交信息</span><br><span class="line">$ git commit --amend -m [message]</span><br><span class="line"># 重做上一次commit,并包括指定文件的新变化</span><br><span class="line">$ git commit --amend [file1] [file2] ...</span><br></pre></td></tr></table></figure>
<h3 id="五、分支"><a href="#五、分支" class="headerlink" title="五、分支"></a>五、分支</h3><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line"># 列出所有本地分支</span><br><span class="line">$ git branch</span><br><span class="line"># 列出所有远程分支</span><br><span class="line">$ git branch -r</span><br><span class="line"># 列出所有本地分支和远程分支</span><br><span class="line">$ git branch -a</span><br><span class="line"># 新建一个分支,但依然停留在当前分支</span><br><span class="line">$ git branch [branch-name]</span><br><span class="line"># 新建一个分支,并切换到该分支</span><br><span class="line">$ git checkout -b [branch]</span><br><span class="line">git checkout -b appoint_box(别名) origin/feature/20181128_1491627_appoint_box_1(分支名)</span><br><span class="line"># 新建一个分支,指向指定commit</span><br><span class="line">$ git branch [branch] [commit]</span><br><span class="line"># 新建一个分支,与指定的远程分支建立追踪关系</span><br><span class="line">$ git branch --track [branch] [remote-branch]</span><br><span class="line"># 切换到指定分支,并更新工作区</span><br><span class="line">$ git checkout [branch-name]</span><br><span class="line"># 切换到上一个分支</span><br><span class="line">$ git checkout -</span><br><span class="line"># 建立追踪关系,在现有分支与指定的远程分支之间</span><br><span class="line">$ git branch --set-upstream [branch] [remote-branch]</span><br><span class="line"># 合并指定分支到当前分支</span><br><span class="line">$ git merge [branch]</span><br><span class="line"># 选择一个commit,合并进当前分支</span><br><span class="line">$ git cherry-pick [commit]</span><br><span class="line"># 删除分支</span><br><span class="line">$ git branch -d [branch-name]</span><br><span class="line"># 删除远程分支</span><br><span class="line">$ git push origin --delete [branch-name]</span><br><span class="line">$ git branch -dr [remote/branch]</span><br></pre></td></tr></table></figure>
<h3 id="六、标签"><a href="#六、标签" class="headerlink" title="六、标签"></a>六、标签</h3><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line"># 列出所有tag</span><br><span class="line">$ git tag</span><br><span class="line"># 新建一个tag在当前commit</span><br><span class="line">$ git tag [tag]</span><br><span class="line"># 新建一个tag在指定commit</span><br><span class="line">$ git tag [tag] [commit]</span><br><span class="line"># 删除本地tag</span><br><span class="line">$ git tag -d [tag]</span><br><span class="line"># 删除远程tag</span><br><span class="line">$ git push origin :refs/tags/[tagName]</span><br><span class="line"># 查看tag信息</span><br><span class="line">$ git show [tag]</span><br><span class="line"># 提交指定tag</span><br><span class="line">$ git push [remote] [tag]</span><br><span class="line"># 提交所有tag</span><br><span class="line">$ git push [remote] --tags</span><br><span class="line"># 新建一个分支,指向某个tag</span><br><span class="line">$ git checkout -b [branch] [tag]</span><br></pre></td></tr></table></figure>
<h3 id="七、查看信息"><a href="#七、查看信息" class="headerlink" title="七、查看信息"></a>七、查看信息</h3><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line"># 显示有变更的文件</span><br><span class="line">$ git status</span><br><span class="line"># 显示当前分支的版本历史</span><br><span class="line">$ git log</span><br><span class="line"># 显示commit历史,以及每次commit发生变更的文件</span><br><span class="line">$ git log --stat</span><br><span class="line"># 搜索提交历史,根据关键词</span><br><span class="line">$ git log -S [keyword]</span><br><span class="line"># 显示某个commit之后的所有变动,每个commit占据一行</span><br><span class="line">$ git log [tag] HEAD --pretty=format:%s</span><br><span class="line"># 显示某个commit之后的所有变动,其"提交说明"必须符合搜索条件</span><br><span class="line">$ git log [tag] HEAD --grep feature</span><br><span class="line"># 显示某个文件的版本历史,包括文件改名</span><br><span class="line">$ git log --follow [file]</span><br><span class="line">$ git whatchanged [file]</span><br><span class="line"># 显示指定文件相关的每一次diff</span><br><span class="line">$ git log -p [file]</span><br><span class="line"># 显示过去5次提交</span><br><span class="line">$ git log -5 --pretty --oneline</span><br><span class="line"># 显示所有提交过的用户,按提交次数排序</span><br><span class="line">$ git shortlog -sn</span><br><span class="line"># 显示指定文件是什么人在什么时间修改过</span><br><span class="line">$ git blame [file]</span><br><span class="line"># 显示暂存区和工作区的代码差异</span><br><span class="line">$ git diff</span><br><span class="line"># 显示暂存区和上一个commit的差异</span><br><span class="line">$ git diff --cached [file]</span><br><span class="line"># 显示工作区与当前分支最新commit之间的差异</span><br><span class="line">$ git diff HEAD</span><br><span class="line"># 显示两次提交之间的差异</span><br><span class="line">$ git diff [first-branch]...[second-branch]</span><br><span class="line"># 显示今天你写了多少行代码</span><br><span class="line">$ git diff --shortstat "@{0 day ago}"</span><br><span class="line"># 显示某次提交的元数据和内容变化</span><br><span class="line">$ git show [commit]</span><br><span class="line"># 显示某次提交发生变化的文件</span><br><span class="line">$ git show --name-only [commit]</span><br><span class="line"># 显示某次提交时,某个文件的内容</span><br><span class="line">$ git show [commit]:[filename]</span><br><span class="line"># 显示当前分支的最近几次提交</span><br><span class="line">$ git reflog</span><br><span class="line">可以得到cimmit id</span><br><span class="line"># 从本地master拉取代码更新当前分支:branch 一般为master</span><br><span class="line">$ git rebase [branch]</span><br></pre></td></tr></table></figure>
<h3 id="八、远程同步"><a href="#八、远程同步" class="headerlink" title="八、远程同步"></a>八、远程同步</h3><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">$ git remote update --更新远程仓储</span><br><span class="line"># 下载远程仓库的所有变动</span><br><span class="line">$ git fetch [remote]</span><br><span class="line"># 显示所有远程仓库</span><br><span class="line">$ git remote -v</span><br><span class="line"># 显示某个远程仓库的信息</span><br><span class="line">$ git remote show [remote]</span><br><span class="line"># 增加一个新的远程仓库,并命名</span><br><span class="line">$ git remote add [shortname] [url]</span><br><span class="line"># 取回远程仓库的变化,并与本地分支合并</span><br><span class="line">$ git pull [remote] [branch]</span><br><span class="line"># 上传本地指定分支到远程仓库</span><br><span class="line">$ git push [remote] [branch]</span><br><span class="line"># 强行推送当前分支到远程仓库,即使有冲突</span><br><span class="line">$ git push [remote] --force</span><br><span class="line"># 推送所有分支到远程仓库</span><br><span class="line">$ git push [remote] --all</span><br></pre></td></tr></table></figure>
<h3 id="九、撤销"><a href="#九、撤销" class="headerlink" title="九、撤销"></a>九、撤销</h3><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line"># 恢复暂存区的指定文件到工作区</span><br><span class="line">$ git checkout [file]</span><br><span class="line"># 恢复某个commit的指定文件到暂存区和工作区</span><br><span class="line">$ git checkout [commit] [file]</span><br><span class="line"># 恢复暂存区的所有文件到工作区</span><br><span class="line">$ git checkout .</span><br><span class="line"># 重置暂存区的指定文件,与上一次commit保持一致,但工作区不变</span><br><span class="line">$ git reset [file]</span><br><span class="line"># 重置暂存区与工作区,与上一次commit保持一致</span><br><span class="line">$ git reset --hard</span><br><span class="line"># 重置当前分支的指针为指定commit,同时重置暂存区,但工作区不变</span><br><span class="line">$ git reset [commit]</span><br><span class="line"># 重置当前分支的HEAD为指定commit,同时重置暂存区和工作区,与指定commit一致</span><br><span class="line">$ git reset --hard [commit]</span><br><span class="line"># 重置当前HEAD为指定commit,但保持暂存区和工作区不变</span><br><span class="line">$ git reset --keep [commit]</span><br><span class="line"># 新建一个commit,用来撤销指定commit</span><br><span class="line"># 后者的所有变化都将被前者抵消,并且应用到当前分支</span><br><span class="line">$ git revert [commit]</span><br><span class="line"># 暂时将未提交的变化移除,稍后再移入</span><br><span class="line">$ git stash</span><br><span class="line">$ git stash pop</span><br></pre></td></tr></table></figure>
<h3 id="十、其他"><a href="#十、其他" class="headerlink" title="十、其他"></a>十、其他</h3><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line"># 生成一个可供发布的压缩包</span><br><span class="line">$ git archive</span><br></pre></td></tr></table></figure>
<h2 id="示例"><a href="#示例" class="headerlink" title="示例"></a>示例</h2><blockquote>
<p>上传本地项目到远程仓库</p>
</blockquote>
<h3 id="1-初使化仓库"><a href="#1-初使化仓库" class="headerlink" title="1.初使化仓库"></a>1.初使化仓库</h3><blockquote>
<p>(先进入项目文件夹)通过命令 git init 把这个目录变成git可以管理的仓库</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">git init</span><br></pre></td></tr></table></figure>
<h3 id="2-把文件添加到版本库"><a href="#2-把文件添加到版本库" class="headerlink" title="2.把文件添加到版本库"></a>2.把文件添加到版本库</h3><blockquote>
<p>使用命令 git add .添加到暂存区里面去,不要忘记后面的小数点“.”,意为添加文件夹下的所有文件</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">git add .</span><br></pre></td></tr></table></figure>
<h3 id="3-文件提交到仓库"><a href="#3-文件提交到仓库" class="headerlink" title="3.文件提交到仓库"></a>3.文件提交到仓库</h3><blockquote>
<p>用命令 git commit告诉Git,把文件提交到仓库。引号内为提交说明</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">git commit -m 'first commit'</span><br></pre></td></tr></table></figure>
<h3 id="4-关联到远程库"><a href="#4-关联到远程库" class="headerlink" title="4.关联到远程库"></a>4.关联到远程库</h3><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">git remote add origin 你的远程库地址</span><br></pre></td></tr></table></figure>
<h3 id="5-拉取且合并"><a href="#5-拉取且合并" class="headerlink" title="5.拉取且合并"></a>5.拉取且合并</h3><blockquote>
<p>获取远程库与本地同步合并(如果远程库不为空必须做这一步,否则后面的提交会失败)</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">git pull --rebase origin master</span><br></pre></td></tr></table></figure>
<h3 id="6-推送到远程"><a href="#6-推送到远程" class="headerlink" title="6.推送到远程"></a>6.推送到远程</h3><blockquote>
<p>使用 git push命令,实际上是把当前分支master推送到远程。执行此命令后会要求输入用户名、密码,验证通过后即开始上传。</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">git push -u origin master</span><br></pre></td></tr></table></figure>
<h3 id="7-状态查询"><a href="#7-状态查询" class="headerlink" title="7.状态查询"></a>7.状态查询</h3><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">git status</span><br></pre></td></tr></table></figure>
<h2 id="注意"><a href="#注意" class="headerlink" title="注意"></a>注意</h2><h3 id="一、fetch-vs-pull"><a href="#一、fetch-vs-pull" class="headerlink" title="一、fetch vs pull"></a>一、fetch vs pull</h3><ul>
<li><p>git fetch是将远程主机的最新内容拉到本地,用户在检查了以后决定是否合并到工作本机分支中。</p>
</li>
<li><p>git pull 则是将远程主机的最新内容拉下来后直接合并,即:git pull = git fetch + git merge,这样可能会产生冲突,需要手动解决。</p>
</li>
</ul>
]]></content>
<categories>
<category>life</category>
</categories>
<tags>
<tag>git</tag>
<tag>command</tag>
</tags>
</entry>
<entry>
<title>两数之和</title>
<url>/java/algorithm/two-number-sum-1/</url>
<content><![CDATA[<h1 id="两数之和"><a href="#两数之和" class="headerlink" title="两数之和"></a>两数之和</h1><h2 id="题目"><a href="#题目" class="headerlink" title="题目"></a>题目</h2><blockquote>
<p>给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。</p>
<p>你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。</p>
</blockquote>
<ul>
<li>示例</li>
</ul>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">给定 nums = [2, 7, 11, 15], target = 9</span><br><span class="line"></span><br><span class="line">因为 nums[0] + nums[1] = 2 + 7 = 9</span><br><span class="line">所以返回 [0, 1]</span><br></pre></td></tr></table></figure>
<h2 id="解题思路"><a href="#解题思路" class="headerlink" title="解题思路"></a>解题思路</h2><blockquote>
</blockquote>
<h2 id="代码实现"><a href="#代码实现" class="headerlink" title="代码实现"></a>代码实现</h2><figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">int</span>[] twoSum(<span class="keyword">int</span>[] nums, <span class="keyword">int</span> target) {</span><br><span class="line"> Map<Integer,Integer> map = <span class="keyword">new</span> HashMap<>();</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i<nums.length;i++){</span><br><span class="line"> <span class="keyword">int</span> complement = target - nums[i];</span><br><span class="line"> <span class="keyword">if</span>(map.containsKey(complement)){</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">new</span> <span class="keyword">int</span>[]{map.get(complement),i};</span><br><span class="line"> }</span><br><span class="line"> map.put(nums[i],i);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h2 id="性能分析"><a href="#性能分析" class="headerlink" title="性能分析"></a>性能分析</h2><h3 id="一、执行用时分布图表"><a href="#一、执行用时分布图表" class="headerlink" title="一、执行用时分布图表"></a>一、执行用时分布图表</h3><ul>
<li><img src="../../../images/post/2020/java/algorithm/two-number-sum-1/time.png" alt="time"></li>
</ul>
<h3 id="二、执行消耗内存分布图表"><a href="#二、执行消耗内存分布图表" class="headerlink" title="二、执行消耗内存分布图表"></a>二、执行消耗内存分布图表</h3><ul>
<li><img src="../../../images/post/2020/java/algorithm/two-number-sum-1/memory.png" alt="memory"></li>
</ul>
]]></content>
<categories>
<category>java</category>
<category>algorithm</category>
</categories>
<tags>
<tag>算法</tag>
<tag>面试</tag>
<tag>leetcode</tag>
<tag>腾讯</tag>
<tag>阿里巴巴</tag>
<tag>字节跳动</tag>
<tag>微软</tag>
<tag>苹果</tag>
</tags>
</entry>
<entry>
<title>Command line is too long. Shorten command line for SpringBootMainApplication or also for Application</title>
<url>/exception/springboot/springboot-main-pplication-command-line-long-shorten/</url>
<content><![CDATA[<h1 id="Command-line-is-too-long-Shorten-command-line-for-SpringBootMainApplication-or-also-for-Application"><a href="#Command-line-is-too-long-Shorten-command-line-for-SpringBootMainApplication-or-also-for-Application" class="headerlink" title="Command line is too long. Shorten command line for SpringBootMainApplication or also for Application"></a>Command line is too long. Shorten command line for SpringBootMainApplication or also for Application</h1><h2 id="问题"><a href="#问题" class="headerlink" title="问题"></a>问题</h2><blockquote>
<ul>
<li><p>Srping Boot 项目在启动报错</p>
</li>
<li><p>Error running ‘SpringBootMainApplication’: Command line is too long. Shorten command line for SpringBootMainApplication or also for Application default configuration.</p>
</li>
</ul>
</blockquote>
<ul>
<li><img src="../../../images/post/2020/exception/springboot/springboot-main-pplication-command-line-long-shorten/error-img.png" alt="error-img"></li>
</ul>
<h2 id="解决方案"><a href="#解决方案" class="headerlink" title="解决方案"></a>解决方案</h2><ul>
<li>修改 修改项目下 .idea\workspace.xml</li>
<li><img src="../../../images/post/2020/exception/springboot/springboot-main-pplication-command-line-long-shorten/idea-workspace.png" alt="idea-workspace"></li>
<li>找到节点 </li>
</ul>
<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line"><span class="tag"><<span class="name">component</span> <span class="attr">name</span>=<span class="string">"PropertiesComponent"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"RunOnceActivity.ShowReadmeOnStart"</span> <span class="attr">value</span>=<span class="string">"true"</span> /></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"WebServerToolWindowFactoryState"</span> <span class="attr">value</span>=<span class="string">"false"</span> /></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"aspect.path.notification.shown"</span> <span class="attr">value</span>=<span class="string">"true"</span> /></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"settings.editor.selected.configurable"</span> <span class="attr">value</span>=<span class="string">"preferences.pluginManager"</span> /></span></span><br><span class="line"><span class="tag"></<span class="name">component</span>></span></span><br></pre></td></tr></table></figure>
<ul>
<li>增加 </li>
</ul>
<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line"><span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"dynamic.classpath"</span> <span class="attr">value</span>=<span class="string">"true"</span> /></span></span><br></pre></td></tr></table></figure>
<ul>
<li>最后如</li>
</ul>
<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line"><span class="tag"><<span class="name">component</span> <span class="attr">name</span>=<span class="string">"PropertiesComponent"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"RunOnceActivity.ShowReadmeOnStart"</span> <span class="attr">value</span>=<span class="string">"true"</span> /></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"WebServerToolWindowFactoryState"</span> <span class="attr">value</span>=<span class="string">"false"</span> /></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"aspect.path.notification.shown"</span> <span class="attr">value</span>=<span class="string">"true"</span> /></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"dynamic.classpath"</span> <span class="attr">value</span>=<span class="string">"true"</span> /></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"settings.editor.selected.configurable"</span> <span class="attr">value</span>=<span class="string">"preferences.pluginManager"</span> /></span></span><br><span class="line"><span class="tag"></<span class="name">component</span>></span></span><br></pre></td></tr></table></figure>]]></content>
<categories>
<category>exception</category>
<category>springboot</category>
</categories>
<tags>
<tag>idea</tag>
<tag>exception</tag>
</tags>
</entry>
<entry>
<title>homebrew最新国内源安装方法</title>
<url>/life/macos-home-brew-install-cn/</url>
<content><![CDATA[<h1 id="Homebrew最新国内源安装方法"><a href="#Homebrew最新国内源安装方法" class="headerlink" title="Homebrew最新国内源安装方法"></a>Homebrew最新国内源安装方法</h1><h2 id="Homebrew是什么"><a href="#Homebrew是什么" class="headerlink" title="Homebrew是什么"></a>Homebrew是什么</h2><blockquote>
<p>Homebrew是一款Mac OS平台下的软件包管理工具,拥有安装、卸载、更新、查看、搜索等很多实用的功能。简单的一条指令,就可以实现包管理,而不用你关心各种依赖和文件路径的情况,十分方便快捷。</p>
<p>援引官方的一句话:又提示缺少套件啦?别担心,Homebrew 随时守候。Homebrew —— OS X 不可或缺的套件管理器。</p>
</blockquote>
<h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><ul>
<li>macOs中使用homebrew是很广泛,但是用官网的安装方法很慢且容易中断,想用国内的源进行安装。网上大部分的方法都是,然后修改源后,用ruby进行安装</li>
</ul>
<figure class="highlight shell"><table><tr><td class="code"><pre><span class="line">cd ~</span><br><span class="line">curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install >> brew_install</span><br></pre></td></tr></table></figure>
<ul>
<li>但是这个地址已经被“取缔”了,在浏览器输入这个地址显示以下内容</li>
</ul>
<figure class="highlight shell"><table><tr><td class="code"><pre><span class="line"><span class="meta">#</span>!/usr/bin/ruby</span><br><span class="line"></span><br><span class="line">STDERR.print <<~EOS</span><br><span class="line"> Warning: The Ruby Homebrew installer is now deprecated and has been rewritten in</span><br><span class="line"> Bash. Please migrate to the following command:</span><br><span class="line"> /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"</span><br><span class="line"></span><br><span class="line">EOS</span><br><span class="line"></span><br><span class="line">Kernel.exec "/bin/bash", "-c", '/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"'</span><br></pre></td></tr></table></figure>
<h2 id="Homebrew国内源安装"><a href="#Homebrew国内源安装" class="headerlink" title="Homebrew国内源安装"></a>Homebrew国内源安装</h2><ul>
<li>先下载安装脚本,或直接浏览器打开复制内容</li>
</ul>
<figure class="highlight shell"><table><tr><td class="code"><pre><span class="line">cd ~</span><br><span class="line">curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh >> brew_install</span><br></pre></td></tr></table></figure>
<ul>
<li>然后修改BREW_REPO的值</li>
</ul>
<figure class="highlight shell"><table><tr><td class="code"><pre><span class="line"><span class="meta">#</span>BREW_REPO="https://github.com/Homebrew/brew"</span><br><span class="line">BREW_REPO="git://mirrors.ustc.edu.cn/brew.git"</span><br></pre></td></tr></table></figure>
<ul>
<li>如果实在下载脚本都不能下载,就用以下脚本,不能保证最新版</li>
</ul>
<figure class="highlight shell"><table><tr><td class="code"><pre><span class="line"><span class="meta">#</span>!/bin/bash</span><br><span class="line">set -u</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> First check if the OS is Linux.</span><br><span class="line">if [[ "$(uname)" = "Linux" ]]; then</span><br><span class="line"> HOMEBREW_ON_LINUX=1</span><br><span class="line">fi</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> On macOS, this script installs to /usr/local only.</span><br><span class="line"><span class="meta">#</span> On Linux, it installs to /home/linuxbrew/.linuxbrew if you have sudo access</span><br><span class="line"><span class="meta">#</span> and ~/.linuxbrew otherwise.</span><br><span class="line"><span class="meta">#</span> To install elsewhere (which is unsupported)</span><br><span class="line"><span class="meta">#</span> you can untar https://github.com/Homebrew/brew/tarball/master</span><br><span class="line"><span class="meta">#</span> anywhere you like.</span><br><span class="line">if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then</span><br><span class="line"> HOMEBREW_PREFIX="/usr/local"</span><br><span class="line"> HOMEBREW_REPOSITORY="/usr/local/Homebrew"</span><br><span class="line"> HOMEBREW_CACHE="${HOME}/Library/Caches/Homebrew"</span><br><span class="line"></span><br><span class="line"> STAT="stat -f"</span><br><span class="line"> CHOWN="/usr/sbin/chown"</span><br><span class="line"> CHGRP="/usr/bin/chgrp"</span><br><span class="line"> GROUP="admin"</span><br><span class="line">else</span><br><span class="line"> HOMEBREW_PREFIX_DEFAULT="/home/linuxbrew/.linuxbrew"</span><br><span class="line"> HOMEBREW_CACHE="${HOME}/.cache/Homebrew"</span><br><span class="line"></span><br><span class="line"> STAT="stat --printf"</span><br><span class="line"> CHOWN="/bin/chown"</span><br><span class="line"> CHGRP="/bin/chgrp"</span><br><span class="line"> GROUP="$(id -gn)"</span><br><span class="line">fi</span><br><span class="line"><span class="meta">#</span>BREW_REPO="https://github.com/Homebrew/brew"</span><br><span class="line">BREW_REPO="git://mirrors.ustc.edu.cn/brew.git"</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> TODO: bump version when new macOS is released</span><br><span class="line">MACOS_LATEST_SUPPORTED="10.15"</span><br><span class="line"><span class="meta">#</span> TODO: bump version when new macOS is released</span><br><span class="line">MACOS_OLDEST_SUPPORTED="10.13"</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> no analytics during installation</span><br><span class="line">export HOMEBREW_NO_ANALYTICS_THIS_RUN=1</span><br><span class="line">export HOMEBREW_NO_ANALYTICS_MESSAGE_OUTPUT=1</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> string formatters</span><br><span class="line">if [[ -t 1 ]]; then</span><br><span class="line"> tty_escape() { printf "\033[%sm" "$1"; }</span><br><span class="line">else</span><br><span class="line"> tty_escape() { :; }</span><br><span class="line">fi</span><br><span class="line">tty_mkbold() { tty_escape "1;$1"; }</span><br><span class="line">tty_underline="$(tty_escape "4;39")"</span><br><span class="line">tty_blue="$(tty_mkbold 34)"</span><br><span class="line">tty_red="$(tty_mkbold 31)"</span><br><span class="line">tty_bold="$(tty_mkbold 39)"</span><br><span class="line">tty_reset="$(tty_escape 0)"</span><br><span class="line"></span><br><span class="line">have_sudo_access() {</span><br><span class="line"> if [[ -z "${HAVE_SUDO_ACCESS-}" ]]; then</span><br><span class="line"> /usr/bin/sudo -l mkdir &>/dev/null</span><br><span class="line"> HAVE_SUDO_ACCESS="$?"</span><br><span class="line"> fi</span><br><span class="line"></span><br><span class="line"> if [[ -z "${HOMEBREW_ON_LINUX-}" ]] && [[ "$HAVE_SUDO_ACCESS" -ne 0 ]]; then</span><br><span class="line"> abort "Need sudo access on macOS!"</span><br><span class="line"> fi</span><br><span class="line"></span><br><span class="line"> return "$HAVE_SUDO_ACCESS"</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">shell_join() {</span><br><span class="line"> local arg</span><br><span class="line"> printf "%s" "$1"</span><br><span class="line"> shift</span><br><span class="line"> for arg in "$@"; do</span><br><span class="line"> printf " "</span><br><span class="line"> printf "%s" "${arg// /\ }"</span><br><span class="line"> done</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">chomp() {</span><br><span class="line"> printf "%s" "${1/"$'\n'"/}"</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">ohai() {</span><br><span class="line"> printf "${tty_blue}==>${tty_bold} %s${tty_reset}\n" "$(shell_join "$@")"</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">warn() {</span><br><span class="line"> printf "${tty_red}Warning${tty_reset}: %s\n" "$(chomp "$1")"</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">abort() {</span><br><span class="line"> printf "%s\n" "$1"</span><br><span class="line"> exit 1</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">execute() {</span><br><span class="line"> if ! "$@"; then</span><br><span class="line"> abort "$(printf "Failed during: %s" "$(shell_join "$@")")"</span><br><span class="line"> fi</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">execute_sudo() {</span><br><span class="line"> local -a args=("$@")</span><br><span class="line"> if [[ -n "${SUDO_ASKPASS-}" ]]; then</span><br><span class="line"> args=("-A" "${args[@]}")</span><br><span class="line"> fi</span><br><span class="line"> if have_sudo_access; then</span><br><span class="line"> ohai "/usr/bin/sudo" "${args[@]}"</span><br><span class="line"> execute "/usr/bin/sudo" "${args[@]}"</span><br><span class="line"> else</span><br><span class="line"> ohai "${args[@]}"</span><br><span class="line"> execute "${args[@]}"</span><br><span class="line"> fi</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">getc() {</span><br><span class="line"> local save_state</span><br><span class="line"> save_state=$(/bin/stty -g)</span><br><span class="line"> /bin/stty raw -echo</span><br><span class="line"> IFS= read -r -n 1 -d '' "$@"</span><br><span class="line"> /bin/stty "$save_state"</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">wait_for_user() {</span><br><span class="line"> local c</span><br><span class="line"> echo</span><br><span class="line"> echo "Press RETURN to continue or any other key to abort"</span><br><span class="line"> getc c</span><br><span class="line"><span class="meta"> #</span> we test for \r and \n because some stuff does \r instead</span><br><span class="line"> if ! [[ "$c" == $'\r' || "$c" == $'\n' ]]; then</span><br><span class="line"> exit 1</span><br><span class="line"> fi</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">major_minor() {</span><br><span class="line"> echo "${1%%.*}.$(x="${1#*.}"; echo "${x%%.*}")"</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then</span><br><span class="line"> macos_version="$(major_minor "$(/usr/bin/sw_vers -productVersion)")"</span><br><span class="line">fi</span><br><span class="line"></span><br><span class="line">version_gt() {</span><br><span class="line"> [[ "${1%.*}" -gt "${2%.*}" ]] || [[ "${1%.*}" -eq "${2%.*}" && "${1#*.}" -gt "${2#*.}" ]]</span><br><span class="line">}</span><br><span class="line">version_ge() {</span><br><span class="line"> [[ "${1%.*}" -gt "${2%.*}" ]] || [[ "${1%.*}" -eq "${2%.*}" && "${1#*.}" -ge "${2#*.}" ]]</span><br><span class="line">}</span><br><span class="line">version_lt() {</span><br><span class="line"> [[ "${1%.*}" -lt "${2%.*}" ]] || [[ "${1%.*}" -eq "${2%.*}" && "${1#*.}" -lt "${2#*.}" ]]</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">should_install_git() {</span><br><span class="line"> if [[ $(command -v git) ]]; then</span><br><span class="line"> return 1</span><br><span class="line"> fi</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">should_install_command_line_tools() {</span><br><span class="line"> if [[ -n "${HOMEBREW_ON_LINUX-}" ]]; then</span><br><span class="line"> return 1</span><br><span class="line"> fi</span><br><span class="line"></span><br><span class="line"> if version_gt "$macos_version" "10.13"; then</span><br><span class="line"> ! [[ -e "/Library/Developer/CommandLineTools/usr/bin/git" ]]</span><br><span class="line"> else</span><br><span class="line"> ! [[ -e "/Library/Developer/CommandLineTools/usr/bin/git" ]] ||</span><br><span class="line"> ! [[ -e "/usr/include/iconv.h" ]]</span><br><span class="line"> fi</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">get_permission() {</span><br><span class="line"><span class="meta"> $</span>STAT "%A" "$1"</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">user_only_chmod() {</span><br><span class="line"> [[ -d "$1" ]] && [[ "$(get_permission "$1")" != "755" ]]</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">exists_but_not_writable() {</span><br><span class="line"> [[ -e "$1" ]] && ! [[ -r "$1" && -w "$1" && -x "$1" ]]</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">get_owner() {</span><br><span class="line"><span class="meta"> $</span>STAT "%u" "$1"</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">file_not_owned() {</span><br><span class="line"> [[ "$(get_owner "$1")" != "$(id -u)" ]]</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">get_group() {</span><br><span class="line"><span class="meta"> $</span>STAT "%g" "$1"</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">file_not_grpowned() {</span><br><span class="line"> [[ " $(id -G "$USER") " != *" $(get_group "$1") "* ]]</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> USER isn't always set so provide a fall back for the installer and subprocesses.</span><br><span class="line">if [[ -z "${USER-}" ]]; then</span><br><span class="line"> USER="$(chomp "$(id -un)")"</span><br><span class="line"> export USER</span><br><span class="line">fi</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> Invalidate sudo timestamp before exiting (if it wasn't active before).</span><br><span class="line">if ! /usr/bin/sudo -n -v 2>/dev/null; then</span><br><span class="line"> trap '/usr/bin/sudo -k' EXIT</span><br><span class="line">fi</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> Things can fail later if `pwd` doesn't exist.</span><br><span class="line"><span class="meta">#</span> Also sudo prints a warning message for no good reason</span><br><span class="line">cd "/usr" || exit 1</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span>###################################################################### script</span><br><span class="line">if should_install_git; then</span><br><span class="line"> abort "$(cat <<EOABORT</span><br><span class="line">You must install Git before installing Homebrew. See:</span><br><span class="line"><span class="meta"> $</span>{tty_underline}https://docs.brew.sh/Installation${tty_reset}</span><br><span class="line">EOABORT</span><br><span class="line">)"</span><br><span class="line">fi</span><br><span class="line"></span><br><span class="line">if [[ -n "${HOMEBREW_ON_LINUX-}" ]]; then</span><br><span class="line"> if [[ -n "${CI-}" ]] || [[ -w "$HOMEBREW_PREFIX_DEFAULT" ]] || [[ -w "/home/linuxbrew" ]] || [[ -w "/home" ]]; then</span><br><span class="line"> HOMEBREW_PREFIX="$HOMEBREW_PREFIX_DEFAULT"</span><br><span class="line"> else</span><br><span class="line"> trap exit SIGINT</span><br><span class="line"> sudo_output="$(/usr/bin/sudo -n -l mkdir 2>&1)"</span><br><span class="line"> sudo_exit_code="$?"</span><br><span class="line"> if [[ "$sudo_exit_code" -ne 0 ]] && [[ "$sudo_output" = "sudo: a password is required" ]]; then</span><br><span class="line"> ohai "Select the Homebrew installation directory"</span><br><span class="line"> echo "- ${tty_bold}Enter your password${tty_reset} to install to ${tty_underline}${HOMEBREW_PREFIX_DEFAULT}${tty_reset} (${tty_bold}recommended${tty_reset})"</span><br><span class="line"> echo "- ${tty_bold}Press Control-D${tty_reset} to install to ${tty_underline}$HOME/.linuxbrew${tty_reset}"</span><br><span class="line"> echo "- ${tty_bold}Press Control-C${tty_reset} to cancel installation"</span><br><span class="line"> fi</span><br><span class="line"> if have_sudo_access; then</span><br><span class="line"> HOMEBREW_PREFIX="$HOMEBREW_PREFIX_DEFAULT"</span><br><span class="line"> else</span><br><span class="line"> HOMEBREW_PREFIX="$HOME/.linuxbrew"</span><br><span class="line"> fi</span><br><span class="line"> trap - SIGINT</span><br><span class="line"> fi</span><br><span class="line"> HOMEBREW_REPOSITORY="${HOMEBREW_PREFIX}/Homebrew"</span><br><span class="line">fi</span><br><span class="line"></span><br><span class="line">if [[ "$UID" == "0" ]]; then</span><br><span class="line"> abort "Don't run this as root!"</span><br><span class="line">elif [[ -d "$HOMEBREW_PREFIX" && ! -x "$HOMEBREW_PREFIX" ]]; then</span><br><span class="line"> abort "$(cat <<EOABORT</span><br><span class="line">The Homebrew prefix, ${HOMEBREW_PREFIX}, exists but is not searchable. If this is</span><br><span class="line">not intentional, please restore the default permissions and try running the</span><br><span class="line">installer again:</span><br><span class="line"> sudo chmod 775 ${HOMEBREW_PREFIX}</span><br><span class="line">EOABORT</span><br><span class="line">)"</span><br><span class="line">fi</span><br><span class="line"></span><br><span class="line">if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then</span><br><span class="line"> if version_lt "$macos_version" "10.7"; then</span><br><span class="line"> abort "$(cat <<EOABORT</span><br><span class="line">Your Mac OS X version is too old. See:</span><br><span class="line"><span class="meta"> $</span>{tty_underline}https://github.com/mistydemeo/tigerbrew${tty_reset}</span><br><span class="line">EOABORT</span><br><span class="line">)"</span><br><span class="line"> elif version_lt "$macos_version" "10.9"; then</span><br><span class="line"> abort "Your OS X version is too old"</span><br><span class="line"> elif ! [[ "$(dsmemberutil checkmembership -U "$USER" -G "$GROUP")" = *"user is a member"* ]]; then</span><br><span class="line"> abort "This script requires the user $USER to be an Administrator."</span><br><span class="line"> elif version_gt "$macos_version" "$MACOS_LATEST_SUPPORTED" || \</span><br><span class="line"> version_lt "$macos_version" "$MACOS_OLDEST_SUPPORTED"; then</span><br><span class="line"> who="We"</span><br><span class="line"> what=""</span><br><span class="line"> if version_gt "$macos_version" "$MACOS_LATEST_SUPPORTED"; then</span><br><span class="line"> what="pre-release version"</span><br><span class="line"> else</span><br><span class="line"> who+=" (and Apple)"</span><br><span class="line"> what="old version"</span><br><span class="line"> fi</span><br><span class="line"> ohai "You are using macOS ${macos_version}."</span><br><span class="line"> ohai "${who} do not provide support for this ${what}."</span><br><span class="line"></span><br><span class="line"> echo "$(cat <<EOS</span><br><span class="line">This installation may not succeed.</span><br><span class="line">After installation, you will encounter build failures with some formulae.</span><br><span class="line">Please create pull requests instead of asking for help on Homebrew\'s GitHub,</span><br><span class="line">Discourse, Twitter or IRC. You are responsible for resolving any issues you</span><br><span class="line">experience while you are running this ${what}.</span><br><span class="line">EOS</span><br><span class="line">)</span><br><span class="line">"</span><br><span class="line"> fi</span><br><span class="line">fi</span><br><span class="line"></span><br><span class="line">ohai "This script will install:"</span><br><span class="line">echo "${HOMEBREW_PREFIX}/bin/brew"</span><br><span class="line">echo "${HOMEBREW_PREFIX}/share/doc/homebrew"</span><br><span class="line">echo "${HOMEBREW_PREFIX}/share/man/man1/brew.1"</span><br><span class="line">echo "${HOMEBREW_PREFIX}/share/zsh/site-functions/_brew"</span><br><span class="line">echo "${HOMEBREW_PREFIX}/etc/bash_completion.d/brew"</span><br><span class="line">echo "${HOMEBREW_REPOSITORY}"</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> Keep relatively in sync with</span><br><span class="line"><span class="meta">#</span> https://github.com/Homebrew/brew/blob/master/Library/Homebrew/keg.rb</span><br><span class="line">directories=(bin etc include lib sbin share opt var</span><br><span class="line"> Frameworks</span><br><span class="line"> etc/bash_completion.d lib/pkgconfig</span><br><span class="line"> share/aclocal share/doc share/info share/locale share/man</span><br><span class="line"> share/man/man1 share/man/man2 share/man/man3 share/man/man4</span><br><span class="line"> share/man/man5 share/man/man6 share/man/man7 share/man/man8</span><br><span class="line"> var/log var/homebrew var/homebrew/linked</span><br><span class="line"> bin/brew)</span><br><span class="line">group_chmods=()</span><br><span class="line">for dir in "${directories[@]}"; do</span><br><span class="line"> if exists_but_not_writable "${HOMEBREW_PREFIX}/${dir}"; then</span><br><span class="line"> group_chmods+=("${HOMEBREW_PREFIX}/${dir}")</span><br><span class="line"> fi</span><br><span class="line">done</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> zsh refuses to read from these directories if group writable</span><br><span class="line">directories=(share/zsh share/zsh/site-functions)</span><br><span class="line">zsh_dirs=()</span><br><span class="line">for dir in "${directories[@]}"; do</span><br><span class="line"> zsh_dirs+=("${HOMEBREW_PREFIX}/${dir}")</span><br><span class="line">done</span><br><span class="line"></span><br><span class="line">directories=(bin etc include lib sbin share var opt</span><br><span class="line"> share/zsh share/zsh/site-functions</span><br><span class="line"> var/homebrew var/homebrew/linked</span><br><span class="line"> Cellar Caskroom Homebrew Frameworks)</span><br><span class="line">mkdirs=()</span><br><span class="line">for dir in "${directories[@]}"; do</span><br><span class="line"> if ! [[ -d "${HOMEBREW_PREFIX}/${dir}" ]]; then</span><br><span class="line"> mkdirs+=("${HOMEBREW_PREFIX}/${dir}")</span><br><span class="line"> fi</span><br><span class="line">done</span><br><span class="line"></span><br><span class="line">user_chmods=()</span><br><span class="line">if [[ "${#zsh_dirs[@]}" -gt 0 ]]; then</span><br><span class="line"> for dir in "${zsh_dirs[@]}"; do</span><br><span class="line"> if user_only_chmod "${dir}"; then</span><br><span class="line"> user_chmods+=("${dir}")</span><br><span class="line"> fi</span><br><span class="line"> done</span><br><span class="line">fi</span><br><span class="line"></span><br><span class="line">chmods=()</span><br><span class="line">if [[ "${#group_chmods[@]}" -gt 0 ]]; then</span><br><span class="line"> chmods+=("${group_chmods[@]}")</span><br><span class="line">fi</span><br><span class="line">if [[ "${#user_chmods[@]}" -gt 0 ]]; then</span><br><span class="line"> chmods+=("${user_chmods[@]}")</span><br><span class="line">fi</span><br><span class="line"></span><br><span class="line">chowns=()</span><br><span class="line">chgrps=()</span><br><span class="line">if [[ "${#chmods[@]}" -gt 0 ]]; then</span><br><span class="line"> for dir in "${chmods[@]}"; do</span><br><span class="line"> if file_not_owned "${dir}"; then</span><br><span class="line"> chowns+=("${dir}")</span><br><span class="line"> fi</span><br><span class="line"> if file_not_grpowned "${dir}"; then</span><br><span class="line"> chgrps+=("${dir}")</span><br><span class="line"> fi</span><br><span class="line"> done</span><br><span class="line">fi</span><br><span class="line"></span><br><span class="line">if [[ "${#group_chmods[@]}" -gt 0 ]]; then</span><br><span class="line"> ohai "The following existing directories will be made group writable:"</span><br><span class="line"> printf "%s\n" "${group_chmods[@]}"</span><br><span class="line">fi</span><br><span class="line">if [[ "${#user_chmods[@]}" -gt 0 ]]; then</span><br><span class="line"> ohai "The following existing directories will be made writable by user only:"</span><br><span class="line"> printf "%s\n" "${user_chmods[@]}"</span><br><span class="line">fi</span><br><span class="line">if [[ "${#chowns[@]}" -gt 0 ]]; then</span><br><span class="line"> ohai "The following existing directories will have their owner set to ${tty_underline}${USER}${tty_reset}:"</span><br><span class="line"> printf "%s\n" "${chowns[@]}"</span><br><span class="line">fi</span><br><span class="line">if [[ "${#chgrps[@]}" -gt 0 ]]; then</span><br><span class="line"> ohai "The following existing directories will have their group set to ${tty_underline}${GROUP}${tty_reset}:"</span><br><span class="line"> printf "%s\n" "${chgrps[@]}"</span><br><span class="line">fi</span><br><span class="line">if [[ "${#mkdirs[@]}" -gt 0 ]]; then</span><br><span class="line"> ohai "The following new directories will be created:"</span><br><span class="line"> printf "%s\n" "${mkdirs[@]}"</span><br><span class="line">fi</span><br><span class="line"></span><br><span class="line">if should_install_command_line_tools; then</span><br><span class="line"> ohai "The Xcode Command Line Tools will be installed."</span><br><span class="line">fi</span><br><span class="line"></span><br><span class="line">if [[ -t 0 && -z "${CI-}" ]]; then</span><br><span class="line"> wait_for_user</span><br><span class="line">fi</span><br><span class="line"></span><br><span class="line">if [[ -d "${HOMEBREW_PREFIX}" ]]; then</span><br><span class="line"> if [[ "${#chmods[@]}" -gt 0 ]]; then</span><br><span class="line"> execute_sudo "/bin/chmod" "u+rwx" "${chmods[@]}"</span><br><span class="line"> fi</span><br><span class="line"> if [[ "${#group_chmods[@]}" -gt 0 ]]; then</span><br><span class="line"> execute_sudo "/bin/chmod" "g+rwx" "${group_chmods[@]}"</span><br><span class="line"> fi</span><br><span class="line"> if [[ "${#user_chmods[@]}" -gt 0 ]]; then</span><br><span class="line"> execute_sudo "/bin/chmod" "755" "${user_chmods[@]}"</span><br><span class="line"> fi</span><br><span class="line"> if [[ "${#chowns[@]}" -gt 0 ]]; then</span><br><span class="line"> execute_sudo "$CHOWN" "$USER" "${chowns[@]}"</span><br><span class="line"> fi</span><br><span class="line"> if [[ "${#chgrps[@]}" -gt 0 ]]; then</span><br><span class="line"> execute_sudo "$CHGRP" "$GROUP" "${chgrps[@]}"</span><br><span class="line"> fi</span><br><span class="line">else</span><br><span class="line"> execute_sudo "/bin/mkdir" "-p" "${HOMEBREW_PREFIX}"</span><br><span class="line"> if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then</span><br><span class="line"> execute_sudo "$CHOWN" "root:wheel" "${HOMEBREW_PREFIX}"</span><br><span class="line"> else</span><br><span class="line"> execute_sudo "$CHOWN" "$USER:$GROUP" "${HOMEBREW_PREFIX}"</span><br><span class="line"> fi</span><br><span class="line">fi</span><br><span class="line"></span><br><span class="line">if [[ "${#mkdirs[@]}" -gt 0 ]]; then</span><br><span class="line"> execute_sudo "/bin/mkdir" "-p" "${mkdirs[@]}"</span><br><span class="line"> execute_sudo "/bin/chmod" "g+rwx" "${mkdirs[@]}"</span><br><span class="line"> execute_sudo "$CHOWN" "$USER" "${mkdirs[@]}"</span><br><span class="line"> execute_sudo "$CHGRP" "$GROUP" "${mkdirs[@]}"</span><br><span class="line">fi</span><br><span class="line"></span><br><span class="line">if ! [[ -d "${HOMEBREW_CACHE}" ]]; then</span><br><span class="line"> if [[ -z "${HOMEBREW_ON_LINUX-}" ]]; then</span><br><span class="line"> execute_sudo "/bin/mkdir" "-p" "${HOMEBREW_CACHE}"</span><br><span class="line"> else</span><br><span class="line"> execute "/bin/mkdir" "-p" "${HOMEBREW_CACHE}"</span><br><span class="line"> fi</span><br><span class="line">fi</span><br><span class="line">if exists_but_not_writable "${HOMEBREW_CACHE}"; then</span><br><span class="line"> execute_sudo "/bin/chmod" "g+rwx" "${HOMEBREW_CACHE}"</span><br><span class="line">fi</span><br><span class="line">if file_not_owned "${HOMEBREW_CACHE}"; then</span><br><span class="line"> execute_sudo "$CHOWN" "$USER" "${HOMEBREW_CACHE}"</span><br><span class="line">fi</span><br><span class="line">if file_not_grpowned "${HOMEBREW_CACHE}"; then</span><br><span class="line"> execute_sudo "$CHGRP" "$GROUP" "${HOMEBREW_CACHE}"</span><br><span class="line">fi</span><br><span class="line">if [[ -d "${HOMEBREW_CACHE}" ]]; then</span><br><span class="line"> execute "/usr/bin/touch" "${HOMEBREW_CACHE}/.cleaned"</span><br><span class="line">fi</span><br><span class="line"></span><br><span class="line">if should_install_command_line_tools && version_ge "$macos_version" "10.13"; then</span><br><span class="line"> ohai "Searching online for the Command Line Tools"</span><br><span class="line"><span class="meta"> #</span> This temporary file prompts the 'softwareupdate' utility to list the Command Line Tools</span><br><span class="line"> clt_placeholder="/tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress"</span><br><span class="line"> execute_sudo "/usr/bin/touch" "$clt_placeholder"</span><br><span class="line"></span><br><span class="line"> clt_label_command="/usr/sbin/softwareupdate -l |</span><br><span class="line"> grep -B 1 -E 'Command Line Tools' |</span><br><span class="line"> awk -F'*' '/^ *\\*/ {print \$2}' |</span><br><span class="line"> sed -e 's/^ *Label: //' -e 's/^ *//' |</span><br><span class="line"> sort -V |</span><br><span class="line"> tail -n1"</span><br><span class="line"> clt_label="$(chomp "$(/bin/bash -c "$clt_label_command")")"</span><br><span class="line"></span><br><span class="line"> if [[ -n "$clt_label" ]]; then</span><br><span class="line"> ohai "Installing $clt_label"</span><br><span class="line"> execute_sudo "/usr/sbin/softwareupdate" "-i" "$clt_label"</span><br><span class="line"> execute_sudo "/bin/rm" "-f" "$clt_placeholder"</span><br><span class="line"> execute_sudo "/usr/bin/xcode-select" "--switch" "/Library/Developer/CommandLineTools"</span><br><span class="line"> fi</span><br><span class="line">fi</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> Headless install may have failed, so fallback to original 'xcode-select' method</span><br><span class="line">if should_install_command_line_tools && test -t 0; then</span><br><span class="line"> ohai "Installing the Command Line Tools (expect a GUI popup):"</span><br><span class="line"> execute_sudo "/usr/bin/xcode-select" "--install"</span><br><span class="line"> echo "Press any key when the installation has completed."</span><br><span class="line"> getc</span><br><span class="line"> execute_sudo "/usr/bin/xcode-select" "--switch" "/Library/Developer/CommandLineTools"</span><br><span class="line">fi</span><br><span class="line"></span><br><span class="line">if [[ -z "${HOMEBREW_ON_LINUX-}" ]] && ! output="$(/usr/bin/xcrun clang 2>&1)" && [[ "$output" == *"license"* ]]; then</span><br><span class="line"> abort "$(cat <<EOABORT</span><br><span class="line">You have not agreed to the Xcode license.</span><br><span class="line">Before running the installer again please agree to the license by opening</span><br><span class="line">Xcode.app or running:</span><br><span class="line"> sudo xcodebuild -license</span><br><span class="line">EOABORT</span><br><span class="line">)"</span><br><span class="line">fi</span><br><span class="line"></span><br><span class="line">ohai "Downloading and installing Homebrew..."</span><br><span class="line">(</span><br><span class="line"> cd "${HOMEBREW_REPOSITORY}" >/dev/null || return</span><br><span class="line"></span><br><span class="line"><span class="meta"> #</span> we do it in four steps to avoid merge errors when reinstalling</span><br><span class="line"> execute "git" "init" "-q"</span><br><span class="line"></span><br><span class="line"><span class="meta"> #</span> "git remote add" will fail if the remote is defined in the global config</span><br><span class="line"> execute "git" "config" "remote.origin.url" "${BREW_REPO}"</span><br><span class="line"> execute "git" "config" "remote.origin.fetch" "+refs/heads/*:refs/remotes/origin/*"</span><br><span class="line"></span><br><span class="line"><span class="meta"> #</span> ensure we don't munge line endings on checkout</span><br><span class="line"> execute "git" "config" "core.autocrlf" "false"</span><br><span class="line"></span><br><span class="line"> execute "git" "fetch" "origin" "--force"</span><br><span class="line"> execute "git" "fetch" "origin" "--tags" "--force"</span><br><span class="line"></span><br><span class="line"> execute "git" "reset" "--hard" "origin/master"</span><br><span class="line"></span><br><span class="line"> execute "ln" "-sf" "${HOMEBREW_REPOSITORY}/bin/brew" "${HOMEBREW_PREFIX}/bin/brew"</span><br><span class="line"></span><br><span class="line"> execute "${HOMEBREW_PREFIX}/bin/brew" "update" "--force"</span><br><span class="line">)</span><br><span class="line"></span><br><span class="line">if [[ ":${PATH}:" != *":${HOMEBREW_PREFIX}/bin:"* ]]; then</span><br><span class="line"> warn "${HOMEBREW_PREFIX}/bin is not in your PATH."</span><br><span class="line">fi</span><br><span class="line"></span><br><span class="line">ohai "Installation successful!"</span><br><span class="line">echo</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> Use the shell's audible bell.</span><br><span class="line">if [[ -t 1 ]]; then</span><br><span class="line"> printf "\a"</span><br><span class="line">fi</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> Use an extra newline and bold to avoid this being missed.</span><br><span class="line">ohai "Homebrew has enabled anonymous aggregate formulae and cask analytics."</span><br><span class="line">echo "$(cat <<EOS</span><br><span class="line"><span class="meta">$</span>{tty_bold}Read the analytics documentation (and how to opt-out) here:</span><br><span class="line"><span class="meta"> $</span>{tty_underline}https://docs.brew.sh/Analytics${tty_reset}</span><br><span class="line">No analytics data has been sent yet (or will be during this \`install\` run).</span><br><span class="line">EOS</span><br><span class="line">)</span><br><span class="line">"</span><br><span class="line"></span><br><span class="line">ohai "Homebrew is run entirely by unpaid volunteers. Please consider donating:"</span><br><span class="line">echo "$(cat <<EOS</span><br><span class="line"><span class="meta"> $</span>{tty_underline}https://github.com/Homebrew/brew#donations${tty_reset}</span><br><span class="line">EOS</span><br><span class="line">)</span><br><span class="line">"</span><br><span class="line"></span><br><span class="line">(</span><br><span class="line"> cd "${HOMEBREW_REPOSITORY}" >/dev/null || return</span><br><span class="line"> execute "git" "config" "--replace-all" "homebrew.analyticsmessage" "true"</span><br><span class="line"> execute "git" "config" "--replace-all" "homebrew.caskanalyticsmessage" "true"</span><br><span class="line">)</span><br><span class="line"></span><br><span class="line">ohai "Next steps:"</span><br><span class="line">echo "- Run \`brew help\` to get started"</span><br><span class="line">echo "- Further documentation: "</span><br><span class="line">echo " ${tty_underline}https://docs.brew.sh${tty_reset}"</span><br><span class="line"></span><br><span class="line">if [[ -n "${HOMEBREW_ON_LINUX-}" ]]; then</span><br><span class="line"> case "$SHELL" in</span><br><span class="line"> */bash*)</span><br><span class="line"> if [[ -r "$HOME/.bash_profile" ]]; then</span><br><span class="line"> shell_profile="$HOME/.bash_profile"</span><br><span class="line"> else</span><br><span class="line"> shell_profile="$HOME/.profile"</span><br><span class="line"> fi</span><br><span class="line"> ;;</span><br><span class="line"> */zsh*)</span><br><span class="line"> shell_profile="$HOME/.zprofile"</span><br><span class="line"> ;;</span><br><span class="line"> *)</span><br><span class="line"> shell_profile="$HOME/.profile"</span><br><span class="line"> ;;</span><br><span class="line"> esac</span><br><span class="line"></span><br><span class="line"> cat <<EOS</span><br><span class="line">- Install the Homebrew dependencies if you have sudo access:</span><br><span class="line"><span class="meta"> $</span>{tty_bold}Debian, Ubuntu, etc.${tty_reset}</span><br><span class="line"> sudo apt-get install build-essential</span><br><span class="line"><span class="meta"> $</span>{tty_bold}Fedora, Red Hat, CentOS, etc.${tty_reset}</span><br><span class="line"> sudo yum groupinstall 'Development Tools'</span><br><span class="line"> See ${tty_underline}https://docs.brew.sh/linux${tty_reset} for more information.</span><br><span class="line">- Configure Homebrew in your ${tty_underline}${shell_profile}${tty_reset} by running</span><br><span class="line"> echo 'eval \$(${HOMEBREW_PREFIX}/bin/brew shellenv)' >> ${shell_profile}</span><br><span class="line">- Add Homebrew to your ${tty_bold}PATH${tty_reset}</span><br><span class="line"> eval \$(${HOMEBREW_PREFIX}/bin/brew shellenv)</span><br><span class="line">- We recommend that you install GCC by running:</span><br><span class="line"> brew install gcc</span><br><span class="line"></span><br><span class="line">EOS</span><br><span class="line">fi</span><br></pre></td></tr></table></figure>
<ul>
<li>执行以下命令后会快速进行安装</li>
</ul>
<figure class="highlight shell"><table><tr><td class="code"><pre><span class="line">/bin/bash brew_install</span><br></pre></td></tr></table></figure>
<ul>
<li>安装的中间会卡在</li>
</ul>
<figure class="highlight shell"><table><tr><td class="code"><pre><span class="line">Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core'...</span><br></pre></td></tr></table></figure>
<ul>
<li>这时候直接Ctrl+C结束,通过ustc的源进行配置</li>
</ul>
<figure class="highlight shell"><table><tr><td class="code"><pre><span class="line">mkdir -p /usr/local/Homebrew/Library/Taps/homebrew/</span><br><span class="line">cd /usr/local/Homebrew/Library/Taps/homebrew</span><br><span class="line">git clone https://mirrors.ustc.edu.cn/homebrew-core.git</span><br></pre></td></tr></table></figure>
<ul>
<li>配置完成后终端输入<code>brew help</code>显示帮助信息则说明安装成功,如果提示失败请检查/usr/local/bin下是否有brew,有的话添加环境变量即可</li>
</ul>
<h2 id="Homebrew-Cask安装"><a href="#Homebrew-Cask安装" class="headerlink" title="Homebrew Cask安装"></a>Homebrew Cask安装</h2><ul>
<li>Homebrew Cask需要用homebrew进行安装,同样建议修改homebrew的镜像源为ustc源(或者国内其他源)。如果是按照上面的方法安装的homebrew,镜像源应该已经是ustc的源,用下面的命令查看。</li>
</ul>
<figure class="highlight shell"><table><tr><td class="code"><pre><span class="line"><span class="meta">#</span> 查看brew镜像源</span><br><span class="line">git -C "$(brew --repo)" remote -v</span><br><span class="line"><span class="meta">#</span> 查看homebrew-core镜像源</span><br><span class="line">git -C "$(brew --repo homebrew/core)" remote -v</span><br><span class="line"><span class="meta">#</span> 查看homebrew-cask镜像源(需要安装后才能查看)</span><br><span class="line">git -C "$(brew --repo homebrew/cask)" remote -v</span><br></pre></td></tr></table></figure>
<ul>
<li>如果是<code>https://github.com/Homebrew/brew.git</code>的源,需要进行修改:</li>
</ul>
<figure class="highlight shell"><table><tr><td class="code"><pre><span class="line"><span class="meta">#</span> 修改brew镜像源</span><br><span class="line">git -C "$(brew --repo)" remote set-url origin https://mirrors.ustc.edu.cn/brew.git</span><br><span class="line"><span class="meta">#</span> 修改homebrew-core镜像源</span><br><span class="line">git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git</span><br><span class="line"><span class="meta">#</span> 修改homebrew-cask镜像源(需要安装后才能修改)</span><br><span class="line">git -C "$(brew --repo homebrew/cask)" remote set-url origin https://mirrors.ustc.edu.cn/homebrew-cask.git</span><br><span class="line"><span class="meta">#</span> 更新</span><br><span class="line">brew update</span><br></pre></td></tr></table></figure>
<ul>
<li>顺便附上恢复源的地址:</li>
</ul>
<figure class="highlight shell"><table><tr><td class="code"><pre><span class="line">git -C "$(brew --repo)" remote set-url origin https://github.com/Homebrew/brew.git</span><br><span class="line">git -C "$(brew --repo homebrew/core)" remote set-url origin https://github.com/Homebrew/homebrew-core.git</span><br><span class="line">git -C "$(brew --repo homebrew/cask)" remote set-url origin https://github.com/Homebrew/homebrew-cask.git</span><br><span class="line">brew update</span><br></pre></td></tr></table></figure>
<ul>
<li>修改源之后,输入:</li>
</ul>
<figure class="highlight shell"><table><tr><td class="code"><pre><span class="line">brew install brew-cask-completion</span><br></pre></td></tr></table></figure>
<ul>
<li>安装完成后输入<code>brew cask help</code>测试,如果出现一下内容并卡住</li>
</ul>
<figure class="highlight shell"><table><tr><td class="code"><pre><span class="line">Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask'...</span><br></pre></td></tr></table></figure>
<ul>
<li><p>同样的方法,通过ustc的源进行配置</p>
<figure class="highlight shell"><table><tr><td class="code"><pre><span class="line">cd /usr/local/Homebrew/Library/Taps/homebrew</span><br><span class="line">git clone https://mirrors.ustc.edu.cn/homebrew-cask.git</span><br></pre></td></tr></table></figure>
</li>
<li><p>再输入brew cask help测试,应该就会显示帮助信息,接着按照上面的方法修改homebrew-cask镜像源即可。</p>
</li>
</ul>
]]></content>
<categories>
<category>life</category>
</categories>
<tags>
<tag>macOs</tag>
<tag>homeBrew</tag>
<tag>brew</tag>
</tags>
</entry>
<entry>
<title>最长连续序列</title>
<url>/java/algorithm/longest-consecutive-sequence/</url>
<content><![CDATA[<h1 id="最长连续序列"><a href="#最长连续序列" class="headerlink" title="最长连续序列"></a>最长连续序列</h1><h2 id="题目"><a href="#题目" class="headerlink" title="题目"></a>题目</h2><blockquote>
<p>给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。</p>
<p>你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。</p>
</blockquote>
<ul>
<li>示例</li>
</ul>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">给定一个未排序的整数数组,找出最长连续序列的长度。</span><br><span class="line"></span><br><span class="line">要求算法的时间复杂度为 O(n)。</span><br></pre></td></tr></table></figure>
<ul>
<li><strong>示例:</strong></li>
</ul>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">输入: [100, 4, 200, 1, 3, 2]</span><br><span class="line">输出: 4</span><br><span class="line">解释: 最长连续序列是 [1, 2, 3, 4]。它的长度为 4。</span><br></pre></td></tr></table></figure>
<h2 id="解题思路"><a href="#解题思路" class="headerlink" title="解题思路"></a>解题思路</h2><blockquote>
<ul>
<li>围绕 O(n)<em>O</em>(<em>n</em>) 的时间复杂度</li>
<li>这些数字用一个 HashSet 保存,实现 O(1)O(1) 时间的查询</li>
<li>我们只对 当前数字 - 1 不在哈希表里的数字,作为连续序列的第一个数字去找对应的最长序列,这是因为其他数字一定已经出现在了某个序列里。</li>
</ul>
</blockquote>
<h2 id="代码实现"><a href="#代码实现" class="headerlink" title="代码实现"></a>代码实现</h2><figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">int</span> <span class="title">longestConsecutive</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">if</span> (nums == <span class="keyword">null</span> || nums.length == <span class="number">0</span>) {</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"> }</span><br><span class="line"> Set<Integer> tempNums = <span class="keyword">new</span> HashSet<>();</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> num : nums) {</span><br><span class="line"> tempNums.add(num);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">int</span> result = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span> (Integer item : tempNums) {</span><br><span class="line"> <span class="keyword">if</span> (!tempNums.contains(item - <span class="number">1</span>)) {</span><br><span class="line"> <span class="keyword">int</span> currentNum = item;</span><br><span class="line"> <span class="keyword">int</span> index = <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">while</span> (tempNums.contains(currentNum + <span class="number">1</span>)) {</span><br><span class="line"> index++;</span><br><span class="line"> currentNum++;</span><br><span class="line"> }</span><br><span class="line"> result = Math.max(result, index);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> result;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h2 id="性能分析"><a href="#性能分析" class="headerlink" title="性能分析"></a>性能分析</h2><h3 id="一、执行用时分布图表"><a href="#一、执行用时分布图表" class="headerlink" title="一、执行用时分布图表"></a>一、执行用时分布图表</h3><ul>
<li><img src="../../../images/post/2020/java/algorithm/longest-consecutive-sequence/time.png" alt="time"></li>
</ul>
<h3 id="二、执行消耗内存分布图表"><a href="#二、执行消耗内存分布图表" class="headerlink" title="二、执行消耗内存分布图表"></a>二、执行消耗内存分布图表</h3><ul>
<li><img src="../../../images/post/2020/java/algorithm/longest-consecutive-sequence/memory.png" alt="memory"></li>
</ul>
<h3 id="三、复杂度分析"><a href="#三、复杂度分析" class="headerlink" title="三、复杂度分析"></a>三、复杂度分析</h3><ul>
<li>时间复杂度:O(n)O(n)</li>
</ul>
<blockquote>
<p>尽管在 for 循环中嵌套了一个 while 循环,时间复杂度看起来像是二次方级别的。但其实它是线性的算法。因为只有当 currentNum 遇到了一个序列的开始, while 循环才会被执行(也就是 currentNum-1 不在数组 nums 里), while 循环在整个运行过程中只会被迭代 nn 次。这意味着尽管看起来时间复杂度为 O(n \cdot n)O(n⋅n) ,实际这个嵌套循环只会运行 O(n + n) = O(n)O(n+n)=O(n) 次。所有的计算都是线性时间的,所以总的时间复杂度是 O(n)O(n) 的。</p>
</blockquote>
<ul>
<li>空间复杂度:O(n)O(n)</li>
</ul>
<blockquote>
<p>为了实现 O(1)O(1) 的查询,我们对哈希表分配线性空间,以保存 nums 数组中的 O(n)O(n) 个数字。除此以外,所需空间与暴力解法一致。</p>
</blockquote>