-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.xml
2524 lines (2474 loc) · 521 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>IDEA中查看maven的依赖关系</title>
<url>/posts/c9331070.html</url>
<content><![CDATA[<h2 id="1-使用maven自带的工具"><a href="#1-使用maven自带的工具" class="headerlink" title="1. 使用maven自带的工具"></a>1. 使用maven自带的工具</h2><h3 id="1-1-确保idea中UML的插件可以使用"><a href="#1-1-确保idea中UML的插件可以使用" class="headerlink" title="1.1 确保idea中UML的插件可以使用"></a>1.1 确保idea中UML的插件可以使用</h3><p><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1gfhb2uvgs8j314r0u0afz.jpg" alt=""></p>
<h3 id="1-2-pom-xml文件依赖分析"><a href="#1-2-pom-xml文件依赖分析" class="headerlink" title="1.2 pom.xml文件依赖分析"></a>1.2 pom.xml文件依赖分析</h3><ol>
<li>在需要分析的pom.xml文件中右键选择<code>maven</code>-><code>Show Dependencies</code><br><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1gfhb5cwrgej30u00zyx6f.jpg" alt=""><br>打开后的图形<br><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1gfhbae8o9oj315c0u04qr.jpg" alt=""></li>
<li>使用技巧<ul>
<li>按住<code>option</code>,移动鼠标会出现放大镜,放大当前的依赖</li>
<li>按住<code>control</code>,滚轮向前 放大整个屏幕</li>
<li>红色实现表示冲突的文件,右键,排除,他就自动在pom文件里面给exclud啦</li>
<li>红色的虚线表示用一个jar包都在哪里被多次引用</li>
</ul>
</li>
</ol>
<h2 id="2-使用maven-helper插件"><a href="#2-使用maven-helper插件" class="headerlink" title="2. 使用maven helper插件"></a>2. 使用maven helper插件</h2><ol>
<li><p>打开需要查看的pom.xml文件,下边会出现<code>Denpendency Anlyzar</code>,点击按钮<br><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1gfhbmhob3ij31cm0u01l1.jpg" alt=""><br><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1gfhbnq116yj31fk0u0hdw.jpg" alt=""></p>
</li>
<li><p>菜单功能</p>
<ul>
<li>Conflicts:冲突</li>
<li>All Dependencies as List:列表形式查看所有依赖</li>
<li>All Dependencies as Tree:树结构查看所有依赖</li>
</ul>
</li>
<li><p>冲突jar包的解决<br> 切换到maven 依赖视图选择冲突选项,如果有冲突,在左下面区域会有红色显示。<br> 解决冲突,右键单击红色区域,弹出菜单选择Exclude命令,对冲突进行排除<br> <img src="https://tva1.sinaimg.cn/large/007S8ZIlly1gfhcdt8upij316c0u0qv7.jpg" alt=""></p>
</li>
</ol>
]]></content>
<categories>
<category>java</category>
</categories>
<tags>
<tag>maven</tag>
</tags>
</entry>
<entry>
<title>Centos7下静默安装Oracle 11g</title>
<url>/posts/undefined.html</url>
<content><![CDATA[<p>[TOC]</p>
<h1 id="Centos7下静默安装Oracle-11g-无图形化界面"><a href="#Centos7下静默安装Oracle-11g-无图形化界面" class="headerlink" title="Centos7下静默安装Oracle 11g(无图形化界面)"></a>Centos7下静默安装Oracle 11g(无图形化界面)</h1><h2 id="说明"><a href="#说明" class="headerlink" title="说明:"></a>说明:</h2><ul>
<li>因为使用的是静默安装,所以我们不需要安装Linux的图形界面,只需要对Linux进行最小化安装即可</li>
</ul>
<h2 id="前提准备"><a href="#前提准备" class="headerlink" title="前提准备"></a>前提准备</h2><h3 id="1-Centos7-的系统环境"><a href="#1-Centos7-的系统环境" class="headerlink" title="1. Centos7 的系统环境"></a>1. Centos7 的系统环境</h3><ul>
<li>系统版本:CentOS Linux release 7.6.1810 (Core) </li>
<li>腾讯云系统</li>
<li>内核版本:</li>
<li>系统配置</li>
<li>交换空间</li>
</ul>
<p>因为腾讯云的原因,默认是不提供swap分区的,这里需要自己手动添加一下。在别的系统上可以视情况而定。如果没有swap的分区的,在安装数据库的时候会出现报错。</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">dd if=/dev/zero of=/swap bs=1024 count=2048000</span><br><span class="line">mkswap /swap</span><br><span class="line">chmod 600 /swap</span><br><span class="line">swapon /swap</span><br><span class="line">echo "/swap swap swap default 0 0">>/etc/fstab #设置成自动挂载</span><br></pre></td></tr></table></figure>
<h3 id="2-配置hostname"><a href="#2-配置hostname" class="headerlink" title="2.配置hostname"></a>2.配置hostname</h3><p>此步骤不是必须滴</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">hostnamectl set-hostname oracledb</span><br><span class="line">echo "127.0.0.1 oracledb" >>/etc/hosts</span><br></pre></td></tr></table></figure>
<h3 id="3-关闭selinux"><a href="#3-关闭selinux" class="headerlink" title="3.关闭selinux"></a>3.关闭selinux</h3><p>此步骤可以视情况而定,本次案例安装的时候执行了此命令</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">sed -i "s/SELINUX=enforcing/SELINUX=disabled/" /etc/selinux/config </span><br><span class="line">setenforce 0</span><br></pre></td></tr></table></figure>
<h2 id="2-下载Oracle"><a href="#2-下载Oracle" class="headerlink" title="2. 下载Oracle"></a>2. 下载Oracle</h2><p><a href="https://www.oracle.com/database/technologies/oracle-database-software-downloads.html" target="_blank" rel="noopener">官网下载地址</a><br>根据自己的服务器,下载相应的软件,我的系统是centos7 64位的,所以下载是Linux x86-64的,下载完成后的两个文件如下:</p>
<ul>
<li>linux.x64_11gR2_database_1of2.zip</li>
<li>linux.x64_11gR2_database_2of2.zip<br><img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1gi0rgdg0jhj31ek0u0wnn.jpg" alt=""></li>
<li><strong>说明:*</strong><br>Oracle系统下载的时候请下载Linux x86-64位的系统,请不要下载zlinux,zlinux是IBM的操作系统的一bai个版本。zLinux就是在baiIBM大机上装Linux,目前主要有RedHat与SUSE for z</li>
</ul>
<h2 id="3-检查软件依赖环境"><a href="#3-检查软件依赖环境" class="headerlink" title="3.检查软件依赖环境"></a>3.检查软件依赖环境</h2><p><span style="color:red">注意:从Oracle 11g 11.2.0.2版本开始,在Linux x86_64上安装时,只需要安装64位的软件包,但是,对于11.2.0.2之前的任何Oracle database 11g,下表中列出的32位和64位的安装包都必须安装</span></p>
<p>需要安装的依赖包(版本号只能大于规定的版本,不能小于)</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">binutils-2.23.52.0.1-12.el7.x86_64 </span><br><span class="line">compat-libcap1-1.10-3.el7.x86_64 </span><br><span class="line">compat-libstdc++-33-3.2.3-71.el7.i686</span><br><span class="line">compat-libstdc++-33-3.2.3-71.el7.x86_64</span><br><span class="line">gcc-4.8.2-3.el7.x86_64 </span><br><span class="line">gcc-c++-4.8.2-3.el7.x86_64 </span><br><span class="line">glibc-2.17-36.el7.i686 </span><br><span class="line">glibc-2.17-36.el7.x86_64 </span><br><span class="line">glibc-devel-2.17-36.el7.i686 </span><br><span class="line">glibc-devel-2.17-36.el7.x86_64 </span><br><span class="line">ksh</span><br><span class="line">libaio-0.3.109-9.el7.i686 </span><br><span class="line">libaio-0.3.109-9.el7.x86_64 </span><br><span class="line">libaio-devel-0.3.109-9.el7.i686 </span><br><span class="line">libaio-devel-0.3.109-9.el7.x86_64 </span><br><span class="line">libgcc-4.8.2-3.el7.i686 </span><br><span class="line">libgcc-4.8.2-3.el7.x86_64 </span><br><span class="line">libstdc++-4.8.2-3.el7.i686 </span><br><span class="line">libstdc++-4.8.2-3.el7.x86_64 </span><br><span class="line">libstdc++-devel-4.8.2-3.el7.i686 </span><br><span class="line">libstdc++-devel-4.8.2-3.el7.x86_64 </span><br><span class="line">libXi-1.7.2-1.el7.i686 </span><br><span class="line">libXi-1.7.2-1.el7.x86_64 </span><br><span class="line">libXtst-1.2.2-1.el7.i686 </span><br><span class="line">libXtst-1.2.2-1.el7.x86_64 </span><br><span class="line">make-3.82-19.el7.x86_64 </span><br><span class="line">sysstat-10.1.5-1.el7.x86_64</span><br><span class="line">unixODBC-2.3.1-6.el7.i686</span><br><span class="line">unixODBC-2.3.1-6.el7.x86_64</span><br><span class="line">unixODBC-devel-2.3.1-6.el7.i686</span><br><span class="line">unixODBC-devel-2.3.1-6.el7.x86_64</span><br></pre></td></tr></table></figure>
<p>安装命令</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">yum -y install binutils compat-libcap1 compat-libstdc++-33 gcc gcc-c++ glibc glibc-devel ksh libaio libaio-devel libgcc libstdc++ libstdc++-devel libXi libXtst make sysstat unixODBC unixODBC-devel</span><br></pre></td></tr></table></figure>
<p>安装完成后,检查依赖是否安装成功</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">rpm -q binutils compat-libcap1 compat-libstdc++-33 gcc gcc-c++ glibc glibc-devel ksh libaio libaio-devel libgcc libstdc++ libstdc++-devel libXi libXtst make sysstat unixODBC unixODBC-devel | grep "not installed"</span><br></pre></td></tr></table></figure>
<p><strong>注:本次安装使用的是腾讯云的centos服务器,腾讯云的yum源已经有了。如果您使用的是其他环境,缺少那个依赖包,就手动下载安装即可。</strong></p>
<h2 id="4-创建所需的操作系统组和用户"><a href="#4-创建所需的操作系统组和用户" class="headerlink" title="4. 创建所需的操作系统组和用户"></a>4. 创建所需的操作系统组和用户</h2><p>如果要安装Oracle数据库,则需要以下本地操作系统组和用户:</p>
<ul>
<li>Oracle inventory组(通常为 oinstall)</li>
<li>OSDBA组 (通常为 dba)</li>
<li>OSOPER组 (通常为 oper)</li>
<li>Oracle软件所有者(通常为 oracle)</li>
</ul>
<p>添加命令</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">groupadd oinstall</span><br><span class="line">groupadd dba</span><br><span class="line">groupadd oper</span><br><span class="line">useradd -g oinstall -G dba,oper oracle</span><br></pre></td></tr></table></figure>
<p>添加完成后,查看oracle用户</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">id oracle</span><br></pre></td></tr></table></figure>
<p>修改oracle用户密码</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">passwd oracle</span><br></pre></td></tr></table></figure>
<h2 id="5-配置内核参数和资源限制"><a href="#5-配置内核参数和资源限制" class="headerlink" title="5. 配置内核参数和资源限制"></a>5. 配置内核参数和资源限制</h2><ol>
<li>在<code>/etc/sysctl.conf</code>添加如下的参数,如果系统中某个参数高于下面的参数的值。保留较大的值,下面的数值之事官方要求的最小值。可以根据系统调整数值。以优化系统性能。具体参数说明请详见[内核参数说明](# 6.内核参数说明)</li>
</ol>
<pre><code><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">vim /etc/sysctl.conf</span><br><span class="line"></span><br><span class="line">fs.aio-max-nr = 1048576</span><br><span class="line">fs.file-max = 6815744</span><br><span class="line">kernel.shmall = 2097152</span><br><span class="line">kernel.shmmax = 536870912</span><br><span class="line">kernel.shmmni = 4096</span><br><span class="line">kernel.sem = 250 32000 100 128</span><br><span class="line">net.ipv4.ip_local_port_range = 9000 65500</span><br><span class="line">net.core.rmem_default = 262144</span><br><span class="line">net.core.rmem_max = 4194304</span><br><span class="line">net.core.wmem_default = 262144</span><br><span class="line">net.core.wmem_max = 1048576</span><br></pre></td></tr></table></figure></code></pre><ol>
<li><p>使内核参数生效</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">sysctl -p 或者执行:/sbin/sysctl -p</span><br></pre></td></tr></table></figure></li>
<li><p>在<code>/etc/security/limits.conf</code>中添加如下参数</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">vim /etc/security/limits.conf</span><br><span class="line"></span><br><span class="line">oracle soft nproc 2047</span><br><span class="line">oracle hard nproc 16384</span><br><span class="line">oracle soft nofile 1024</span><br><span class="line">oracle hard nofile 65536</span><br><span class="line">oracle soft stack 10240</span><br></pre></td></tr></table></figure></li>
<li><p>在<code>/etc/pam.d/login</code>文件中,添加下面内容</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">session required /lib64/security/pam_limits.so</span><br><span class="line">session required pam_limits.so</span><br></pre></td></tr></table></figure></li>
<li><p><code>/etc/profile</code>文件中添加如下内容</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">if [ $USER = "oracle" ]; then</span><br><span class="line"> if [ $SHELL = "/bin/ksh" ]; then</span><br><span class="line"> ulimit -p 16384</span><br><span class="line"> ulimit -n 65536</span><br><span class="line"> else</span><br><span class="line"> ulimit -u 16384 -n 65536</span><br><span class="line"> fi</span><br><span class="line">fi</span><br></pre></td></tr></table></figure></li>
<li><p>使用<code>/etc/profile</code>文件生效</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">source /etc/profile</span><br></pre></td></tr></table></figure></li>
<li><p>禁用使用Transparent HugePages(启用Transparent HugePages,可能会导致造成内存在运行时的延迟分配,Oracle官方建议使用标准的HugePages)</p>
<ul>
<li><p>查看是否启用 如果显示 [always]说明启用了</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">cat /sys/kernel/mm/transparent_hugepage/enabled</span><br></pre></td></tr></table></figure></li>
<li><p>禁用Transparent HugePages,在/etc/grub.conf添加如下内容</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">echo never > /sys/kernel/mm/transparent_hugepage/enabled</span><br></pre></td></tr></table></figure></li>
<li><p>重启系统以使更改成为永久更改:<code>reboot</code></p>
</li>
</ul>
</li>
</ol>
<h2 id="6-内核参数说明"><a href="#6-内核参数说明" class="headerlink" title="6.内核参数说明"></a>6.内核参数说明</h2><ul>
<li><code>fs.aio-max-nr</code>:同时可以拥有的的异步IO请求数目。1048576 即 1024*1024 也就是 1024K 个</li>
<li><code>fs.file-max</code>:系统允许打开的文件数。</li>
<li><code>kernel.shmall</code>:设置共享内存总页数。这个值太小有可能导致数据库启动报错,计算公式为:内存(G)* 1024 <em>1024</em>*1024/4096,4096为getconfPAGE_SIZE得到分页大小</li>
<li><code>kernel.shmmax</code>:Linux进程可以分配的单独共享内存段的最大值。一般设置为内存总大小的一半。这个值的设置应该大于SGA_MAX_TARGET或MEMORY_MAX_TARGET的值,因此对于安装Oracle数据库的系统,shmmax的值应该比内存的二分之一大一些。</li>
<li><code>kernel.shmmni</code>:设置系统级最大共享内存段数量。推荐最小值为4096</li>
<li><code>kernel.sem</code>:从左到右分别为SEMMSL、SEMMNS、SEMOPM和SEMMNI。<ul>
<li><code>SEMMSL</code>:设置每个信号灯组中信号灯最大数量,推荐的最小值是250。对于系统中存在大量并发连接的系统,推荐将这个值设置为PROCESSES初始化参数加10</li>
<li><code>SEMMNS</code>:设置系统中信号灯的最大数量。操作系统在分配信号灯时不会超过LEAST(SEMMNS,SEMMSL<em>SEMMNI)。事实上,如果SEMMNS的值超过了SEMMSL</em>SEMMNI是非法的,因此推荐SEMMNS的值就设置为SEMMSL<em>SEMMNI。Oracle推荐SEMMNS的设置不小于32000,假如数据库的PROCESSES参数设置为600,则SEMMNS的设置应为:SQL> select (600+10)</em>142 from dual;(600+10)*142/86620</li>
<li><code>SEMOPM</code>:设置每次系统调用可以同时执行的最大信号灯操作的数量。由于一个信号灯组最多拥有SEMMSL个信号灯,因此有推荐将SEMOPM设置为SEMMSL的值。Oracle验证的10.2和11.1的SEMOPM的配置为100。</li>
<li><code>SEMMNI</code>:设置系统中信号灯组的最大数量。Oracle10g和11g的推荐值为142</li>
</ul>
</li>
<li><code>net.ipv4.ip_local_port_range</code>:ip_local_port_range表示端口的范围,为指定的内容</li>
<li><code>net.core.rmem_default</code>:表示接收套接字缓冲区大小的缺省值(以字节为单位)。</li>
<li><code>net.core.rmem_max</code>:表示接收套接字缓冲区大小的最大值(以字节为单位)</li>
<li><code>net.core.wmem_default</code>:表示发送套接字缓冲区大小的缺省值(以字节为单位)</li>
<li><code>net.core.wmem_max</code>:表示发送套接字缓冲区大小的最大值(以字节为单位)</li>
</ul>
<h2 id="7-创建oracle安装目录"><a href="#7-创建oracle安装目录" class="headerlink" title="7. 创建oracle安装目录"></a>7. 创建oracle安装目录</h2><p>如下目录,可以根据自己的实际情况可做修改。我这里使用的是<code>/u01</code>作为根目录的,这样在以后的配置的时候感觉会省很多的事情。主要也是为了与公司产品数据库安装的时候配置路径保持一致。</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">mkdir -p /u01/app/oracle </span><br><span class="line">mkdir -p /u01/app/oraInventory</span><br><span class="line">chown -R oracle:oinstall /u01/</span><br><span class="line">chown -R oracle:oinstall /u01/app/oracle</span><br><span class="line">chown -R oracle:oinstall /u01/app/oraInventory</span><br><span class="line">chmod -R 775 /u01/</span><br></pre></td></tr></table></figure>
<p>部分路径说明</p>
<ul>
<li>./app/oracle/product/11.2.0 :数据库系统安装目录</li>
<li>./app/oracle/backup:数据备份目录</li>
<li>./app/oracle/oraInventory:清单目录</li>
</ul>
<h2 id="8-配置oracle用户环境变量"><a href="#8-配置oracle用户环境变量" class="headerlink" title="8. 配置oracle用户环境变量"></a>8. 配置oracle用户环境变量</h2><p>在文件<code>~/.bash_profile</code>里添加下面的内容(具体值根据实际情况修改)<br>切换到oracle用户:<code>su - oracle</code></p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">TMP=/tmp; export TMP</span><br><span class="line">TMPDIR=$TMP; export TMPDIR</span><br><span class="line">ORACLE_UNQNAME=ecology; export ORACLE_UNQNAME</span><br><span class="line">ORACLE_BASE=/u01/app/oracle; export ORACLE_BASE</span><br><span class="line">ORACLE_HOME=/u01/app/oracle/product/11.2.0/db_1; export ORACLE_HOME</span><br><span class="line">ORACLE_SID=ecology; export ORACLE_SID</span><br><span class="line">PATH=$PATH:/u01/app/oracle/product/11.2.0/db_1/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin; export PATH</span><br><span class="line">LD_LIBRARY_PATH=/lib:/lib:/usr/lib:$ORACLE_HOME/lib; exoprt LD_LIBRARY_PATH</span><br></pre></td></tr></table></figure>
<h2 id="9-重启一下系统"><a href="#9-重启一下系统" class="headerlink" title="9. 重启一下系统"></a>9. 重启一下系统</h2><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">reboot</span><br></pre></td></tr></table></figure>
<h2 id="10-解压下载好的两个Oracle数据库"><a href="#10-解压下载好的两个Oracle数据库" class="headerlink" title="10. 解压下载好的两个Oracle数据库"></a>10. 解压下载好的两个Oracle数据库</h2><p><span style="color:red"><strong>解压的顺序需要注意一下,先解压一,然后在解压二</strong></span><br>将上传到系统的oracle安装包解压到上边新建的<code>/u01</code>路径下</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">unzip -q linux.x64_11gR2_database_1of2.zip -d /u01</span><br><span class="line">unzip -q linux.x64_11gR2_database_2of2.zip -d /u01</span><br></pre></td></tr></table></figure>
<p>创建一个新的文件夹,将解压后的应答文件放到新建的文件夹中,或者是在原路径下copy一份文件,最好是不要在原文件上修改</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">mkdir -p /data/etc</span><br><span class="line"># 复制响应文件</span><br><span class="line">cp /u01/database/response/* /data/etc/</span><br><span class="line">#设置响应文件权限</span><br><span class="line">su - root</span><br><span class="line">chmod 700 /data/etc/*.rsp</span><br></pre></td></tr></table></figure>
<p>复制到<code>/data/etc</code>目录下的文件作用</p>
<ul>
<li>db_install.rsp:安装应答</li>
<li>dbca.rsp:创建数据库应答</li>
<li>netca.rsp:建立监听、本地服务名等网络设置应答</li>
<li>特别是组件配置事后请用如右语句查询核实(select comp_id, comp_name, version, status from dba_registry)</li>
</ul>
<p>在<code>/data/etc/db_install.rsp</code>修改以下变量的值<br><span style="color:red">在配置对应的文件前,请先阅读 11.<code>db_install.rsp</code>应答文件参数说明 </span></p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">cd /data/etc/</span><br><span class="line">vim db_install.rsp</span><br><span class="line"></span><br><span class="line">oracle.install.option=INSTALL_DB_SWONLY</span><br><span class="line">ORACLE_HOSTNAME=xiaofeifei #用hostname命令查看</span><br><span class="line">UNIX_GROUP_NAME=oinstall</span><br><span class="line">INVENTORY_LOCATION=/u01/app/oracle/inventory</span><br><span class="line">SELECTED_LANGUAGES=en,zh_CN</span><br><span class="line">ORACLE_HOME=/u01/app/oracle/product/11.2.0/db_1</span><br><span class="line">ORACLE_BASE=/u01/app/oracle</span><br><span class="line">oracle.install.db.InstallEdition=EE</span><br><span class="line">oracle.install.db.DBA_GROUP=dba</span><br><span class="line">oracle.install.db.OPER_GROUP=dba</span><br><span class="line">DECLINE_SECURITY_UPDATES=true</span><br></pre></td></tr></table></figure>
<h2 id="11-db-install-rsp应答文件参数说明"><a href="#11-db-install-rsp应答文件参数说明" class="headerlink" title="11.db_install.rsp应答文件参数说明"></a>11.<code>db_install.rsp</code>应答文件参数说明</h2><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">oracle.install.responseFileVersion=/oracle/install/rspfmt_dbinstall_response_schema_v11_2_0//标注响应文件版本.注解文件版本,这个版本必须和要#安装的数据库版本相同,安装检验无法通过,不能更改 </span><br><span class="line"> oracle.install.option=INSTALL_DB_SWONLY #INSTALL_DB_SWONLY:只装数据库软件;INSTALL_DB_AND_CONFIG:安装数据库软件并建库;UPGRADE_DB:升级数据库</span><br><span class="line"> ORACLE_HOSTNAME=主机名 #指定操作系统主机名,通过hostname 命令获得</span><br><span class="line"> UNIX_GROUP_NAME=oinstall//指定oracle inventory目录的所有者</span><br><span class="line"> INVENTORY_LOCATION=/u01/app/oraInventory指定产品清单oracle inventory目录的路径,win平台下可以生路</span><br><span class="line"> SELECTED_LANGUAGES=en,zh_CN//指定语言,可以选择多个,用逗号隔开。选择en, zh_CN(英文和简体中文)</span><br><span class="line"> ORACLE_HOME=/u01/app/oracle/product/11.2.0/db_home1//设置ORALCE_HOME的路径</span><br><span class="line"> ORACLE_BASE=/u01/app/oracle//指定ORALCE_BASE的路径</span><br><span class="line"> oracle.install.db.InstallEdition=EE//安装数据库软件的版本,EE:企业版;SE:标准版;SEONE:标准版1</span><br><span class="line"> oracle.install.db.EEOptionsSelection=true//手动指定企业安装组件 选true对安装大小影响不大所以选true</span><br><span class="line"> oracle.install.db.optionalComponents=oracle.rdbms.partitioning:11.2.0.4.0,oracle.oraolap:11.2.0.4.0,oracle.rdbms.dm:11.2.0.4.0,oracle.rdbms.dv:11.2.0.4.0,oracle.rdbms.lbac:11.2.0.4.0,oracle.rdbms.rat:11.2.0.4.0//如果上面选true,这些就是手动指定的组件</span><br><span class="line"> oracle.install.db.DBA_GROUP=dba//指定拥有DBA用户组,通常会是dba组</span><br><span class="line"> oracle.install.db.OPER_GROUP=oper//指定oper用户组</span><br><span class="line"> oracle.install.db.CLUSTER_NODES=//指定所有的节点</span><br><span class="line"> oracle.install.db.isRACOneInstall=false//是否是RACO方式安装</span><br><span class="line"> oracle.install.db.racOneServiceName=</span><br><span class="line"> oracle.install.db.config.starterdb.type=//选择数据库的用途,一般用途/事物处理,数据仓库</span><br><span class="line"> oracle.install.db.config.starterdb.globalDBName=指定GlobalName</span><br><span class="line"> oracle.install.db.config.starterdb.SID=//你指定的SID</span><br><span class="line"> oracle.install.db.config.starterdb.characterSet=AL32UTF8//设置数据库编码</span><br><span class="line"> oracle.install.db.config.starterdb.memoryOption=true//11g的新特性自动内存管理,也就是SGA_TARGET和PAG_AGGREGATE_TARGET都,不用设置了,Oracle会自动调配两部分大小,这个要选true</span><br><span class="line"> oracle.install.db.config.starterdb.memoryLimit=上面是true的情况下,指定Oracle自动管理内存的大小,这里不用填或者填写物理内存的40%左右等等自定义;</span><br><span class="line"> oracle.install.db.config.starterdb.installExampleSchemas=false是否载入模板示例</span><br><span class="line"> oracle.install.db.config.starterdb.enableSecuritySettings=true 是否启用安全设置</span><br><span class="line"> oracle.install.db.config.starterdb.password.ALL=123456所有用户名的密码</span><br><span class="line"> oracle.install.db.config.starterdb.password.SYS=</span><br><span class="line"> oracle.install.db.config.starterdb.password.SYSTEM=</span><br><span class="line"> oracle.install.db.config.starterdb.password.SYSMAN=</span><br><span class="line"> oracle.install.db.config.starterdb.password.DBSNMP=</span><br><span class="line"> oracle.install.db.config.starterdb.control=DB_CONTROL数据库本地管理工具DB_CONTROL,远程集中管理工具GRID_CONTROL</span><br><span class="line"> oracle.install.db.config.starterdb.gridcontrol.gridControlServiceURL=GRID_CONTROL需要设定grid control的远程路径URL</span><br><span class="line"> oracle.install.db.config.starterdb.automatedBackup.enable=false设置自动备份</span><br><span class="line"> oracle.install.db.config.starterdb.automatedBackup.osuid=.自动备份会启动一个job,指定启动JOB的系统用户ID</span><br><span class="line"> oracle.install.db.config.starterdb.automatedBackup.ospwd=自动备份会开启一个job,需要指定OSUser的密码</span><br><span class="line"> oracle.install.db.config.starterdb.storageType=要求指定使用的文件系统存放数据库文件还是ASM</span><br><span class="line"> oracle.install.db.config.starterdb.fileSystemStorage.dataLocation=使用文件系统存放数据库文件才需要指定数据文件、控制文件、Redo log的存放目录</span><br><span class="line"> oracle.install.db.config.starterdb.fileSystemStorage.recoveryLocation=使用文件系统存放数据库文件才需要指定备份恢复目录</span><br><span class="line"> oracle.install.db.config.asm.diskGroup=使用ASM存放数据库文件才需要指定存放的磁盘组</span><br><span class="line"> oracle.install.db.config.asm.ASMSNMPPassword=使用ASM存放数据库文件才需要指定ASM实例密码</span><br><span class="line"> MYORACLESUPPORT_USERNAME=指定metalink账户用户名</span><br><span class="line"> MYORACLESUPPORT_PASSWORD=指定metalink账户密码</span><br><span class="line"> SECURITY_UPDATES_VIA_MYORACLESUPPORT=用户是否可以设置metalink密码</span><br><span class="line"> DECLINE_SECURITY_UPDATES=true是否设置安全更新,</span><br><span class="line"> PROXY_HOST=代理服务器名</span><br><span class="line"> PROXY_PORT=代理服务器端口</span><br><span class="line"> PROXY_USER=代理服务器用户名</span><br><span class="line"> PROXY_PWD=代理服务器密码</span><br><span class="line"> PROXY_REALM=</span><br><span class="line"> COLLECTOR_SUPPORTHUB_URL=</span><br><span class="line"> oracle.installer.autoupdates.option=SKIP_UPDATES 自动更新</span><br><span class="line"> oracle.installer.autoupdates.downloadUpdatesLoc=自动更新下载目录</span><br><span class="line"> AUTOUPDATES_MYORACLESUPPORT_USERNAME=自动更新的用户名</span><br><span class="line"> AUTOUPDATES_MYORACLESUPPORT_PASSWORD=自动更新的密码</span><br></pre></td></tr></table></figure>
<h2 id="12-开始安装"><a href="#12-开始安装" class="headerlink" title="12. 开始安装"></a>12. 开始安装</h2><p>开始安装的命令:</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">su - oracle</span><br><span class="line">cd /u01/database</span><br><span class="line">./runInstaller -silent -responseFile /data/etc/db_install.rsp -ignorePrereq</span><br></pre></td></tr></table></figure>
<p>安装期间可以使用<code>tail</code>命令监看oracle的安装日志</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">#日志文件名称根据自己的实际执行时间变更,这里的日志文件会在界面上打出,直接粘贴复制就OK</span><br><span class="line">tail -f /data/app/oracle/inventory/logs/installActions2019-01-02_06-03-30PM.log</span><br></pre></td></tr></table></figure>
<p>安装完成后有如下的提示,如果有类似的提示,说明安装完成</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">You can find the log of this install session at:</span><br><span class="line"> /u01/app/oracle/inventory/logs/installActions2020-08-23_12-42-59AM.log</span><br><span class="line">The following configuration scripts need to be executed as the "root" user. </span><br><span class="line"> #!/bin/sh </span><br><span class="line"> #Root scripts to run</span><br><span class="line"></span><br><span class="line">/u01/app/oracle/inventory/orainstRoot.sh</span><br><span class="line">/u01/app/oracle/product/11.2.0/db_1/root.sh</span><br><span class="line">To execute the configuration scripts:</span><br><span class="line"> 1. Open a terminal window </span><br><span class="line"> 2. Log in as "root" </span><br><span class="line"> 3. Run the scripts </span><br><span class="line"> 4. Return to this window and hit "Enter" key to continue </span><br><span class="line"></span><br><span class="line">Successfully Setup Software.</span><br></pre></td></tr></table></figure>
<p>日志部分内容说明<br><img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1gi0tm4nn0aj314o0j6k29.jpg" alt=""></p>
<h2 id="13-使用root用户执行对应的脚本"><a href="#13-使用root用户执行对应的脚本" class="headerlink" title="13. 使用root用户执行对应的脚本"></a>13. 使用<code>root</code>用户执行对应的脚本</h2><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">su - root</span><br><span class="line">sh /u01/app/oracle/inventory/orainstRoot.sh</span><br><span class="line">sh /u01/app/oracle/product/11.2.0/db_1/root.sh</span><br></pre></td></tr></table></figure>
<h2 id="14-配置监听程序"><a href="#14-配置监听程序" class="headerlink" title="14. 配置监听程序"></a>14. 配置监听程序</h2><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">su - oracle</span><br><span class="line">netca /silent /responsefile /data/etc/netca.rsp</span><br></pre></td></tr></table></figure>
<p>注意:这里使用的命令是:<code>netca /silent /responsefile /data/etc/netca.rsp</code><br>而不是使用的<code>netca -silent -responseFile /home/oracle/database/response/netca.rsp</code>否则会报出如下的错误</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">****DISPLAY environment variable not set!</span><br><span class="line"> Oracle Net Configuration Assistant is a GUI tool</span><br><span class="line"> which requires that DISPLAY specify a location</span><br><span class="line"> where GUI tools can display.</span><br><span class="line"> Set and export DISPLAY, then re-run.</span><br></pre></td></tr></table></figure>
<p>如果使用的是xmanage的连接工具,可以使用如下的命令进行设置(这个没有进行测试过)</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">解决方法:export DISPLAY=192.168.8.146:0.0</span><br><span class="line">export DISPLAY=自己的ip:0.0</span><br><span class="line">也有说的是把0.0改成1.0具体情况需要可以进行百度,网上有好多的处理方法</span><br></pre></td></tr></table></figure>
<p>查看监听端口:</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">netstat -tnpl | grep 1521</span><br><span class="line">[oracle@xiaofeifei database]$ netstat -tnpl | grep 1521</span><br><span class="line"></span><br><span class="line">tcp6 0 0 :::1521 :::* LISTEN 8943/tnslsnr</span><br></pre></td></tr></table></figure>
<h2 id="15-静默创建数据库"><a href="#15-静默创建数据库" class="headerlink" title="15. 静默创建数据库"></a>15. 静默创建数据库</h2><ol>
<li><p>编辑应答文件<code>/data/etc/dbca.rsp</code>,部分参数说明请详见 [16.静默创建数据库部分参数说明](# 16. 静默创建数据库部分参数说明)</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">GDBNAME = "ecology"</span><br><span class="line">SID = "ecology"</span><br><span class="line">SYSPASSWORD = "192612"</span><br><span class="line">SYSTEMPASSWORD = "192612"</span><br><span class="line">CHARACTERSET = "AL32UTF8"</span><br><span class="line">INSTANCENAME = "ecology"</span><br><span class="line">TOTALMEMORY = "800"</span><br><span class="line">DB_UNIQUE_NAME = "ecology"</span><br></pre></td></tr></table></figure></li>
<li><p>执行静默建库</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">su - oracle</span><br><span class="line">dbca -silent -responseFile /data/etc/dbca.rsp</span><br></pre></td></tr></table></figure></li>
<li><p>执行过程如下</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">[oracle@xiaofeifei database]$ dbca -silent -responseFile /data/etc/dbca.rsp</span><br><span class="line">Copying database files </span><br><span class="line">1% complete </span><br><span class="line">3% complete </span><br><span class="line">11% complete </span><br><span class="line">18% complete </span><br><span class="line">26% complete </span><br><span class="line">37% complete </span><br><span class="line">Creating and starting Oracle instance </span><br><span class="line">40% complete </span><br><span class="line">45% complete </span><br><span class="line">50% complete</span><br><span class="line">55% complete</span><br><span class="line">56% complete</span><br><span class="line">60% complete</span><br><span class="line">62% complete</span><br><span class="line">Completing Database Creation</span><br><span class="line">66% complete</span><br><span class="line">70% complete</span><br><span class="line">73% complete</span><br><span class="line">85% complete</span><br><span class="line">96% complete</span><br><span class="line">100% complete</span><br><span class="line">Look at the log file "/u01/app/oracle/cfgtoollogs/dbca/ecology/ecology.log" for further details.</span><br></pre></td></tr></table></figure></li>
<li><p>查看进程</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">ps -ef | grep ora_ | grep -v grep</span><br></pre></td></tr></table></figure></li>
<li><p>查看监听状态</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">lsnrctl status</span><br></pre></td></tr></table></figure>
</li>
</ol>
<h2 id="16-静默创建数据库部分参数说明"><a href="#16-静默创建数据库部分参数说明" class="headerlink" title="16. 静默创建数据库部分参数说明"></a>16. 静默创建数据库部分参数说明</h2><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">RESPONSEFILE_VERSION = "11.2.0" #不能更改</span><br><span class="line"> OPERATION_TYPE = "createDatabase" #不变 默认即可</span><br><span class="line"> GDBNAME = "orcl" #数据库的名字,随便怎么改,不影响,自定义 比如"oracle11g.com"</span><br><span class="line"> SID = "orcl" #对应的实例名字instance_name,随便怎么改,不影响,自定义 比如"oracle11g.com"</span><br><span class="line"> TEMPLATENAME = "General_Purpose.dbc" #不变 默认即可 建库用的模板文件</span><br><span class="line"> SYSPASSWORD = "oracle" #SYS管理员密码 自定义</span><br><span class="line"> SYSTEMPASSWORD = "oracle" #SYSTEM管理员密码 自定义</span><br><span class="line"> DATAFILEDESTINATION = /data/app/oracle/oradata #数据文件存放目录 可自定义如/data目录但用户和组要是oracle:oinstall即mkdir /data ; chown -R oracle:oinstall /data ; chmod -R 775 /data; 当然此项默认是$ORACLE_BASE/oradata 此项本来是注释掉的,而$ORACLE_BASE/oradata就是/data/app/oracle/oradata,故此项可不用动,即保持注释掉即也可;</span><br><span class="line"> RECOVERYAREADESTINATION=/data/app/oracle/flash_recovery_area #恢复数据存放目录 闪回区 可自定义</span><br><span class="line"> CHARACTERSET = "AL32UTF8" #字符集,重要!!! 建库后一般不能更改,所以建库前要确定清楚,选AL32UTF8比较合适;</span><br><span class="line"> NATIONALCHARACTERSET= "AL16UTF16" #最好还是保持默认的"AL16UTF16",这里我选的UTF8可能出现了后面的PLSQL中文乱码;</span><br><span class="line"> #MEMORYPERCENTAGE = "40" #服务器物理内存分配给oracle的内存比例,这里先不选</span><br><span class="line"> #TOTALMEMORY = "3500" # //物理内存的60%左右,分配给oracle的总内存3500MB,可自行设定 或者注释不选因为oracle会自行管理;</span><br><span class="line"> #-----------------------*** End of CREATEDATABASE section ***------------------------</span><br><span class="line"> SOURCEDB = "myhost:1521:orcl" #End of CREATEDATABASE sectio后面的很多没有注释掉的选项不用管。</span><br></pre></td></tr></table></figure>
<h2 id="17-至此数据库就安装成功了,下面我们登陆下数据库"><a href="#17-至此数据库就安装成功了,下面我们登陆下数据库" class="headerlink" title="17. 至此数据库就安装成功了,下面我们登陆下数据库"></a>17. 至此数据库就安装成功了,下面我们登陆下数据库</h2><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">su - oracle</span><br><span class="line">sqlplus / as sysdba</span><br><span class="line">select status from v$instance;</span><br></pre></td></tr></table></figure>
<p>执行<code>select</code>时,会出现以下情况</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SQL> select status from v$instance;</span><br><span class="line">select status from v$instance</span><br><span class="line">*</span><br><span class="line">ERROR at line 1:</span><br><span class="line">ORA-01034: ORACLE not available</span><br><span class="line">Process ID: 0</span><br><span class="line">Session ID: 0 Serial number: 0</span><br></pre></td></tr></table></figure>
<p>解决方式:<br>输入<code>startup</code></p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">#startup的输出提示:</span><br><span class="line">SQL> statup</span><br><span class="line">SP2-0042: unknown command "statup" - rest of line ignored.</span><br><span class="line">SQL> startup</span><br><span class="line">#如果是在/u01的路径下则不会出现以下问题</span><br><span class="line">#ORA-01078: failure in processing system parameters</span><br><span class="line">#LRM-00109: could not open parameter file '/data/app/oracle/product/11.2.0/dbs/initORCL.ora'</span><br></pre></td></tr></table></figure>
<p>激活scott用户:</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">alter user scott account unlock;</span><br><span class="line">alter user scott identified by tiger;</span><br><span class="line">select username,account_status from dba_users;</span><br></pre></td></tr></table></figure>
<h2 id="18-设置Oracle开机启动"><a href="#18-设置Oracle开机启动" class="headerlink" title="18. 设置Oracle开机启动"></a>18. 设置Oracle开机启动</h2><p><span style="color:red"><strong><em>这个步骤还未进行操作….</em></strong></span></p>
<h2 id="19-结合业务系统配置ecology数据库"><a href="#19-结合业务系统配置ecology数据库" class="headerlink" title="19. 结合业务系统配置ecology数据库"></a>19. 结合业务系统配置ecology数据库</h2><ol>
<li><p>创建Ecology数据库用户前的检查工作,首选需要确认监听是否正常</p>
<ul>
<li><p>切换到oracle用户下:<code>su - oracle</code></p>
</li>
<li><p>启动监听:<code>lsnrctl start</code></p>
</li>
<li><p>查看监听状态:<code>lsnrctl status</code></p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">[oracle@xiaofeifei database]$ lsnrctl status</span><br><span class="line">LSNRCTL for Linux: Version 11.2.0.1.0 - Production on 23-AUG-2020 16:49:31 </span><br><span class="line">Copyright (c) 1991, 2009, Oracle. All rights reserved. </span><br><span class="line">Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=IPC)(KEY=EXTPROC1521)))</span><br><span class="line">STATUS of the LISTENER</span><br><span class="line">Alias LISTENER</span><br><span class="line">Version TNSLSNR for Linux: Version 11.2.0.1.0 - Production</span><br><span class="line">Start Date 23-AUG-2020 11:02:27</span><br><span class="line">Uptime 0 days 5 hr. 47 min. 4 sec</span><br><span class="line">Trace Level off</span><br><span class="line">Security ON: Local OS Authentication</span><br><span class="line">SNMP OFF</span><br><span class="line">Listener Parameter File /u01/app/oracle/product/11.2.0/db_1/network/admin/listener.ora</span><br><span class="line">Listener Log File /u01/app/oracle/diag/tnslsnr/xiaofeifei/listener/alert/log.xml</span><br><span class="line">Listening Endpoints Summary...</span><br><span class="line"> (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521)))</span><br><span class="line"> (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=xiaofeifei)(PORT=1521)))</span><br><span class="line">Services Summary...</span><br><span class="line">Service "ecology" has 1 instance(s).</span><br><span class="line"> Instance "ecology", status READY, has 1 handler(s) for this service...</span><br><span class="line">Service "ecologyXDB" has 1 instance(s).</span><br><span class="line"> Instance "ecology", status READY, has 1 handler(s) for this service...</span><br><span class="line">The command completed successfully</span><br></pre></td></tr></table></figure></li>
<li><p>使用sysdba登录后,查看数据库状态(READ WRITE 为正常读写状态)</p>
</li>
<li><p>命令:<code>select open_mode from v$database;</code></p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SQL> select open_mode from v$database;</span><br><span class="line">OPEN_MODE</span><br><span class="line">READ WRITE</span><br></pre></td></tr></table></figure>
<p> <img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1gi0uit4zvzj31da0oogoq.jpg" alt=""></p>
</li>
<li><p>查看数据库数据文件存放目录。命令为:<code>select name from v$datafile;</code></p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SQL> select name from v$datafile;</span><br><span class="line"></span><br><span class="line">NAME</span><br><span class="line"></span><br><span class="line">/u01/app/oracle/oradata/ecology/system01.dbf</span><br><span class="line">/u01/app/oracle/oradata/ecology/sysaux01.dbf</span><br><span class="line">/u01/app/oracle/oradata/ecology/undotbs01.dbf</span><br><span class="line">/u01/app/oracle/oradata/ecology/users01.dbf</span><br><span class="line">/u01/app/oracle/oradata/ecology/ecology.dbf</span><br></pre></td></tr></table></figure>
<p> <img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1gi0uk1jac2j31580qa41x.jpg" alt=""></p>
</li>
</ul>
</li>
<li><p>创建OA数据库使用用户和表空间,并赋予用户权限</p>
<ul>
<li><p>创建OA 程序使用的表空间,表空间名为ECOLOGY,存储路径为/u01/app/oracle/oradata/ecology/ecology.dbf(此处存储路径是根据上面查询数据库数据文件存放目录确定的)。相关命令</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">create tablespace ecology datafile '/u01/app/oracle/oradata/ecology/ecology.dbf' size 2000M</span><br><span class="line">AutoExtend On Next 10M</span><br><span class="line">segment space management auto;</span><br></pre></td></tr></table></figure>
</li>
<li><p>创建用户,并关联用户使用的表空间和临时表空间,相关命令</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">CREATE USER ecology IDENTIFIED BY ecology DEFAULT TABLESPACE ecology TEMPORARY TABLESPACE Temp;</span><br></pre></td></tr></table></figure></li>
<li><p>赋予OA 数据库用户权限(金融等行业客户无法给予DBA 权限),相关命令为:</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">grant connect,resource to ecology;</span><br><span class="line">alter user ecology quota unlimited on ecology;</span><br><span class="line">grant create view to ecology;</span><br><span class="line">grant DATAPUMP_EXP_FULL_DATABASE ,DATAPUMP_IMP_FULL_DATABASE to ecology;</span><br></pre></td></tr></table></figure></li>
<li><p>需要使用sysdba 用户执行,避免初始化时出现报错。相关命令为</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">alter system set "_allow_level_without_connect_by" = true;</span><br></pre></td></tr></table></figure></li>
<li><p>(对于上述第三步操作说明)需要修改此参数主要是因为我们脚本中有些递归查询和上下级的查询使用了level 关<br>键字,此语法在Oracle9i 版本中默认兼容支持,在10g 及之后的版本中需要修改上面的参<br>数之后才能支持。</p>
</li>
</ul>
</li>
</ol>
]]></content>
<tags>
<tag>oracle</tag>
</tags>
</entry>
<entry>
<title>Activiti6.0工作流引擎学习(二) -- 引擎配置</title>
<url>/posts/1d8f65f1.html</url>
<content><![CDATA[<h1 id="Activiti6-0工作流引擎学习-二-–-引擎配置"><a href="#Activiti6-0工作流引擎学习-二-–-引擎配置" class="headerlink" title="Activiti6.0工作流引擎学习(二) – 引擎配置"></a>Activiti6.0工作流引擎学习(二) – 引擎配置</h1><h2 id="1-Activiti流程引擎配置涉及的类"><a href="#1-Activiti流程引擎配置涉及的类" class="headerlink" title="1.Activiti流程引擎配置涉及的类"></a>1.Activiti流程引擎配置涉及的类</h2><p><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1gf6rcr6dzaj31b40nw4bp.jpg" alt="Activiti流程引擎配置"></p>
<h3 id="1-1-流程引擎配置类的作用"><a href="#1-1-流程引擎配置类的作用" class="headerlink" title="1.1 流程引擎配置类的作用"></a>1.1 流程引擎配置类的作用</h3><ol>
<li>ProcessEngineConfiguration<ul>
<li>查找并解析xml配置文件activiti.cfg.xml</li>
<li>提供多个静态方法创建配置对象<br> <img src="https://tva1.sinaimg.cn/large/007S8ZIlly1gf6rna78thj310407mwsc.jpg" alt="静态方法创建配置对象"></li>
<li>实现几个基于不同场景场景的子类,配置方式非常的灵活<br><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1gf6ry2ub4qj318808oat3.jpg" alt="使用场景"></li>
</ul>
</li>
</ol>
<h3 id="1-2-部分子类的使用"><a href="#1-2-部分子类的使用" class="headerlink" title="1.2 部分子类的使用"></a>1.2 部分子类的使用</h3><figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testConfig1</span><span class="params">()</span></span>{</span><br><span class="line"> <span class="comment">//通过默认来创建引擎;会通过spring的解析进行创建引擎对象</span></span><br><span class="line"> ProcessEngineConfiguration engine</span><br><span class="line"> = ProcessEngineConfiguration.createProcessEngineConfigurationFromResourceDefault();</span><br><span class="line"> log.info(<span class="string">"engine ={}"</span>,engine);</span><br><span class="line"> }</span><br><span class="line"> <span class="meta">@Test</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testConfig2</span><span class="params">()</span></span>{</span><br><span class="line"> <span class="comment">//直接创建标准的引擎,直接new一个引擎对象</span></span><br><span class="line"> ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createStandaloneProcessEngineConfiguration();</span><br><span class="line"> log.info(<span class="string">"configuration = {}"</span>,configuration);</span><br><span class="line"> }</span><br></pre></td></tr></table></figure>
<h2 id="2-数据库配置"><a href="#2-数据库配置" class="headerlink" title="2.数据库配置"></a>2.数据库配置</h2><h3 id="2-1-缺省配置默认,使用H2内存数据库"><a href="#2-1-缺省配置默认,使用H2内存数据库" class="headerlink" title="2.1 缺省配置默认,使用H2内存数据库"></a>2.1 缺省配置默认,使用H2内存数据库</h3><h3 id="2-2-配置JDBC属性,使用mybatis提供的链接池"><a href="#2-2-配置JDBC属性,使用mybatis提供的链接池" class="headerlink" title="2.2 配置JDBC属性,使用mybatis提供的链接池"></a>2.2 配置JDBC属性,使用mybatis提供的链接池</h3><p><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1gf6trk2he0j315o0bydor.jpg" alt=""></p>
<h3 id="2-3-配置DataSource,可自选第三方实现"><a href="#2-3-配置DataSource,可自选第三方实现" class="headerlink" title="2.3 配置DataSource,可自选第三方实现"></a>2.3 配置DataSource,可自选第三方实现</h3><ol>
<li>Druid 为监控而生的数据库连接池 来自阿里<br><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1gf6tzwnzmgj31a806awm5.jpg" alt=""><br><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1gf6u1fe54sj319o0c417z.jpg" alt=""></li>
<li>Dbcp 老牌数据源连接池,稳定可靠,Tomcat自带</li>
<li>HikariCP 来自日本的极速数据源连接池,Spring默选</li>
</ol>
<h3 id="2-4-数据库更新策略"><a href="#2-4-数据库更新策略" class="headerlink" title="2.4 数据库更新策略"></a>2.4 数据库更新策略</h3><ol>
<li>配置databaseSchemaUpdate<ul>
<li>false:启动时检查数据库版本,发生不匹配抛异常</li>
<li>true: 启动时自动检查并甭信数据库表,不存在会创建</li>
<li>create-drop: 启动时创建数据库表结构,结束时删除表结构</li>
</ul>
</li>
</ol>
<h3 id="2-5-数据库配置编码"><a href="#2-5-数据库配置编码" class="headerlink" title="2.5 数据库配置编码"></a>2.5 数据库配置编码</h3><h4 id="2-5-1-使用默认的配置文件创建流程引擎"><a href="#2-5-1-使用默认的配置文件创建流程引擎" class="headerlink" title="2.5.1 使用默认的配置文件创建流程引擎"></a>2.5.1 使用默认的配置文件创建流程引擎</h4><p><code>activiti.cfg.xml</code>文件</p>
<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line"><span class="meta"><?xml version="1.0" encoding="UTF-8"?></span></span><br><span class="line"></span><br><span class="line"><span class="tag"><<span class="name">beans</span> <span class="attr">xmlns</span>=<span class="string">"http://www.springframework.org/schema/beans"</span></span></span><br><span class="line"><span class="tag"> <span class="attr">xmlns:xsi</span>=<span class="string">"http://www.w3.org/2001/XMLSchema-instance"</span></span></span><br><span class="line"><span class="tag"> <span class="attr">xsi:schemaLocation</span>=<span class="string">"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="tag"><<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"processEngineConfiguration"</span></span></span><br><span class="line"><span class="tag"> <span class="attr">class</span>=<span class="string">"org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration"</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="comment"><!--<property name="jdbcUrl" value="jdbc:h2:mem:activiti;DB_CLOSE_DELAY=1000" /></span></span><br><span class="line"><span class="comment"> <property name="jdbcDriver" value="org.h2.Driver" /></span></span><br><span class="line"><span class="comment"> <property name="jdbcUsername" value="sa" /></span></span><br><span class="line"><span class="comment"> <property name="jdbcPassword" value="" /></span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment"> &lt;!&ndash; Database configurations &ndash;&gt;</span></span><br><span class="line"><span class="comment"> <property name="databaseSchemaUpdate" value="drop-create" /></span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment"> <property name="history" value="full" />--></span></span><br><span class="line"> <span class="tag"></<span class="name">bean</span>></span></span><br><span class="line"></span><br><span class="line"><span class="tag"></<span class="name">beans</span>></span></span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test1</span><span class="params">()</span></span>{</span><br><span class="line"> ProcessEngineConfiguration configuration =</span><br><span class="line"> ProcessEngineConfiguration.createProcessEngineConfigurationFromResourceDefault();</span><br><span class="line"> log.info(<span class="string">"configuration = {}"</span>,configuration);</span><br><span class="line"> ProcessEngine processEngine = configuration.buildProcessEngine();</span><br><span class="line"> log.info(<span class="string">"获取流程引擎 = [{}]"</span>,processEngine.getName());</span><br><span class="line"> processEngine.close();</span><br><span class="line"> }</span><br></pre></td></tr></table></figure>
<h4 id="2-5-2-使用druid数据源"><a href="#2-5-2-使用druid数据源" class="headerlink" title="2.5.2 使用druid数据源"></a>2.5.2 使用druid数据源</h4><p>activiti_druid.cfg.xml配置文件</p>
<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line"><span class="meta"><?xml version="1.0" encoding="UTF-8"?></span></span><br><span class="line"></span><br><span class="line"><span class="tag"><<span class="name">beans</span> <span class="attr">xmlns</span>=<span class="string">"http://www.springframework.org/schema/beans"</span></span></span><br><span class="line"><span class="tag"> <span class="attr">xmlns:xsi</span>=<span class="string">"http://www.w3.org/2001/XMLSchema-instance"</span></span></span><br><span class="line"><span class="tag"> <span class="attr">xsi:schemaLocation</span>=<span class="string">"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="tag"><<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"processEngineConfiguration"</span></span></span><br><span class="line"><span class="tag"> <span class="attr">class</span>=<span class="string">"org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration"</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="comment"><!--<property name="jdbcUrl" value="jdbc:h2:mem:activiti;DB_CLOSE_DELAY=1000" /></span></span><br><span class="line"><span class="comment"> <property name="jdbcDriver" value="org.h2.Driver" /></span></span><br><span class="line"><span class="comment"> <property name="jdbcUsername" value="sa" /></span></span><br><span class="line"><span class="comment"> <property name="jdbcPassword" value="" /></span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment"> &lt;!&ndash; Database configurations &ndash;&gt;--></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"databaseSchemaUpdate"</span> <span class="attr">value</span>=<span class="string">"drop-create"</span> /></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"dataSource"</span> <span class="attr">ref</span>=<span class="string">"dataSource"</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"history"</span> <span class="attr">value</span>=<span class="string">"full"</span> /></span></span><br><span class="line"> <span class="comment"><!--引擎是否使用历史数据--></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"dbHistoryUsed"</span> <span class="attr">value</span>=<span class="string">"true"</span>/></span></span><br><span class="line"> <span class="comment"><!--引擎是否使用身份验证--></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"dbIdentityUsed"</span> <span class="attr">value</span>=<span class="string">"true"</span>/></span></span><br><span class="line"> <span class="tag"></<span class="name">bean</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="tag"><<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"dataSource"</span> <span class="attr">class</span>=<span class="string">"com.alibaba.druid.pool.DruidDataSource"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"url"</span> <span class="attr">value</span>=<span class="string">"jdbc:mysql://111.229.203.5:3306/activiti?serverTimezone=CTT<span class="symbol">&amp;</span>useUnicode=true<span class="symbol">&amp;</span>characterEncoding=utf-8<span class="symbol">&amp;</span>allowMultiQueries=true"</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"driverClassName"</span> <span class="attr">value</span>=<span class="string">"com.mysql.cj.jdbc.Driver"</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"username"</span> <span class="attr">value</span>=<span class="string">"weaver"</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"password"</span> <span class="attr">value</span>=<span class="string">"192612"</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"initialSize"</span> <span class="attr">value</span>=<span class="string">"1"</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"maxActive"</span> <span class="attr">value</span>=<span class="string">"10"</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"filters"</span> <span class="attr">value</span>=<span class="string">"stat,slf4j"</span>/></span></span><br><span class="line"> <span class="tag"></<span class="name">bean</span>></span></span><br><span class="line"></span><br><span class="line"><span class="tag"></<span class="name">beans</span>></span></span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test2</span><span class="params">()</span></span>{</span><br><span class="line"> ProcessEngineConfiguration configuration =</span><br><span class="line"> ProcessEngineConfiguration.createProcessEngineConfigurationFromResource(<span class="string">"activiti_druid.cfg.xml"</span>);</span><br><span class="line"> log.info(<span class="string">"configuration = {}"</span>,configuration);</span><br><span class="line"> ProcessEngine processEngine = configuration.buildProcessEngine();</span><br><span class="line"> log.info(<span class="string">"获取流程引擎 = [{}]"</span>,processEngine.getName());</span><br><span class="line"> processEngine.close();</span><br><span class="line"> }</span><br></pre></td></tr></table></figure>
<h2 id="3-日志记录配置"><a href="#3-日志记录配置" class="headerlink" title="3.日志记录配置"></a>3.日志记录配置</h2><h3 id="3-1-日志组件的关系及MDC"><a href="#3-1-日志组件的关系及MDC" class="headerlink" title="3.1 日志组件的关系及MDC"></a>3.1 日志组件的关系及MDC</h3><p><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1gf6z7p2plmj311o0ps194.jpg" alt=""></p>
<h4 id="3-1-1-配置开启MDC-Mapped-Diagnostic-Contexts"><a href="#3-1-1-配置开启MDC-Mapped-Diagnostic-Contexts" class="headerlink" title="3.1.1 配置开启MDC(Mapped Diagnostic Contexts)"></a>3.1.1 配置开启MDC(Mapped Diagnostic Contexts)</h4><ol>
<li>默认没有开启,需要手动设置<code>LogMDC.setMDCEnable(true)</code></li>
<li>配置logback.xml日志模版 <code>%X{mdcProcessInstanceID}</code></li>
<li>流程只有在执行过程出现异常才会记录MDC信息</li>
</ol>
<h3 id="3-2-配置历史记录级别-HistoryLevel"><a href="#3-2-配置历史记录级别-HistoryLevel" class="headerlink" title="3.2 配置历史记录级别(HistoryLevel)"></a>3.2 配置历史记录级别(HistoryLevel)</h3><h4 id="3-2-1-配置HistoryLevel"><a href="#3-2-1-配置HistoryLevel" class="headerlink" title="3.2.1 配置HistoryLevel"></a>3.2.1 配置HistoryLevel</h4><ol>
<li>none:不记录历史流程,性能高,流程结束后不可读取</li>
<li>activiti:归档流程实例和活动实例,流程变量不同步</li>
<li>addit:默认值,在activiti基础上同步变量值,保存表单属性</li>
<li>full:性能较差,记录所有实例和变量细节变化</li>
</ol>
<h3 id="3-3-配置基于db的事件日志-Event-logging"><a href="#3-3-配置基于db的事件日志-Event-logging" class="headerlink" title="3.3 配置基于db的事件日志(Event logging)"></a>3.3 配置基于db的事件日志(Event logging)</h3><h4 id="3-3-1-配置Event-Logging"><a href="#3-3-1-配置Event-Logging" class="headerlink" title="3.3.1 配置Event Logging"></a>3.3.1 配置Event Logging</h4><ol>
<li>试验性的事件记录机制,性能影响较大</li>
<li>开启默认记录所有数据的变化过程,表记录快速增长</li>
<li>日志内容json格式,建议存入mongoDB、Elastic Search</li>
</ol>
<h3 id="3-4-日志记录-mdc"><a href="#3-4-日志记录-mdc" class="headerlink" title="3.4 日志记录 mdc"></a>3.4 日志记录 mdc</h3><h2 id="4-历史记录配置"><a href="#4-历史记录配置" class="headerlink" title="4 历史记录配置"></a>4 历史记录配置</h2><figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">package</span> com.atguigu.test;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.google.common.collect.Maps;</span><br><span class="line"><span class="keyword">import</span> org.activiti.engine.history.HistoricActivityInstance;</span><br><span class="line"><span class="keyword">import</span> org.activiti.engine.history.HistoricDetail;</span><br><span class="line"><span class="keyword">import</span> org.activiti.engine.runtime.Execution;</span><br><span class="line"><span class="keyword">import</span> org.activiti.engine.runtime.ProcessInstance;</span><br><span class="line"><span class="keyword">import</span> org.activiti.engine.task.Task;</span><br><span class="line"><span class="keyword">import</span> org.activiti.engine.test.ActivitiRule;</span><br><span class="line"><span class="keyword">import</span> org.activiti.engine.test.Deployment;</span><br><span class="line"><span class="keyword">import</span> org.junit.Rule;</span><br><span class="line"><span class="keyword">import</span> org.junit.Test;</span><br><span class="line"><span class="keyword">import</span> org.slf4j.Logger;</span><br><span class="line"><span class="keyword">import</span> org.slf4j.LoggerFactory;</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"> * <span class="doctag">@program</span>: activiti6</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@description</span>: 测试MDC</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span>: Mr.Wang</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@create</span>: 2020-14-31</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">TestMDC</span> </span>{</span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">static</span> Logger log = LoggerFactory.getLogger(TestMDC<span class="class">.<span class="keyword">class</span>)</span>;</span><br><span class="line"> <span class="meta">@Rule</span></span><br><span class="line"> <span class="keyword">public</span> ActivitiRule activitiRule = <span class="keyword">new</span> ActivitiRule();</span><br><span class="line"> <span class="meta">@Test</span></span><br><span class="line"> <span class="comment">//定义流程文件部署</span></span><br><span class="line"> <span class="meta">@Deployment</span>(resources = {<span class="string">"my-process.bpmn20.xml"</span>})</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test</span><span class="params">()</span></span>{</span><br><span class="line"> Map<String,Object> params = Maps.newHashMap();</span><br><span class="line"> params.put(<span class="string">"keyStart1"</span>,<span class="string">"value1"</span>);</span><br><span class="line"> params.put(<span class="string">"keyStart2"</span>,<span class="string">"value2"</span>);</span><br><span class="line"> <span class="comment">//启动流程</span></span><br><span class="line"> ProcessInstance processInstance =</span><br><span class="line"> activitiRule.getRuntimeService().startProcessInstanceByKey(<span class="string">"my-process"</span>,params);</span><br><span class="line"> <span class="comment">//修改变量</span></span><br><span class="line"> List<Execution> executions = activitiRule.getRuntimeService().createExecutionQuery().listPage(<span class="number">0</span>, <span class="number">100</span>);</span><br><span class="line"> <span class="keyword">for</span> (Execution execution : executions) {</span><br><span class="line"> log.info(<span class="string">"executions {}"</span>,executions);</span><br><span class="line"> }</span><br><span class="line"> log.info(<span class="string">"executions size = {}"</span>,executions.size());</span><br><span class="line"> String id = executions.iterator().next().getId();</span><br><span class="line"> activitiRule.getRuntimeService().setVariable(id,<span class="string">"keyStart1"</span>,<span class="string">"value1_"</span>);</span><br><span class="line"> <span class="comment">//提交表单task 修改变量</span></span><br><span class="line"> Task task = activitiRule.getTaskService().createTaskQuery().singleResult();</span><br><span class="line"> Map<String,String> properties = Maps.newHashMap();</span><br><span class="line"> properties.put(<span class="string">"formkey1"</span>,<span class="string">"valuef1"</span>);</span><br><span class="line"> properties.put(<span class="string">"formkey2"</span>,<span class="string">"valuef2"</span>);</span><br><span class="line"> activitiRule.getFormService().submitTaskFormData(task.getId(),properties);</span><br><span class="line"> <span class="comment">//activitiRule.getTaskService().complete(task.getId());</span></span><br><span class="line"> <span class="comment">//输出历史活动</span></span><br><span class="line"> List<HistoricActivityInstance> historicActivityInstances = activitiRule.getHistoryService().createHistoricActivityInstanceQuery().listPage(<span class="number">0</span>, <span class="number">100</span>);</span><br><span class="line"> <span class="keyword">for</span> (HistoricActivityInstance historicActivityInstance : historicActivityInstances) {</span><br><span class="line"> log.info(<span class="string">"historicActivityInstances = {}"</span>,historicActivityInstances);</span><br><span class="line"> }</span><br><span class="line"> log.info(<span class="string">"historicActivityInstances size = {}"</span>,historicActivityInstances.size());</span><br><span class="line"></span><br><span class="line"> <span class="comment">//输出历史表单</span></span><br><span class="line"></span><br><span class="line"> <span class="comment">//输出历史详情</span></span><br><span class="line"> List<HistoricDetail> historicDetails = activitiRule.getHistoryService().createHistoricDetailQuery().listPage(<span class="number">0</span>, <span class="number">100</span>);</span><br><span class="line"> <span class="keyword">for</span> (HistoricDetail historicDetail : historicDetails) {</span><br><span class="line"> log.info(<span class="string">"historicDetail [{}]"</span>,historicDetail);</span><br><span class="line"> }</span><br><span class="line"> log.info(<span class="string">"historicDetail size = [{}]"</span>, historicDetails.size());</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h2 id="5-事件处理及监听配置-eventlog"><a href="#5-事件处理及监听配置-eventlog" class="headerlink" title="5.事件处理及监听配置-eventlog"></a>5.事件处理及监听配置-eventlog</h2><h3 id="5-1-事件及监听器原理"><a href="#5-1-事件及监听器原理" class="headerlink" title="5.1 事件及监听器原理"></a>5.1 事件及监听器原理</h3><p><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1gf7zqvwqsnj31js0fmq5s.jpg" alt=""></p>
<h3 id="5-2-监听器的配置方式"><a href="#5-2-监听器的配置方式" class="headerlink" title="5.2 监听器的配置方式"></a>5.2 监听器的配置方式</h3><ol>
<li>配置Listener<ol>
<li>eventListeners:监听所有事件派发通知</li>
<li>typedEventListeners:监听指定事件类型的通知</li>
<li>activiti:eventListener:只监听特定流程定义的事件<br><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1gf7zr4t5btj31jo0hytck.jpg" alt=""></li>
</ol>
</li>
</ol>
<h3 id="5-3-Activiti事件监听"><a href="#5-3-Activiti事件监听" class="headerlink" title="5.3 Activiti事件监听"></a>5.3 Activiti事件监听</h3><h4 id="5-3-1-相关API"><a href="#5-3-1-相关API" class="headerlink" title="5.3.1 相关API"></a>5.3.1 相关API</h4><ul>
<li>ActivitiEvent:事件对象</li>
<li>ActivitiEventListener:监听器</li>
<li>ActivitiEventType:事件类型</li>
</ul>
<h3 id="5-4-ActivitiEventListener监听器的使用"><a href="#5-4-ActivitiEventListener监听器的使用" class="headerlink" title="5.4 ActivitiEventListener监听器的使用"></a>5.4 ActivitiEventListener监听器的使用</h3><ol>
<li>配置文件中增加对应的配置</li>
</ol>
<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">property</span> <span class="attr">name</span>=<span class="string">"eventListeners"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">list</span>></span></span><br><span class="line"> <span class="comment"><!--监听器的实现类--></span></span><br><span class="line"> <span class="tag"><<span class="name">bean</span> <span class="attr">class</span>=<span class="string">"com.atguigu.event.ProcessEventListener"</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">property</span>></span></span><br></pre></td></tr></table></figure>
<ol start="2">
<li>创建一个实体类,实现<code>ActivitiEventListener</code>类</li>
</ol>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">package</span> com.atguigu.event;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.activiti.bpmn.model.ActivitiListener;</span><br><span class="line"><span class="keyword">import</span> org.activiti.engine.delegate.event.ActivitiEvent;</span><br><span class="line"><span class="keyword">import</span> org.activiti.engine.delegate.event.ActivitiEventListener;</span><br><span class="line"><span class="keyword">import</span> org.activiti.engine.delegate.event.ActivitiEventType;</span><br><span class="line"><span class="keyword">import</span> org.slf4j.Logger;</span><br><span class="line"><span class="keyword">import</span> org.slf4j.LoggerFactory;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@program</span>: activiti6</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@description</span>: 流程监听</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span>: Mr.Wang</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@create</span>: 2020-05-27-21-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">ProcessEventListener</span> <span class="keyword">implements</span> <span class="title">ActivitiEventListener</span> </span>{</span><br><span class="line"><span class="keyword">private</span> <span class="keyword">static</span> Logger log = LoggerFactory.getLogger(ProcessEventListener<span class="class">.<span class="keyword">class</span>)</span>;</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">onEvent</span><span class="params">(ActivitiEvent event)</span> </span>{</span><br><span class="line"> ActivitiEventType type = event.getType();</span><br><span class="line"> <span class="comment">//判断是是否为流程启动</span></span><br><span class="line"> <span class="keyword">if</span>(ActivitiEventType.PROCESS_STARTED.equals(type)){</span><br><span class="line"> log.info(<span class="string">"流程启动 {}"</span>,event.getProcessInstanceId());</span><br><span class="line"> }<span class="keyword">else</span> <span class="keyword">if</span>(ActivitiEventType.PROCESS_COMPLETED.equals(type)){</span><br><span class="line"> log.info(<span class="string">"流程结束"</span>);</span><br><span class="line"> }</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> <span class="keyword">boolean</span> <span class="title">isFailOnException</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h3 id="5-5-TypedEventListeners监听器的使用"><a href="#5-5-TypedEventListeners监听器的使用" class="headerlink" title="5.5 TypedEventListeners监听器的使用"></a>5.5 TypedEventListeners监听器的使用</h3><ol>
<li>配置文件</li>
</ol>
<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">"typedEventListeners"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">map</span>></span></span><br><span class="line"> <span class="comment"><!--这样配置的时候,只会监听流程启动--></span></span><br><span class="line"> <span class="tag"><<span class="name">entry</span> <span class="attr">key</span>=<span class="string">"PROCESS_STARTED"</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">bean</span> <span class="attr">class</span>=<span class="string">"com.atguigu.event.ProcessEventListener"</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">entry</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">map</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">property</span>></span></span><br></pre></td></tr></table></figure>
<ol start="2">
<li>流程监听实体类</li>
</ol>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">package</span> com.atguigu.event;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.activiti.bpmn.model.ActivitiListener;</span><br><span class="line"><span class="keyword">import</span> org.activiti.engine.delegate.event.ActivitiEvent;</span><br><span class="line"><span class="keyword">import</span> org.activiti.engine.delegate.event.ActivitiEventListener;</span><br><span class="line"><span class="keyword">import</span> org.activiti.engine.delegate.event.ActivitiEventType;</span><br><span class="line"><span class="keyword">import</span> org.slf4j.Logger;</span><br><span class="line"><span class="keyword">import</span> org.slf4j.LoggerFactory;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@program</span>: activiti6</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@description</span>: 流程监听</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span>: Mr.Wang</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@create</span>: 2020-05-27-21-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">ProcessEventListener</span> <span class="keyword">implements</span> <span class="title">ActivitiEventListener</span> </span>{</span><br><span class="line"><span class="keyword">private</span> <span class="keyword">static</span> Logger log = LoggerFactory.getLogger(ProcessEventListener<span class="class">.<span class="keyword">class</span>)</span>;</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">onEvent</span><span class="params">(ActivitiEvent event)</span> </span>{</span><br><span class="line"> ActivitiEventType type = event.getType();</span><br><span class="line"> <span class="comment">//判断是是否为流程启动</span></span><br><span class="line"> <span class="keyword">if</span>(ActivitiEventType.PROCESS_STARTED.equals(type)){</span><br><span class="line"> log.info(<span class="string">"流程启动 {}"</span>,event.getProcessInstanceId());</span><br><span class="line"> }<span class="keyword">else</span> <span class="keyword">if</span>(ActivitiEventType.PROCESS_COMPLETED.equals(type)){</span><br><span class="line"> log.info(<span class="string">"流程结束"</span>);</span><br><span class="line"> }</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> <span class="keyword">boolean</span> <span class="title">isFailOnException</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"><span class="comment">//输出的结果为:22:18:38.450 [main] [INFO ]流程启动 4 c.a.e.ProcessEventListener.onEvent:23</span></span><br></pre></td></tr></table></figure>
<h2 id="6-命令拦截器配置–command"><a href="#6-命令拦截器配置–command" class="headerlink" title="6. 命令拦截器配置–command"></a>6. 命令拦截器配置–command</h2><h3 id="6-1-命令模式与责任链模式"><a href="#6-1-命令模式与责任链模式" class="headerlink" title="6.1 命令模式与责任链模式"></a>6.1 命令模式与责任链模式</h3><h4 id="6-1-1-命令模式"><a href="#6-1-1-命令模式" class="headerlink" title="6.1.1 命令模式"></a>6.1.1 命令模式</h4><p><img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1gf7dv4a99yj31dk0qy4bt.jpg" alt=""><br><img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1gf7dx177oaj319e0qo16v.jpg" alt="时序图"></p>
<h4 id="6-1-2-责任链模式"><a href="#6-1-2-责任链模式" class="headerlink" title="6.1.2 责任链模式"></a>6.1.2 责任链模式</h4><ul>
<li>CommandInterceptor<br><img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1gf7dzxamvhj31fk0ca7ba.jpg" alt=""></li>
<li>配置Interceptor<ul>
<li>customPreCommandInterceptors:配置在默认拦截器之前</li>
<li>customPostCommandInterceptors:配置在默认拦截器之后</li>
<li>commandInvoker:配置最后的执行器</li>
</ul>
</li>
</ul>
<h3 id="6-2-拦截器的配置方式"><a href="#6-2-拦截器的配置方式" class="headerlink" title="6.2 拦截器的配置方式"></a>6.2 拦截器的配置方式</h3><ol>
<li>activiti.cfg.xml文件的修改</li>
</ol>
<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">property</span> <span class="attr">name</span>=<span class="string">"customPreCommandInterceptors"</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">bean</span> <span class="attr">class</span>=<span class="string">"com.atguigu.interceptor.DurationInterceptor"</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">property</span>></span></span><br></pre></td></tr></table></figure>
<ol start="2">
<li>实体类的编写</li>
</ol>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">package</span> com.atguigu.interceptor;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.activiti.engine.impl.interceptor.AbstractCommandInterceptor;</span><br><span class="line"><span class="keyword">import</span> org.activiti.engine.impl.interceptor.Command;</span><br><span class="line"><span class="keyword">import</span> org.activiti.engine.impl.interceptor.CommandConfig;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@program</span>: activiti6</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@description</span>: 执行的时间</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span>: Mr.Wang</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@create</span>: 2020-05-27-22-53</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">DurationInterceptor</span> <span class="keyword">extends</span> <span class="title">AbstractCommandInterceptor</span> </span>{</span><br><span class="line"> <span class="meta">@Override</span></span><br><span class="line"> <span class="keyword">public</span> <T> <span class="function">T <span class="title">execute</span><span class="params">(CommandConfig config, Command<T> command)</span> </span>{</span><br><span class="line"> <span class="keyword">long</span> l = System.currentTimeMillis();</span><br><span class="line"> <span class="keyword">this</span>.getNext().execute(config,command);</span><br><span class="line"> <span class="keyword">long</span> l1 = System.currentTimeMillis();</span><br><span class="line"> <span class="keyword">long</span> duration = l1 - l;</span><br><span class="line"> System.out.println(<span class="string">"执行的时长----->"</span>+duration);</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></pre></td></tr></table></figure>
<h3 id="6-3-Activiti的拦截器"><a href="#6-3-Activiti的拦截器" class="headerlink" title="6.3 Activiti的拦截器"></a>6.3 Activiti的拦截器</h3><p><img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1gf7e7154rqj30xw0dekfa.jpg" alt=""></p>
<h2 id="7-作业执行器-Job-Executor"><a href="#7-作业执行器-Job-Executor" class="headerlink" title="7. 作业执行器 Job Executor"></a>7. 作业执行器 Job Executor</h2><h3 id="7-1-作业执行器配置"><a href="#7-1-作业执行器配置" class="headerlink" title="7.1 作业执行器配置"></a>7.1 作业执行器配置</h3><p>(1) asyncExecutorActivate:激活作业执行器<br>(2) asyncExectuorXXX:异步执行器的属性配置<br>(3) asyncExecutor:异步执行器的bean</p>
<h3 id="7-2-配置自定义线程池"><a href="#7-2-配置自定义线程池" class="headerlink" title="7.2 配置自定义线程池"></a>7.2 配置自定义线程池</h3><h4 id="7-2-1-自定义线程池ExecutorService"><a href="#7-2-1-自定义线程池ExecutorService" class="headerlink" title="7.2.1 自定义线程池ExecutorService"></a>7.2.1 自定义线程池ExecutorService</h4><p>(1) corePoolSize:核心线程数<br>(2) maxPoolSize: 最大线程数<br>(3) queueCapacity:堵塞队列大小</p>
<h4 id="7-2-2-基于spring线程池的配置"><a href="#7-2-2-基于spring线程池的配置" class="headerlink" title="7.2.2 基于spring线程池的配置"></a>7.2.2 基于spring线程池的配置</h4><p><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1gf7wkem35qj31580ben91.jpg" alt=""></p>
<h3 id="7-3-流程定义定时启动流程"><a href="#7-3-流程定义定时启动流程" class="headerlink" title="7.3 流程定义定时启动流程"></a>7.3 流程定义定时启动流程</h3><h4 id="7-3-1-定时开始事件"><a href="#7-3-1-定时开始事件" class="headerlink" title="7.3.1 定时开始事件"></a>7.3.1 定时开始事件</h4><ol>
<li>timeDate:指定启动时间</li>
<li>timeDuration:指定持续时间间隔后执行</li>
<li>timeCycle:R5/P1DT1H指定事件段后周期执行</li>
</ol>
<h3 id="7-4-配置文件修改"><a href="#7-4-配置文件修改" class="headerlink" title="7.4 配置文件修改"></a>7.4 配置文件修改</h3><p>activiti.cfg.xml文件修改</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line"><bean id="processEngineConfiguration"</span><br><span class="line"> class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration"></span><br><span class="line"> <!--打开定时任务的激活器--></span><br><span class="line"> <property name="asyncExecutorActivate" value="true"/></span><br><span class="line"> <!--定义异步执行器--></span><br><span class="line"> <property name="asyncExecutor" ref="defaultAsyncJobExecutor"/></span><br><span class="line"> </bean></span><br><span class="line"> <!--系统默认的自定义任务执行器--></span><br><span class="line"> <bean id = "defaultAsyncJobExecutor" class="org.activiti.engine.impl.asyncexecutor.DefaultAsyncJobExecutor"></span><br><span class="line"> <!--需要的服务--></span><br><span class="line"> <property name="executorService" ref="executorService"/></span><br><span class="line"> </bean></span><br><span class="line"> <!--使用spring的线程池--></span><br><span class="line"> <bean id = "executorService" class="org.springframework.scheduling.concurrent.ThreadPoolExecutorFactoryBean"></span><br><span class="line"> <!--具体的配置--></span><br><span class="line"> </bean></span><br></pre></td></tr></table></figure>
<h2 id="8-Activiti与Spring的集成"><a href="#8-Activiti与Spring的集成" class="headerlink" title="8 Activiti与Spring的集成"></a>8 Activiti与Spring的集成</h2><h3 id="8-1-集成Spring配置"><a href="#8-1-集成Spring配置" class="headerlink" title="8.1 集成Spring配置"></a>8.1 集成Spring配置</h3><h4 id="8-1-1-相关配置"><a href="#8-1-1-相关配置" class="headerlink" title="8.1.1 相关配置"></a>8.1.1 相关配置</h4><p>(1) 添加pom依赖activiti-spring</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line"><!-- https://mvnrepository.com/artifact/org.activiti/activiti-spring --></span><br><span class="line"><dependency></span><br><span class="line"> <groupId>org.activiti</groupId></span><br><span class="line"> <artifactId>activiti-spring</artifactId></span><br><span class="line"> <version>6.0.0</version></span><br><span class="line"></dependency></span><br></pre></td></tr></table></figure>
<p>(2) 基于Spring的默认配置activiti-context.xml</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line"><?xml version="1.0" encoding="UTF-8"?></span><br><span class="line"></span><br><span class="line"><beans xmlns="http://www.springframework.org/schema/beans"</span><br><span class="line"> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"</span><br><span class="line"> xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"></span><br><span class="line"><!--数据源配置--></span><br><span class="line"> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"></span><br><span class="line"> <property name="url" value="jdbc:mysql://111.229.203.5:3306/activiti?serverTimezone=CTT&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true"/></span><br><span class="line"> <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/></span><br><span class="line"> <property name="username" value="weaver"/></span><br><span class="line"> <property name="password" value="192612"/></span><br><span class="line"> <property name="initialSize" value="1"/></span><br><span class="line"> <property name="maxActive" value="10"/></span><br><span class="line"> <property name="filters" value="stat,slf4j"/></span><br><span class="line"> </bean></span><br><span class="line"> <!--事务管理器配置--></span><br><span class="line"> <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"></span><br><span class="line"> <!--指定数据源--></span><br><span class="line"> <property name="dataSource" ref="dataSource"/></span><br><span class="line"> </bean></span><br><span class="line"> <bean id="processEngineConfiguration"</span><br><span class="line"> class="org.activiti.spring.SpringProcessEngineConfiguration"></span><br><span class="line"> <property name="dataSource" ref="dataSource"/></span><br><span class="line"> <!--配置事务管理--></span><br><span class="line"> <property name="transactionManager" ref="dataSourceTransactionManager"/></span><br><span class="line"> <!--数据源的创建格式--></span><br><span class="line"> <property name="databaseSchemaUpdate" value="true"/></span><br><span class="line"> </bean></span><br><span class="line"> <!--构造流程引擎对象--></span><br><span class="line"> <bean id = "processEngineFactoryBean" class="org.activiti.spring.ProcessEngineFactoryBean"></span><br><span class="line"> <!--指定流程配置对象--></span><br><span class="line"> <property name="processEngineConfiguration" ref="processEngineConfiguration"/></span><br><span class="line"> </bean></span><br><span class="line"><!--报漏服务给Spring--></span><br><span class="line"> <bean id="runtimeService" factory-bean="processEngineFactoryBean" factory-method="getRuntimeService"></bean></span><br><span class="line"> <bean id="repositoryService" factory-bean="processEngineFactoryBean" factory-method="getRepositoryService"></bean></span><br><span class="line"> <bean id="formService" factory-bean="processEngineFactoryBean" factory-method="getFormService"></bean></span><br><span class="line"> <bean id="taskService" factory-bean="processEngineFactoryBean" factory-method="getTaskService"></bean></span><br><span class="line"> <bean id="historyService" factory-bean="processEngineFactoryBean" factory-method="getHistoryService"></bean></span><br><span class="line"> <bean id="activitiRule" class="org.activiti.engine.test.ActivitiRule"></span><br><span class="line"> <property name="processEngine" ref="processEngineFactoryBean"/></span><br><span class="line"> </bean></span><br><span class="line"></beans></span><br></pre></td></tr></table></figure>
<p>(3) Activiti核心服务注入Spring容器<br><strong><em>详细配置见上一步</em></strong></p>
<h3 id="8-2-基于Spring对Activiti管理"><a href="#8-2-基于Spring对Activiti管理" class="headerlink" title="8.2 基于Spring对Activiti管理"></a>8.2 基于Spring对Activiti管理</h3><h4 id="8-2-1-功能特征"><a href="#8-2-1-功能特征" class="headerlink" title="8.2.1 功能特征"></a>8.2.1 功能特征</h4><p>(1) 集成Spring事务管理器<br>(2) 定义文件表达式中使用Spring bean<br>(3) 自动部署资源文件</p>
<h3 id="8-3-基于Spring的流程单元测试"><a href="#8-3-基于Spring的流程单元测试" class="headerlink" title="8.3 基于Spring的流程单元测试"></a>8.3 基于Spring的流程单元测试</h3><h4 id="8-3-1-单元测试"><a href="#8-3-1-单元测试" class="headerlink" title="8.3.1 单元测试"></a>8.3.1 单元测试</h4><p>(1) 添加pom依赖spring-test</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line"><!-- https://mvnrepository.com/artifact/org.springframework/spring-test --></span><br><span class="line"><dependency></span><br><span class="line"> <groupId>org.springframework</groupId></span><br><span class="line"> <artifactId>spring-test</artifactId></span><br><span class="line"> <version>4.2.5.RELEASE</version></span><br><span class="line"> <scope>test</scope></span><br><span class="line"></dependency></span><br></pre></td></tr></table></figure>
<p>(2) 辅助测试Rule:ActivitiRule</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">package</span> com.atguigu.test;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.activiti.engine.RuntimeService;</span><br><span class="line"><span class="keyword">import</span> org.activiti.engine.TaskService;</span><br><span class="line"><span class="keyword">import</span> org.activiti.engine.runtime.ProcessInstance;</span><br><span class="line"><span class="keyword">import</span> org.activiti.engine.task.Task;</span><br><span class="line"><span class="keyword">import</span> org.activiti.engine.test.ActivitiRule;</span><br><span class="line"><span class="keyword">import</span> org.activiti.engine.test.Deployment;</span><br><span class="line"><span class="keyword">import</span> org.junit.Rule;</span><br><span class="line"><span class="keyword">import</span> org.junit.Test;</span><br><span class="line"><span class="keyword">import</span> org.junit.runner.RunWith;</span><br><span class="line"><span class="keyword">import</span> org.slf4j.Logger;</span><br><span class="line"><span class="keyword">import</span> org.slf4j.LoggerFactory;</span><br><span class="line"><span class="keyword">import</span> org.springframework.beans.factory.annotation.Autowired;</span><br><span class="line"><span class="keyword">import</span> org.springframework.test.context.ContextConfiguration;</span><br><span class="line"><span class="keyword">import</span> org.springframework.test.context.junit4.SpringJUnit4ClassRunner;</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"> * <span class="doctag">@program</span>: activiti6</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@description</span>: 测试与Spring的集成</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span>: Mr.Wang</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@create</span>: 2020-05-28-10-26</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@RunWith</span>(SpringJUnit4ClassRunner<span class="class">.<span class="keyword">class</span>)</span></span><br><span class="line"><span class="class">@<span class="title">ContextConfiguration</span>(<span class="title">locations</span> </span>= {<span class="string">"classpath:activiti-context.xml"</span>})</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TestSpring</span> </span>{</span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">static</span> Logger log = LoggerFactory.getLogger(TestSpring<span class="class">.<span class="keyword">class</span>)</span>;</span><br><span class="line"></span><br><span class="line"> <span class="meta">@Rule</span></span><br><span class="line"> <span class="meta">@Autowired</span></span><br><span class="line"> <span class="keyword">public</span> ActivitiRule activitiRule;</span><br><span class="line"></span><br><span class="line"> <span class="meta">@Autowired</span></span><br><span class="line"> <span class="keyword">private</span> RuntimeService runtimeService;</span><br><span class="line"></span><br><span class="line"> <span class="meta">@Autowired</span></span><br><span class="line"> <span class="keyword">private</span> TaskService taskService;</span><br><span class="line"> <span class="meta">@Test</span></span><br><span class="line"> <span class="meta">@Deployment</span>(resources = {<span class="string">"my-process.bpmn20.xml"</span>})</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test</span><span class="params">()</span></span>{</span><br><span class="line"> ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(<span class="string">"my-process"</span>);</span><br><span class="line"> <span class="comment">/*List<Task> tasks = taskService.createTaskQuery().listPage(0, 100);</span></span><br><span class="line"><span class="comment"> for (Task task : tasks) {</span></span><br><span class="line"><span class="comment"> log.info("task 的某些测试数据 {}",task);</span></span><br><span class="line"><span class="comment"> }*/</span></span><br><span class="line"> Task task = taskService.createTaskQuery().singleResult();</span><br><span class="line"> taskService.complete(task.getId());</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>
<p>(3) 辅助测试TestCase:SpringActivitiTestCase</p>
]]></content>
<categories>
<category>java</category>
</categories>
<tags>
<tag>activiti6.0</tag>
</tags>
</entry>
<entry>
<title>Java8日期和时间处理</title>
<url>/posts/58f1702a.html</url>
<content><![CDATA[<h2 id="1-java8新特性-新时间与日期API本地时间与时间戳"><a href="#1-java8新特性-新时间与日期API本地时间与时间戳" class="headerlink" title="1.java8新特性-新时间与日期API本地时间与时间戳"></a>1.java8新特性-新时间与日期API本地时间与时间戳</h2><p><code>LocalDate</code>、<code>LocalTime</code>、<code>LocalDateTime</code>类的实例是不可变的对象,分别表示使用ISO-8601的日历系统的日期、时间、日期和时间。新的API提供了简单的日期和时间的获取方法。不包含与时区有关的信息</p>
<h3 id="1-1-使用静态方法now获取本地时间"><a href="#1-1-使用静态方法now获取本地时间" class="headerlink" title="1.1 使用静态方法now获取本地时间"></a>1.1 使用静态方法now获取本地时间</h3><figure class="highlight java"><table><tr><td class="code"><pre><span class="line">LocalDate now = LocalDate.now();</span><br><span class="line">System.out.println(<span class="string">"获取系统的日期:"</span>+now);<span class="comment">// 2020-05-25</span></span><br><span class="line">LocalTime localTime = LocalTime.now();</span><br><span class="line">System.out.println(<span class="string">"获取系统的时间:"</span>+localTime);<span class="comment">//20:32:55.091</span></span><br><span class="line">LocalDateTime localDateTime = LocalDateTime.now();</span><br><span class="line">System.out.println(<span class="string">"获取系统的日期和时间:"</span>+localDateTime);<span class="comment">//2020-05-25T20:32:55.092</span></span><br></pre></td></tr></table></figure>
<h3 id="1-2-使用静态方法of获取日期和时间"><a href="#1-2-使用静态方法of获取日期和时间" class="headerlink" title="1.2 使用静态方法of获取日期和时间"></a>1.2 使用静态方法of获取日期和时间</h3><figure class="highlight java"><table><tr><td class="code"><pre><span class="line">LocalDate localDate = LocalDate.of(<span class="number">2012</span>, <span class="number">3</span>, <span class="number">23</span>);</span><br><span class="line">System.out.println(localDate);<span class="comment">//2012-03-23</span></span><br><span class="line">LocalTime localTime = LocalTime.of(<span class="number">12</span>, <span class="number">12</span>);</span><br><span class="line">System.out.println(localTime);<span class="comment">//12:12</span></span><br><span class="line">LocalDateTime localDateTime = LocalDateTime.of(<span class="number">2012</span>, <span class="number">3</span>, <span class="number">24</span>, <span class="number">12</span>, <span class="number">12</span>, <span class="number">12</span>);</span><br><span class="line">System.out.println(localDateTime);<span class="comment">//2012-03-24T12:12:12</span></span><br></pre></td></tr></table></figure>
<h3 id="1-3-plusDays、plusWeeks、plusMonths、plusYear向当前LocalDate对象添加几天、几周、几个月、几年"><a href="#1-3-plusDays、plusWeeks、plusMonths、plusYear向当前LocalDate对象添加几天、几周、几个月、几年" class="headerlink" title="1.3 plusDays、plusWeeks、plusMonths、plusYear向当前LocalDate对象添加几天、几周、几个月、几年"></a>1.3 plusDays、plusWeeks、plusMonths、plusYear向当前LocalDate对象添加几天、几周、几个月、几年</h3><figure class="highlight java"><table><tr><td class="code"><pre><span class="line">LocalDate localDate = LocalDate.now();</span><br><span class="line">System.out.println(localDate);<span class="comment">//2020-05-25</span></span><br><span class="line">LocalDate localDate1 = localDate.plusDays(<span class="number">2L</span>);</span><br><span class="line">System.out.println(localDate1);<span class="comment">//2020-05-27</span></span><br><span class="line">LocalDate localDate2 = localDate.plusMonths(<span class="number">2L</span>);</span><br><span class="line">System.out.println(localDate2);<span class="comment">//2020-07-25</span></span><br><span class="line">LocalDate localDate3 = localDate.plusWeeks(<span class="number">2L</span>);</span><br><span class="line">System.out.println(localDate3);<span class="comment">//2020-06-08</span></span><br><span class="line">LocalDate localDate4 = localDate.plusYears(<span class="number">2L</span>);</span><br><span class="line">System.out.println(localDate4);<span class="comment">//2022-05-25</span></span><br></pre></td></tr></table></figure>
<h3 id="1-4-Instant时间戳"><a href="#1-4-Instant时间戳" class="headerlink" title="1.4 Instant时间戳"></a>1.4 Instant时间戳</h3><p>时间戳(以Unix元年:1970年1月1日00:00:00到某个时间之间的毫秒值)</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">//默认获取的是以UTC时区</span></span><br><span class="line">Instant now = Instant.now();</span><br><span class="line">System.out.println(now);<span class="comment">//2020-05-25T13:32:32.718Z</span></span><br><span class="line"><span class="comment">//对时间进行偏移量处理,以便获取当前所在时区的时间</span></span><br><span class="line">OffsetDateTime offsetDateTime=now.atOffset(ZoneOffset.ofHours(<span class="number">8</span>));</span><br><span class="line">System.out.println(offsetDateTime);<span class="comment">//2020-05-25T21:32:32.718+08:00</span></span><br><span class="line"><span class="comment">//把获取的时间转换成对应的毫秒值</span></span><br><span class="line"><span class="keyword">long</span> l = now.toEpochMilli();</span><br><span class="line">System.out.println(l);<span class="comment">//1590413552718</span></span><br></pre></td></tr></table></figure>
<h2 id="1-5-Duration计算两个时间之间的间隔-Period计算两个日期之间的间隔"><a href="#1-5-Duration计算两个时间之间的间隔-Period计算两个日期之间的间隔" class="headerlink" title="1.5 Duration计算两个时间之间的间隔,Period计算两个日期之间的间隔"></a>1.5 Duration计算两个时间之间的间隔,Period计算两个日期之间的间隔</h2><p><code>Duration</code>和<code>Period</code>的使用方式相同</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line">Instant now = Instant.now();</span><br><span class="line"><span class="keyword">try</span> {</span><br><span class="line"> <span class="comment">//用线程睡眠一秒钟来测试Duration的时间差</span></span><br><span class="line"> Thread.sleep(<span class="number">1000L</span>);</span><br><span class="line">} <span class="keyword">catch</span> (InterruptedException e) {</span><br><span class="line"> e.printStackTrace();</span><br><span class="line">}</span><br><span class="line">Instant now2 = Instant.now();</span><br><span class="line"><span class="comment">//比较两个时间的间隔</span></span><br><span class="line">Duration duration = Duration.between(now, now2);</span><br><span class="line"><span class="comment">//默认输出`PT1.002S`为ISO-861的表示方法</span></span><br><span class="line">System.out.println(duration);</span><br><span class="line"><span class="comment">//将ISO的表示的方法转换为纳秒、或者毫秒值</span></span><br><span class="line"><span class="keyword">long</span> l = duration.toNanos();<span class="comment">//1002000000</span></span><br><span class="line"><span class="comment">//转换为毫秒值</span></span><br><span class="line"><span class="comment">//long l1 = duration.toMillis();</span></span><br><span class="line">System.out.println(l);</span><br><span class="line"><span class="comment">//System.out.println(l1);</span></span><br></pre></td></tr></table></figure>
<h2 id="2-java8新特性-新时间与日期API-时间校正器"><a href="#2-java8新特性-新时间与日期API-时间校正器" class="headerlink" title="2.java8新特性-新时间与日期API-时间校正器"></a>2.java8新特性-新时间与日期API-时间校正器</h2><p><code>TemporalAdjuster</code>:时间校正器。有时候我们需要获取例如:将日期调整到“下周日”等操作。<br><code>TemporalAdjusters</code>:该类通过静态方法提供了大量的常用<code>TemporalAdjuster</code>的实现</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line">LocalDateTime ldt = LocalDateTime.now();</span><br><span class="line">System.out.println(ldt);<span class="comment">//2020-05-25T22:00:35.532</span></span><br><span class="line"><span class="comment">//获取下一个周日</span></span><br><span class="line">LocalDateTime dateTime = ldt.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));</span><br><span class="line">System.out.println(dateTime);<span class="comment">//2020-05-31T22:00:35.532</span></span><br><span class="line"><span class="comment">//自定义:下一个工作日是什么时候</span></span><br><span class="line">LocalDateTime with = ldt.with(l -> {</span><br><span class="line"> LocalDateTime ldt4 = (LocalDateTime) l;</span><br><span class="line"> <span class="comment">//获取今天是星期几</span></span><br><span class="line"> DayOfWeek dayOfWeek = ldt4.getDayOfWeek();</span><br><span class="line"> System.out.println(dayOfWeek);<span class="comment">//MONDAY</span></span><br><span class="line"> <span class="keyword">if</span> (dayOfWeek.equals(DayOfWeek.FRIDAY)) {</span><br><span class="line"> <span class="keyword">return</span> ldt4.plusDays(<span class="number">3L</span>);</span><br><span class="line"> } <span class="keyword">else</span> <span class="keyword">if</span> (dayOfWeek.equals(DayOfWeek.SUNDAY)) {</span><br><span class="line"> <span class="keyword">return</span> ldt4.plusDays(<span class="number">2L</span>);</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> <span class="keyword">return</span> ldt4.plusDays(<span class="number">1L</span>);</span><br><span class="line"> }</span><br><span class="line"> });</span><br><span class="line"> System.out.println(with);<span class="comment">//2020-05-26T22:06:51.576</span></span><br></pre></td></tr></table></figure>
<h2 id="3-java8新特性-新时间与日期API-时间格式化与时区处理"><a href="#3-java8新特性-新时间与日期API-时间格式化与时区处理" class="headerlink" title="3.java8新特性-新时间与日期API-时间格式化与时区处理"></a>3.java8新特性-新时间与日期API-时间格式化与时区处理</h2><h3 id="3-1时间和日期的格式化"><a href="#3-1时间和日期的格式化" class="headerlink" title="3.1时间和日期的格式化"></a>3.1时间和日期的格式化</h3><figure class="highlight java"><table><tr><td class="code"><pre><span class="line">DateTimeFormatter isoLocalDate = DateTimeFormatter.ISO_LOCAL_DATE;</span><br><span class="line">LocalDateTime now = LocalDateTime.now();</span><br><span class="line">System.out.println(now);</span><br><span class="line">String format = now.format(isoLocalDate);<span class="comment">//2020-05-25T22:22:48.083</span></span><br><span class="line">System.out.println(format);<span class="comment">//2020-05-25</span></span><br><span class="line"><span class="comment">//定义自己的格式</span></span><br><span class="line">DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(<span class="string">"yyyy年MM月dd日"</span>);</span><br><span class="line">String format1 = dateTimeFormatter.format(now);</span><br><span class="line">System.out.println(format1);<span class="comment">//2020年05月25日</span></span><br></pre></td></tr></table></figure>
<h3 id="3-2-时区的处理"><a href="#3-2-时区的处理" class="headerlink" title="3.2 时区的处理"></a>3.2 时区的处理</h3><p>java8中加入了对时区的支持,带时区的时间分别为<code>ZonedDate</code>,<code>ZonedTIme</code>,<code>ZoneDateTime</code>。其中每个时区都对应着ID,地区ID都是<code>区域/城市</code>的格式,例如:<code>Asia/Shanghai</code>等。<br><code>ZoneId</code>该类中包含了所有的时区信息:<br>getAvailableZoneIds():可以获取所有时区的信息<br>of(id):用指定的时区信息获取ZoneId对象</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">//获取所有的时区</span></span><br><span class="line">Set<String> availableZoneIds = ZoneId.getAvailableZoneIds();</span><br><span class="line">availableZoneIds.forEach(System.out::println);</span><br><span class="line"><span class="comment">//获取时区为上海的时间</span></span><br><span class="line">LocalDateTime now = LocalDateTime.now(ZoneId.of(<span class="string">"Asia/Shanghai"</span>));<span class="comment">//2020-05-25T22:47:57.216</span></span><br><span class="line">System.out.println(now);</span><br><span class="line"><span class="comment">//也可以使用以下的方法设置时区,返回的是带有时区的时间和日期</span></span><br><span class="line">LocalDateTime now1 = LocalDateTime.now();</span><br><span class="line">ZonedDateTime zonedDateTime = now1.atZone(ZoneId.of(<span class="string">"Asia/Shanghai"</span>));<span class="comment">//2020-05-25T22:47:57.222+08:00[Asia/Shanghai]</span></span><br><span class="line">System.out.println(zonedDateTime);</span><br></pre></td></tr></table></figure>
]]></content>
<categories>
<category>java</category>
</categories>
<tags>
<tag>java</tag>
<tag>JDK1.8</tag>
</tags>
</entry>
<entry>
<title>Maven学习笔记</title>
<url>/posts/be02a930.html</url>
<content><![CDATA[<h2 id="1-Maven-简介"><a href="#1-Maven-简介" class="headerlink" title="1.Maven 简介"></a>1.Maven 简介</h2><p>Maven是Apache软件基金会组织维护的一款自动化构建工具专注服务于Java平台的项目构建和依赖管理 。</p>
<h2 id="2-构建过程的主要几个环节"><a href="#2-构建过程的主要几个环节" class="headerlink" title="2. 构建过程的主要几个环节"></a>2. 构建过程的主要几个环节</h2><p>①清理 :删除以前的编译结果,为重新编译做好准备。<br>②编译 :将Java源程序编译为字节码文件。<br>③测试 :针对项目中的关键点进行测试,确保项目在迭代开发过程中关键点的正确性。<br>④报告 :在每一次测试后以标准的格式记录和展示测试结果。<br>⑤打包 :将一个包含诸多文件的工程封装为一个压缩文件用于安装或部署。Java工程对应jar包,Web工程对应war包。<br>⑥安装 :在 Maven 环境下特指将打包的结果jar包或war包安装到本地仓库中。<br>⑦部署 :将打包的结果部署到远程仓库或将war包部署到服务器上运行。</p>
<h2 id="3-maven程序安装"><a href="#3-maven程序安装" class="headerlink" title="3.maven程序安装"></a>3.maven程序安装</h2><ol>
<li><p><a href="https://maven.apache.org/download.cgi" target="_blank" rel="noopener">maven下载</a><br><img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1gffi2t9uvej31vk0mgq7v.jpg" alt=""></p>
<ul>
<li>Binary:编译之后的二进制文件</li>
<li>Source:表示可以查看源代码的,比Binary大一点</li>
<li>tar.gz archive:Linux、macOS系统使用</li>
<li>zip archive:windows系统使用</li>
</ul>
</li>
<li><p>解压到本地,注意解压的目录下不要有中文</p>
</li>
<li><p>配置环境变量</p>
<ul>
<li><p>mac<br>打开terminel输入以下命令:<code>vim ~/.bash_profile</code>打开.bash_profile文件,在次文件中添加设置环境变量的命令</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">export M2_HOME= ***这里换成自己解压maven的地址***</span><br><span class="line">//示例:export M2_HOME=/Users/XXX/Maven/apache-maven-3.6.3</span><br><span class="line">export PATH=$PATH:$M2_HOME/bin</span><br></pre></td></tr></table></figure>
<p> 添加之后保存并推出,执行以下命令使配置生效:<code>source ~/.bash_profile</code><br> mac中解决<code>source ~/.bash_profile</code>关闭终端后在打开不生效的问题,需要在~/.zshrc文件最后,增加一行:<code>source ~/.bash_profile</code>。如果没有此文件可以使用<code>vim ~/.zshr</code>新建一个文件,在添加对应的命令</p>
</li>
<li><p>win<br> 参考<a href="https://blog.csdn.net/weixin_41557632/article/details/78939663" target="_blank" rel="noopener">WIN10下安装配置mave</a>文章进行配置</p>
</li>
</ul>
</li>
</ol>
<h2 id="4-配置maven本地仓库"><a href="#4-配置maven本地仓库" class="headerlink" title="4.配置maven本地仓库"></a>4.配置maven本地仓库</h2><h3 id="4-1-仓库的分类"><a href="#4-1-仓库的分类" class="headerlink" title="4.1 仓库的分类"></a>4.1 仓库的分类</h3><ol>
<li>本地仓库:为当前本机电脑上的所有Maven工程服务。</li>
<li>远程仓库<ul>
<li>私服:架设在当前局域网环境下,为当前局域网范围内的所有Maven工程服务。<br><img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1gffiq931w6j30ie06h0ta.jpg" alt=""></li>
<li>中央仓库:架设在 Internet 上,为全世界所有Maven工程服务。</li>
<li>中央仓库的镜像:架设在各个大洲,为中央仓库分担流量。减轻中央仓库的压力,同时更快的响应用户请求。</li>
</ul>
</li>
</ol>
<h3 id="4-2-本地仓库的配置"><a href="#4-2-本地仓库的配置" class="headerlink" title="4.2 本地仓库的配置"></a>4.2 本地仓库的配置</h3><p>在 conf/setting.xml的文件中更改默认的仓库位置,也就是我们jar包下载后存放的位置</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line"><!--需要添加的配置--></span><br><span class="line"> <localRepository>/Users/wangyufei/Maven/repository</localRepository></span><br></pre></td></tr></table></figure>
<p><img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1gffj8b8m19j316s0n0gqp.jpg" alt=""></p>
<h2 id="5-maven的标准目录结构"><a href="#5-maven的标准目录结构" class="headerlink" title="5.maven的标准目录结构"></a>5.maven的标准目录结构</h2><p><img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1gffu24cncjj30pt0bntbh.jpg" alt=""></p>
<h2 id="6-maven的常用命令"><a href="#6-maven的常用命令" class="headerlink" title="6.maven的常用命令"></a>6.maven的常用命令</h2><h3 id="6-1-clean命令"><a href="#6-1-clean命令" class="headerlink" title="6.1 clean命令"></a>6.1 clean命令</h3><p>清除编译产生的target文件夹内容,可以配合相应命令一起使用,如mvn clean package, mvn clean test</p>
<h3 id="6-2-complie命令"><a href="#6-2-complie命令" class="headerlink" title="6.2 complie命令"></a>6.2 complie命令</h3><p>该命令可以对src/main/java目录的下的代码进行编译</p>
<h3 id="6-3-test命令"><a href="#6-3-test命令" class="headerlink" title="6.3 test命令"></a>6.3 test命令</h3><p>测试命令,或执行src/test/java/下junit的测试用例</p>
<h3 id="6-4-package命令"><a href="#6-4-package命令" class="headerlink" title="6.4 package命令"></a>6.4 package命令</h3><p>mvn package,打包项目</p>
<h3 id="6-5-install命令"><a href="#6-5-install命令" class="headerlink" title="6.5 install命令"></a>6.5 install命令</h3><p>mvn install,打包后将其安装在本地仓库</p>
<h2 id="7-maven的生命周期"><a href="#7-maven的生命周期" class="headerlink" title="7. maven的生命周期"></a>7. maven的生命周期</h2><h3 id="7-1-Maven-有三套相互独立的生命周期"><a href="#7-1-Maven-有三套相互独立的生命周期" class="headerlink" title="7.1 Maven 有三套相互独立的生命周期"></a>7.1 Maven 有三套相互独立的生命周期</h3><ol>
<li>Clean Lifecycle 在进行真正的构建之前进行一些清理工作。</li>
<li>Default Lifecycle 构建的核心部分,编译,测试,打包,安装,部署等等。</li>
<li>Site Lifecycle 生成项目报告,站点,发布站点。<br>它们是相互独立的,你可以仅仅调用<br>clean 来清理工作目录,仅仅调用 site 来生 成站点。 当然你也可以<br>直接运行 mvn clean install site 运行所有这三套生命周期。</li>
</ol>
<h2 id="8-maven的概念模型图"><a href="#8-maven的概念模型图" class="headerlink" title="8. maven的概念模型图"></a>8. maven的概念模型图</h2><h3 id="8-1-POM"><a href="#8-1-POM" class="headerlink" title="8.1 POM"></a>8.1 POM</h3><p>Project Object Model:项目对象模型。将 Java 工程 的相关信息封装为 对象 作为便于操作和管理的 模型 。Maven 工程的核心配置。可以说学习Maven 就是学习 pom.xml 文件中的配置。</p>
<h3 id="8-2-Maven-的坐标"><a href="#8-2-Maven-的坐标" class="headerlink" title="8.2 Maven 的坐标"></a>8.2 Maven 的坐标</h3><p>使用如下三个向量在Maven的仓库中唯一的确定一个Maven工程。</p>
<ol>
<li><p>groupid:公司或组织的域名倒序当前项目名称</p>
</li>
<li><p>artifactId:当前项目的模块名称</p>
</li>
<li><p>version:当前模块的版本</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line"><groupId>org.example</groupId></span><br><span class="line"><artifactId>wugujiawebmaven</artifactId></span><br><span class="line"><version>1.0-SNAPSHOT</version></span><br></pre></td></tr></table></figure>
</li>
</ol>
<h3 id="8-3-如何通过坐标到仓库中查找-jar包"><a href="#8-3-如何通过坐标到仓库中查找-jar包" class="headerlink" title="8.3 如何通过坐标到仓库中查找 jar包"></a>8.3 如何通过坐标到仓库中查找 jar包</h3><ol>
<li>将 gav 三个向量连起来<code>org.example+wugujiawebmaven+1.0-SNAPSHOT</code></li>
<li>以连起来的字符串作为目录结构到仓库中查找<code>org/example/wugujiawebmaven/1.0-SNAPSHOT</code></li>
</ol>
<p><strong><em>※注意:我们自己的Maven工程必须执行安装操作才会进入仓库 。安装的命令是: mvn install</em></strong></p>
<h2 id="9-idea集成maven插件"><a href="#9-idea集成maven插件" class="headerlink" title="9. idea集成maven插件"></a>9. idea集成maven插件</h2><p><img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1gffrb1dkjqj31c00u07wh.jpg" alt=""><br>配置创建java工程<code>-DarchetypeCatalog=local</code> 是为了防止在无网络的情况下可是使用本地已经下载好的maven的骨架<br>配置路径如下:<br><img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1gffreyumrvj314f0u0tf8.jpg" alt=""></p>
<h2 id="10-使用骨架创建maven的java工程"><a href="#10-使用骨架创建maven的java工程" class="headerlink" title="10. 使用骨架创建maven的java工程"></a>10. 使用骨架创建maven的java工程</h2><p><img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1gffri7ezbpj31iu0u0k3u.jpg" alt=""></p>
<p><img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1gffrkwyag3j31i50u079a.jpg" alt=""><br><img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1gffrnc34uej31gz0u00xc.jpg" alt=""></p>
<h2 id="11-不实用骨架创建maven的java工程"><a href="#11-不实用骨架创建maven的java工程" class="headerlink" title="11. 不实用骨架创建maven的java工程"></a>11. 不实用骨架创建maven的java工程</h2><p><img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1gffrtdfg0nj31h70u0gyf.jpg" alt=""><br><img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1gffrtxvpltj31ne0t0whp.jpg" alt=""></p>
<h2 id="12-使用骨架创建maven的web工程"><a href="#12-使用骨架创建maven的web工程" class="headerlink" title="12. 使用骨架创建maven的web工程"></a>12. 使用骨架创建maven的web工程</h2><ol>
<li>创建web工程<br><img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1gffryrg9q3j31h40u0aom.jpg" alt=""><br><img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1gffs07au3bj31i30u00wt.jpg" alt=""><br><img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1gffs0ll4c2j31hf0u079r.jpg" alt=""></li>
<li>创建好的javaweb工程(图1),所以我们需要手动添加一个java的目录用于编写java代码(图2),还要将java目录添加为Source Root(图3)<br> <img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1gffs5okhm8j317m0m4e81.jpg" alt="图1"><br> <img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1gffs77nvf6j31h40u0x6p.jpg" alt="图2"><br> <img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1gffs9f0i5cj312h0u0b2a.jpg" alt="图3"></li>
</ol>
<h2 id="13-创建自定义的JavaWeb工程"><a href="#13-创建自定义的JavaWeb工程" class="headerlink" title="13. 创建自定义的JavaWeb工程"></a>13. 创建自定义的JavaWeb工程</h2><p><img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1gfft5u46n5j31hb0u0dqk.jpg" alt=""><br><img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1gfft6zy94pj31ih0u0gsk.jpg" alt=""><br>创建web工程信息<br><img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1gfft8j06icj30u00wt4qp.jpg" alt=""><br><img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1gffta8mvonj31080u07am.jpg" alt=""><br>创建好的javaweb工程目录如图所示<br><img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1gfftb47ep7j326i0qwqv6.jpg" alt=""></p>
<h2 id="14-idea中执行maven的命令"><a href="#14-idea中执行maven的命令" class="headerlink" title="14.idea中执行maven的命令"></a>14.idea中执行maven的命令</h2><ol>
<li>clean、test、complie、package、install的命令输入方法相同<br><img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1gfftegsyzaj31a50u0kjl.jpg" alt=""></li>
</ol>
<h2 id="15-Maven插件"><a href="#15-Maven插件" class="headerlink" title="15. Maven插件"></a>15. Maven插件</h2><p>Maven是一个核心引擎,提供了基本的项目处理能力和建设过程的管理,以及一系列的插件是用来执行实际建设任务。maven插件可以完成一些特定的功能。例如,集成jdk插件可以方便的修改项目的编译环境;集成tomcat插件后,无需安装tomcat服务器就可以运行tomcat进行项目的发布与测试。在pom.xml中通过plugin标签引入maven的功能插件。</p>
<h3 id="15-1-JDK编译版本的插件"><a href="#15-1-JDK编译版本的插件" class="headerlink" title="15.1 JDK编译版本的插件"></a>15.1 JDK编译版本的插件</h3><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line"><!-- jdk版本插件 --></span><br><span class="line"><plugin></span><br><span class="line"> <groupId>org.apache.maven.plugins</groupId></span><br><span class="line"> <artifactId>maven-compiler-plugin</artifactId></span><br><span class="line"> <version>3.2</version></span><br><span class="line"> <configuration></span><br><span class="line"> <source>1.8</source></span><br><span class="line"> <target>1.8</target></span><br><span class="line"> <encoding>UTF-8</encoding></span><br><span class="line"> <showWarnings>true</showWarnings></span><br><span class="line"> </configuration></span><br><span class="line"></plugin></span><br></pre></td></tr></table></figure>
<h3 id="15-2-Tomcat7服务端的插件"><a href="#15-2-Tomcat7服务端的插件" class="headerlink" title="15.2 Tomcat7服务端的插件"></a>15.2 Tomcat7服务端的插件</h3><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line"><!-- tomcat7插件 --></span><br><span class="line"><plugin></span><br><span class="line"> <groupId>org.apache.tomcat.maven</groupId></span><br><span class="line"> <artifactId>tomcat7-maven-plugin</artifactId></span><br><span class="line"> <version>2.1</version></span><br><span class="line"> <configuration></span><br><span class="line"> <port>8080</port></span><br><span class="line"> <server>tomcat7</server></span><br><span class="line"> </configuration></span><br><span class="line"></plugin></span><br></pre></td></tr></table></figure>
<p><strong><em>tomcat插件注意问题</em></strong><br>Maven的中央仓库中只有Tomcat7.X版本的插件,而之前我们使用的是8.X的版本,如果想使用Tomcat8.X的插件可以去其他第三方仓库进行寻找,或者使用IDEA集成外部Tomcat8极其以上版本,进行项目的发布</p>
<h2 id="16-依赖范围"><a href="#16-依赖范围" class="headerlink" title="16. 依赖范围"></a>16. 依赖范围</h2><p><img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1gfftnby1jsj30dn07g0sq.jpg" alt=""></p>
<ul>
<li>compile 编译、测试、运行,A在编译时依赖B,并且在测试和运行时也依赖<br> strus-core、spring-beans。打到war包或jar包</li>
<li>provided 编译、和测试有效,A在编译和测试时需要B<br> servlet-api就是编译和测试有用,在运行时不用(tomcat容器已提供)<br>不会打到war</li>
<li>runtime:测试、运行有效<br> 在运行和测试时需要通过jdbc驱动包(mysql驱动)连接数据库,需要的会打到war</li>
<li>test:只是测试有效,只在单元测试类中用<br> 例如:junit 不会打到war</li>
<li>按照依赖强度,由强到弱来排序:(理解)<br> compile> provided> runtime> test</li>
</ul>
<h2 id="17-maven倒入jar包冲突解决"><a href="#17-maven倒入jar包冲突解决" class="headerlink" title="17. maven倒入jar包冲突解决"></a>17. maven倒入jar包冲突解决</h2><ol>
<li>路径最短者优先<br> <img src="https://tva1.sinaimg.cn/large/007S8ZIlly1gffzi2u0s7j30dw03oaah.jpg" alt=""></li>
<li>路径相同时先声明者优先。这里声明的先后顺序指的是dependency标签配置的先后顺序。<br> <img src="https://tva1.sinaimg.cn/large/007S8ZIlly1gffzigrc5cj30di03caae.jpg" alt=""></li>
<li>使用<code><exclusions></code>标签排除</li>
</ol>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line"><dependency></span><br><span class="line"> <groupId>com.a tguigu.maven</groupId></span><br><span class="line"> <artifactId>HelloFriend</artifactId></span><br><span class="line"> <version>0.0.1 SNAPSHOT</version></span><br><span class="line"> <type>jar</type></span><br><span class="line"> <scope>compile</scope></span><br><span class="line"> <exclusions></span><br><span class="line"> <exclusion></span><br><span class="line"> <groupId>commons logging</groupId></span><br><span class="line"> <artifactId>commons logging</artifactId></span><br><span class="line"> </exclusion></span><br><span class="line"> </exclusions></span><br><span class="line"></dependency></span><br></pre></td></tr></table></figure>
<h2 id="18-统一管理所依赖的版本"><a href="#18-统一管理所依赖的版本" class="headerlink" title="18. 统一管理所依赖的版本"></a>18. 统一管理所依赖的版本</h2><p>对同一个框架的一组jar包最好使用相同的版本。为了方便升级框架,可以将 jar 包的版本信息统一提取出来</p>
<ol>
<li><p>统一声明版本号</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line"> //其中</span><br><span class="line">atguigu.spring.version 部分 是自定义标签</span><br><span class="line"> <properties></span><br><span class="line"> <atguigu.spring.version>4.1.1.RELEASE</atguigu.s pring.version></span><br><span class="line"></properties></span><br></pre></td></tr></table></figure></li>
<li><p>引用前面声明的版本号</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line"> <dependencies></span><br><span class="line"> <dependency></span><br><span class="line"> <groupId>org.springframework</groupId></span><br><span class="line"> <artifactId>springcore</artifactId></span><br><span class="line"> <version>${atguigu.spring. version}</version></span><br><span class="line"> </dependency></span><br><span class="line"> ……</span><br><span class="line"></dependencies></span><br></pre></td></tr></table></figure>
</li>
<li><p>完整示例</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line"><?xml version="1.0" encoding="UTF-8"?></span><br><span class="line"><project xmlns="http://maven.apache.org/POM/4.0.0"</span><br><span class="line"> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"</span><br><span class="line"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"></span><br><span class="line"> <modelVersion>4.0.0</modelVersion></span><br><span class="line"> <groupId>com.itheima</groupId></span><br><span class="line"> <artifactId>ssm_parent_331</artifactId></span><br><span class="line"> <packaging>pom</packaging></span><br><span class="line"> <version>1.0-SNAPSHOT</version></span><br><span class="line"> <modules></span><br><span class="line"> <module>ssm_dao_331</module></span><br><span class="line"> <module>ssm_service_331</module></span><br><span class="line"> <module>ssm_web_331</module></span><br><span class="line"> <module>ssm_domain_331</module></span><br><span class="line"> <module>ssm_utils_331</module></span><br><span class="line"> </modules></span><br><span class="line"> <properties></span><br><span class="line"> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></span><br><span class="line"> <maven.compiler.source>1.7</maven.compiler.source></span><br><span class="line"> <maven.compiler.target>1.7</maven.compiler.target></span><br><span class="line"> <spring.version>5.0.2.RELEASE</spring.version></span><br><span class="line"> <spring.security.version>5.0.2.RELEASE</spring.security.version></span><br><span class="line"> </properties></span><br><span class="line"> <dependencies></span><br><span class="line"> <!-- spring相关的jar包 --></span><br><span class="line"> <!-- 容器 --></span><br><span class="line"> <dependency></span><br><span class="line"> <groupId>org.springframework</groupId></span><br><span class="line"> <artifactId>spring-context</artifactId></span><br><span class="line"> <version>${spring.version}</version></span><br><span class="line"> </dependency></span><br><span class="line"> <!-- 事务 --></span><br><span class="line"> <dependency></span><br><span class="line"> <groupId>org.springframework</groupId></span><br><span class="line"> <artifactId>spring-tx</artifactId></span><br><span class="line"> <version>${spring.version}</version></span><br><span class="line"> </dependency></span><br><span class="line"> <!-- JDBC --></span><br><span class="line"> <dependency></span><br><span class="line"> <groupId>org.springframework</groupId></span><br><span class="line"> <artifactId>spring-jdbc</artifactId></span><br><span class="line"> <version>${spring.version}</version></span><br><span class="line"> </dependency></span><br><span class="line"> <!-- 测试 --></span><br><span class="line"> <dependency></span><br><span class="line"> <groupId>org.springframework</groupId></span><br><span class="line"> <artifactId>spring-test</artifactId></span><br><span class="line"> <version>${spring.version}</version></span><br><span class="line"> </dependency></span><br><span class="line"> <!-- springMVC --></span><br><span class="line"> <dependency></span><br><span class="line"> <groupId>org.springframework</groupId></span><br><span class="line"> <artifactId>spring-webmvc</artifactId></span><br><span class="line"> <version>${spring.version}</version></span><br><span class="line"> </dependency></span><br><span class="line"> <!-- mybatis --></span><br><span class="line"> <dependency></span><br><span class="line"> <groupId>org.mybatis</groupId></span><br><span class="line"> <artifactId>mybatis</artifactId></span><br><span class="line"> <version>3.4.5</version></span><br><span class="line"> </dependency></span><br><span class="line"> <!-- mybatis与Spring整合 --></span><br><span class="line"> <dependency></span><br><span class="line"> <groupId>org.mybatis</groupId></span><br><span class="line"> <artifactId>mybatis-spring</artifactId></span><br><span class="line"> <version>1.3.1</version></span><br><span class="line"> </dependency></span><br><span class="line"> <!-- AOP切面 --></span><br><span class="line"> <dependency></span><br><span class="line"> <groupId>org.aspectj</groupId></span><br><span class="line"> <artifactId>aspectjweaver</artifactId></span><br><span class="line"> <version>1.8.7</version></span><br><span class="line"> </dependency></span><br><span class="line"> <!-- 数据源 --></span><br><span class="line"> <dependency></span><br><span class="line"> <groupId>com.alibaba</groupId></span><br><span class="line"> <artifactId>druid</artifactId></span><br><span class="line"> <version>1.1.9</version></span><br><span class="line"> </dependency></span><br><span class="line"> <!-- 单元测试 --></span><br><span class="line"> <dependency></span><br><span class="line"> <groupId>junit</groupId></span><br><span class="line"> <artifactId>junit</artifactId></span><br><span class="line"> <version>4.12</version></span><br><span class="line"> <scope>test</scope></span><br><span class="line"> </dependency></span><br><span class="line"> <!-- servletAPI --></span><br><span class="line"> <!-- JSP应用 --></span><br><span class="line"> <dependency></span><br><span class="line"> <groupId>javax.servlet</groupId></span><br><span class="line"> <artifactId>jsp-api</artifactId></span><br><span class="line"> <version>2.0</version></span><br><span class="line"> <scope>provided</scope></span><br><span class="line"> </dependency></span><br><span class="line"> <!-- servlet应用 --></span><br><span class="line"> <dependency></span><br><span class="line"> <groupId>javax.servlet</groupId></span><br><span class="line"> <artifactId>servlet-api</artifactId></span><br><span class="line"> <version>2.5</version></span><br><span class="line"> <scope>provided</scope></span><br><span class="line"> </dependency></span><br><span class="line"> <!-- 日志记录工具 --></span><br><span class="line"> <dependency></span><br><span class="line"> <groupId>log4j</groupId></span><br><span class="line"> <artifactId>log4j</artifactId></span><br><span class="line"> <version>1.2.17</version></span><br><span class="line"> </dependency></span><br><span class="line"> <dependency></span><br><span class="line"> <groupId>org.apache.logging.log4j</groupId></span><br><span class="line"> <artifactId>log4j-api</artifactId></span><br><span class="line"> <version>2.10.0</version></span><br><span class="line"> </dependency></span><br><span class="line"> <dependency></span><br><span class="line"> <groupId>org.apache.logging.log4j</groupId></span><br><span class="line"> <artifactId>log4j-core</artifactId></span><br><span class="line"> <version>2.10.0</version></span><br><span class="line"> </dependency></span><br><span class="line"> <dependency></span><br><span class="line"> <groupId>org.apache.logging.log4j</groupId></span><br><span class="line"> <artifactId>log4j-web</artifactId></span><br><span class="line"> <version>2.9.1</version></span><br><span class="line"> </dependency></span><br><span class="line"> <dependency></span><br><span class="line"> <groupId>org.slf4j</groupId></span><br><span class="line"> <artifactId>slf4j-api</artifactId></span><br><span class="line"> <version>1.7.25</version></span><br><span class="line"> </dependency></span><br><span class="line"> <dependency></span><br><span class="line"> <groupId>org.apache.logging.log4j</groupId></span><br><span class="line"> <artifactId>log4j-slf4j-impl</artifactId></span><br><span class="line"> <version>2.9.1</version></span><br><span class="line"> </dependency></span><br><span class="line"> <dependency></span><br><span class="line"> <groupId>org.apache.logging.log4j</groupId></span><br><span class="line"> <artifactId>log4j-jcl</artifactId></span><br><span class="line"> <version>2.9.1</version></span><br><span class="line"> </dependency></span><br><span class="line"> <!-- mysql --></span><br><span class="line"> <dependency></span><br><span class="line"> <groupId>mysql</groupId></span><br><span class="line"> <artifactId>mysql-connector-java</artifactId></span><br><span class="line"> <version>5.1.6</version></span><br><span class="line"> </dependency></span><br><span class="line"> <!--oracle的jar包--></span><br><span class="line"> <dependency></span><br><span class="line"> <groupId>com.oracle</groupId></span><br><span class="line"> <artifactId>ojdbc14</artifactId></span><br><span class="line"> <version>10.2.0.2.0</version></span><br><span class="line"> </dependency></span><br><span class="line"> <!-- JSTL --></span><br><span class="line"> <dependency></span><br><span class="line"> <groupId>jstl</groupId></span><br><span class="line"> <artifactId>jstl</artifactId></span><br><span class="line"> <version>1.2</version></span><br><span class="line"> </dependency></span><br><span class="line"> <dependency></span><br><span class="line"> <groupId>taglibs</groupId></span><br><span class="line"> <artifactId>standard</artifactId></span><br><span class="line"> <version>1.1.1</version></span><br><span class="line"> </dependency></span><br><span class="line"> <!-- 文件上传 --></span><br><span class="line"> <dependency></span><br><span class="line"> <groupId>commons-fileupload</groupId></span><br><span class="line"> <artifactId>commons-fileupload</artifactId></span><br><span class="line"> <version>1.3.1</version></span><br><span class="line"> </dependency></span><br><span class="line"> <dependency></span><br><span class="line"> <groupId>commons-io</groupId></span><br><span class="line"> <artifactId>commons-io</artifactId></span><br><span class="line"> <version>2.5</version></span><br><span class="line"> </dependency></span><br><span class="line"> <dependency></span><br><span class="line"> <groupId>c3p0</groupId></span><br><span class="line"> <artifactId>c3p0</artifactId></span><br><span class="line"> <version>0.9.1.2</version></span><br><span class="line"> </dependency></span><br><span class="line"> <dependency></span><br><span class="line"> <groupId>com.github.pagehelper</groupId></span><br><span class="line"> <artifactId>pagehelper</artifactId></span><br><span class="line"> <version>5.1.2</version></span><br><span class="line"> </dependency></span><br><span class="line"> <dependency></span><br><span class="line"> <groupId>org.springframework.security</groupId></span><br><span class="line"> <artifactId>spring-security-web</artifactId></span><br><span class="line"> <version>${spring.security.version}</version></span><br><span class="line"> </dependency></span><br><span class="line"> <dependency></span><br><span class="line"> <groupId>org.springframework.security</groupId></span><br><span class="line"> <artifactId>spring-security-config</artifactId></span><br><span class="line"> <version>${spring.security.version}</version></span><br><span class="line"> </dependency></span><br><span class="line"> <dependency></span><br><span class="line"> <groupId>org.springframework.security</groupId></span><br><span class="line"> <artifactId>spring-security-core</artifactId></span><br><span class="line"> <version>${spring.security.version}</version></span><br><span class="line"> </dependency></span><br><span class="line"> <dependency></span><br><span class="line"> <groupId>org.springframework.security</groupId></span><br><span class="line"> <artifactId>spring-security-taglibs</artifactId></span><br><span class="line"> <version>${spring.security.version}</version></span><br><span class="line"> </dependency></span><br><span class="line"> </dependencies></span><br><span class="line"></project></span><br></pre></td></tr></table></figure>
</li>
</ol>
<h2 id="19-maven工程的拆分与聚合"><a href="#19-maven工程的拆分与聚合" class="headerlink" title="19. maven工程的拆分与聚合"></a>19. maven工程的拆分与聚合</h2><h3 id="19-1-创建父工程-只需要有pom-xnl文件"><a href="#19-1-创建父工程-只需要有pom-xnl文件" class="headerlink" title="19.1 创建父工程(只需要有pom.xnl文件)"></a>19.1 创建父工程(只需要有pom.xnl文件)</h3><p><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1gfg1lp5p48j31ih0u0wrb.jpg" alt=""><br><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1gfg1nppqvjj31i50u0gqy.jpg" alt=""><br>父工程中可以将<code>src</code>的文件夹删除掉,只留下<code>pom.xml</code>文件<br><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1gfg1prv2npj31fy0u01ky.jpg" alt=""></p>
<h3 id="19-2-子模块的创建"><a href="#19-2-子模块的创建" class="headerlink" title="19.2 子模块的创建"></a>19.2 子模块的创建</h3><p><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1gfg1s2lmzmj310i0u01ky.jpg" alt=""><br><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1gfg1tfvpygj31io0u013q.jpg" alt=""><br><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1gfg1upasaqj31ij0u0ted.jpg" alt=""></p>
<h3 id="19-3-工程和模块的区别"><a href="#19-3-工程和模块的区别" class="headerlink" title="19.3 工程和模块的区别"></a>19.3 工程和模块的区别</h3><p>区别:</p>
<ol>
<li>工程不等于完整的项目,模块也不等于一个完整的项目。代码完整,就可以说这是一个完整的项目和此项目和模块没有关系</li>
<li>工程只能使用自己的内部资源,工程天生时独立的。后天可以和其他工程和模块建立关联关系</li>
<li>模块天生是属于父工程的,模块一旦创建,所有父工程的资源都可以使用</li>
</ol>
<p>关系:</p>
<ol>
<li>父子工程中,子模块天生集成父工程,可以使用父工程所有的资源</li>
<li>子模块之间是没有关联的</li>
<li>子模块之间可以使用<code>denpendency</code>之间来互相引用,建立关联</li>
<li>平级之间引用叫做依赖,依赖不是先天的,是后天建立的</li>
<li>父子工程之间不用建立关系,继承关系是先天的,不需要手动建立</li>
</ol>
<h3 id="19-4-maven父子工程三种启动方式"><a href="#19-4-maven父子工程三种启动方式" class="headerlink" title="19.4 maven父子工程三种启动方式"></a>19.4 maven父子工程三种启动方式</h3><ol>
<li>可以在父工程中使用 mvn run</li>
<li>使用web模块中启动时,需要先将其依赖的jar发布到本地仓库或者远程仓库中</li>
<li>添加本地的tomcat,不在使用maven中自带的tomcat</li>
</ol>
<h3 id="19-5-搭建maven的私服环境"><a href="#19-5-搭建maven的私服环境" class="headerlink" title="19.5 搭建maven的私服环境"></a>19.5 搭建maven的私服环境</h3><ol>
<li><p>下载<a href=":http://www.sonatype.org/nexus/archived/">nexus</a><br>Nexus 是Maven仓库管理器,通过nexus可以搭建maven仓库,同时nexus还提供强大的仓库管理功能,构件搜索功能等。</p>
</li>
<li><p>Centos7上搭建私服</p>
<ul>
<li><p>上传到Centos7服务器<br><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1gfg5gm5vknj31iw0fojv2.jpg" alt=""></p>
</li>
<li><p>使用命令<code>tar -avxf nexus-3.23.0-03-unix.tar.gz</code>解压完成后会出现一下两个文件(一个是nexus服务,一个是私有仓库)<br><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1gfg5iheh9tj31gg0f60wc.jpg" alt=""></p>
</li>
<li><p>修改默认的端口<br><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1gfg72x96olj31fo0gigoo.jpg" alt=""></p>
</li>
<li><p>启动nexus<br>启动 Nexus(默认端口是8081),Nexus 常用的一些命令包括:/nexus-3.23.0-03/bin/nexus {start|stop|run|run-redirect|status|restart|force-reload},下面我们启动Nexus: </p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">WARNING: ************************************************************</span><br><span class="line">WARNING: Detected execution as "root" user. This is NOT recommended!</span><br><span class="line">WARNING: ************************************************************</span><br><span class="line">Starting nexus</span><br></pre></td></tr></table></figure>
<p>上面在启动过程中出现警告:“不推荐使用root用户启动”。这个警告不影响Nexus的正常访问和使用。 如果上面的环境变量配置了,试着修改</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">/bin/nexus.rc文件中的run_as_user= “root”</span><br></pre></td></tr></table></figure>
<h3 id="19-6-私服的应用"><a href="#19-6-私服的应用" class="headerlink" title="19.6 私服的应用"></a>19.6 私服的应用</h3><h4 id="19-6-1-账号和密码是固定的-admin-admin123"><a href="#19-6-1-账号和密码是固定的-admin-admin123" class="headerlink" title="19.6.1 账号和密码是固定的 admin/admin123"></a>19.6.1 账号和密码是固定的 admin/admin123</h4><h4 id="19-6-2-登陆私服的配置-server-xml"><a href="#19-6-2-登陆私服的配置-server-xml" class="headerlink" title="19.6.2 登陆私服的配置(server.xml)"></a>19.6.2 登陆私服的配置(server.xml)</h4> <figure class="highlight plain"><table><tr><td class="code"><pre><span class="line"><server></span><br><span class="line"> <id>releases</id></span><br><span class="line"> <username>admin</username></span><br><span class="line"> <password>admin123</password></span><br><span class="line"> </server></span><br><span class="line"> <server></span><br><span class="line"> <id>snapshots</id></span><br><span class="line"> <username>admin</username></span><br><span class="line"> <password>admin123</password></span><br><span class="line"> </server></span><br></pre></td></tr></table></figure>
<p><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1gfgc8gj788j31a80lwjvk.jpg" alt=""></p>
</li>
</ul>
</li>
</ol>
<h4 id="19-6-3-配置项目pom-xml"><a href="#19-6-3-配置项目pom-xml" class="headerlink" title="19.6.3 配置项目pom.xml"></a>19.6.3 配置项目pom.xml</h4><p>配置私服仓库的地址,本公司的自己的jar包会上传到私服的宿主仓库,根据工程的版本号决定上传到哪个宿主仓库,如果版本为release则上传到私服的release仓库,如果版本为snapshot则上传到私服的snapshot仓库</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line"><distributionManagement></span><br><span class="line"> <repository></span><br><span class="line"> <id>release</id></span><br><span class="line"> <name>Release Repository</name></span><br><span class="line"> <url>http://ip/nexus/content/repositories/releases</url></span><br><span class="line"> </repository></span><br><span class="line"> <snapshotRepository></span><br><span class="line"> <id>snapshot</id></span><br><span class="line"> <name>Snapshot Repository</name></span><br><span class="line"> <url>http://ip/nexus/content/repositories/snapshots</url></span><br><span class="line"> </snapshotRepository></span><br><span class="line"></distributionManagement></span><br></pre></td></tr></table></figure>
<p><strong><em>注意:pom.xml这里<id> 和 settings.xml 配置 <id> 对应!</em></strong></p>
<h4 id="19-6-4-相关jar包的下载"><a href="#19-6-4-相关jar包的下载" class="headerlink" title="19.6.4 相关jar包的下载"></a>19.6.4 相关jar包的下载</h4><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line"><!-- 下载jar包配置 --></span><br><span class="line"> <profile> </span><br><span class="line"> <!--profile的id --></span><br><span class="line"> <id>dev</id></span><br><span class="line"> <repositories></span><br><span class="line"> <repository> <!--仓库id,repositories可以配置多个仓库,保证id不重复 --></span><br><span class="line"> <id>nexus</id> <!--仓库地址,即nexus仓库组的地址 --></span><br><span class="line"> <url>http://localhost:8081/nexus/content/groups/public/</url> <!--是否下载releases构件 --></span><br><span class="line"> <releases></span><br><span class="line"> <enabled>true</enabled></span><br><span class="line"> </releases> <!--是否下载snapshots构件 --></span><br><span class="line"> <snapshots></span><br><span class="line"> <enabled>true</enabled></span><br><span class="line"> </snapshots></span><br><span class="line"> </repository></span><br><span class="line"> </repositories></span><br><span class="line"> <pluginRepositories> <!-- 插件仓库,maven的运行依赖插件,也需要从私服下载插件 --></span><br><span class="line"> <pluginRepository> <!-- 插件仓库的id不允许重复,如果重复后边配置会覆盖前边 --></span><br><span class="line"> <id>public</id></span><br><span class="line"> <name>Public Repositories</name></span><br><span class="line"> <url>http://localhost:8081/nexus/content/groups/public/</url></span><br><span class="line"> </pluginRepository></span><br><span class="line"> </pluginRepositories></span><br><span class="line"> </profile></span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line"><activeProfiles></span><br><span class="line"> <activeProfile>dev</activeProfile></span><br><span class="line"> </activeProfiles></span><br></pre></td></tr></table></figure>
<p><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1gfgcmn2bayj311r0u0qit.jpg" alt=""></p>
<h4 id="19-6-5-安装第三方jar包到本地仓库和私服"><a href="#19-6-5-安装第三方jar包到本地仓库和私服" class="headerlink" title="19.6.5 安装第三方jar包到本地仓库和私服"></a>19.6.5 安装第三方jar包到本地仓库和私服</h4><ol>
<li>安装第三方jar包到本地仓库</li>
</ol>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">----进入jar包所在目录运行</span><br><span class="line">mvn install:install-file -DgroupId=com.alibaba -DartifactId=fastjson -Dversion=1.1.37 -Dfile=fastjson-1.1.37.jar -Dpackaging=jar</span><br><span class="line">----打开cmd直接运行</span><br><span class="line">mvn install:install-file -DgroupId=com.alibaba -DartifactId=fastjson -Dversion=1.1.37 -Dpackaging=jar -Dfile=C:\my_java\授课资料\资料:maven【高级】\安装第三方jar包\fastjson-1.1.37.jar</span><br></pre></td></tr></table></figure>
<ol>
<li>安装第三方jar包到私服</li>
</ol>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">--在settings配置文件中添加登录私服第三方登录信息</span><br><span class="line"><server></span><br><span class="line"><id>thirdparty</id></span><br><span class="line"><username>admin</username></span><br><span class="line"><password>admin123</password></span><br><span class="line"></server></span><br><span class="line">----进入jar包所在目录运行</span><br><span class="line">mvn deploy:deploy-file -DgroupId=com.alibaba -DartifactId=fastjson -Dversion=1.1.37 -Dpackaging=jar -Dfile=fastjson-1.1.37.jar -Durl=http://localhost:8081/nexus/content/repositories/thirdparty/ -DrepositoryId=thirdparty</span><br><span class="line">----打开cmd直接运行</span><br><span class="line">mvn deploy:deploy-file -DgroupId=com.alibaba -DartifactId=fastjson -Dversion=1.1.37 -Dpackaging=jar -Dfile=C:\my_java\授课资料\资料:maven【高级】\安装第三方jar包\fastjson-1.1.37.jar -Durl=http://localhost:8081/nexus/content/repositories/thirdparty/ -DrepositoryId=thirdparty</span><br></pre></td></tr></table></figure>
<h2 id="20-配置全局的jdk"><a href="#20-配置全局的jdk" class="headerlink" title="20. 配置全局的jdk"></a>20. 配置全局的jdk</h2><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line"><!--配置全局的jdk为1.8--></span><br><span class="line"> <profile></span><br><span class="line"> <id>jdk-1.8</id> </span><br><span class="line"> <activation></span><br><span class="line"> <activeByDefault>true</activeByDefault></span><br><span class="line"> <jdk>1.8</jdk> </span><br><span class="line"> </activation> </span><br><span class="line"> </span><br><span class="line"> <properties> </span><br><span class="line"> <maven.compiler.source>1.8</maven.compiler.source> </span><br><span class="line"> <maven.compiler.target>1.8</maven.compiler.target> </span><br><span class="line"> <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion> </span><br><span class="line"> </properties> </span><br><span class="line"> </profile></span><br></pre></td></tr></table></figure>
<p><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1gfgd6p48qbj315y0u0qar.jpg" alt=""></p>
]]></content>
<categories>
<category>java</category>
</categories>
<tags>
<tag>maven</tag>
</tags>
</entry>
<entry>
<title>Mybatis学习笔记二</title>
<url>/posts/a71dc8d.html</url>
<content><![CDATA[<h2 id="1-Mybatis逆向工程"><a href="#1-Mybatis逆向工程" class="headerlink" title="1. Mybatis逆向工程"></a>1. Mybatis逆向工程</h2><h3 id="1-1-说明"><a href="#1-1-说明" class="headerlink" title="1.1 说明"></a>1.1 说明</h3><ol>
<li><p>MyBatis Generator<a href="http://mybatis.org/generator/#" target="_blank" rel="noopener">官方文档地址</a></p>
</li>
<li><p><a href="https://github.com/mybatis/generator/releases" target="_blank" rel="noopener">官方工程地址</a></p>
</li>
<li><p>依赖的包</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line"><!--mybatis逆向工程依赖包--></span><br><span class="line"> <dependency></span><br><span class="line"> <groupId>org.mybatis.generator</groupId></span><br><span class="line"> <artifactId>mybatis-generator-core</artifactId></span><br><span class="line"> <version>1.4.0</version></span><br><span class="line"> </dependency></span><br><span class="line"> <dependency></span><br><span class="line"> <groupId>org.mybatis.generator</groupId></span><br><span class="line"> <artifactId>mybatis-generator-maven-plugin</artifactId></span><br><span class="line"> <version>1.4.0</version></span><br><span class="line"> </dependency></span><br></pre></td></tr></table></figure>
<h3 id="1-2-逆向功能的配置文件"><a href="#1-2-逆向功能的配置文件" class="headerlink" title="1.2 逆向功能的配置文件"></a>1.2 逆向功能的配置文件</h3> <figure class="highlight plain"><table><tr><td class="code"><pre><span class="line"><?xml version="1.0" encoding="UTF-8"?></span><br><span class="line"><!DOCTYPE generatorConfiguration</span><br><span class="line"> PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"</span><br><span class="line"> "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"></span><br><span class="line"><generatorConfiguration></span><br><span class="line"></span><br><span class="line"> <!-- </span><br><span class="line"> targetRuntime="MyBatis3Simple":生成简单版的CRUD</span><br><span class="line"> MyBatis3:豪华版</span><br><span class="line"> </span><br><span class="line"> --></span><br><span class="line"> <context id="DB2Tables" targetRuntime="MyBatis3"></span><br><span class="line"> <!-- jdbcConnection:指定如何连接到目标数据库 --></span><br><span class="line"> <jdbcConnection driverClass="com.mysql.jdbc.Driver"</span><br><span class="line"> connectionURL="jdbc:mysql://localhost:3306/mybatis?allowMultiQueries=true"</span><br><span class="line"> userId="root"</span><br><span class="line"> password="123456"></span><br><span class="line"> </jdbcConnection></span><br><span class="line"></span><br><span class="line"> <!-- --></span><br><span class="line"> <javaTypeResolver ></span><br><span class="line"> <property name="forceBigDecimals" value="false" /></span><br><span class="line"> </javaTypeResolver></span><br><span class="line"></span><br><span class="line"> <!-- javaModelGenerator:指定javaBean的生成策略 </span><br><span class="line"> targetPackage="test.model":目标包名</span><br><span class="line"> targetProject="\MBGTestProject\src":目标工程</span><br><span class="line"> --></span><br><span class="line"> <javaModelGenerator targetPackage="com.atguigu.mybatis.bean" </span><br><span class="line"> targetProject=".\src"></span><br><span class="line"> <property name="enableSubPackages" value="true" /></span><br><span class="line"> <property name="trimStrings" value="true" /></span><br><span class="line"> </javaModelGenerator></span><br><span class="line"></span><br><span class="line"> <!-- sqlMapGenerator:sql映射生成策略: --></span><br><span class="line"> <sqlMapGenerator targetPackage="com.atguigu.mybatis.dao" </span><br><span class="line"> targetProject=".\conf"></span><br><span class="line"> <property name="enableSubPackages" value="true" /></span><br><span class="line"> </sqlMapGenerator></span><br><span class="line"></span><br><span class="line"> <!-- javaClientGenerator:指定mapper接口所在的位置 --></span><br><span class="line"> <javaClientGenerator type="XMLMAPPER" targetPackage="com.atguigu.mybatis.dao" </span><br><span class="line"> targetProject=".\src"></span><br><span class="line"> <property name="enableSubPackages" value="true" /></span><br><span class="line"> </javaClientGenerator></span><br><span class="line"></span><br><span class="line"> <!-- 指定要逆向分析哪些表:根据表要创建javaBean --></span><br><span class="line"> <table tableName="tbl_dept" domainObjectName="Department"></table></span><br><span class="line"> <table tableName="tbl_employee" domainObjectName="Employee"></table></span><br><span class="line"> </context></span><br><span class="line"></generatorConfiguration></span><br></pre></td></tr></table></figure>
</li>
</ol>
<h2 id="2-Mybatis的运行原理"><a href="#2-Mybatis的运行原理" class="headerlink" title="2. Mybatis的运行原理"></a>2. Mybatis的运行原理</h2><p><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1gfn1ec5vpzj30pc0eqdi7.jpg" alt="运行原理图"><br><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1gfn1fqhsrdj315l0u07b9.jpg" alt="框架原理图"></p>
<h3 id="2-1-sqlsessionFactory的初始化"><a href="#2-1-sqlsessionFactory的初始化" class="headerlink" title="2.1 sqlsessionFactory的初始化"></a>2.1 sqlsessionFactory的初始化</h3><ol>
<li>根据配置文件创建sqlsessionFactory<br><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1gfn8jtopjwj315e0q6jwj.jpg" alt=""></li>
<li>几个重要的类<br><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1gfn8l20ikdj30oy08amyc.jpg" alt=""><br><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1gfn8lcrpp2j30wy0n6gqg.jpg" alt=""><br><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1gfn8lnaxpkj310m0pijwy.jpg" alt=""><br><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1gfn8lu4tvtj31ca094tbp.jpg" alt=""><br><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1gfn8m1jq87j316q0cotc7.jpg" alt=""><br><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1gfn8mhihjrj30s408mdhp.jpg" alt=""><br><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1gfn8mo15jrj30s408mdhp.jpg" alt="缓存中保存的key:方法id+sql+参数xxx"></li>
</ol>
<h3 id="2-2-openSession获取SqlSession对象"><a href="#2-2-openSession获取SqlSession对象" class="headerlink" title="2.2 openSession获取SqlSession对象"></a>2.2 openSession获取SqlSession对象</h3><p>返回SqlSession的实现类DefaultSqlSession对象。他里面包含了Executor和Configuration;Executor会在这一步被创建<br><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1gfn931bghjj317a0mcn16.jpg" alt=""></p>
<h3 id="2-3-getMapper获取到接口的代理对象"><a href="#2-3-getMapper获取到接口的代理对象" class="headerlink" title="2.3 getMapper获取到接口的代理对象"></a>2.3 getMapper获取到接口的代理对象</h3><p>getMapper返回接口的代理对象包含了SqlSession对象<br><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1gfn9ey08pxj312a0lon0i.jpg" alt=""></p>
<h3 id="2-4-查询实现"><a href="#2-4-查询实现" class="headerlink" title="2.4 查询实现"></a>2.4 查询实现</h3><p><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1gfnagofu78j30zm0u043h.jpg" alt=""></p>
<h2 id="3-插件"><a href="#3-插件" class="headerlink" title="3.插件"></a>3.插件</h2><p>MyBatis在四大对象的创建过程中,都会有插件进行介入。插件可以利用动态代理机制一层层的包装目标对象,而实现在目标对象执行目标方法之前进行拦截的效果。<br>MyBatis 允许在已映射语句执行过程中的某一点进行拦截调用。<br>默认情况下,MyBatis 允许使用插件来拦截的方法调用包括:</p>
<ul>
<li>Executor(update, query, flushStatements, commit, rollback, getTransaction, close, isClosed)</li>
<li>ParameterHandler(getParameterObject, setParameters)</li>
<li>ResultSetHandler(handleResultSets, handleOutputParameters)</li>
<li>StatementHandler(prepare, parameterize, batch, update, query)</li>
</ul>
<h3 id="3-1-插件开发的步骤"><a href="#3-1-插件开发的步骤" class="headerlink" title="3.1 插件开发的步骤"></a>3.1 插件开发的步骤</h3><ol>
<li><p>编写插件实现Interceptor接口并使用@Intercepts注解完成插件签名</p>
<pre><code><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">package com.atguigu.in;</span><br><span class="line"></span><br><span class="line">import org.apache.ibatis.executor.statement.StatementHandler;</span><br><span class="line">import org.apache.ibatis.plugin.*;</span><br><span class="line"></span><br><span class="line">import java.util.Properties;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * intercept:拦截目标对象的目标方法的执行</span><br><span class="line"> * 完成插件的签名:</span><br><span class="line"> * 告诉mybatis当前插件来拦截那个执行器的那个方法</span><br><span class="line"> */</span><br><span class="line">@Intercepts({</span><br><span class="line"> @Signature(</span><br><span class="line"> type = StatementHandler.class,method = "parameterize",args=java.sql.Statement.class</span><br><span class="line"> )</span><br><span class="line">})</span><br><span class="line">public class MyFirstIn implements Interceptor {</span><br><span class="line"> @Override</span><br><span class="line"> public Object intercept(Invocation invocation) throws Throwable {</span><br><span class="line"> //放行执行目标方法</span><br><span class="line"> Object proceed = invocation.proceed();</span><br><span class="line"> return proceed;</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"> * @param target</span><br><span class="line"> * @return</span><br><span class="line"> */</span><br><span class="line"> @Override</span><br><span class="line"> public Object plugin(Object target) {</span><br><span class="line"> Object wrap = Plugin.wrap(target, this);</span><br><span class="line"> //我们可以借助Plugin的wrap方法来使用当前的拦截器包装我们的对象</span><br><span class="line"> //返回为当前target创建的动态代理</span><br><span class="line"> return wrap;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> /**</span><br><span class="line"> * 将插件注册时的properties属性注册出来</span><br><span class="line"> * @param properties</span><br><span class="line"> */</span><br><span class="line"> @Override</span><br><span class="line"> public void setProperties(Properties properties) {</span><br><span class="line"> System.out.println("可以拿到插件的信息");</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure></code></pre></li>
<li><p>在全局配置文件中注册插件</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line"><plugins></span><br><span class="line"> <plugin interceptor="com.atguigu.in.MyFirstIn"></span><br><span class="line"> <property name="name" value="ceshi"/></span><br><span class="line"> </plugin></span><br><span class="line"> </plugins></span><br></pre></td></tr></table></figure>
</li>
</ol>
<h2 id="4-mybatis批量操作"><a href="#4-mybatis批量操作" class="headerlink" title="4. mybatis批量操作"></a>4. mybatis批量操作</h2><ul>
<li><p>默认的openSession() 方法没有参数,它会创建有如下特性的</p>
<ul>
<li>会开启一个事务(也就是不自动提交)</li>
<li>连接对象会从由活动环境配置的数据源实例得到</li>
<li>事务隔离级别将会使用驱动或数据源的默认设置</li>
<li>预处理语句不会被复用,也不会批量处理更</li>
</ul>
</li>
<li><p>openSession 方法的ExecutorType类型的参数,枚举类型:</p>
<ul>
<li>ExecutorType.SIMPLE: 这个执行器类型不做特殊的事情(这是默认装配的)。它为每个语句的执行创建一个新的预处理语句</li>
<li>ExecutorType.REUSE: 这个执行器类型会复用预处理语句</li>
<li>ExecutorType.BATCH: 这个执行器会批量执行所有更新语句<br> <img src="https://tva1.sinaimg.cn/large/007S8ZIlly1gfng3gzkm0j30qh05f0uv.jpg" alt=""></li>
</ul>
</li>
<li><p>批量操作我们是使用MyBatis提供的BatchExecutor进行的,他的底层就是通过jdbc攒sql的方式进行的。我们可以让他攒够一定数量后发给数据库一次</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line"> publicvoidtest01() {</span><br><span class="line">SqlSession openSession = build.openSession(ExecutorType.BATCH);</span><br><span class="line">UserDao mapper = openSession.getMapper(UserDao.class);</span><br><span class="line">longstart = System.currentTimeMillis();</span><br><span class="line">for(inti = 0; i < 1000000; i++) {</span><br><span class="line">String name = UUID.randomUUID().toString().substring(0, 5);</span><br><span class="line">mapper.addUser(newUser(null, name, 13));</span><br><span class="line">}</span><br><span class="line">openSession.commit();</span><br><span class="line">openSession.close();</span><br><span class="line">longend = System.currentTimeMillis();</span><br><span class="line">System.out.println("耗时时间:"+(end-start));</span><br><span class="line">}</span><br></pre></td></tr></table></figure></li>
<li><p>与Spring整合中,我们推荐,额外的配置一个可以专门用来执行批量操作的sqlSession<br> <img src="https://tva1.sinaimg.cn/large/007S8ZIlly1gfngak3h1uj30ur02kt9l.jpg" alt=""></p>
</li>
<li><p>需要用到批量操作的时候,我们可以注入配置的这个批量SqlSession。通过他获取到mapper映射器进行操作</p>
</li>
</ul>
<p><strong><em>注意</em></strong></p>
<ol>
<li>批量操作是在session.commit()以后才发送sql语句给数据库进行执行的</li>
<li>如果我们想让其提前执行,以方便后续可能的查询操作获取数据,我们可以使用sqlSession.flushStatements()方法,让其直接冲刷到数据库进行执行</li>
</ol>
<h2 id="5-一个带游标的存储过程"><a href="#5-一个带游标的存储过程" class="headerlink" title="5. 一个带游标的存储过程"></a>5. 一个带游标的存储过程</h2><ul>
<li><p>MyBatis对存储过程的游标提供了一个JdbcType=CURSOR的支持,可以智能的把游标读取到的数据,映射到我们声明的结果集中</p>
</li>
<li><p>存储过程的调用</p>
<ul>
<li><p>select标签中statementType=“CALLABLE”</p>
</li>
<li><p>标签体中调用语法:{call procedure_name(#{param1_info},#{param2_info})}</p>
<p><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1gfngl1c895j30ol066gn3.jpg" alt=""></p>
</li>
</ul>
</li>
</ul>
<h2 id="6-自定义TypeHandler处理枚举"><a href="#6-自定义TypeHandler处理枚举" class="headerlink" title="6.自定义TypeHandler处理枚举"></a>6.自定义TypeHandler处理枚举</h2><ul>
<li>我们可以通过自定义TypeHandler的形式来在设置参数或者取出结果集的时候自定义参数封装策略</li>
<li>步骤<ul>
<li>实现TypeHandler接口或者继承BaseTypeHandler</li>
<li>使用@MappedTypes定义处理的java类型</li>
<li>使用@MappedJdbcTypes定义jdbcType类型</li>
<li>在自定义结果集标签或者参数处理的时候声明使用自定义TypeHandler进行处理,或者在全局配置TypeHandler要处理的javaType</li>
</ul>
</li>
</ul>
]]></content>
<categories>
<category>mybatis</category>
</categories>
<tags>
<tag>mybatis</tag>
</tags>
</entry>
<entry>
<title>MySQL学习笔记二</title>
<url>/posts/671486fb.html</url>
<content><![CDATA[<h1 id="MySQL学习笔记二"><a href="#MySQL学习笔记二" class="headerlink" title="MySQL学习笔记二"></a>MySQL学习笔记二</h1><h2 id="1-分组查询"><a href="#1-分组查询" class="headerlink" title="1.分组查询"></a>1.分组查询</h2><h3 id="1-1-添加分组签筛选"><a href="#1-1-添加分组签筛选" class="headerlink" title="1.1 添加分组签筛选"></a>1.1 添加分组签筛选</h3><p>作为条件的字段都来自于原始表单,可以将筛选条件放在where子句的后边</p>
<h3 id="1-2-添加分组后筛选"><a href="#1-2-添加分组后筛选" class="headerlink" title="1.2 添加分组后筛选"></a>1.2 添加分组后筛选</h3><p>案例:查询那个部门的员工个数>2<br>作为条件的字段不是来自于原始的表单,需要使用<code>having</code>的关键字来筛选,需要放在<code>group by</code>关键字后边</p>
<h3 id="1-3-函数分组"><a href="#1-3-函数分组" class="headerlink" title="1.3 函数分组"></a>1.3 函数分组</h3><p><code>group by</code>子句后边还支持按照表达式或者函数进行分组<br>案例:按照员工姓名长度分组,查询每一组员工的个数,筛选员工个数>5的有哪些</p>
<h3 id="1-4-按多个字段分组"><a href="#1-4-按多个字段分组" class="headerlink" title="1.4 按多个字段分组"></a>1.4 按多个字段分组</h3><p>案例:每个部门,每个工种的员工的平均工资</p>
<h3 id="1-5-添加排序"><a href="#1-5-添加排序" class="headerlink" title="1.5 添加排序"></a>1.5 添加排序</h3><p>分组查询也可以支持排序</p>
<h2 id="2-连接查询"><a href="#2-连接查询" class="headerlink" title="2. 连接查询"></a>2. 连接查询</h2><p>查询的字段来自于多个表单,需要用到连接查询</p>
<h3 id="2-1-笛卡尔乘积"><a href="#2-1-笛卡尔乘积" class="headerlink" title="2.1 笛卡尔乘积"></a>2.1 笛卡尔乘积</h3><p>现象:表1有m行,表2有n行,结果=m*n行<br>避免:添加有效的连接条件</p>
<h3 id="2-2-连接查询分类"><a href="#2-2-连接查询分类" class="headerlink" title="2.2 连接查询分类"></a>2.2 连接查询分类</h3><p>按照年代分类:sql92标准、sql99标准<br>按照功能分类:内连接、外连接、交叉连接</p>
<h3 id="2-3-等值连接介绍"><a href="#2-3-等值连接介绍" class="headerlink" title="2.3 等值连接介绍"></a>2.3 等值连接介绍</h3><p><strong><em>如果给表起了别名,就不能用原来的表名称去限定字段的</em></strong><br>特点:</p>
<ol>
<li>多表等值连接的结果为多表的交集部分</li>
<li>n表连接,至少需要n-1个连接条件</li>
<li>多表连接的顺序没有要求</li>
<li>一般需要为表起别名</li>
<li>可以搭配前面介绍的所有的字句使用<br>案例:查询女神和对应男神的名称</li>
</ol>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">select beauty.`name`,boys.boyNamefrom boys,beauty where beauty.boyfriend_id = boys.id</span><br></pre></td></tr></table></figure>
<p><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1gh8ysht42tj30v00a2q5q.jpg" alt=""></p>
<h3 id="2-4-非等值连接"><a href="#2-4-非等值连接" class="headerlink" title="2.4 非等值连接"></a>2.4 非等值连接</h3><p>案例:查询员工工资和工资级别</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT salary,grade_level</span><br><span class="line">FROM employees e,job_grades g</span><br><span class="line">WHERE salary BETWEEN g.`lowest_sal` AND g.`highest_sal`</span><br><span class="line">AND g.`grade_level`='A';</span><br></pre></td></tr></table></figure>
<h3 id="2-5-自连接"><a href="#2-5-自连接" class="headerlink" title="2.5 自连接"></a>2.5 自连接</h3><p>自己连接自己<br>案例:查询员工名称和上级员工名称</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT e.employee_id,e.last_name,m.employee_id,m.last_name</span><br><span class="line">FROM employees e,employees m</span><br><span class="line">WHERE e.`manager_id`=m.`employee_id`;</span><br></pre></td></tr></table></figure>
<h2 id="3-sql99语法"><a href="#3-sql99语法" class="headerlink" title="3. sql99语法"></a>3. sql99语法</h2><h3 id="3-1-语法介绍"><a href="#3-1-语法介绍" class="headerlink" title="3.1 语法介绍"></a>3.1 语法介绍</h3><p>1999年新推出的sql标准<br>语法</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">select 查询列表</span><br><span class="line">from 表1 【连接类型】</span><br><span class="line">join 表2 别名 on 连接条件</span><br><span class="line">【where 筛选条件】</span><br><span class="line">【group by 分组】</span><br><span class="line">【having 筛选条件】</span><br><span class="line">【order by 排序列表】</span><br></pre></td></tr></table></figure>
<h3 id="3-2-等值连接"><a href="#3-2-等值连接" class="headerlink" title="3.2 等值连接"></a>3.2 等值连接</h3><p><strong><em>表的位置是可以进行互换的</em></strong><br>语法:</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">select 查询列表</span><br><span class="line">from 表1 别名</span><br><span class="line">inner join 表2 别名 on 连接条件</span><br></pre></td></tr></table></figure>
<p>案例1:查询员工名称和部门名称</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">select last_name,department_name</span><br><span class="line">from employees e</span><br><span class="line">inner join departments d</span><br><span class="line">on e.department_id = d.department_id</span><br></pre></td></tr></table></figure>
<p><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1gh90ilmfsvj312s0iedlk.jpg" alt=""></p>
<p>案例2:查询员工名、部门名、工种名,并按部门名降序(三表连接)</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">-- 连接的表需要和前边有联系(有一定的联系)</span><br><span class="line">SELECT last_name,department_name,job_title</span><br><span class="line">FROM employees e</span><br><span class="line">INNER JOIN departments d ON e.`department_id`=d.`department_id`</span><br><span class="line">INNER JOIN jobs j ON e.`job_id` = j.`job_id`</span><br><span class="line">ORDER BY department_name DESC;</span><br></pre></td></tr></table></figure>
<h3 id="3-3-非等值连接"><a href="#3-3-非等值连接" class="headerlink" title="3.3 非等值连接"></a>3.3 非等值连接</h3><p>案例1:查询员工的工资级别</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT salary,grade_level</span><br><span class="line">FROM employees e</span><br><span class="line"> JOIN job_grades g</span><br><span class="line"> ON e.`salary` BETWEEN g.`lowest_sal` AND g.`highest_sal`;</span><br></pre></td></tr></table></figure>
<p>案例2:查询工资级别的个数>20的个数,并且按工资级别降序</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT COUNT(*),grade_level</span><br><span class="line">FROM employees e</span><br><span class="line"> JOIN job_grades g</span><br><span class="line"> ON e.`salary` BETWEEN g.`lowest_sal` AND g.`highest_sal`</span><br><span class="line"> GROUP BY grade_level</span><br><span class="line"> HAVING COUNT(*)>20</span><br></pre></td></tr></table></figure>
<h3 id="3-4-自连接"><a href="#3-4-自连接" class="headerlink" title="3.4 自连接"></a>3.4 自连接</h3><p>案例1:查询员工的名字、上级的名字</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT e.last_name,m.last_name</span><br><span class="line"> FROM employees e</span><br><span class="line"> JOIN employees m</span><br><span class="line"> ON e.`manager_id`= m.`employee_id`;</span><br></pre></td></tr></table></figure>
<p>案例2:查询姓名中包含字符k的员工的名字、上级的名字</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT e.last_name,m.last_name</span><br><span class="line">FROM employees e</span><br><span class="line">JOIN employees m</span><br><span class="line">ON e.`manager_id`= m.`employee_id`</span><br><span class="line">WHERE e.`last_name` LIKE '%k%';</span><br></pre></td></tr></table></figure>
<h3 id="3-5-左-右-外连接"><a href="#3-5-左-右-外连接" class="headerlink" title="3.5 左(右)外连接"></a>3.5 左(右)外连接</h3><p>应用场景:用于查询一个表中有,另一个表没有的记录<br>特点:</p>
<ol>
<li>外连接的查询结果为主表中的所有记录<ul>
<li>如果从表中有和它匹配的,则显示匹配的值</li>
<li>如果从表中没有和它匹配的,则显示null</li>
<li>外连接查询结果=内连接结果+主表中有而从表没有的记录</li>
</ul>
</li>
<li>左外连接,left join左边的是主表<ul>
<li>右外连接,right join右边的是主表</li>
</ul>
</li>
<li>左外和右外交换两个表的顺序,可以实现同样的效果</li>
<li>全外连接=内连接的结果+表1中有但表2没有的+表2中有但表1没有的</li>
</ol>
<p>案例1:查询哪个部门没有员工<br>左外:</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT d.*,e.employee_id</span><br><span class="line"> FROM departments d</span><br><span class="line"> LEFT OUTER JOIN employees e</span><br><span class="line"> ON d.`department_id` = e.`department_id`</span><br><span class="line"> WHERE e.`employee_id` IS NULL;</span><br></pre></td></tr></table></figure>
<p><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1gh912codxhj312q0jk7cp.jpg" alt=""></p>
<p>右外:</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT d.*,e.employee_id</span><br><span class="line"> FROM employees e</span><br><span class="line"> RIGHT OUTER JOIN departments d</span><br><span class="line"> ON d.`department_id` = e.`department_id`</span><br><span class="line"> WHERE e.`employee_id` IS NULL;</span><br></pre></td></tr></table></figure>
<p><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1gh916ckf3tj312m0j6tgz.jpg" alt=""></p>
<h3 id="3-6-全外连接"><a href="#3-6-全外连接" class="headerlink" title="3.6 全外连接"></a>3.6 全外连接</h3><p><strong><em>==mysql数据库不支持全外连接查询==</em></strong></p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">USE girls;</span><br><span class="line"> SELECT b.*,bo.*</span><br><span class="line"> FROM beauty b</span><br><span class="line"> FULL OUTER JOIN boys bo</span><br><span class="line"> ON b.`boyfriend_id` = bo.id;</span><br></pre></td></tr></table></figure>
<h3 id="3-7-交叉连接"><a href="#3-7-交叉连接" class="headerlink" title="3.7 交叉连接"></a>3.7 交叉连接</h3><p>代表笛卡尔乘积</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT b.*,bo.*</span><br><span class="line"> FROM beauty b</span><br><span class="line"> CROSS JOIN boys bo;</span><br></pre></td></tr></table></figure>
<h2 id="4-子查询介绍"><a href="#4-子查询介绍" class="headerlink" title="4. 子查询介绍"></a>4. 子查询介绍</h2><p>含义:出现在其他语句中的select语句,称为子查询或内查询<br> 外部的查询语句,称为主查询或外查询<br>分类:</p>
<ol>
<li><p>按子查询出现的位置:</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">select后面:</span><br><span class="line"> 仅仅支持标量子查询</span><br><span class="line"> </span><br><span class="line"> from后面:</span><br><span class="line"> 支持表子查询</span><br><span class="line"> where或having后面:★</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"> exists后面(相关子查询)</span><br><span class="line"> 表子查询</span><br></pre></td></tr></table></figure></li>
<li><p>按结果集的行列数不同:</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><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="4-1-where后面标量子查询"><a href="#4-1-where后面标量子查询" class="headerlink" title="4.1 where后面标量子查询"></a>4.1 where后面标量子查询</h3><p>位置是在<code>where</code>或者<code>having</code>的后面<br>1、标量子查询(单行子查询)<br>2、列子查询(多行子查询)<br>3、行子查询(多列多行)<br>特点:<br>①子查询放在小括号内<br>②子查询一般放在条件的右侧<br>③标量子查询,一般搭配着单行操作符使用:> < >= <= = <><br>列子查询,一般搭配着多行操作符使用:in、any/some、all<br>④子查询的执行优先于主查询执行,主查询的条件用到了子查询的结果</p>
</li>
</ol>
<p>案例1:查询员工的信息,满足 salary>①结果(标量子查询)</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT *</span><br><span class="line">FROM employees</span><br><span class="line">WHERE salary>(</span><br><span class="line"></span><br><span class="line"> SELECT salary</span><br><span class="line"> FROM employees</span><br><span class="line"> WHERE last_name = 'Abel'</span><br><span class="line"></span><br><span class="line">);</span><br></pre></td></tr></table></figure>
<p>案例2:返回job_id与141号员工相同,salary比143号员工多的员工 姓名,job_id 和工资</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT last_name,job_id,salary</span><br><span class="line">FROM employees</span><br><span class="line">WHERE job_id = (</span><br><span class="line"> SELECT job_id</span><br><span class="line"> FROM employees</span><br><span class="line"> WHERE employee_id = 141</span><br><span class="line">) AND salary>(</span><br><span class="line"> SELECT salary</span><br><span class="line"> FROM employees</span><br><span class="line"> WHERE employee_id = 143</span><br><span class="line"></span><br><span class="line">);</span><br></pre></td></tr></table></figure>
<p>案例3:查询最低工资大于50号部门最低工资的部门id和其最低工资</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT MIN(salary),department_id</span><br><span class="line">FROM employees</span><br><span class="line">GROUP BY department_id</span><br><span class="line">HAVING MIN(salary)>(</span><br><span class="line"> SELECT MIN(salary)</span><br><span class="line"> FROM employees</span><br><span class="line"> WHERE department_id = 50</span><br></pre></td></tr></table></figure>
<h3 id="4-2-where后面的列子查询"><a href="#4-2-where后面的列子查询" class="headerlink" title="4.2 where后面的列子查询"></a>4.2 where后面的列子查询</h3><p>案例1:返回location_id是1400或1700的部门中的所有员工姓名</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT last_name</span><br><span class="line">FROM employees</span><br><span class="line">WHERE department_id <>ALL(</span><br><span class="line"> SELECT DISTINCT department_id</span><br><span class="line"> FROM departments</span><br><span class="line"> WHERE location_id IN(1400,1700)</span><br><span class="line">);</span><br></pre></td></tr></table></figure>
<p>案例2:返回其它工种中比job_id为‘IT_PROG’工种任一工资低的员工的员工号、姓名、job_id 以及salary</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT last_name,employee_id,job_id,salary</span><br><span class="line">FROM employees</span><br><span class="line">WHERE salary<ANY(</span><br><span class="line"> SELECT DISTINCT salary</span><br><span class="line"> FROM employees</span><br><span class="line"> WHERE job_id = 'IT_PROG'</span><br><span class="line"></span><br><span class="line">) AND job_id<>'IT_PROG';</span><br><span class="line"></span><br><span class="line">#或</span><br><span class="line">SELECT last_name,employee_id,job_id,salary</span><br><span class="line">FROM employees</span><br><span class="line">WHERE salary<(</span><br><span class="line"> SELECT MAX(salary)</span><br><span class="line"> FROM employees</span><br><span class="line"> WHERE job_id = 'IT_PROG'</span><br><span class="line"></span><br><span class="line">) AND job_id<>'IT_PROG';</span><br></pre></td></tr></table></figure>
<p>案例3:返回其它部门中比job_id为‘IT_PROG’部门所有工资都低的员工 的员工号、姓名、job_id 以及salary</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT last_name,employee_id,job_id,salary</span><br><span class="line">FROM employees</span><br><span class="line">WHERE salary<ALL(</span><br><span class="line"> SELECT DISTINCT salary</span><br><span class="line"> FROM employees</span><br><span class="line"> WHERE job_id = 'IT_PROG'</span><br><span class="line"></span><br><span class="line">) AND job_id<>'IT_PROG';</span><br><span class="line"></span><br><span class="line">#或</span><br><span class="line"></span><br><span class="line">SELECT last_name,employee_id,job_id,salary</span><br><span class="line">FROM employees</span><br><span class="line">WHERE salary<(</span><br><span class="line"> SELECT MIN( salary)</span><br><span class="line"> FROM employees</span><br><span class="line"> WHERE job_id = 'IT_PROG'</span><br><span class="line"></span><br><span class="line">) AND job_id<>'IT_PROG';</span><br></pre></td></tr></table></figure>
<h3 id="4-3-where后面的行子查询"><a href="#4-3-where后面的行子查询" class="headerlink" title="4.3 where后面的行子查询"></a>4.3 where后面的行子查询</h3><p>案例:查询员工编号最小并且工资最高的员工信息</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT *</span><br><span class="line">FROM employees</span><br><span class="line">WHERE employee_id=(</span><br><span class="line"> SELECT MIN(employee_id)</span><br><span class="line"> FROM employees</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">)AND salary=(</span><br><span class="line"> SELECT MAX(salary)</span><br><span class="line"> FROM employees</span><br><span class="line"></span><br><span class="line">);</span><br></pre></td></tr></table></figure>
<h3 id="4-4-select后面的子查询"><a href="#4-4-select后面的子查询" class="headerlink" title="4.4 select后面的子查询"></a>4.4 select后面的子查询</h3><p><strong><em>==仅仅支持标量子查询==</em></strong><br>案例:查询每个部门的员工个数</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT d.*,(</span><br><span class="line"></span><br><span class="line"> SELECT COUNT(*)</span><br><span class="line"> FROM employees e</span><br><span class="line"> WHERE e.department_id = d.`department_id`</span><br><span class="line"> ) 个数</span><br><span class="line"> FROM departments d;</span><br></pre></td></tr></table></figure>
<p>案例2:查询员工号=102的部门名</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT (</span><br><span class="line"> SELECT department_name,e.department_id</span><br><span class="line"> FROM departments d</span><br><span class="line"> INNER JOIN employees e</span><br><span class="line"> ON d.department_id=e.department_id</span><br><span class="line"> WHERE e.employee_id=102</span><br><span class="line"> </span><br><span class="line">) 部门名;</span><br></pre></td></tr></table></figure>
<h3 id="4-5-from后面的子查询"><a href="#4-5-from后面的子查询" class="headerlink" title="4.5 from后面的子查询"></a>4.5 from后面的子查询</h3><p><strong><em>==将子查询结果充当一张表,要求必须起别名==</em></strong><br>案例:查询每个部门的平均工资的工资等级</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT ag_dep.*,g.`grade_level`</span><br><span class="line">FROM (</span><br><span class="line"> SELECT AVG(salary) ag,department_id</span><br><span class="line"> FROM employees</span><br><span class="line"> GROUP BY department_id</span><br><span class="line">) ag_dep</span><br><span class="line">INNER JOIN job_grades g</span><br><span class="line">ON ag_dep.ag BETWEEN lowest_sal AND highest_sal;</span><br></pre></td></tr></table></figure>
<h3 id="4-6-exists后面的子查询"><a href="#4-6-exists后面的子查询" class="headerlink" title="4.6 exists后面的子查询"></a>4.6 exists后面的子查询</h3><p>语法:</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">exists(完整的查询语句)</span><br><span class="line">结果:1或0</span><br></pre></td></tr></table></figure>
<p>案例1:查询有员工的部门名</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">#in</span><br><span class="line">SELECT department_name</span><br><span class="line">FROM departments d</span><br><span class="line">WHERE d.`department_id` IN(</span><br><span class="line"> SELECT department_id</span><br><span class="line"> FROM employees</span><br><span class="line"></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line">#exists</span><br><span class="line"></span><br><span class="line">SELECT department_name</span><br><span class="line">FROM departments d</span><br><span class="line">WHERE EXISTS(</span><br><span class="line"> SELECT *</span><br><span class="line"> FROM employees e</span><br><span class="line"> WHERE d.`department_id`=e.`department_id`</span><br><span class="line">);</span><br></pre></td></tr></table></figure>
<p>案例2:查询没有女朋友的男神信息</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">#in</span><br><span class="line"></span><br><span class="line">SELECT bo.*</span><br><span class="line">FROM boys bo</span><br><span class="line">WHERE bo.id NOT IN(</span><br><span class="line"> SELECT boyfriend_id</span><br><span class="line"> FROM beauty</span><br><span class="line">)</span><br><span class="line"></span><br><span class="line">#exists</span><br><span class="line">SELECT bo.*</span><br><span class="line">FROM boys bo</span><br><span class="line">WHERE NOT EXISTS(</span><br><span class="line"> SELECT boyfriend_id</span><br><span class="line"> FROM beauty b</span><br><span class="line"> WHERE bo.`id`=b.`boyfriend_id`</span><br><span class="line"></span><br><span class="line">);</span><br></pre></td></tr></table></figure>
<h2 id="5-分页查询"><a href="#5-分页查询" class="headerlink" title="5. 分页查询"></a>5. 分页查询</h2><p>应用场景:当要显示的数据,一页显示不全,需要分页提交sql请求<br>语法:</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">select 查询列表</span><br><span class="line"> from 表</span><br><span class="line"> 【join type join 表2</span><br><span class="line"> on 连接条件</span><br><span class="line"> where 筛选条件</span><br><span class="line"> group by 分组字段</span><br><span class="line"> having 分组后的筛选</span><br><span class="line"> order by 排序的字段】</span><br><span class="line"> limit 【offset,】size;</span><br><span class="line"> </span><br><span class="line"> offset要显示条目的起始索引(起始索引从0开始)</span><br><span class="line"> size 要显示的条目个数</span><br></pre></td></tr></table></figure>
<p>特点:<br>①limit语句放在查询语句的最后<br> ②公式<br> 要显示的页数 page,每页的条目数size</p>
<pre><code>select 查询列表
from 表
limit (page-1)*size,size;</code></pre><h2 id="6-联合查询介绍"><a href="#6-联合查询介绍" class="headerlink" title="6.联合查询介绍"></a>6.联合查询介绍</h2><p>应用场景:要查询的结果来自多个表,且多个表没有直接的关系,但查询信息节本一样<br>union 联合 合并:将多条查询语句的结果合并成一个结果<br>案例:查询中国用户中男性的信息以及外国用户中年男性的用户信息</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">-- 去掉重复的</span><br><span class="line">SELECT * FROM employees WHERE email LIKE '%a%'</span><br><span class="line">UNION</span><br><span class="line">SELECT * FROM employees WHERE department_id>90;</span><br><span class="line"></span><br><span class="line">-- 包含重复项</span><br><span class="line">SELECT id,cname FROM t_ca WHERE csex='男'</span><br><span class="line">UNION ALL</span><br><span class="line">SELECT t_id,tname FROM t_ua WHERE tGender='male';</span><br></pre></td></tr></table></figure>
<h2 id="7-联合查询特点"><a href="#7-联合查询特点" class="headerlink" title="7.联合查询特点"></a>7.联合查询特点</h2><p>特点:★<br>1、要求多条查询语句的查询列数是一致的!<br>2、要求多条查询语句的查询的每一列的类型和顺序最好一致<br>3、union关键字默认去重,如果使用union all 可以包含重复项</p>
<h2 id="8-插入语句"><a href="#8-插入语句" class="headerlink" title="8. 插入语句"></a>8. 插入语句</h2><h3 id="8-1-插入语句方式一"><a href="#8-1-插入语句方式一" class="headerlink" title="8.1 插入语句方式一"></a>8.1 插入语句方式一</h3><h3 id="8-2-插入语句方式二"><a href="#8-2-插入语句方式二" class="headerlink" title="8.2 插入语句方式二"></a>8.2 插入语句方式二</h3><h2 id="9-修改表单记录"><a href="#9-修改表单记录" class="headerlink" title="9.修改表单记录"></a>9.修改表单记录</h2><h3 id="9-1-修改单表记录"><a href="#9-1-修改单表记录" class="headerlink" title="9.1 修改单表记录"></a>9.1 修改单表记录</h3><h3 id="9-2-修改多表记录"><a href="#9-2-修改多表记录" class="headerlink" title="9.2 修改多表记录"></a>9.2 修改多表记录</h3><p>语法:</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">sql92语法:</span><br><span class="line">update 表1 别名,表2 别名</span><br><span class="line">set 列 = 值</span><br><span class="line">where 连接条件 and 筛选条件</span><br><span class="line"></span><br><span class="line">sql99语法:</span><br><span class="line">update 表1 别名</span><br><span class="line">inner|left|right join 表2 别名</span><br><span class="line">on 连接条件</span><br><span class="line">set 列 = 值</span><br><span class="line">where 筛选条件</span><br></pre></td></tr></table></figure>
<h2 id="10-删除语句介绍"><a href="#10-删除语句介绍" class="headerlink" title="10. 删除语句介绍"></a>10. 删除语句介绍</h2><h3 id="10-1-删除方式一"><a href="#10-1-删除方式一" class="headerlink" title="10.1 删除方式一"></a>10.1 删除方式一</h3><p>删除表单的整行记录,自增长列的值从断点开始<br>语法:</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">delete from 表名 where 筛选条件</span><br></pre></td></tr></table></figure>
<h3 id="10-2-删除方式二"><a href="#10-2-删除方式二" class="headerlink" title="10.2 删除方式二"></a>10.2 删除方式二</h3><p>删除了整个表单的记录,不能添加where条件,自增长的值从1开始<br>语法:</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">truncate table 表名</span><br></pre></td></tr></table></figure>
<h2 id="11-DDL语言介绍"><a href="#11-DDL语言介绍" class="headerlink" title="11. DDL语言介绍"></a>11. DDL语言介绍</h2><p>创建: create<br>修改: alter<br>删除: drop</p>
<h3 id="11-1-库的管理"><a href="#11-1-库的管理" class="headerlink" title="11.1 库的管理"></a>11.1 库的管理</h3><p>案例:创建库Books</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">CREATE DATABASE IF NOT EXISTS books ;</span><br></pre></td></tr></table></figure>
<p>案例2:库的修改</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">RENAME DATABASE books TO 新库名;</span><br></pre></td></tr></table></figure>
<p>案例3:更改库的字符集</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">ALTER DATABASE books CHARACTER SET gbk;</span><br></pre></td></tr></table></figure>
<p>案例4:库的删除</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">DROP DATABASE IF EXISTS books;</span><br></pre></td></tr></table></figure>
<h3 id="11-2-表的创建"><a href="#11-2-表的创建" class="headerlink" title="11.2 表的创建"></a>11.2 表的创建</h3><p>语法:</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">create table 表名(</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></pre></td></tr></table></figure>
<p>案例:创建表Book</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">CREATE TABLE book(</span><br><span class="line"> id INT,#编号</span><br><span class="line"> bName VARCHAR(20),#图书名</span><br><span class="line"> price DOUBLE,#价格</span><br><span class="line"> authorId INT,#作者编号</span><br><span class="line"> publishDate DATETIME#出版日期</span><br><span class="line">);</span><br></pre></td></tr></table></figure>
<p>案例:创建表author</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">CREATE TABLE IF NOT EXISTS author(</span><br><span class="line"> id INT,</span><br><span class="line"> au_name VARCHAR(20),</span><br><span class="line"> nation VARCHAR(10)</span><br><span class="line">)</span><br><span class="line">DESC author;</span><br></pre></td></tr></table></figure>
<h3 id="11-3-表的修改"><a href="#11-3-表的修改" class="headerlink" title="11.3 表的修改"></a>11.3 表的修改</h3><p>语法:</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">alter table 表名 add|drop|modify|change column 列名 【列类型 约束】;</span><br></pre></td></tr></table></figure>
<p>案例</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">#①修改列名</span><br><span class="line">ALTER TABLE book CHANGE COLUMN publishdate pubDate DATETIME;</span><br><span class="line">#②修改列的类型或约束</span><br><span class="line">ALTER TABLE book MODIFY COLUMN pubdate TIMESTAMP;</span><br><span class="line">#③添加新列</span><br><span class="line">ALTER TABLE author ADD COLUMN annual DOUBLE; </span><br><span class="line">#④删除列</span><br><span class="line">ALTER TABLE book_author DROP COLUMN annual;</span><br><span class="line">#⑤修改表名</span><br><span class="line">ALTER TABLE author RENAME TO book_author;</span><br><span class="line">DESC book;</span><br></pre></td></tr></table></figure>
<h3 id="11-4-表的删除"><a href="#11-4-表的删除" class="headerlink" title="11.4 表的删除"></a>11.4 表的删除</h3><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">DROP TABLE IF EXISTS book_author;</span><br><span class="line">SHOW TABLES;</span><br></pre></td></tr></table></figure>
<h3 id="11-5-表的复制"><a href="#11-5-表的复制" class="headerlink" title="11.5 表的复制"></a>11.5 表的复制</h3><p>案例:仅仅复制表的结构</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">CREATE TABLE copy LIKE author;</span><br></pre></td></tr></table></figure>
<p>案例:复制表的结构+数据</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">CREATE TABLE copy2 </span><br><span class="line">SELECT * FROM author;</span><br></pre></td></tr></table></figure>
<p>案例:只复制部分数据</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">CREATE TABLE copy3</span><br><span class="line">SELECT id,au_name</span><br><span class="line">FROM author </span><br><span class="line">WHERE nation='中国';</span><br></pre></td></tr></table></figure>
<p>案例:仅仅复制某些字段</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">CREATE TABLE copy4 </span><br><span class="line">SELECT id,au_name</span><br><span class="line">FROM author</span><br><span class="line">WHERE 0;</span><br></pre></td></tr></table></figure>
<h2 id="12-数据类型介绍"><a href="#12-数据类型介绍" class="headerlink" title="12. 数据类型介绍"></a>12. 数据类型介绍</h2><p>常见的数据类型<br>数值型:<br> 整型<br> 小数:<br> 定点数<br> 浮点数<br>字符型:<br> 较短的文本:char、varchar<br> 较长的文本:text、blob(较长的二进制数据)</p>
<p>日期型:</p>
<h3 id="12-1-整型"><a href="#12-1-整型" class="headerlink" title="12.1 整型"></a>12.1 整型</h3><p>分类:<br>tinyint、smallint、mediumint、int/integer、bigint<br> 1 2 3 4 8</p>
<p>特点:<br>① 如果不设置无符号还是有符号,默认是有符号,如果想设置无符号,需要添加unsigned关键字<br>② 如果插入的数值超出了整型的范围,会报out of range异常,并且插入临界值<br>③ 如果不设置长度,会有默认的长度<br>长度代表了显示的最大宽度,如果不够会用0在左边填充,但必须搭配zerofill使用!</p>
<h3 id="12-2-浮点型"><a href="#12-2-浮点型" class="headerlink" title="12.2 浮点型"></a>12.2 浮点型</h3><p>分类:<br>1.浮点型<br>float(M,D)<br>double(M,D)<br>2.定点型<br>dec(M,D)<br>decimal(M,D)<br>特点:<br>①M:整数部位+小数部位 D:小数部位 如果超过范围,则插入临界值<br>②M和D都可以省略,如果是decimal,则M默认为10,D默认为0,如果是float和double,则会根据插入的数值的精度来决定精度<br>③定点型的精确度较高,如果要求插入数值的精度较高如货币运算等则考虑使用</p>
<h3 id="12-3-字符型"><a href="#12-3-字符型" class="headerlink" title="12.3 字符型"></a>12.3 字符型</h3><p>较短的文本:char、varchar<br>其他:<br>binary和varbinary用于保存较短的二进制<br>enum用于保存枚举<br>set用于保存集合<br>较长的文本:text、blob(较大的二进制)</p>
<h3 id="12-4-日期型"><a href="#12-4-日期型" class="headerlink" title="12.4 日期型"></a>12.4 日期型</h3><p>分类:<br>date只保存日期<br>time 只保存时间<br>year只保存年<br>datetime保存日期+时间<br>timestamp保存日期+时间</p>
<h2 id="13-常见约束介绍"><a href="#13-常见约束介绍" class="headerlink" title="13. 常见约束介绍"></a>13. 常见约束介绍</h2><p>not null:非空约束,用于保证该字段的值不能为空<br>DEFAULT:默认约束,用于保证该字段有默认值<br>PRIMARY KEY:主键约束,保证该字段的值具有唯一性,并且非空<br>UNIQUE:唯一约束,用于保证该字段的值具有唯一性,可以为空<br>CHECK:检查约束【mysql中不支持】<br>FOREIGN KEY:外键,用于限制两个表的关系,用于保证该字段的值必须来自于主表的关联列的值在从表添加外键约束,用于引用主表中某列的值</p>
<h3 id="13-1-创建表时添加列级约束"><a href="#13-1-创建表时添加列级约束" class="headerlink" title="13.1 创建表时添加列级约束"></a>13.1 创建表时添加列级约束</h3><p>直接在字段名和类型后面追加 约束类型即可。<br>只支持:默认、非空、主键、唯一</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">USE students;</span><br><span class="line">DROP TABLE stuinfo;</span><br><span class="line">CREATE TABLE stuinfo(</span><br><span class="line"> id INT PRIMARY KEY,#主键</span><br><span class="line"> stuName VARCHAR(20) NOT NULL UNIQUE,#非空</span><br><span class="line"> gender CHAR(1) CHECK(gender='男' OR gender ='女'),#检查</span><br><span class="line"> seat INT UNIQUE,#唯一</span><br><span class="line"> age INT DEFAULT 18,#默认约束</span><br><span class="line"> majorId INT REFERENCES major(id)#外键</span><br><span class="line">);</span><br></pre></td></tr></table></figure>
<h3 id="13-2-创建表时添加表级约束"><a href="#13-2-创建表时添加表级约束" class="headerlink" title="13.2 创建表时添加表级约束"></a>13.2 创建表时添加表级约束</h3><p>语法:在各个字段的最下面<br> 【constraint 约束名】 约束类型(字段名) </p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">DROP TABLE IF EXISTS stuinfo;</span><br><span class="line">CREATE TABLE stuinfo(</span><br><span class="line"> id INT,</span><br><span class="line"> stuname VARCHAR(20),</span><br><span class="line"> gender CHAR(1),</span><br><span class="line"> seat INT,</span><br><span class="line"> age INT,</span><br><span class="line"> majorid INT,</span><br><span class="line"> </span><br><span class="line"> CONSTRAINT pk PRIMARY KEY(id),#主键</span><br><span class="line"> CONSTRAINT uq UNIQUE(seat),#唯一键</span><br><span class="line"> CONSTRAINT ck CHECK(gender ='男' OR gender = '女'),#检查</span><br><span class="line"> CONSTRAINT fk_stuinfo_major FOREIGN KEY(majorid) REFERENCES major(id)#外键</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">CREATE TABLE IF NOT EXISTS stuinfo(</span><br><span class="line"> id INT PRIMARY KEY,</span><br><span class="line"> stuname VARCHAR(20),</span><br><span class="line"> sex CHAR(1),</span><br><span class="line"> age INT DEFAULT 18,</span><br><span class="line"> seat INT UNIQUE,</span><br><span class="line"> majorid INT,</span><br><span class="line"> CONSTRAINT fk_stuinfo_major FOREIGN KEY(majorid) REFERENCES major(id)</span><br><span class="line"></span><br><span class="line">);</span><br></pre></td></tr></table></figure>
<h3 id="13-3-主键和唯一的区别"><a href="#13-3-主键和唯一的区别" class="headerlink" title="13.3 主键和唯一的区别"></a>13.3 主键和唯一的区别</h3><h3 id="13-4-外键的特点"><a href="#13-4-外键的特点" class="headerlink" title="13.4 外键的特点"></a>13.4 外键的特点</h3><h3 id="13-5-修改表时添加约束"><a href="#13-5-修改表时添加约束" class="headerlink" title="13.5 修改表时添加约束"></a>13.5 修改表时添加约束</h3><p>1、添加列级约束<br>alter table 表名 modify column 字段名 字段类型 新约束;<br>2、添加表级约束<br>alter table 表名 add 【constraint 约束名】 约束类型(字段名) 【外键的引用】;</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">#1.添加非空约束</span><br><span class="line">ALTER TABLE stuinfo MODIFY COLUMN stuname VARCHAR(20) NOT NULL;</span><br><span class="line">#2.添加默认约束</span><br><span class="line">ALTER TABLE stuinfo MODIFY COLUMN age INT DEFAULT 18;</span><br><span class="line">#3.添加主键</span><br><span class="line">#①列级约束</span><br><span class="line">ALTER TABLE stuinfo MODIFY COLUMN id INT PRIMARY KEY;</span><br><span class="line">#②表级约束</span><br><span class="line">ALTER TABLE stuinfo ADD PRIMARY KEY(id);</span><br><span class="line">#4.添加唯一</span><br><span class="line">#①列级约束</span><br><span class="line">ALTER TABLE stuinfo MODIFY COLUMN seat INT UNIQUE;</span><br><span class="line">#②表级约束</span><br><span class="line">ALTER TABLE stuinfo ADD UNIQUE(seat);</span><br><span class="line">#5.添加外键</span><br><span class="line">ALTER TABLE stuinfo ADD CONSTRAINT fk_stuinfo_major FOREIGN KEY(majorid) REFERENCES major(id);</span><br></pre></td></tr></table></figure>
<h3 id="13-6-修改表时去掉约束"><a href="#13-6-修改表时去掉约束" class="headerlink" title="13.6 修改表时去掉约束"></a>13.6 修改表时去掉约束</h3><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">#1.删除非空约束</span><br><span class="line">ALTER TABLE stuinfo MODIFY COLUMN stuname VARCHAR(20) NULL;</span><br><span class="line">#2.删除默认约束</span><br><span class="line">ALTER TABLE stuinfo MODIFY COLUMN age INT ;</span><br><span class="line">#3.删除主键</span><br><span class="line">ALTER TABLE stuinfo DROP PRIMARY KEY;</span><br><span class="line">#4.删除唯一</span><br><span class="line">ALTER TABLE stuinfo DROP INDEX seat;</span><br><span class="line">#5.删除外键</span><br><span class="line">ALTER TABLE stuinfo DROP FOREIGN KEY fk_stuinfo_major;</span><br></pre></td></tr></table></figure>
<h2 id="14-标识列"><a href="#14-标识列" class="headerlink" title="14. 标识列"></a>14. 标识列</h2><p>又称为自增长列<br>含义:可以不用手动的插入值,系统提供默认的序列值</p>
<p>特点:<br>1、标识列必须和主键搭配吗?不一定,但要求是一个key<br>2、一个表可以有几个标识列?至多一个!<br>3、标识列的类型只能是数值型<br>4、标识列可以通过 SET auto_increment_increment=3;设置步长<br>可以通过 手动插入值,设置起始值</p>
<h2 id="15-事务介绍"><a href="#15-事务介绍" class="headerlink" title="15. 事务介绍"></a>15. 事务介绍</h2><p>Transaction Control Language 事务控制语言<br>一个或一组sql语句组成一个执行单元,这个执行单元要么全部执行,要么全部不执行。<br>事务的特性:<br>ACID<br>原子性:一个事务不可再分割,要么都执行要么都不执行<br>一致性:一个事务执行会使数据从一个一致状态切换到另外一个一致状态<br>隔离性:一个事务的执行不受其他事务的干扰<br>持久性:一个事务一旦提交,则会永久的改变数据库的数据.</p>
<p>事务的创建<br>隐式事务:事务没有明显的开启和结束的标记<br>比如insert、update、delete语句</p>
<p>delete from 表 where id =1;</p>
<p>显式事务:事务具有明显的开启和结束的标记<br>前提:必须先设置自动提交功能为禁用</p>
<p>set autocommit=0;</p>
<p>步骤1:开启事务<br>set autocommit=0;<br>start transaction;可选的<br>步骤2:编写事务中的sql语句(select insert update delete)<br>语句1;<br>语句2;<br>…</p>
<p>步骤3:结束事务<br>commit;提交事务<br>rollback;回滚事务</p>
<p>savepoint 节点名;设置保存点</p>
<h3 id="15-1-事务的隔离"><a href="#15-1-事务的隔离" class="headerlink" title="15.1 事务的隔离"></a>15.1 事务的隔离</h3><p><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1gh9ymnhf8fj30h206gaal.jpg" alt=""> </p>
<h3 id="15-2-回滚"><a href="#15-2-回滚" class="headerlink" title="15.2 回滚"></a>15.2 回滚</h3><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">#1.演示事务的使用步骤</span><br><span class="line"></span><br><span class="line">#开启事务</span><br><span class="line">SET autocommit=0;</span><br><span class="line">START TRANSACTION;</span><br><span class="line">#编写一组事务的语句</span><br><span class="line">UPDATE account SET balance = 1000 WHERE username='张无忌';</span><br><span class="line">UPDATE account SET balance = 1000 WHERE username='赵敏';</span><br><span class="line"></span><br><span class="line">#结束事务</span><br><span class="line">ROLLBACK;</span><br><span class="line">#commit;</span><br><span class="line"></span><br><span class="line">SELECT * FROM account;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">#2.演示事务对于delete和truncate的处理的区别</span><br><span class="line"></span><br><span class="line">SET autocommit=0;</span><br><span class="line">START TRANSACTION;</span><br><span class="line"></span><br><span class="line">DELETE FROM account;</span><br><span class="line">ROLLBACK;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">#3.演示savepoint 的使用</span><br><span class="line">SET autocommit=0;</span><br><span class="line">START TRANSACTION;</span><br><span class="line">DELETE FROM account WHERE id=25;</span><br><span class="line">SAVEPOINT a;#设置保存点</span><br><span class="line">DELETE FROM account WHERE id=28;</span><br><span class="line">ROLLBACK TO a;#回滚到保存点</span><br></pre></td></tr></table></figure>
<h2 id="16-视图介绍"><a href="#16-视图介绍" class="headerlink" title="16. 视图介绍"></a>16. 视图介绍</h2><p>含义:虚拟表,和普通的表一样使用</p>
<h3 id="16-1-视图创建"><a href="#16-1-视图创建" class="headerlink" title="16.1 视图创建"></a>16.1 视图创建</h3><p>语法:<br>create view 视图名<br>as<br>查询语句;</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">CREATE VIEW myv1</span><br><span class="line">AS</span><br><span class="line">SELECT last_name,department_name,job_title</span><br><span class="line">FROM employees e</span><br><span class="line">JOIN departments d ON e.department_id = d.department_id</span><br><span class="line">JOIN jobs j ON j.job_id = e.job_id;</span><br></pre></td></tr></table></figure>
<h3 id="16-2-视图修改"><a href="#16-2-视图修改" class="headerlink" title="16.2 视图修改"></a>16.2 视图修改</h3><p>方式一:<br>create or replace view 视图名<br>as<br>查询语句;</p>
<p>方式二:<br>语法:<br>alter view 视图名<br>as<br>查询语句;</p>
<h3 id="16-3-视图查看和删除"><a href="#16-3-视图查看和删除" class="headerlink" title="16.3 视图查看和删除"></a>16.3 视图查看和删除</h3><p>删除视图:语法:drop view 视图名,视图名,…;<br>查看视图:DESC myv3;SHOW CREATE VIEW myv3;</p>
<h3 id="16-4-视图更新"><a href="#16-4-视图更新" class="headerlink" title="16.4 视图更新"></a>16.4 视图更新</h3><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">CREATE OR REPLACE VIEW myv1</span><br><span class="line">AS</span><br><span class="line">SELECT last_name,email,salary*12*(1+IFNULL(commission_pct,0)) "annual salary"</span><br><span class="line">FROM employees;</span><br><span class="line"></span><br><span class="line">CREATE OR REPLACE VIEW myv1</span><br><span class="line">AS</span><br><span class="line">SELECT last_name,email</span><br><span class="line">FROM employees;</span><br></pre></td></tr></table></figure>
<p>具备以下特点的视图不允许更新:<br>1.含以下关键字的sql语句:分组函数、distinct、group by、having、union或者union all<br>2.常量视图<br>3.Select中包含子查询<br>4.join<br>5.from一个不能更新的视图<br>6.where子句的子查询引用了from子句中的表</p>
<h2 id="17-函数和存储过程"><a href="#17-函数和存储过程" class="headerlink" title="17. 函数和存储过程"></a>17. 函数和存储过程</h2><p>说明:变量由系统定义,不是用户定义,属于服务器层面<br>注意:全局变量需要添加global关键字,会话变量需要添加session关键字,如果不写,默认会话级别<br>使用步骤:<br>1、查看所有系统变量<br>show global|【session】variables;<br>2、查看满足条件的部分系统变量<br>show global|【session】 variables like ‘%char%’;<br>3、查看指定的系统变量的值<br>select @@global|【session】系统变量名;<br>4、为某个系统变量赋值<br>方式一:<br>set global|【session】系统变量名=值;<br>方式二:<br>set @@global|【session】系统变量名=值;</p>
<h3 id="17-1-全局变量"><a href="#17-1-全局变量" class="headerlink" title="17.1 全局变量"></a>17.1 全局变量</h3><p>作用域:针对于所有会话(连接)有效,但不能跨重启</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">#①查看所有全局变量</span><br><span class="line">SHOW GLOBAL VARIABLES;</span><br><span class="line">#②查看满足条件的部分系统变量</span><br><span class="line">SHOW GLOBAL VARIABLES LIKE '%char%';</span><br><span class="line">#③查看指定的系统变量的值</span><br><span class="line">SELECT @@global.autocommit;</span><br><span class="line">#④为某个系统变量赋值</span><br><span class="line">SET @@global.autocommit=0;</span><br><span class="line">SET GLOBAL autocommit=0;</span><br></pre></td></tr></table></figure>
<h3 id="17-2-会话变量"><a href="#17-2-会话变量" class="headerlink" title="17.2 会话变量"></a>17.2 会话变量</h3><p>作用域:针对于当前会话(连接)有效</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">#①查看所有会话变量</span><br><span class="line">SHOW SESSION VARIABLES;</span><br><span class="line">#②查看满足条件的部分会话变量</span><br><span class="line">SHOW SESSION VARIABLES LIKE '%char%';</span><br><span class="line">#③查看指定的会话变量的值</span><br><span class="line">SELECT @@autocommit;</span><br><span class="line">SELECT @@session.tx_isolation;</span><br><span class="line">#④为某个会话变量赋值</span><br><span class="line">SET @@session.tx_isolation='read-uncommitted';</span><br><span class="line">SET SESSION tx_isolation='read-committed';</span><br></pre></td></tr></table></figure>
<h3 id="17-3-自定义变量-用户变量"><a href="#17-3-自定义变量-用户变量" class="headerlink" title="17.3 自定义变量-用户变量"></a>17.3 自定义变量-用户变量</h3><p>说明:变量由用户自定义,而不是系统提供的<br>使用步骤:<br>1、声明<br>2、赋值<br>3、使用(查看、比较、运算等)<br>作用域:针对于当前会话(连接)有效,作用域同于会话变量</p>
<p>赋值操作符:=或:=<br>①声明并初始化<br>SET @变量名=值;<br>SET @变量名:=值;<br>SELECT @变量名:=值;</p>
<p>②赋值(更新变量的值)<br>方式一:<br> SET @变量名=值;<br> SET @变量名:=值;<br> SELECT @变量名:=值;<br>方式二:<br> SELECT 字段 INTO @变量名<br> FROM 表;<br>③使用(查看变量的值)<br>SELECT @变量名;</p>
<h3 id="17-4-自定义变量-局部变量"><a href="#17-4-自定义变量-局部变量" class="headerlink" title="17.4 自定义变量-局部变量"></a>17.4 自定义变量-局部变量</h3><p>作用域:仅仅在定义它的begin end块中有效<br>应用在 begin end中的第一句话</p>
<p>①声明<br>DECLARE 变量名 类型;<br>DECLARE 变量名 类型 【DEFAULT 值】;</p>
<p>②赋值(更新变量的值)<br>方式一:<br> SET 局部变量名=值;<br> SET 局部变量名:=值;<br> SELECT 局部变量名:=值;<br>方式二:<br> SELECT 字段 INTO 具备变量名<br> FROM 表;<br>③使用(查看变量的值)<br>SELECT 局部变量名;</p>
<p>案例:声明两个变量,求和并打印</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">#用户变量</span><br><span class="line">SET @m=1;</span><br><span class="line">SET @n=1;</span><br><span class="line">SET @sum=@m+@n;</span><br><span class="line">SELECT @sum;</span><br><span class="line"></span><br><span class="line">#局部变量</span><br><span class="line">DECLARE m INT DEFAULT 1;</span><br><span class="line">DECLARE n INT DEFAULT 1;</span><br><span class="line">DECLARE SUM INT;</span><br><span class="line">SET SUM=m+n;</span><br><span class="line">SELECT SUM;</span><br></pre></td></tr></table></figure>
<h3 id="17-5-存储过程介绍"><a href="#17-5-存储过程介绍" class="headerlink" title="17.5 存储过程介绍"></a>17.5 存储过程介绍</h3><p>含义:一组预先编译好的SQL语句的集合,理解成批处理语句<br>1、提高代码的重用性<br>2、简化操作<br>3、减少了编译次数并且减少了和数据库服务器的连接次数,提高了效率</p>
<h3 id="17-6-存储过程语法"><a href="#17-6-存储过程语法" class="headerlink" title="17.6 存储过程语法"></a>17.6 存储过程语法</h3><ol>
<li>创建语法 <figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">CREATE PROCEDURE 存储过程名(参数列表)</span><br><span class="line">BEGIN</span><br><span class="line"></span><br><span class="line"> 存储过程体(一组合法的SQL语句)</span><br><span class="line">END</span><br><span class="line"> ⚠️</span><br><span class="line"> 1.参数列表包含三部分: in stuname varchar(20)</span><br><span class="line"> 参数模式:</span><br><span class="line"> in:该参数可以作为输入,也就是该参数需要调用方传入值</span><br><span class="line"> out:该参数可以作为输出,也就是该参数可以作为返回值</span><br><span class="line"> inout:该参数既可以作为输入又可以作为输出,也就是该参数既需要传入值,又 可以返回值</span><br><span class="line"> 参数名:</span><br><span class="line"> 参数类型:</span><br><span class="line"> 2.如果存储过程体仅仅只有一句话,begin end可以省略</span><br><span class="line"> 3.存储过程体中的每条sql语句的结尾要求必须加分号</span><br><span class="line"> 4.存储过程的结尾可以使用 delimiter 重新设置</span><br><span class="line"> 语法:</span><br><span class="line"> delimiter 结束标记</span><br><span class="line"> 案例:</span><br><span class="line"> delimiter $</span><br></pre></td></tr></table></figure></li>
<li>调用方法<br> CALL 存储过程名(实参列表);</li>
</ol>
<h3 id="17-7-空参的存储过程"><a href="#17-7-空参的存储过程" class="headerlink" title="17.7 空参的存储过程"></a>17.7 空参的存储过程</h3><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line"># 定义存储过程</span><br><span class="line">delimiter $</span><br><span class="line">create PROCEDURE myp2()</span><br><span class="line">begin</span><br><span class="line"> insert into admin(username,`password`) values('测试','12345');</span><br><span class="line">end $</span><br><span class="line"></span><br><span class="line">call myp2()$</span><br></pre></td></tr></table></figure>
<h3 id="17-8-带in模式的存储过程"><a href="#17-8-带in模式的存储过程" class="headerlink" title="17.8 带in模式的存储过程"></a>17.8 带in模式的存储过程</h3><p>案例1:创建存储过程实现 根据女神名,查询对应的男神信息</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">create PROCEDURE myp3(in beatyName VARCHAR(20))</span><br><span class="line">begin</span><br><span class="line"> select be.*,bo.*</span><br><span class="line"> from beauty be</span><br><span class="line"> left join boys bo on be.boyfriend_id = bo.id</span><br><span class="line"> where be.name = beatyName;</span><br><span class="line">end</span><br><span class="line"></span><br><span class="line">call myp3('柳岩')</span><br></pre></td></tr></table></figure>
<p>案例2 :创建存储过程实现,用户是否登录成功</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">create PROCEDURE myp4(in username VARCHAR(20),in password VARCHAR(20))</span><br><span class="line">begin</span><br><span class="line"> DECLARE result VARCHAR(20) DEFAULT ''; # 声明并初始化</span><br><span class="line"> select count(*) into result # 赋值</span><br><span class="line"> from admin</span><br><span class="line"> where admin.username = username</span><br><span class="line"> and admin.password = password;</span><br><span class="line"> </span><br><span class="line"> select result; # 使用</span><br><span class="line">end</span><br><span class="line"></span><br><span class="line">call myp4('ceshi','123')</span><br></pre></td></tr></table></figure>
<h3 id="17-9-带out模式的存储过程"><a href="#17-9-带out模式的存储过程" class="headerlink" title="17.9 带out模式的存储过程"></a>17.9 带out模式的存储过程</h3><p>案例1:根据输入的女神名,返回对应的男神名</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">CREATE PROCEDURE myp6(IN beautyName VARCHAR(20),OUT boyName VARCHAR(20))</span><br><span class="line">BEGIN</span><br><span class="line"> SELECT bo.boyname INTO boyname</span><br><span class="line"> FROM boys bo</span><br><span class="line"> RIGHT JOIN</span><br><span class="line"> beauty b ON b.boyfriend_id = bo.id</span><br><span class="line"> WHERE b.name=beautyName ;</span><br><span class="line"> </span><br><span class="line">END $</span><br></pre></td></tr></table></figure>
<p>案例2:根据输入的女神名,返回对应的男神名和魅力值</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">create PROCEDURE myp8(in beatyName varchar(20),out boyName varchar(20),out usercp int)</span><br><span class="line">BEGIN</span><br><span class="line"> select bo.boyName ,bo.userCP into boyName ,usercp</span><br><span class="line"> from boys bo</span><br><span class="line"> inner join beauty b on bo.id = b.boyfriend_id</span><br><span class="line"> where b.name = beatyName;</span><br><span class="line">END</span><br><span class="line"></span><br><span class="line">call myp8('小昭',@bName,@cp)</span><br><span class="line"></span><br><span class="line">select @bName,@cp</span><br></pre></td></tr></table></figure>
<h3 id="17-10-带inout模式的存储过程"><a href="#17-10-带inout模式的存储过程" class="headerlink" title="17.10 带inout模式的存储过程"></a>17.10 带inout模式的存储过程</h3><p>案例1:传入a和b两个值,最终a和b都翻倍并返回</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">create PROCEDURE myp9(inout a int,inout b int)</span><br><span class="line">BEGIN</span><br><span class="line"> set a = a * 2;</span><br><span class="line"> set b =b * 2;</span><br><span class="line">END</span><br><span class="line"># 调用</span><br><span class="line">set @n=8;</span><br><span class="line">set @m=10;</span><br><span class="line">call myp9(@n,@m);</span><br><span class="line">select @m,@n</span><br></pre></td></tr></table></figure>
<h3 id="17-11-存储过程的删除"><a href="#17-11-存储过程的删除" class="headerlink" title="17.11 存储过程的删除"></a>17.11 存储过程的删除</h3><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">语法:drop procedure 存储过程名</span><br><span class="line">DROP PROCEDURE p1;</span><br></pre></td></tr></table></figure>
<h3 id="17-12-存储过程查看"><a href="#17-12-存储过程查看" class="headerlink" title="17.12 存储过程查看"></a>17.12 存储过程查看</h3><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">查看存储过程的信息</span><br><span class="line">SHOW CREATE PROCEDURE myp2;</span><br></pre></td></tr></table></figure>
<h2 id="18-函数的介绍"><a href="#18-函数的介绍" class="headerlink" title="18. 函数的介绍"></a>18. 函数的介绍</h2><p>含义:一组预先编译好的SQL语句的集合,理解成批处理语句<br>1、提高代码的重用性<br>2、简化操作<br>3、减少了编译次数并且减少了和数据库服务器的连接次数,提高了效率</p>
<p>区别:</p>
<p>存储过程:可以有0个返回,也可以有多个返回,适合做批量插入、批量更新<br>函数:有且仅有1 个返回,适合做处理数据后返回一个结果</p>
<h3 id="18-1-函数的创建和调用语法"><a href="#18-1-函数的创建和调用语法" class="headerlink" title="18.1 函数的创建和调用语法"></a>18.1 函数的创建和调用语法</h3><ol>
<li><p>创建语法</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">CREATE FUNCTION 函数名(参数列表) RETURNS 返回类型</span><br><span class="line">BEGIN</span><br><span class="line"> 函数体</span><br><span class="line">END</span><br><span class="line"></span><br><span class="line">⚠️</span><br><span class="line">1.参数列表包括两部分:参数名和参数类型</span><br><span class="line">2.函数体:肯定会有return语句,如果没有会报错</span><br><span class="line"> 如果return语句没有放在函数体的最后也不报错,但不建议</span><br><span class="line">3.函数体中仅有一句话,则可以省略begin end</span><br><span class="line">4.使用 delimiter语句设置结束标记</span><br></pre></td></tr></table></figure></li>
<li><p>调用语法:SELECT 函数名(参数列表)</p>
</li>
<li><p>案例</p>
<ul>
<li><p>无参有返回</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line"> #案例:返回公司的员工个数</span><br><span class="line"> create function myf1() returns int</span><br><span class="line">BEGIN</span><br><span class="line">DECLARE c int default 0;# 定义变量</span><br><span class="line"> select count(*) into c# 为变量赋值</span><br><span class="line"> from employees;</span><br><span class="line"> return c;</span><br><span class="line">END</span><br><span class="line">select myf1();</span><br></pre></td></tr></table></figure>
<ul>
<li><p>有参有返回</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line"> #案例1:根据员工名,返回它的工资</span><br><span class="line"> create function myf2(empName varchar(20)) returns double</span><br><span class="line">BEGIN</span><br><span class="line"> set @sal=0; # 定义了一个用户变量</span><br><span class="line"> select salary into @sal from employees where last_name = empName;</span><br><span class="line"> return @sal;</span><br><span class="line">END</span><br><span class="line">select myf2('John');</span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
</ul>
</li>
</ol>
<h3 id="18-2-函数的查看和删除"><a href="#18-2-函数的查看和删除" class="headerlink" title="18.2 函数的查看和删除"></a>18.2 函数的查看和删除</h3><p>SHOW CREATE FUNCTION myf3;<br>DROP FUNCTION myf3;</p>
<h2 id="19-流程控制结构介绍"><a href="#19-流程控制结构介绍" class="headerlink" title="19. 流程控制结构介绍"></a>19. 流程控制结构介绍</h2><p>顺序结构:程序从上往下依次执行<br>分支结构:程序从两条或多条路径中选择一条去执行<br>循环结构:程序在满足一定条件的基础上,重复执行一段代码</p>
<h3 id="19-1-分支结构—if函数"><a href="#19-1-分支结构—if函数" class="headerlink" title="19.1 分支结构—if函数"></a>19.1 分支结构—if函数</h3><p>语法:if(条件,值1,值2)<br>功能:实现双分支<br>应用在begin end中或外面<br>执行顺序:如果表达式的条件成立,则if返回值1,否则返回值2</p>
<h3 id="19-2-分支结构—case结构"><a href="#19-2-分支结构—case结构" class="headerlink" title="19.2 分支结构—case结构"></a>19.2 分支结构—case结构</h3><p>语法:</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">情况1:类似于switch语句,一般用于实现等值判断</span><br><span class="line">case 变量或表达式</span><br><span class="line">when 值1 then 语句1;</span><br><span class="line">when 值2 then 语句2;</span><br><span class="line">...</span><br><span class="line">else 语句n;</span><br><span class="line">end </span><br><span class="line"></span><br><span class="line">情况2:类似于多重if语句,一般用于实现区间判断</span><br><span class="line">case </span><br><span class="line">when 条件1 then 语句1;</span><br><span class="line">when 条件2 then 语句2;</span><br><span class="line">...</span><br><span class="line">else 语句n;</span><br><span class="line">end</span><br></pre></td></tr></table></figure>
<p>特点:应用在begin end 中或外面</p>
<ul>
<li>可以作为表达式,嵌套在其他语句中使用,可以放在任何地方,begin end中或外面</li>
<li>可以作为独立的语句去使用,只能放在begin end中</li>
<li>如果when中的值满足或条件成立,则执行对应的then后面的语句,并且结束case</li>
<li>如果都不满足则执行else中的语句或值</li>
<li>else可以省略,如果else省略了,并且所有where条件都不满足,则返回null</li>
</ul>
<p>案例:</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">#案例1:创建函数,实现传入成绩,如果成绩>90,返回A,如果成绩>80,返回B,如果成绩>60,返回C,否则返回D</span><br><span class="line">create PROCEDURE test_case(in score int)</span><br><span class="line">BEGIN</span><br><span class="line"> case </span><br><span class="line"> when score >= 90 then SELECT 'A';</span><br><span class="line"> when score >=80 then select 'B';</span><br><span class="line"> when score >=70 then select 'C';</span><br><span class="line"> else select 'D';</span><br><span class="line"> end case;</span><br><span class="line">END</span><br><span class="line"></span><br><span class="line">call test_case(75)</span><br></pre></td></tr></table></figure>
<h3 id="19-3-分支结构—if结构"><a href="#19-3-分支结构—if结构" class="headerlink" title="19.3 分支结构—if结构"></a>19.3 分支结构—if结构</h3><p>语法:</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">语法:</span><br><span class="line">if 条件1 then 语句1;</span><br><span class="line">elseif 条件2 then 语句2;</span><br><span class="line">....</span><br><span class="line">else 语句n;</span><br><span class="line">end if;</span><br><span class="line">功能:类似于多重if</span><br><span class="line"></span><br><span class="line">只能应用在begin end 中</span><br></pre></td></tr></table></figure>
<h3 id="19-4循环结构的介绍"><a href="#19-4循环结构的介绍" class="headerlink" title="19.4循环结构的介绍"></a>19.4循环结构的介绍</h3><p>分类:</p>
<ul>
<li>while:</li>
<li>loop:</li>
<li>repeat:</li>
</ul>
<p>循环控制:</p>
<ul>
<li>iterate类似于 continue,继续,结束本次循环,继续下一次</li>
<li>leave 类似于 break,跳出,结束当前所在的循环</li>
</ul>
<p>语法:</p>
<ul>
<li><p>while </p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line"> 【标签:】while 循环条件 do</span><br><span class="line"> 循环体;</span><br><span class="line">end while【标签】;</span><br><span class="line">联想:</span><br><span class="line">while(循环条件){</span><br><span class="line"> 循环体;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
</li>
<li><p>loop</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">标签:】loop</span><br><span class="line"> 循环体;</span><br><span class="line">end loop 【标签】;</span><br><span class="line"># 可以用来模拟简单的死循环</span><br></pre></td></tr></table></figure></li>
<li><p>repeat</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">【标签:】repeat</span><br><span class="line"> 循环体;</span><br><span class="line">until 结束循环的条件</span><br><span class="line">end repeat 【标签】;</span><br></pre></td></tr></table></figure>
<h3 id="19-5-循环结构演示"><a href="#19-5-循环结构演示" class="headerlink" title="19.5 循环结构演示"></a>19.5 循环结构演示</h3><p>案例:批量插入,根据次数插入到admin表中多条记录</p>
</li>
</ul>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">create PROCEDURE pro_while1(in insertcount int)</span><br><span class="line">BEGIN</span><br><span class="line"> DECLARE i int DEFAULT 1;</span><br><span class="line"> while i <= insertcount do</span><br><span class="line"> insert into admin(username,password) values(CONCAT('ceshi',i),'6666');</span><br><span class="line"> set i = i + 1;</span><br><span class="line"> END WHILE;</span><br><span class="line">END</span><br><span class="line"></span><br><span class="line">call pro_while1(10);</span><br></pre></td></tr></table></figure>
<p>案例2:批量插入,根据次数插入到admin表中多条记录,如果次数>20则停止(添加leave语句)</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">create PROCEDURE pro_while2(in insertcount int)</span><br><span class="line">BEGIN</span><br><span class="line"> DECLARE i int DEFAULT 1;</span><br><span class="line"> a:while i <= insertcount do</span><br><span class="line"> insert into admin(username,password) values(CONCAT('xiaohua',i),'6666');</span><br><span class="line"> if i>=20 then leave a;</span><br><span class="line"> end if;</span><br><span class="line"> set i = i + 1;</span><br><span class="line"> END WHILE a;</span><br><span class="line">END</span><br><span class="line"></span><br><span class="line">call pro_while2(30);</span><br></pre></td></tr></table></figure>
<p>案例:批量插入,根据次数插入到admin表中多条记录,只插入偶数次(添加iterate语句)</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">TRUNCATE TABLE admin$</span><br><span class="line">DROP PROCEDURE test_while1$</span><br><span class="line">CREATE PROCEDURE test_while1(IN insertCount INT)</span><br><span class="line">BEGIN</span><br><span class="line"> DECLARE i INT DEFAULT 0;</span><br><span class="line"> a:WHILE i<=insertCount DO</span><br><span class="line"> SET i=i+1;</span><br><span class="line"> IF MOD(i,2)!=0 THEN ITERATE a;</span><br><span class="line"> END IF;</span><br><span class="line"> </span><br><span class="line"> INSERT INTO admin(username,`password`) VALUES(CONCAT('xiaohua',i),'0000');</span><br><span class="line"> </span><br><span class="line"> END WHILE a;</span><br><span class="line">END $</span><br><span class="line">CALL test_while1(100)$</span><br></pre></td></tr></table></figure>]]></content>
</entry>
<entry>
<title>MySQL高级部分学习一</title>
<url>/posts/f2612c14.html</url>
<content><![CDATA[<h1 id="MySQL高级部分学习一"><a href="#MySQL高级部分学习一" class="headerlink" title="MySQL高级部分学习一"></a>MySQL高级部分学习一</h1><h2 id="1-mysql的架构介绍"><a href="#1-mysql的架构介绍" class="headerlink" title="1. mysql的架构介绍"></a>1. mysql的架构介绍</h2><h3 id="1-1-mysql简介"><a href="#1-1-mysql简介" class="headerlink" title="1.1 mysql简介"></a>1.1 mysql简介</h3><h3 id="1-2-mysqlLinux版的安装"><a href="#1-2-mysqlLinux版的安装" class="headerlink" title="1.2 mysqlLinux版的安装"></a>1.2 mysqlLinux版的安装</h3><ol>
<li>检查当前系统是否安装过mysql:<code>rpm -qa|grep -i mysql</code></li>
<li>查看mysql安装时创建的mysql用户和mysql用户组:<code>cat /etc/password|grep mysql</code>、<code>cat /etc/group|grep mysql</code></li>
<li>mysql服务的启动和停止:<code>service mysql start</code>,查看是否启动成功:<code>ps -ef|grep mysql</code></li>
<li>root密码设置和设置开机随机启动:<code>chkconfig mysql on</code></li>
<li>mysql的安装位置:<code>/var/lib/mysql</code></li>
</ol>
<h3 id="1-3-MySQL的配置文件-my-cnf"><a href="#1-3-MySQL的配置文件-my-cnf" class="headerlink" title="1.3 MySQL的配置文件:my.cnf"></a>1.3 MySQL的配置文件:my.cnf</h3><p><del>1. client:客户端的配置</del></p>
<h3 id="1-4-Mysql逻辑架构介绍"><a href="#1-4-Mysql逻辑架构介绍" class="headerlink" title="1.4 Mysql逻辑架构介绍"></a>1.4 Mysql逻辑架构介绍</h3><h3 id="1-5-mysql存储引擎"><a href="#1-5-mysql存储引擎" class="headerlink" title="1.5 mysql存储引擎"></a>1.5 mysql存储引擎</h3><ol>
<li>查看mysql提供的存储引擎:<code>show engines;</code></li>
<li>查看mysql抢天默认的存储引擎:<code>show variables like '%storage_engine%';</code></li>
</ol>
<h2 id="2-索引优化分析"><a href="#2-索引优化分析" class="headerlink" title="2. 索引优化分析"></a>2. 索引优化分析</h2><h3 id="2-1-SQL性能下降原因"><a href="#2-1-SQL性能下降原因" class="headerlink" title="2.1 SQL性能下降原因"></a>2.1 SQL性能下降原因</h3><ol>
<li>索引失效</li>
<li>关联查询太多join(设计缺陷或不得已的需求)</li>
<li>服务器调优及各个参数设置(缓冲、线程数等)</li>
</ol>
<h3 id="2-2-SQL执行加载顺序"><a href="#2-2-SQL执行加载顺序" class="headerlink" title="2.2 SQL执行加载顺序"></a>2.2 SQL执行加载顺序</h3><p>from–on–join–where–group by–having–select–distinct–order by –limit<br><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1ghgxfq06gqj30i60ecwh3.jpg" alt=""><br><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1ghgxh4irx6j312o0aw419.jpg" alt=""></p>
<h3 id="2-3-七种JOIN理论"><a href="#2-3-七种JOIN理论" class="headerlink" title="2.3 七种JOIN理论"></a>2.3 七种JOIN理论</h3><p><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1ghgxiygg3sj30ze0haqew.jpg" alt=""><br><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1ghgxqhppfbj30zm0aw477.jpg" alt=""></p>
<h3 id="2-4-七种JOIN的SQL编写"><a href="#2-4-七种JOIN的SQL编写" class="headerlink" title="2.4 七种JOIN的SQL编写"></a>2.4 七种JOIN的SQL编写</h3><h3 id="2-5-索引是什么"><a href="#2-5-索引是什么" class="headerlink" title="2.5 索引是什么"></a>2.5 索引是什么</h3><p>索引(index)是帮助mysql高效获取数据的数据结构。可以理解为<strong><em>“排好顺序快速查找数据结构”</em></strong></p>
<h3 id="2-6-索引优势劣势"><a href="#2-6-索引优势劣势" class="headerlink" title="2.6 索引优势劣势"></a>2.6 索引优势劣势</h3><p>优势:</p>
<ol>
<li>提高检索效率,降低数据库的IO成本</li>
<li>通过索引对数据进行排序,降低数据排序的成本,降低了COU的小号</li>
</ol>
<p>劣势:</p>
<ol>
<li>实际上索引也是一张表,该表保存了主键与索引字段,并且指向实体表记录,所以索引也是要占用空间的</li>
<li>虽然索引大大提高了查询速度,但是却会降低更新表的速度</li>
<li>都会调整因为更新所带来的键值变化后的索引信息</li>