-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.xml
275 lines (132 loc) · 142 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
<?xml version="1.0" encoding="utf-8"?>
<search>
<entry>
<title>JavaEE课程踩坑记录</title>
<link href="/2020/12/21/solving-problem/"/>
<url>/2020/12/21/solving-problem/</url>
<content type="html"><![CDATA[<h2 id="踩坑记录"><a href="#踩坑记录" class="headerlink" title="踩坑记录"></a>踩坑记录</h2><h4 id="1-数据库时区有关异常"><a href="#1-数据库时区有关异常" class="headerlink" title="1. 数据库时区有关异常"></a>1. 数据库时区有关异常</h4><ul><li>解决方案:添加时区参数<code>jdbc:mysql://localhost:3306/DATABASE?serverTimezone=UTC</code></li></ul><h4 id="2-更换Maven源"><a href="#2-更换Maven源" class="headerlink" title="2. 更换Maven源"></a>2. 更换Maven源</h4><ul><li>解决方案:在setting.xml中加入国内源</li></ul><a id="more"></a><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"><?xml version="1.0" encoding="UTF-8"?></span></span><br><span class="line"><span class="tag"><<span class="name">settings</span> <span class="attr">xmlns</span>=<span class="string">"http://maven.apache.org/SETTINGS/1.0.0"</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://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">mirrors</span>></span></span><br><span class="line"> <span class="comment"><!-- mirror</span></span><br><span class="line"><span class="comment"> | Specifies a repository mirror site to use instead of a given repository. The repository that</span></span><br><span class="line"><span class="comment"> | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used</span></span><br><span class="line"><span class="comment"> | for inheritance and direct lookup purposes, and must be unique across the set of mirrors.</span></span><br><span class="line"><span class="comment"> |</span></span><br><span class="line"><span class="comment"> <mirror></span></span><br><span class="line"><span class="comment"> <id>mirrorId</id></span></span><br><span class="line"><span class="comment"> <mirrorOf>repositoryId</mirrorOf></span></span><br><span class="line"><span class="comment"> <name>Human Readable Name for this Mirror.</name></span></span><br><span class="line"><span class="comment"> <url>http://my.repository.com/repo/path</url></span></span><br><span class="line"><span class="comment"> </mirror></span></span><br><span class="line"><span class="comment"> --></span></span><br><span class="line"></span><br><span class="line"> <span class="tag"><<span class="name">mirror</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">id</span>></span>alimaven<span class="tag"></<span class="name">id</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">name</span>></span>aliyun maven<span class="tag"></<span class="name">name</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">url</span>></span>http://maven.aliyun.com/nexus/content/groups/public/<span class="tag"></<span class="name">url</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">mirrorOf</span>></span>central<span class="tag"></<span class="name">mirrorOf</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">mirror</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="tag"><<span class="name">mirror</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">id</span>></span>uk<span class="tag"></<span class="name">id</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">mirrorOf</span>></span>central<span class="tag"></<span class="name">mirrorOf</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">name</span>></span>Human Readable Name for this Mirror.<span class="tag"></<span class="name">name</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">url</span>></span>http://uk.maven.org/maven2/<span class="tag"></<span class="name">url</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">mirror</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="tag"><<span class="name">mirror</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">id</span>></span>CN<span class="tag"></<span class="name">id</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">name</span>></span>OSChina Central<span class="tag"></<span class="name">name</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">url</span>></span>http://maven.oschina.net/content/groups/public/<span class="tag"></<span class="name">url</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">mirrorOf</span>></span>central<span class="tag"></<span class="name">mirrorOf</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">mirror</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="tag"><<span class="name">mirror</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">id</span>></span>nexus<span class="tag"></<span class="name">id</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">name</span>></span>internal nexus repository<span class="tag"></<span class="name">name</span>></span></span><br><span class="line"> <span class="comment"><!-- <url>http://192.168.1.100:8081/nexus/content/groups/public/</url>--></span></span><br><span class="line"> <span class="tag"><<span class="name">url</span>></span>http://repo.maven.apache.org/maven2<span class="tag"></<span class="name">url</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">mirrorOf</span>></span>central<span class="tag"></<span class="name">mirrorOf</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">mirror</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="tag"></<span class="name">mirrors</span>></span></span><br><span class="line"><span class="tag"></<span class="name">settings</span>></span></span><br></pre></td></tr></table></figure><h4 id="3-AOP后置通知和最终通知的顺序颠倒"><a href="#3-AOP后置通知和最终通知的顺序颠倒" class="headerlink" title="3. AOP后置通知和最终通知的顺序颠倒"></a>3. AOP后置通知和最终通知的顺序颠倒</h4><ul><li>解决方案:将<code>spring-context</code>版本设为<code>5.2.7.RELEASE</code></li></ul><h4 id="4-有关MyBatis"><a href="#4-有关MyBatis" class="headerlink" title="4. 有关MyBatis"></a>4. 有关MyBatis</h4><p>在<code>Mybatis</code>中的<code>mapper</code>中的<code>xml</code>文件中写<code>SQL</code>语句时,小于号以及大于号要写成<code>&lt;</code>以及<code>&gt;</code>否则会与<code>xml</code>中的尖括号冲突。</p><h4 id="5-有关微信小程序"><a href="#5-有关微信小程序" class="headerlink" title="5. 有关微信小程序"></a>5. 有关微信小程序</h4><p>在微信小程序中,要求使用<code>https</code>协议。在这里采取的方法是将后台应用部署到服务器上,并使用<code>nginx</code>进行反向代理。最后申请域名,上<code>https</code>证书。</p><p><code>nginx</code>的配置如下:</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">server {</span><br><span class="line"> listen 443 ssl; #配置HTTPS的默认访问端口号为443。此处如果未配置HTTPS的默认访问端口,可能会造成Nginx无法启动。Nginx 1.15.0以上版本请使用listen 443 ssl代替listen 443和ssl on。</span><br><span class="line"> server_name wwww.kuumtde.cn; #将www.certificatestests.com修改为您证书绑定的域名,例如:www.example.com。如果您购买的是通配符域名证书,要修改为通配符域名,例如:*.aliyun.com。</span><br><span class="line"> root html;</span><br><span class="line"> index index.html index.htm;</span><br><span class="line"> ssl_certificate cert/4683033_kuumtde.cn.pem; #将domain name.pem替换成您证书的文件名称。</span><br><span class="line"> ssl_certificate_key cert/4683033_kuumtde.cn.key; #将domain name.key替换成您证书的密钥文件名称。</span><br><span class="line"> ssl_session_timeout 5m;</span><br><span class="line"> ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; #使用此加密套件。</span><br><span class="line"> ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #使用该协议进行配置。</span><br><span class="line"> ssl_prefer_server_ciphers on;</span><br><span class="line"></span><br><span class="line"> location / {</span><br><span class="line"> root html; #站点目录。</span><br><span class="line"> index index.html index.htm;</span><br><span class="line"> proxy_pass http://127.0.0.1:8080;</span><br><span class="line"> }</span><br><span class="line"> }</span><br></pre></td></tr></table></figure><p>配置完成后,重启<code>nginx</code>即可</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">cd</span> /usr/<span class="built_in">local</span>/nginx/sbin</span><br><span class="line">./nginx -s stop</span><br><span class="line">./nginx</span><br></pre></td></tr></table></figure><p>或者重新加载配置文件也可:</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">./nginx -s reload</span><br></pre></td></tr></table></figure><h4 id="6-服务端上项目的打包以及部署"><a href="#6-服务端上项目的打包以及部署" class="headerlink" title="6. 服务端上项目的打包以及部署"></a>6. 服务端上项目的打包以及部署</h4><p>首先进入项目目录,然后编译、打包、运行</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">mvn clean</span><br><span class="line">mvn compile</span><br><span class="line">mvn package</span><br><span class="line">nohup java -jar XXX.jar</span><br></pre></td></tr></table></figure>]]></content>
<categories>
<category> Spring </category>
</categories>
<tags>
<tag> Spring </tag>
<tag> Linux </tag>
<tag> Java </tag>
</tags>
</entry>
<entry>
<title>Linux环境配置常见问题</title>
<link href="/2020/12/02/linux-env/"/>
<url>/2020/12/02/linux-env/</url>
<content type="html"><![CDATA[<h1 id="Linux环境配置常见问题"><a href="#Linux环境配置常见问题" class="headerlink" title="Linux环境配置常见问题"></a>Linux环境配置常见问题</h1><h2 id="1-换清华源"><a href="#1-换清华源" class="headerlink" title="1. 换清华源"></a>1. 换清华源</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">$ cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup <span class="comment">#备份</span></span><br><span class="line">$ vim /etc/yum.repos.d/CentOS-Base.repo <span class="comment">#将内容修改如下</span></span><br></pre></td></tr></table></figure><a id="more"></a><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line"># CentOS-Base.repo</span><br><span class="line">#</span><br><span class="line"># The mirror system uses the connecting IP address of the client and the</span><br><span class="line"># update status of each mirror to pick mirrors that are updated to and</span><br><span class="line"># geographically close to the client. You should use this for CentOS updates</span><br><span class="line"># unless you are manually picking other mirrors.</span><br><span class="line">#</span><br><span class="line"># If the mirrorlist= does not work for you, as a fall back you can try the</span><br><span class="line"># remarked out baseurl= line instead.</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">[base]</span><br><span class="line">name=CentOS-$releasever - Base</span><br><span class="line">baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/os/$basearch/</span><br><span class="line">#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os</span><br><span class="line">enabled=1</span><br><span class="line">gpgcheck=1</span><br><span class="line">gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-7</span><br><span class="line"></span><br><span class="line">#released updates</span><br><span class="line">[updates]</span><br><span class="line">name=CentOS-$releasever - Updates</span><br><span class="line">baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/updates/$basearch/</span><br><span class="line">#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates</span><br><span class="line">enabled=1</span><br><span class="line">gpgcheck=1</span><br><span class="line">gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-7</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">#additional packages that may be useful</span><br><span class="line">[extras]</span><br><span class="line">name=CentOS-$releasever - Extras</span><br><span class="line">baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/extras/$basearch/</span><br><span class="line">#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras</span><br><span class="line">enabled=1</span><br><span class="line">gpgcheck=1</span><br><span class="line">gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-7</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">#additional packages that extend functionality of existing packages</span><br><span class="line">[centosplus]</span><br><span class="line">name=CentOS-$releasever - Plus</span><br><span class="line">baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/centosplus/$basearch/</span><br><span class="line">#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus</span><br><span class="line">gpgcheck=1</span><br><span class="line">enabled=0</span><br><span class="line">gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-7</span><br></pre></td></tr></table></figure><p>这里是<a href="https://mirrors.tuna.tsinghua.edu.cn/help/centos/" target="_blank" rel="noopener">参考网址</a></p><h2 id="2-配置环境变量"><a href="#2-配置环境变量" class="headerlink" title="2. 配置环境变量"></a>2. 配置环境变量</h2><p>在<code>/etc/profile</code>下修改环境变量<br>举例:</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">export CMAKE_HOME=/usr/local/cmake-3.18.4-Linux-x86_64</span><br><span class="line">export PATH=$CMAKE_HOME/bin:$PATH</span><br></pre></td></tr></table></figure><p>使用<code>source /etc/profile</code>命令更新环境变量</p><h2 id="3-网络配置"><a href="#3-网络配置" class="headerlink" title="3. 网络配置"></a>3. 网络配置</h2><p> 配置文件位置:<code>cd /etc/sysconfig/network-scripts</code></p><p>配置完成后,重启网络服务<br><code>systemctl restart network.service</code></p><h2 id="4-防火墙"><a href="#4-防火墙" class="headerlink" title="4. 防火墙"></a>4. 防火墙</h2><p>查看防火墙状态:<code>systemctl status firewalld</code></p><p>关闭防火墙:<code>systemctl stop firewalld.service</code></p><p>开放端口:<code>firewall-cmd --zone=public --add-port=5672/tcp --permanent</code></p><p>关闭端口:<code>firewall-cmd --zone=public --remove-port=5672/tcp --permanent</code></p><p>使配置立即生效:<code>firewall-cmd --reload</code></p><h2 id="5-赋予用户sudo权限"><a href="#5-赋予用户sudo权限" class="headerlink" title="5. 赋予用户sudo权限"></a>5. 赋予用户sudo权限</h2><p>修改<code>/etc/sudoers</code>文件 </p><p>加入以下内容</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">username ALL=(ALL) ALL</span><br></pre></td></tr></table></figure>]]></content>
<categories>
<category> Linux </category>
</categories>
<tags>
<tag> Linux </tag>
</tags>
</entry>
<entry>
<title>SpringMVC——文件上传、异常处理、拦截器</title>
<link href="/2020/11/11/springmvc2/"/>
<url>/2020/11/11/springmvc2/</url>
<content type="html"><![CDATA[<h1 id="SpringMVC——文件上传、异常处理、拦截器"><a href="#SpringMVC——文件上传、异常处理、拦截器" class="headerlink" title="SpringMVC——文件上传、异常处理、拦截器"></a>SpringMVC——文件上传、异常处理、拦截器</h1><h2 id="1-异常处理"><a href="#1-异常处理" class="headerlink" title="1. 异常处理"></a>1. 异常处理</h2><p>系统中异常包括两类:预期异常和运行时异常 <code>RuntimeException</code>,前者通过捕获异常从而获取异常信息, 后者主要通过规范代码开发、测试通过手段减少运行时异常的发生。 系统的 dao、service、controller 出现都通过 <code>throws Exception</code>向上抛出,最后由 <code>springmvc</code> 前端 控制器交由异常处理器进行异常处理。</p><h3 id="1-处理方式1"><a href="#1-处理方式1" class="headerlink" title="1. 处理方式1"></a>1. 处理方式1</h3><p>自定义异常处理类,并实现<code>HandlerExceptionResolver</code>接口。</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Component</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">SysExceptionResolver</span> <span class="keyword">implements</span> <span class="title">HandlerExceptionResolver</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> ModelAndView <span class="title">resolveException</span><span class="params">(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e)</span> </span>{</span><br><span class="line"> <span class="comment">//获取异常</span></span><br><span class="line"> SysException ex = <span class="keyword">null</span>;</span><br><span class="line"> <span class="keyword">if</span>(e <span class="keyword">instanceof</span> SysException){</span><br><span class="line"> ex = (SysException) e;</span><br><span class="line"> }<span class="keyword">else</span>{</span><br><span class="line"> ex = <span class="keyword">new</span> SysException(<span class="string">"系统正在维护"</span>);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">//创建ModelAndView对象</span></span><br><span class="line"> ModelAndView mv= <span class="keyword">new</span> ModelAndView();</span><br><span class="line"> mv.addObject(<span class="string">"errorMsg"</span>,e.getMessage());</span><br><span class="line"> mv.setViewName(<span class="string">"error"</span>);</span><br><span class="line"> <span class="keyword">return</span> mv;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><a id="more"></a><h3 id="2-处理方式"><a href="#2-处理方式" class="headerlink" title="2. 处理方式"></a>2. 处理方式</h3><p>使用<code>@ExceptionHandler</code>注解加在方法上,处理异常。</p><p>另外,可以在异常处理类上加上<code>@ControllerAdvice</code>,用来处理控制器的全局配置。</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@ResponseBody</span></span><br><span class="line"><span class="meta">@ControllerAdvice</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">CustmomizeExceptionHandler</span> </span>{</span><br><span class="line"> <span class="meta">@ExceptionHandler</span>(Exception<span class="class">.<span class="keyword">class</span>)</span></span><br><span class="line"><span class="class"> <span class="title">Object</span> <span class="title">handleControllerException</span>(<span class="title">HttpServletRequest</span> <span class="title">request</span>, <span class="title">Throwable</span> <span class="title">e</span>, <span class="title">Model</span> <span class="title">model</span>) </span>{</span><br><span class="line"></span><br><span class="line"> String contentType = request.getContentType();</span><br><span class="line"></span><br><span class="line"> <span class="keyword">if</span> (<span class="string">"application/json"</span>.equals(contentType)) {</span><br><span class="line"> <span class="comment">//返回json</span></span><br><span class="line"> <span class="keyword">if</span> (e <span class="keyword">instanceof</span> CustomizeException) {</span><br><span class="line"> <span class="keyword">return</span> ResultDTO.errorOf((CustomizeException)e);</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> <span class="keyword">return</span> ResultDTO.errorOf(CustomizeErrorCode.SYSTEM_ERROR);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> <span class="comment">//错误页面详情</span></span><br><span class="line"> <span class="keyword">if</span> (e <span class="keyword">instanceof</span> CustomizeException) {</span><br><span class="line"> model.addAttribute(<span class="string">"message"</span>, e.getMessage());</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> model.addAttribute(<span class="string">"message"</span>, CustomizeErrorCode.SYSTEM_ERROR.getMessage());</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">new</span> ModelAndView(<span class="string">"error"</span>);</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><h2 id="2-拦截器"><a href="#2-拦截器" class="headerlink" title="2. 拦截器"></a>2. 拦截器</h2><p>拦截器可以对控制器的方法进行预处理或后处理。</p><p><code>preHandler</code>:在<code>controller</code>方法前执行。如果返回<code>true</code>,放行执行控制器方法,否则不放行。</p><p><code>postHandler</code>:在<code>controller</code>方法执行后执行。</p><p><code>afterHandler</code>:前端控制器完全处理完请求后再执行。</p><p>编写代码时,需要实现<code>HandlerInterceptor</code>接口。</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyInterceptor1</span> <span class="keyword">implements</span> <span class="title">HandlerInterceptor</span> </span>{</span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> request</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> response</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> handler</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span> 如果为true,放行,执行controller方法,否则不放行</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@throws</span> Exception</span></span><br><span class="line"><span class="comment"> */</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">boolean</span> <span class="title">preHandle</span><span class="params">(HttpServletRequest request, HttpServletResponse response, Object handler)</span> <span class="keyword">throws</span> Exception </span>{</span><br><span class="line"> System.out.println(<span class="string">"MyInterceptor1执行了"</span>);</span><br><span class="line"> request.getRequestDispatcher(<span class="string">"/WEB-INF/pages/error.jsp"</span>).forward(request, response);</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * controller方法执行后</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> request</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> response</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> handler</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> modelAndView</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@throws</span> Exception</span></span><br><span class="line"><span class="comment"> */</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">postHandle</span><span class="params">(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)</span> <span class="keyword">throws</span> Exception </span>{</span><br><span class="line"> System.out.println(<span class="string">"MyInterceptor1执行后"</span>);</span><br><span class="line"><span class="comment">// request.getRequestDispatcher("/WEB-INF/pages/error.jsp").forward(request, response);</span></span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * success.jsp页面执行后,该方法执行</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> request</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> response</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> handler</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> ex</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@throws</span> Exception</span></span><br><span class="line"><span class="comment"> */</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">afterCompletion</span><span class="params">(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)</span> <span class="keyword">throws</span> Exception </span>{</span><br><span class="line"> System.out.println(<span class="string">"最后"</span>);</span><br><span class="line"></span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>配置拦截器时,可以在<code>xml</code>文件中配置:</p><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">mvc:interceptors</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">mvc:interceptor</span>></span></span><br><span class="line"> <span class="comment"><!--要拦截的具体方法--></span></span><br><span class="line"> <span class="tag"><<span class="name">mvc:mapping</span> <span class="attr">path</span>=<span class="string">"/user/*"</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">id</span>=<span class="string">"myInterceptor1"</span> <span class="attr">class</span>=<span class="string">"com.billychen.interceptor.MyInterceptor1"</span>></span><span class="tag"></<span class="name">bean</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">mvc:interceptor</span>></span></span><br><span class="line"><span class="tag"></<span class="name">mvc:interceptors</span>></span></span><br></pre></td></tr></table></figure><p>也可以在<code>Java</code>配置类中配置</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Configuration</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">WebConfig</span> <span class="keyword">implements</span> <span class="title">WebMvcConfigurer</span> </span>{</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> SessionInterceptor sessionInterceptor;</span><br><span class="line"></span><br><span class="line"> <span class="meta">@Override</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">addInterceptors</span><span class="params">(InterceptorRegistry registry)</span> </span>{</span><br><span class="line"> registry.addInterceptor(sessionInterceptor).addPathPatterns(<span class="string">"/**"</span>);</span><br><span class="line"> }</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><p><code>Spring MVC</code>可以为我们简化文件的上传,使用<code>MultipartFile</code>对象来获得上传的文件。</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RequestMapping</span>(<span class="string">"/fileUpload2"</span>)</span><br><span class="line"><span class="function"><span class="keyword">public</span> String <span class="title">fileUpload2</span><span class="params">(HttpServletRequest request, MultipartFile upload)</span> <span class="keyword">throws</span> IOException </span>{</span><br><span class="line"> System.out.println(<span class="string">"file upload2"</span>);</span><br><span class="line"> <span class="comment">//使用fileupload组件完成文件上传</span></span><br><span class="line"> <span class="comment">//上传位置</span></span><br><span class="line"> String path = request.getSession().getServletContext().getRealPath(<span class="string">"/upload/"</span>);</span><br><span class="line"> <span class="comment">//判断该路径是否存在</span></span><br><span class="line"> File file = <span class="keyword">new</span> File(path);</span><br><span class="line"> <span class="keyword">if</span> (!file.exists()) {</span><br><span class="line"> file.mkdirs();</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> String filename = upload.getOriginalFilename();</span><br><span class="line"> <span class="comment">//把文件名称设置成唯一值</span></span><br><span class="line"> String uuid = UUID.randomUUID().toString().replace(<span class="string">"-"</span>, <span class="string">""</span>);</span><br><span class="line"> filename = uuid + <span class="string">"_"</span> + filename;</span><br><span class="line"> <span class="comment">//上传文件</span></span><br><span class="line"> upload.transferTo(<span class="keyword">new</span> File(path, filename));</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> <span class="string">"success"</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>在<code>xml</code>中需要配置文件解析器</p><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"><!--配置文件解析器--></span></span><br><span class="line"><span class="comment"><!--id必须为multipartResolver--></span></span><br><span class="line"><span class="tag"><<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"multipartResolver"</span> <span class="attr">class</span>=<span class="string">"org.springframework.web.multipart.commons.CommonsMultipartResolver"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"maxUploadSize"</span> <span class="attr">value</span>=<span class="string">"10485760"</span>></span><span class="tag"></<span class="name">property</span>></span></span><br><span class="line"><span class="tag"></<span class="name">bean</span>></span></span><br></pre></td></tr></table></figure><p>注意:在<code>Spring Boot</code>中已经自动配置文件解析器,如果需要自动配置,可以在配置类中自行配置自定义的文件解析器。</p>]]></content>
<categories>
<category> Spring </category>
</categories>
<tags>
<tag> Spring </tag>
<tag> Java </tag>
<tag> SpringMVC </tag>
</tags>
</entry>
<entry>
<title>SpringMVC——参数和返回值</title>
<link href="/2020/11/11/springmvc1/"/>
<url>/2020/11/11/springmvc1/</url>
<content type="html"><![CDATA[<h1 id="Spring-MVC——参数和返回值"><a href="#Spring-MVC——参数和返回值" class="headerlink" title="Spring MVC——参数和返回值"></a>Spring MVC——参数和返回值</h1><h2 id="0-介绍"><a href="#0-介绍" class="headerlink" title="0. 介绍"></a>0. 介绍</h2><p>SpringMVC 是一种基于<code>Java</code>的实现MVC设计模型的请求驱动类型的轻量级 Web 框架,属于 <code>Spring FrameWork</code>的后续产品,已经融合在 <code>Spring Web Flow</code>里面。<code>Spring</code> 框架提供了构建 Web 应用程序的全功能 MVC 模块。</p><p>它通过一套注解,让一个简单的<code>Java</code>类成为处理请求的控制器,而无须实现任何接口。同时它还支<code>持RESTful</code>编程风格的请求。 </p><img src="/2020/11/11/springmvc1/springmvc.png" class="" title="SpringMVC"><p><code>Spring MVC</code>的执行流程如下</p><img src="/2020/11/11/springmvc1/process.png" class="" title="执行流程"><h2 id="1-常用注解"><a href="#1-常用注解" class="headerlink" title="1. 常用注解"></a>1. 常用注解</h2><p><code>@RequestMapping</code>:建立请求URL和处理方法之间的对应关系,作用在类或者方法上。</p><p><code>@RequestParam</code>:把请求中的指定名称的参数传递给控制器中的形参赋值</p><p>举例:</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RequestMapping</span>(<span class="string">"/useRequestParam"</span>)</span><br><span class="line"><span class="function"><span class="keyword">public</span> String <span class="title">useRequestParam</span><span class="params">(@RequestParam(<span class="string">"name"</span>)</span>String username,</span></span><br><span class="line"><span class="function"> @<span class="title">RequestParam</span><span class="params">(value=<span class="string">"age"</span>,required=<span class="keyword">false</span>)</span> Integer age)</span>{</span><br><span class="line"> System.out.println(username+<span class="string">","</span>+age);</span><br><span class="line"> <span class="keyword">return</span> <span class="string">"success"</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><code>@RequestBody</code>:直接以<code>key=value</code>的形式获取请求体内容(<code>String</code>类型)或者<code>Java</code>对象。用于<code>POST</code>请求。</p><p><code>@PathVaribale</code>:用于绑定 url中的占位符。例如:请求 url 中 <code>/delete/{id}</code>,这个<code>{id}</code>就是 url 占位符。</p><p><code>@ModelAttribute</code>:出现在方法上,表示当前方法会在控制器方法之前执行。也可以出现在参数上,获取指定的数据给参数赋值。</p><p>应用场景:当表单提交数据不是完整的实体类数据时,保证没有提交数据的字段使用数据库对象原来的数据。</p><a id="more"></a><p>举例:</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Controller</span></span><br><span class="line"><span class="meta">@RequestMapping</span>(<span class="string">"modelAtrributionByObject"</span>)</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ModelAtrributionByObject</span> </span>{</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> <span class="meta">@ModelAttribute</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> User <span class="title">showModel</span><span class="params">(String userName)</span> </span>{</span><br><span class="line"> System.out.println(<span class="string">"showModel中接收的参数userName:"</span>+userName);</span><br><span class="line"> <span class="comment">//模拟去数据库查询</span></span><br><span class="line"> User user = findUserByName(userName);</span><br><span class="line"> System.out.println(<span class="string">"执行了 showModel 方法"</span> + user);</span><br><span class="line"> <span class="keyword">return</span> user;</span><br><span class="line"><span class="comment">// return;</span></span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 模拟修改用户方法</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> user</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="meta">@RequestMapping</span>(<span class="string">"/updateUser"</span>)</span><br><span class="line"> <span class="function"><span class="keyword">public</span> String <span class="title">testModelAttribute</span><span class="params">(User user)</span> </span>{</span><br><span class="line"> System.out.println(<span class="string">"控制器中处理请求的方法:修改用户:"</span> + user);</span><br><span class="line"> <span class="keyword">return</span> <span class="string">"success"</span>;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 模拟去数据库查询</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> userName</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="function"><span class="keyword">private</span> User <span class="title">findUserByName</span><span class="params">(String userName)</span> </span>{</span><br><span class="line"> User user = <span class="keyword">new</span> User();</span><br><span class="line"> user.setUserName(<span class="string">"数据库中的姓名"</span>);</span><br><span class="line"> user.setAge(<span class="number">19</span>);</span><br><span class="line"> user.setPassword(<span class="string">"123456"</span>);</span><br><span class="line"> <span class="keyword">return</span> user;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Controller</span></span><br><span class="line"><span class="meta">@RequestMapping</span>(<span class="string">"modelAtrributionByMap"</span>)</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ModelAtrributionByMap</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="meta">@ModelAttribute</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">showModel</span><span class="params">(String userName, Map<String, User> map)</span> </span>{</span><br><span class="line"> <span class="comment">//模拟去数据库查询</span></span><br><span class="line"> User user = findUserByName(userName);</span><br><span class="line"> System.out.println(<span class="string">"执行了 showModel 方法"</span> + user);</span><br><span class="line"> map.put(<span class="string">"abc"</span>, user);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 模拟修改用户方法</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> user</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="meta">@RequestMapping</span>(<span class="string">"/updateUser"</span>)</span><br><span class="line"> <span class="function"><span class="keyword">public</span> String <span class="title">testModelAttribute</span><span class="params">(@ModelAttribute(<span class="string">"abc"</span>)</span> User user) </span>{</span><br><span class="line"> System.out.println(<span class="string">"控制器中处理请求的方法:修改用户:"</span> + user);</span><br><span class="line"> <span class="keyword">return</span> <span class="string">"success"</span>;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">private</span> User <span class="title">findUserByName</span><span class="params">(String userName)</span> </span>{</span><br><span class="line"> User user = <span class="keyword">new</span> User();</span><br><span class="line"> user.setUserName(userName);</span><br><span class="line"> user.setAge(<span class="number">19</span>);</span><br><span class="line"> user.setPassword(<span class="string">"123456"</span>);</span><br><span class="line"> <span class="keyword">return</span> user;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><code>@SessionAttribute</code>:用于多次执行控制器方法间的参数共享。 <code>value</code>为存入属性的名称,<code>type</code>为存入属性的类型。</p><h2 id="2-数据响应"><a href="#2-数据响应" class="headerlink" title="2.数据响应"></a>2.数据响应</h2><h3 id="1-返回字符串"><a href="#1-返回字符串" class="headerlink" title="1. 返回字符串"></a>1. 返回字符串</h3><p><code>controller</code> 方法返回字符串可以指定逻辑视图名,通过视图解析器解析为物理视图地址。 </p><p>视图解析器的配置:</p><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"><!-- 配置视图解析器 --></span></span><br><span class="line"> <span class="tag"><<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"viewResolver"</span> <span class="attr">class</span>=<span class="string">"org.springframework.web.servlet.view.InternalResourceViewResolver"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"prefix"</span> <span class="attr">value</span>=<span class="string">"/WEB-INF/pages/"</span>></span><span class="tag"></<span class="name">property</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"suffix"</span> <span class="attr">value</span>=<span class="string">".jsp"</span>></span><span class="tag"></<span class="name">property</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">bean</span>></span></span><br></pre></td></tr></table></figure><h3 id="2-返回空值(使用原生Servlet)"><a href="#2-返回空值(使用原生Servlet)" class="headerlink" title="2. 返回空值(使用原生Servlet)"></a>2. 返回空值(使用原生Servlet)</h3><ol><li>转发<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">request.getRequestDispatcher(<span class="string">"/WEB-INF/pages/success.jsp"</span>).forward(request,response);</span><br></pre></td></tr></table></figure>(此外,如果不使用原生<code>Servlet</code>,<code>SpringMVC</code>也提供了转发的方式)<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">return</span> <span class="string">"forward:/WEB-INF/pages/success.jsp"</span>;</span><br></pre></td></tr></table></figure></li><li>重定向<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">response.sendRedirect(request.getContextPath()+<span class="string">"/index.jsp"</span>);</span><br></pre></td></tr></table></figure>(此外,如果不使用原生<code>Servlet</code>,<code>SpringMVC</code>也提供了重定向的方式)<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">return</span> <span class="string">""</span>redirect:/index.jsp<span class="string">";</span></span><br></pre></td></tr></table></figure></li><li>通过<code>response</code>指定响应结果<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">response.setCharacterEncoding(<span class="string">"utf-8"</span>);</span><br><span class="line">response.setContentType(<span class="string">"application/json;charset=utf-8"</span>);</span><br><span class="line">response.getWriter().print(<span class="string">"你好"</span>);</span><br></pre></td></tr></table></figure></li></ol><h3 id="3-返回ModelAndView"><a href="#3-返回ModelAndView" class="headerlink" title="3. 返回ModelAndView"></a>3. 返回ModelAndView</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//加入数据</span></span><br><span class="line">modelAndView.addObject(<span class="string">"users"</span>,users);</span><br><span class="line">指定跳转页面的名字</span><br><span class="line">modelAndView.setViewName(<span class="string">"list"</span>);</span><br><span class="line"><span class="keyword">return</span> modelAndView;</span><br></pre></td></tr></table></figure><h3 id="4-返回Json(常用于前后端分离项目)"><a href="#4-返回Json(常用于前后端分离项目)" class="headerlink" title="4. 返回Json(常用于前后端分离项目)"></a>4. 返回Json(常用于前后端分离项目)</h3><p>使用<code>ResponseBody</code>注解,将<code>Java</code>对象转化为<code>Json</code>字符串</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RequestMapping</span>(<span class="string">"/testAjax"</span>)</span><br><span class="line"><span class="meta">@ResponseBody</span> </span><br><span class="line"><span class="function"><span class="keyword">public</span> User <span class="title">testAjax</span><span class="params">(@RequestBody User user)</span></span>{</span><br><span class="line"> System.out.println(<span class="string">"testAjax方法执行了...."</span>);</span><br><span class="line"> System.out.println(user);</span><br><span class="line"></span><br><span class="line"> <span class="comment">//做响应</span></span><br><span class="line"> user.setUsername(<span class="string">"feng"</span>);</span><br><span class="line"> user.setMoney(<span class="number">1000f</span>);</span><br><span class="line"> <span class="comment">//返回</span></span><br><span class="line"> <span class="keyword">return</span> user;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>]]></content>
<categories>
<category> Spring </category>
</categories>
<tags>
<tag> Spring </tag>
<tag> Java </tag>
<tag> SpringMVC </tag>
</tags>
</entry>
<entry>
<title>Mybatis笔记</title>
<link href="/2020/11/11/mybatis-note/"/>
<url>/2020/11/11/mybatis-note/</url>
<content type="html"><![CDATA[<h1 id="MyBatis笔记"><a href="#MyBatis笔记" class="headerlink" title="MyBatis笔记"></a>MyBatis笔记</h1><h2 id="0-概述"><a href="#0-概述" class="headerlink" title="0. 概述"></a>0. 概述</h2><p><code>Mybatis</code>是一个优秀的基于<code>Java</code>的持久层框架,它内部封装了<code>jdbc</code>,使开发者只需要关注sql语句本身,而不需要花费精力去处理加载驱动、创建连接、创建 <code>statement</code>等繁杂的过程。</p><p><strong>官方网站</strong>:<a href="https://mybatis.org/mybatis-3/zh/index.html" target="_blank" rel="noopener">https://mybatis.org/mybatis-3/zh/index.html</a></p><p><code>Mybatis</code>通过<code>xml</code>或注解的方式将要执行的各种<code>statement</code>配置起来,并通过<code>Java</code>对象和<code>statement</code> 中 <code>sql</code>的动态参数进行映射生成最终执行的 <code>sql</code>语句,最后由 <code>Mybatis</code> 框架执行 <code>sql</code>并将结果映射为 <code>java</code> 对象并 返回。</p><h2 id="1-配置文件"><a href="#1-配置文件" class="headerlink" title="1. 配置文件"></a>1. 配置文件</h2><h2 id="1-主配置文件"><a href="#1-主配置文件" class="headerlink" title="1. 主配置文件"></a>1. 主配置文件</h2><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"><?xml version="1.0" encoding="UTF-8"?></span></span><br><span class="line"><span class="meta"><!DOCTYPE <span class="meta-keyword">configuration</span></span></span><br><span class="line"><span class="meta"> <span class="meta-keyword">PUBLIC</span> <span class="meta-string">"-//mybatis.org//DTD Config 3.0//EN"</span></span></span><br><span class="line"><span class="meta"> <span class="meta-string">"http://mybatis.org/dtd/mybatis-3-config.dtd"</span>></span></span><br><span class="line"><span class="tag"><<span class="name">configuration</span>></span></span><br><span class="line"> <span class="comment"><!--引入外部的properties文件,指定一些属性值--></span></span><br><span class="line"> <span class="tag"><<span class="name">properties</span> <span class="attr">resource</span>=<span class="string">"jdbcConfig.properties"</span>></span><span class="tag"></<span class="name">properties</span>></span></span><br><span class="line"> <span class="comment"><!--取类的别名--></span></span><br><span class="line"> <span class="tag"><<span class="name">typeAliases</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">typeAlias</span> <span class="attr">type</span>=<span class="string">"com.billychen.domain.User"</span> <span class="attr">alias</span>=<span class="string">"User"</span>></span><span class="tag"></<span class="name">typeAlias</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">typeAlias</span> <span class="attr">type</span>=<span class="string">"com.billychen.domain.Account"</span> <span class="attr">alias</span>=<span class="string">"Account"</span>></span><span class="tag"></<span class="name">typeAlias</span>></span></span><br><span class="line"> <span class="comment"><!--以下是批量别名方式,直接给出扫描包下的所有类,别名为类名--></span></span><br><span class="line"> <span class="tag"><<span class="name">package</span> <span class="attr">name</span>=<span class="string">"com.billychen.domain"</span>/></span> </span><br><span class="line"> <span class="tag"></<span class="name">typeAliases</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="comment"><!--配置环境--></span></span><br><span class="line"> <span class="tag"><<span class="name">environments</span> <span class="attr">default</span>=<span class="string">"mysql"</span>></span></span><br><span class="line"> <span class="comment"><!--配置mysql环境--></span></span><br><span class="line"> <span class="tag"><<span class="name">environment</span> <span class="attr">id</span>=<span class="string">"mysql"</span>></span></span><br><span class="line"> <span class="comment"><!--配置事务类型--></span></span><br><span class="line"> <span class="tag"><<span class="name">transactionManager</span> <span class="attr">type</span>=<span class="string">"JDBC"</span>></span><span class="tag"></<span class="name">transactionManager</span>></span></span><br><span class="line"> <span class="comment"><!--配置数据源(连接池)--></span></span><br><span class="line"> <span class="tag"><<span class="name">dataSource</span> <span class="attr">type</span>=<span class="string">"POOLED"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"driver"</span> <span class="attr">value</span>=<span class="string">"${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">"url"</span> <span class="attr">value</span>=<span class="string">"${jdbc.url}"</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">"${jdbc.username}"</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">"${jdbc.password}"</span>/></span></span><br><span class="line"> <span class="tag"></<span class="name">dataSource</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">environment</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">environments</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="tag"><<span class="name">mappers</span>></span></span><br><span class="line"> <span class="comment"><!--<mapper resource="com/billychen/dao/IUserDao.xml"></mapper>--></span></span><br><span class="line"> <span class="comment"><!--指定dao接口所在的包,不需要再写mapper标签了--></span></span><br><span class="line"> <span class="tag"><<span class="name">package</span> <span class="attr">name</span>=<span class="string">"com.billychen.dao"</span>></span><span class="tag"></<span class="name">package</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">mappers</span>></span></span><br><span class="line"><span class="tag"></<span class="name">configuration</span>></span></span><br></pre></td></tr></table></figure><a id="more"></a><h2 id="2-和Dao类对应的XML文件"><a href="#2-和Dao类对应的XML文件" class="headerlink" title="2.和Dao类对应的XML文件"></a>2.和Dao类对应的XML文件</h2><p>此处给出一个例子,给出<code>IUserDao.java</code>和<code>IUserDao.xml</code>文件</p><p>以下是<code>Java</code>代码</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">IUserDao</span> </span>{</span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 查询所有,同时获取到用户的所有账户</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="function">List<User> <span class="title">findAll</span><span class="params">()</span></span>;</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 根据id查询用户</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> id</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="function">User <span class="title">findById</span><span class="params">(Integer id)</span></span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>以下是对应的配置文件</p><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"><?xml version="1.0" encoding="UTF-8" ?></span></span><br><span class="line"><span class="meta"><!DOCTYPE <span class="meta-keyword">mapper</span></span></span><br><span class="line"><span class="meta"> <span class="meta-keyword">PUBLIC</span> <span class="meta-string">"-//mybatis.org//DTD Mapper 3.0//EN"</span></span></span><br><span class="line"><span class="meta"> <span class="meta-string">"http://mybatis.org/dtd/mybatis-3-mapper.dtd"</span>></span></span><br><span class="line"><span class="tag"><<span class="name">mapper</span> <span class="attr">namespace</span>=<span class="string">"com.billychen.dao.IUserDao"</span>></span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> <span class="comment"><!--resultMap标签用于实体类属性名和列名的对应--></span></span><br><span class="line"> <span class="comment"><!--如果使用resultMap 之后的resultType也要改为resultType--></span></span><br><span class="line"> <span class="comment"><!--<resultMap id="userMap" type="com.billychen.domain.User">--></span></span><br><span class="line"> <span class="comment"><!--</resultMap>--></span></span><br><span class="line"> <span class="tag"><<span class="name">resultMap</span> <span class="attr">id</span>=<span class="string">"userAccountMap"</span> <span class="attr">type</span>=<span class="string">"User"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">id</span> <span class="attr">property</span>=<span class="string">"id"</span> <span class="attr">column</span>=<span class="string">"id"</span>></span><span class="tag"></<span class="name">id</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">result</span> <span class="attr">property</span>=<span class="string">"username"</span> <span class="attr">column</span>=<span class="string">"username"</span>></span><span class="tag"></<span class="name">result</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">result</span> <span class="attr">property</span>=<span class="string">"birthday"</span> <span class="attr">column</span>=<span class="string">"birthday"</span>></span><span class="tag"></<span class="name">result</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">result</span> <span class="attr">property</span>=<span class="string">"sex"</span> <span class="attr">column</span>=<span class="string">"sex"</span>></span><span class="tag"></<span class="name">result</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">result</span> <span class="attr">property</span>=<span class="string">"address"</span> <span class="attr">column</span>=<span class="string">"address"</span>></span><span class="tag"></<span class="name">result</span>></span></span><br><span class="line"> <span class="comment"><!--配置User对象中accounts集合的映射--></span></span><br><span class="line"> <span class="comment"><!--ofType是集合中元素的类型--></span></span><br><span class="line"> <span class="comment"><!--一对一用association,一对多用collection--></span></span><br><span class="line"> <span class="tag"><<span class="name">collection</span> <span class="attr">property</span>=<span class="string">"accounts"</span> <span class="attr">ofType</span>=<span class="string">"Account"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">id</span> <span class="attr">property</span>=<span class="string">"id"</span> <span class="attr">column</span>=<span class="string">"aid"</span>></span><span class="tag"></<span class="name">id</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">result</span> <span class="attr">property</span>=<span class="string">"uid"</span> <span class="attr">column</span>=<span class="string">"UID"</span>></span><span class="tag"></<span class="name">result</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">result</span> <span class="attr">property</span>=<span class="string">"money"</span> <span class="attr">column</span>=<span class="string">"MONEY"</span>></span><span class="tag"></<span class="name">result</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">collection</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">resultMap</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="tag"><<span class="name">select</span> <span class="attr">id</span>=<span class="string">"findAll"</span> <span class="attr">resultMap</span>=<span class="string">"userAccountMap"</span>></span></span><br><span class="line"> select user.*,account.ID as aid,account.UID,account.MONEY from user left outer join account on account.UID=user.id</span><br><span class="line"> <span class="tag"></<span class="name">select</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="tag"><<span class="name">select</span> <span class="attr">id</span>=<span class="string">"findById"</span> <span class="attr">resultType</span>=<span class="string">"com.billychen.domain.User"</span> <span class="attr">parameterType</span>=<span class="string">"java.lang.Integer"</span>></span></span><br><span class="line"> select * from user where id=#{id};</span><br><span class="line"> <span class="tag"></<span class="name">select</span>></span></span><br><span class="line"><span class="tag"></<span class="name">mapper</span>></span></span><br></pre></td></tr></table></figure><h2 id="3-延迟加载"><a href="#3-延迟加载" class="headerlink" title="3. 延迟加载"></a>3. 延迟加载</h2><p>查询账户(<code>Account</code>)信息并且关联查询用户(<code>User</code>)信息。如果先查询账户(<code>Account</code>)信息即可满足要 求,当我们需要查询用户(<code>User</code>)信息时再查询用户(<code>User</code>)信息。把对用户(<code>User</code>)信息的按需去查询就是延迟加载。</p><p>比如,在查询账户时,需要连带查询其所属的用户。如果使用延迟加载,那么在需要用到用户的时候才进行查询。具体的配置在配置文件<code>association</code>标签中修改如下:</p><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">association</span> <span class="attr">property</span>=<span class="string">"user"</span> <span class="attr">javaType</span>=<span class="string">"User"</span> <span class="attr">select</span>=<span class="string">"com.billychen.dao.IUserDao.findById"</span> <span class="attr">column</span>=<span class="string">"uid"</span>></span> <span class="tag"></<span class="name">association</span>></span></span><br></pre></td></tr></table></figure><p>其含义是,根据用户的<code>uid</code>这一列的值,去查询所属账户的<code>id</code>。</p><p>为了使延迟加载生效,还需要在主配置文件中配置</p><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">settings</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"lazyLoadingEnabled"</span> <span class="attr">value</span>=<span class="string">"true"</span>/></span></span><br><span class="line"><span class="tag"></<span class="name">settings</span>></span></span><br></pre></td></tr></table></figure><h2 id="4-一级缓存和二级缓存"><a href="#4-一级缓存和二级缓存" class="headerlink" title="4. 一级缓存和二级缓存"></a>4. 一级缓存和二级缓存</h2><h3 id="1-一级缓存"><a href="#1-一级缓存" class="headerlink" title="1. 一级缓存"></a>1. 一级缓存</h3><p>一级缓存是 <code>SqlSession</code> 范围的缓存,当调用 <code>SqlSession</code> 的修改,添加,删除,<code>commit()</code>,<code>close()</code>等方法时,就会清空一级缓存</p><p>比如:</p><p>第一次发起查询用户 <code>id</code> 为 1 的用户信息,先去找缓存中是否有 <code>id</code> 为 1 的用户信息,如果没有,从数据库查询用户信息。</p><p>得到用户信息,将用户信息存储到一级缓存中。</p><p>如果 <code>sqlSession</code> 去执行 <code>commi</code>t 操作(执行插入、更新、删除),清空 <code>SqlSession</code> 中的一级缓存,这样 做的目的为了让缓存中存储的是最新的信息,避免脏读。 第二次发起查询用户 <code>id</code> 为 1</p><p>的用户信息,先去找缓存中是否有 <code>id</code> 为 1 的用户信息,缓存中有,直接从缓存 中获取用户信息。</p><h3 id="2-二级缓存"><a href="#2-二级缓存" class="headerlink" title="2. 二级缓存"></a>2. 二级缓存</h3><p>二级缓存是 <code>mapper</code> 映射级别的缓存,多个 <code>SqlSession</code> 去操作同一个 <code>Mapper</code> 映射的 <code>sql</code> 语句,多个 <code>SqlSession</code> 可以共用二级缓存,二级缓存是跨 <code>SqlSession</code> 的。 </p><p>开启二级缓存:</p><ol><li>在<code>SqlMapConfig.xml</code>中开启<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">settings</span>></span> </span><br><span class="line"> <span class="comment"><!-- 开启二级缓存的支持 --></span> </span><br><span class="line"> <span class="tag"><<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"cacheEnabled"</span> <span class="attr">value</span>=<span class="string">"true"</span>/></span></span><br><span class="line"><span class="tag"></<span class="name">settings</span>></span></span><br></pre></td></tr></table></figure></li><li>配置响应的<code>mapper</code>文件<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">mapper</span> <span class="attr">namespace</span>=<span class="string">"com.itheima.dao.IUserDao"</span>></span></span><br><span class="line"> <span class="comment"><!-- 开启二级缓存的支持 --></span> </span><br><span class="line"> <span class="tag"><<span class="name">cache</span>></span><span class="tag"></<span class="name">cache</span>></span></span><br><span class="line"><span class="tag"></<span class="name">mapper</span>></span></span><br></pre></td></tr></table></figure></li><li>配置<code>statement</code>上的<code>useCache</code>属性<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">select</span> <span class="attr">id</span>=<span class="string">"findById"</span> <span class="attr">resultType</span>=<span class="string">"user"</span> <span class="attr">parameterType</span>=<span class="string">"int"</span> <span class="attr">useCache</span>=<span class="string">"true"</span>></span> </span><br><span class="line"> select * from user where id = #{uid} </span><br><span class="line"><span class="tag"></<span class="name">select</span>></span></span><br></pre></td></tr></table></figure></li></ol>]]></content>
<tags>
<tag> Spring </tag>
<tag> Mybatis </tag>
</tags>
</entry>
<entry>
<title>Spring——事务</title>
<link href="/2020/09/21/spring-tx/"/>
<url>/2020/09/21/spring-tx/</url>
<content type="html"><![CDATA[<h1 id="Spring——事务"><a href="#Spring——事务" class="headerlink" title="Spring——事务"></a>Spring——事务</h1><h2 id="1-基于XML的声明式事务控制"><a href="#1-基于XML的声明式事务控制" class="headerlink" title="1. 基于XML的声明式事务控制"></a>1. 基于XML的声明式事务控制</h2><ol><li><p>配置事务管理器</p></li><li><p>配置事务的通知</p><p> 属性<br> <code>id</code>: 事务通知的唯一标识<br> <code>transaction-manager</code>给事务通知提供一个事务管理器的引用</p></li><li><p>配置AOP中的切入点表达式</p></li><li><p>建立事务通知和切入点表达式的关系</p></li><li><p>配置事务的属性(在事务通知tx:advice标签内部配置)</p></li></ol><a id="more"></a><blockquote><p><code>isolation</code>:事务隔离界别,默认值<code>DEFAULT</code>,表示使用数据库的默认隔离级别</p><p><code>propagation</code>:指定传播行为默认值为<code>REQUIRED</code>,表示一定有事务(增删改的选择,而查询方法可选择<code>SUPPORTS</code>)</p><p><code>read-only</code>:指定事务是否只读,只有查询方法才能设置为<code>true</code>,默认值为<code>false</code><br><code>timeout</code>:指定事务的超时时间 默认值-1 永不超时</p><p><code>rollback-for</code>:指定一个异常,产生该异常时事务回滚,产生其他异常时不回滚。没有默认值,表示任何异常都回滚</p><p><code>no-rollback-for</code>:指定一个异常,产生该异常时事务不回滚,产生其他异常时事务回滚。没有默认值,表示任何异常都回滚</p></blockquote><p>以下是一个配置的例子</p><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"transactionManager"</span> <span class="attr">class</span>=<span class="string">"org.springframework.jdbc.datasource.DataSourceTransactionManager"</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 class="tag"></<span class="name">property</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">tx:advice</span> <span class="attr">id</span>=<span class="string">"txAdvice"</span> <span class="attr">transaction-manager</span>=<span class="string">"transactionManager"</span>></span></span><br><span class="line"> <span class="comment"><!--配置事务属性--></span></span><br><span class="line"> <span class="tag"><<span class="name">tx:attributes</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">tx:method</span> <span class="attr">name</span>=<span class="string">"*"</span> <span class="attr">propagation</span>=<span class="string">"REQUIRED"</span> <span class="attr">read-only</span>=<span class="string">"false"</span>/></span></span><br><span class="line"> <span class="comment"><!--通配符指以find开头的所有方法--></span></span><br><span class="line"> <span class="tag"><<span class="name">tx:method</span> <span class="attr">name</span>=<span class="string">"find*"</span> <span class="attr">propagation</span>=<span class="string">"SUPPORTS"</span> <span class="attr">read-only</span>=<span class="string">"true"</span>/></span></span><br><span class="line"> <span class="tag"></<span class="name">tx:attributes</span>></span></span><br><span class="line"><span class="tag"></<span class="name">tx:advice</span>></span></span><br><span class="line"></span><br><span class="line"><span class="tag"><<span class="name">aop:config</span>></span></span><br><span class="line"> <span class="comment"><!--配置切入点表达式--></span></span><br><span class="line"> <span class="tag"><<span class="name">aop:pointcut</span> <span class="attr">id</span>=<span class="string">"pt1"</span> <span class="attr">expression</span>=<span class="string">"execution(* com.billychen.service.impl.*.*(..))"</span>></span><span class="tag"></<span class="name">aop:pointcut</span>></span></span><br><span class="line"> <span class="comment"><!--建立切入点和事务通知的关系--></span></span><br><span class="line"> <span class="tag"><<span class="name">aop:advisor</span> <span class="attr">advice-ref</span>=<span class="string">"txAdvice"</span> <span class="attr">pointcut-ref</span>=<span class="string">"pt1"</span>></span><span class="tag"></<span class="name">aop:advisor</span>></span></span><br><span class="line"><span class="tag"></<span class="name">aop:config</span>></span></span><br></pre></td></tr></table></figure><h2 id="2-基于注解的配置"><a href="#2-基于注解的配置" class="headerlink" title="2.基于注解的配置"></a>2.基于注解的配置</h2><ol><li>配置事务管理器</li><li>开启<code>Spring</code>对注解事务的支持<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">tx:annotation-driven</span> <span class="attr">transaction-manager</span>=<span class="string">"transactionManager"</span>></span><span class="tag"></<span class="name">tx:annotation-driven</span>></span></span><br></pre></td></tr></table></figure></li><li>在需要支持事务的地方(类或方法前)使用<code>@Transactional</code>注解。注解中的属性可以设置事务的属性(隔离级别、传播行为、只读等)</li></ol><h2 id="3-事务隔离级别和传播行为"><a href="#3-事务隔离级别和传播行为" class="headerlink" title="3.事务隔离级别和传播行为"></a>3.事务隔离级别和传播行为</h2><h3 id="1-隔离级别"><a href="#1-隔离级别" class="headerlink" title="1. 隔离级别"></a>1. 隔离级别</h3><ol><li>未提交读:可以读取未提交的数据</li><li>读写提交:只能读取已提交的数据,解决脏读问题(<code>Oracle</code>默认级别)</li><li>可重复读:只能读取其他事务提交修改后的数据,解决不可重复读问题(<code>MySQL</code>默认级别)</li><li>串行化:解决幻读问题</li></ol><h2 id="2-传播行为"><a href="#2-传播行为" class="headerlink" title="2.传播行为"></a>2.传播行为</h2><ol><li><code>REQUIRED</code>:如果当前没有事务,就新建一个事务,如果已经存在一个事务中,加入到这个事务中。一般的选择(默认值)</li><li><code>SUPPORTS</code>:支持当前事务,如果当前没有事务,就以非事务方式执行(没有事务)</li><li><code>MANDATORY</code>:使用当前的事务,如果当前没有事务,就抛出异常</li><li><code>REQUERS_NEW</code>:新建事务,如果当前在事务中,把当前事务挂起。</li><li><code>NOT_SUPPORTED</code>:以非事务方式执行操作,如果当前存在事务,就把当前事务挂起</li><li><code>NEVER</code>:以非事务方式运行,如果当前存在事务,抛出异常</li><li><code>NESTED</code>:如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则执行 <code>REQUIRED</code> 类似的操作。</li></ol>]]></content>
<categories>
<category> Spring </category>
</categories>
<tags>
<tag> Spring </tag>
<tag> Java </tag>
</tags>
</entry>
<entry>
<title>Spring——AOP</title>
<link href="/2020/09/17/spring-aop/"/>
<url>/2020/09/17/spring-aop/</url>
<content type="html"><![CDATA[<h1 id="Spring-AOP"><a href="#Spring-AOP" class="headerlink" title="Spring AOP"></a>Spring AOP</h1><h2 id="0-思想"><a href="#0-思想" class="headerlink" title="0. 思想"></a>0. 思想</h2><p>运用动态代理的方式对核心业务类的方法进行增强,对核心逻辑和非核心逻辑进行隔离,把非核心逻辑织入到核心业务逻辑之中。开发人员在开发时可以专注于核心业务逻辑,使各个业务逻辑之间的耦合度降低,提高程序的可复用性。</p><h2 id="1-AOP的术语和细节"><a href="#1-AOP的术语和细节" class="headerlink" title="1. AOP的术语和细节"></a>1. AOP的术语和细节</h2><ol><li><p><code>Jointpoint</code>(连接点)</p><p> 被拦截的方法,通常是业务层的所有方法。</p></li><li><p><code>Pointcut</code>(切入点)</p><p> 并不是所有业务层的方法都需要被拦截增强。那些需要被增强的方法是切入点。</p></li><li><p><code>Advice</code>(通知/增强)</p><p> 指拦截到的<code>Jointpoint</code>之后需要做的事情。通知包括前置通知、后置通知、异常通知、最终通知、环绕通知。</p></li><li><p><code>Taeget</code>(目标对象)</p><p> 被代理的对象。</p></li><li><p><code>Weaving</code>(织入)</p><p> 是指把增强应用到目标对象来创建代理对象的过程。</p></li><li><p><code>Proxy</code>(代理)</p><p> 一个类被<code>AOP</code>织入增强后,产生的一个结果代理类。</p></li><li><p><code>Aspect</code>(切面)</p><p> 是切入点和通知的结合。(需要手动配置)</p></li></ol><a id="more"></a><h2 id="2-XML配置AOP步骤"><a href="#2-XML配置AOP步骤" class="headerlink" title="2. XML配置AOP步骤"></a>2. XML配置AOP步骤</h2><ol><li><p>把通知<code>bean</code>交给<code>spring</code>管理</p></li><li><p>使用<code>app:config</code>标签表明开始<code>AOP</code>配置</p></li><li><p>使用<code>aop:aspect</code>标签表明开始配置切面</p><p> <code>id</code>:切面的唯一标识<br> <code>ref</code>:指定通知类bean的id</p></li><li><p>在<code>aop:aspect</code>标签内部使用对应标签来配置通知的类型,比如<code>aop:before</code>表示配置前置通知。<br><code>method</code>属性用于指定通知类中哪个方法是前置通知<br><code>pointcut</code>属性用于指定切入点表达式,表示对业务层哪些方法增强</p></li><li><p>切入点表达式写法:</p><ul><li><p>关键字:<code>execution(表达式)</code></p></li><li><p>表达式:访问修饰符 返回值 全限定类名.方法名(参数列表)</p></li><li><p>标准写法:<code>public void com.billychen.service.impl.AccountServiceImpl.saveAccount()</code></p></li><li><p>访问修饰符可以省略 <code>void com.billychen.service.impl.AccountServiceImpl.saveAccount()</code></p></li><li><p>返回值可以使用通配符,表示任意返回值 <code>* com.billychen.service.impl.AccountServiceImpl.saveAccount()</code></p></li><li><p>包名可以使用通配符表示任意包,但是有几个包就要写几个<code>*.</code> 比如<code>* *.*.*.*.AccountServiceImpl.saveAccount()</code></p></li><li><p>包名可以使用..表示当前包及其子包 <code>* *..AccountServiceImpl.saveAccount()</code></p></li><li><p>类名和方法名均可以使用<code>*</code>实现通配</p></li><li><p>参数列表写数据类型或全限定类名</p></li><li><p>可以使用通配符表示任意参数类型,但必须有参数。也可以使用<code>..</code>表示有无参数均可以<br>全通配写法:<code>* *..*.*(..)</code></p></li><li><p>实际开发的通常写法是切到业务层实现类的所有方法<code>* com.billychen.service.impl.*.*(..)</code></p></li></ul></li></ol><p>以下是一个实例:</p><p>在该实例中,<code>Logger</code>是一个通知类,<code>AccountServiceImpl</code>是一个核心业务的类。</p><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"><?xml version="1.0" encoding="UTF-8"?></span></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">xmlns:aop</span>=<span class="string">"http://www.springframework.org/schema/aop"</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</span></span></span><br><span class="line"><span class="tag"><span class="string"> https://www.springframework.org/schema/beans/spring-beans.xsd</span></span></span><br><span class="line"><span class="tag"><span class="string"> http://www.springframework.org/schema/aop</span></span></span><br><span class="line"><span class="tag"><span class="string"> https://www.springframework.org/schema/aop/spring-aop.xsd"</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="comment"><!--配置service对象--></span></span><br><span class="line"> <span class="tag"><<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"accountService"</span> <span class="attr">class</span>=<span class="string">"com.billychen.service.impl.AccountServiceImpl"</span>></span></span><br><span class="line"> <span class="comment"><!-- instructs the container to proxy the surrounding bean --></span></span><br><span class="line"> <span class="tag"><<span class="name">aop:scoped-proxy</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="comment"><!--配置Logger对象--></span></span><br><span class="line"> <span class="tag"><<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"logger"</span> <span class="attr">class</span>=<span class="string">"com.billychen.utils.Logger"</span>></span><span class="tag"></<span class="name">bean</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="comment"><!--配置AOP--></span></span><br><span class="line"> <span class="tag"><<span class="name">aop:config</span>></span></span><br><span class="line"> <span class="comment"><!--配置切面--></span></span><br><span class="line"> <span class="tag"><<span class="name">aop:aspect</span> <span class="attr">id</span>=<span class="string">"logAdvice"</span> <span class="attr">ref</span>=<span class="string">"logger"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">aop:before</span> <span class="attr">method</span>=<span class="string">"beforePrintLog"</span> <span class="attr">pointcut-ref</span>=<span class="string">"ptl"</span>></span><span class="tag"></<span class="name">aop:before</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">aop:after-returning</span> <span class="attr">method</span>=<span class="string">"afterReturningPrintLog"</span> <span class="attr">pointcut-ref</span>=<span class="string">"ptl"</span>></span><span class="tag"></<span class="name">aop:after-returning</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">aop:after-throwing</span> <span class="attr">method</span>=<span class="string">"afterThrowingPrintLog"</span> <span class="attr">pointcut-ref</span>=<span class="string">"ptl"</span>></span><span class="tag"></<span class="name">aop:after-throwing</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">aop:after</span> <span class="attr">method</span>=<span class="string">"afterPrintLog"</span> <span class="attr">pointcut-ref</span>=<span class="string">"ptl"</span>></span><span class="tag"></<span class="name">aop:after</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="comment"><!--仅限当前切面使用</span></span><br><span class="line"><span class="comment"> 如果写在切面之外,则可以供所有切面使用(要写在切面定义之前)--></span></span><br><span class="line"> <span class="tag"><<span class="name">aop:pointcut</span> <span class="attr">id</span>=<span class="string">"ptl"</span> <span class="attr">expression</span>=<span class="string">"execution(* com.billychen.service.impl.*.*(..))"</span>></span><span class="tag"></<span class="name">aop:pointcut</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">aop:aspect</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">aop:config</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><h2 id="3-使用注解"><a href="#3-使用注解" class="headerlink" title="3.使用注解"></a>3.使用注解</h2><p>配置类如下:</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.billychen.config;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.springframework.context.annotation.ComponentScan;</span><br><span class="line"><span class="keyword">import</span> org.springframework.context.annotation.Configuration;</span><br><span class="line"><span class="keyword">import</span> org.springframework.context.annotation.EnableAspectJAutoProxy;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 配置类</span></span><br><span class="line"><span class="comment"> * 开启AOP注解</span></span><br><span class="line"><span class="comment"> * 指定创建IoC容器时需要扫描的包</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@EnableAspectJAutoProxy</span></span><br><span class="line"><span class="meta">@Configuration</span></span><br><span class="line"><span class="meta">@ComponentScan</span>(basePackages = <span class="string">"com.billychen"</span>)</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">SpringConfig</span> </span>{</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><code>Logger</code>类如下(即通知类)</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.billychen.utils;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.aspectj.lang.JoinPoint;</span><br><span class="line"><span class="keyword">import</span> org.aspectj.lang.ProceedingJoinPoint;</span><br><span class="line"><span class="keyword">import</span> org.aspectj.lang.annotation.*;</span><br><span class="line"><span class="keyword">import</span> org.springframework.stereotype.Component;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 模拟记录日志的工具类,提供了共用代码(通知)</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Component</span></span><br><span class="line"><span class="meta">@Aspect</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Logger</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="comment">//指定切入点方法</span></span><br><span class="line"> <span class="meta">@Pointcut</span>(<span class="string">"execution(* com.billychen.service.impl.*.*(..))"</span>)</span><br><span class="line"> <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">ptl</span><span class="params">()</span> </span>{</span><br><span class="line"></span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 前置</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="comment">//可以获取切点方法的参数</span></span><br><span class="line"> <span class="meta">@Before</span>(<span class="string">"ptl() && args(i)"</span>)</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">beforePrintLog</span><span class="params">(JoinPoint joinPoint, <span class="keyword">int</span> i)</span> </span>{</span><br><span class="line"> Object[] args = joinPoint.getArgs();</span><br><span class="line"> System.out.println(<span class="string">"参数为"</span> + args[<span class="number">0</span>]);</span><br><span class="line"> System.out.println(<span class="string">"前置:Logger类中的BeforePrintLog方法开始记录日志!"</span>);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 后置</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="meta">@AfterReturning</span>(<span class="string">"ptl()"</span>)</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">afterReturningPrintLog</span><span class="params">()</span> </span>{</span><br><span class="line"> System.out.println(<span class="string">"后置:Logger类中的AfterReturningPrintLog方法开始记录日志!"</span>);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 异常</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="meta">@AfterThrowing</span>(<span class="string">"ptl()"</span>)</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">afterThrowingPrintLog</span><span class="params">()</span> </span>{</span><br><span class="line"> System.out.println(<span class="string">"异常:Logger类中的AfterThrowingPrintLog方法开始记录日志!"</span>);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 最终</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="meta">@After</span>(<span class="string">"ptl()"</span>)</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">afterPrintLog</span><span class="params">()</span> </span>{</span><br><span class="line"> System.out.println(<span class="string">"最终:Logger类中的AfterPrintLog方法开始记录日志!"</span>);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 环绕</span></span><br><span class="line"><span class="comment"> * ProceedingJoinPoint的方法proceed()可以明确调用切入点的方法</span></span><br><span class="line"><span class="comment"> * 环绕通知 是spring提供的一种可以在代码中手动控制增强方法何时执行的方式</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="comment">//@Around("ptl()")</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> Object <span class="title">aroundPrintLog</span><span class="params">(ProceedingJoinPoint joinPoint)</span> </span>{</span><br><span class="line"> <span class="comment">//明确调用业务层方法</span></span><br><span class="line"> Object returnValue = <span class="keyword">null</span>;</span><br><span class="line"> <span class="keyword">try</span> {</span><br><span class="line"> <span class="comment">//得到方法执行的参数</span></span><br><span class="line"> Object[] args = joinPoint.getArgs();</span><br><span class="line"> System.out.println(<span class="string">"环绕通知——前置"</span>);</span><br><span class="line"> returnValue = joinPoint.proceed(args);</span><br><span class="line"> System.out.println(<span class="string">"环绕通知——后置"</span>);</span><br><span class="line"> <span class="keyword">return</span> returnValue;</span><br><span class="line"> } <span class="keyword">catch</span> (Throwable throwable) {</span><br><span class="line"> System.out.println(<span class="string">"环绕通知——异常"</span>);</span><br><span class="line"> <span class="keyword">throw</span> <span class="keyword">new</span> RuntimeException(throwable);</span><br><span class="line"> } <span class="keyword">finally</span> {</span><br><span class="line"> System.out.println(<span class="string">"环绕通知——最终"</span>);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>]]></content>
<categories>
<category> Spring </category>
</categories>
<tags>
<tag> Spring </tag>
<tag> Java </tag>
</tags>
</entry>
<entry>
<title>Spring——IoC容器</title>
<link href="/2020/09/10/spring-ioc/"/>
<url>/2020/09/10/spring-ioc/</url>
<content type="html"><![CDATA[<h1 id="Spring-IoC容器"><a href="#Spring-IoC容器" class="headerlink" title="Spring IoC容器"></a>Spring IoC容器</h1><h2 id="1-思想"><a href="#1-思想" class="headerlink" title="1.思想"></a>1.思想</h2><p><code>bean</code>对象的创建、依赖关系交给<code>Ioc</code>容器来管理,无需手动<code>new</code>对象,方便解耦,便于开发。</p><h2 id="2-IoC容器"><a href="#2-IoC容器" class="headerlink" title="2.IoC容器"></a>2.IoC容器</h2><p><code>Spring</code>容器的最顶层接口是<code>BeanFactory</code>,实际开发中常用<code>ApplicationContext</code>。</p><p>两个接口的区别:</p><ul><li><code>ApplicationContext</code>:立即加载。读取配置文件后马上创建对象放入容器。</li><li><code>BeanFactory</code>:延迟加载。什么时候根据<code>id</code>获取对象,什么时候才真正创建对象。</li></ul><p><code>ApplicationContext</code>的三个常用实现类</p><ul><li><p><code>ClassPathApplicationContext</code>:加载类路径下的配置文件。</p></li><li><p><code>FileSystemXmlApplicationContext</code>:加载磁盘任意路径下的配置文件。</p></li><li><p><code>AnnotationConfigApplicationContext</code>:读取注解 创建容器。</p></li></ul><h2 id="3-Bean"><a href="#3-Bean" class="headerlink" title="3.Bean"></a>3.Bean</h2><h3 id="1-创建bean的三种方式"><a href="#1-创建bean的三种方式" class="headerlink" title="1.创建bean的三种方式"></a>1.创建bean的三种方式</h3><ol><li>使用默认的无参构造器(如果没有无参构造器,则报错)</li><li>使用静态工厂的静态方法创建对象</li><li>使用实例工厂的方法创建对象</li></ol><a id="more"></a><h3 id="2-bean对象的生命周期"><a href="#2-bean对象的生命周期" class="headerlink" title="2.bean对象的生命周期"></a>2.bean对象的生命周期</h3><ol><li>单例<ul><li>出生:当容器创建时对象出生</li><li>活着:只要容器还在,对象一直活着</li><li>死亡:容器销毁,对象消亡</li><li>总结:单例对象的生命周期与容器相同</li></ul></li><li>多例<ul><li>出生:使用对象时才创建(延迟加载)</li><li>活着:对象只要在使用过程中就一直活着</li><li>死亡:当对象长时间不用且无其他对象引用时,由<code>GC</code>回收</li></ul></li></ol><h3 id="3-依赖注入"><a href="#3-依赖注入" class="headerlink" title="3.依赖注入"></a>3.依赖注入</h3><p> 依赖关系的管理交给Spring来维护在当前类需要用到其他类的对象时,由Spring为我们提供,只需在配置文件中说明。</p><p>依赖注入的数据有三类:</p><ol><li>基本类型和<code>String</code></li><li>其他<code>bean</code>类型(在配置文件中或注解中配置过的bean)</li><li>复杂类型(集合类型)</li></ol><p>注入的方式有三种:</p><ol><li>使用构造方法</li><li>使用<code>set</code>方法</li><li>使用注解</li></ol><h3 id="4-常用注解"><a href="#4-常用注解" class="headerlink" title="4.常用注解"></a>4.常用注解</h3><ol><li>用于创建对象的注解(作用类似于<code>bean</code>标签)</li></ol><ul><li><code>@Component</code></li></ul><blockquote><p><strong>作用</strong>:写在类之前,把当前类的对象注入到容器中</p><p><strong>属性</strong>:value 指定bean的id 默认值是当前类名首字母改为小写</p></blockquote><ul><li><p><code>@Controller</code>:一般用在表现层</p></li><li><p><code>@Serive</code>:一般用在业务层</p></li><li><p><code>@Repository</code>:一般用在持久层</p><p> 以上三个注解的作用与属性和<code>Component</code>一样,<br>他们三个是<code>Spring</code>框架提供的明确的三层使用的注解,使三层对象更加清晰。</p></li></ul><hr><ol start="2"><li>用于注入数据的注解(作用相当于<code>properties</code>标签)</li></ol><ul><li><p><code>@Autowired</code>:</p><blockquote><p><strong>作用</strong>:自动按照类型注入。只要容器中有唯一的一个bean对象类型和要注入的变量类型匹配,就可以注入成功</p><p><strong>使用的位置</strong>:变量上或方法上</p><p><strong>注意</strong>:如果容器中没有任何bean的类型匹配,则报错。<br>如果容器中有多个<code>bean</code>的类型和要注入的变量类型匹配,如果要注入的变量名称和<code>bean</code>的<code>id</code>相匹配,则注入之,否则报错。<br>另外,使用注解的方法注入时,set方法不是必须的</p></blockquote></li><li><p><code>@Qualifier</code></p><blockquote><p><strong>作用</strong>:在按照类中注入的基础上再按名称注入。</p><p><strong>注意</strong>:它给类成员注入时不能单独使用,必须和<code>Autowired</code>配合使用,但是在给方法参数注入时可以单独使用。</p><p><strong>属性</strong>:<code>value</code> 用于指定注入<code>bean</code>的<code>id</code></p></blockquote></li><li><p><code>@Resource</code></p><blockquote><p><strong>作用</strong>:直接按照<code>bean</code>的id注入,可以独立使用,不需要再和<code>Autowired</code>一起使用</p><p><strong>属性</strong>:name 用于指定注入<code>bean</code>的<code>id</code></p><p><strong>注意</strong>:以上三个注解只能注入其他<code>bean</code>类型的数据,而基本类型和<code>String</code>类型无法使用上述注解</p></blockquote></li></ul><p>另外,集合类型注入只能使用xml</p><ul><li><code>@Value</code><blockquote><p><strong>作用</strong>:用于注入基本类型和<code>String</code>类型的数据</p><p><strong>属性</strong>:<code>value</code>:用于指定数据的值</p><p>它可以使用<code>Spring</code>中的<code>SpEL</code>。<code>SpEL</code>写法:<code>${表达式}</code></p></blockquote></li></ul><hr><ol start="3"><li>一些和配置有关的注解</li></ol><ul><li><p>可以使用配置类以代替<code>bean.xml</code>配置文件的作用</p></li><li><p><code>@Configuration</code></p><blockquote><p><strong>作用</strong>:指定当前类是一个配置类</p><p><strong>细节</strong>:当配置类作为<code>AnnotationConfigApplicationContext</code>对象创建的参数时,可以不写。</p></blockquote></li><li><p><code>@ComponentScan</code></p><blockquote><p><strong>作用</strong>:指定创建容器时要扫描的包</p></blockquote></li><li><p><code>@Bean</code></p><blockquote><p><strong>作用</strong>:用在方法前,将方法的返回值作为<code>bean</code>对象存入<code>IoC</code>容器</p><p><strong>属性</strong>:<code>name</code> 用于指定<code>bean</code>的<code>id</code> 默认值为当前方法名</p><p><strong>细节</strong>:使用注解配置方法时,如果方法有参数,<code>Spring</code>框架会去容器中查找是否有该类型的<code>bean</code>,查找方式和<code>Autowired</code>相同</p></blockquote></li><li><p><code>@Import</code></p><blockquote><p><strong>作用</strong>:用于导入其他配置类</p><p><strong>属性</strong>:<code>value</code> 用于指定其他配置类的字节码</p></blockquote></li><li><p><code>@PropertySource</code></p><blockquote><p><strong>作用</strong>:用于指定properties文件的位置</p><p><strong>属性</strong>:<code>value</code> 指定文件名称和路径<br>关键字<code>classpath</code>表示类路径下</p></blockquote></li></ul>]]></content>
<categories>
<category> Spring </category>
</categories>
<tags>
<tag> Spring </tag>
<tag> Java </tag>
</tags>
</entry>
<entry>
<title>Git学习</title>
<link href="/2020/01/30/learngit/"/>
<url>/2020/01/30/learngit/</url>
<content type="html"><![CDATA[<h2 id="基本结构"><a href="#基本结构" class="headerlink" title="基本结构"></a>基本结构</h2><p>Git是一个分布式版本控制系统,分为工作区,版本库和远程仓库。</p><p>工作区是本地能看到的目录,版本库可分为暂存区和本地仓库,远程仓库被置于远程,可由Github托管。</p><p>把本地文件放入本地仓库分两步进行:</p><p>第一步:<code>git add</code>将工作区的文件添加到暂存区;</p><p>第二步:<code>git commit</code>将暂存区的所有内容提交到本地仓库(当前分支)</p><p>接下来,在本地仓库和远程仓库已经关联的条件下,即可使用<code>git push</code>推送当前分支到远程。</p><a id="more"></a><hr><h2 id="多人协作开发"><a href="#多人协作开发" class="headerlink" title="多人协作开发"></a>多人协作开发</h2><p><code>master</code>分支是主分支,用于发布版本;</p><p><code>dev</code>分支是开发分支。每个成员都在各自<code>dev</code>分支上开发,并时不时地推送到远程;</p><p><code>feature</code>分支可用于开发新功能,开发完成后,合并到<code>dev</code>分支,最后删除<code>dev</code>分支;</p><p><code>bug</code>分支可以被临时建立来修改bug,修改后,合并分支,删除<code>bug</code>分支。</p><p>总之,<code>master</code>分支用于发布版本,每个人在各自的<code>dev</code>分支上开发,时不时往远程的<code>dev</code>进行提交,合并。如果向远程提交<code>dev</code>分支时出现冲突(即你的同伴和你对同一份文件进行了修改),则需要先<code>git pull</code>,在本地解决冲突后再提交(<code>git commit</code>、<code>git push</code>)</p><hr><h2 id="常用命令"><a href="#常用命令" class="headerlink" title="常用命令"></a>常用命令</h2><ul><li><p>将指定文件添加进入暂存区</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git add <filename></span><br></pre></td></tr></table></figure><p>(<code>git add .</code>表示将所有文件加入暂存区)</p></li><li><p>删除版本库中暂存区指定文件</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git rm <filename></span><br></pre></td></tr></table></figure></li></ul><ul><li><p>将暂存区的修改提交到本地仓库</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git commit -m <span class="string">"your change"</span></span><br></pre></td></tr></table></figure></li><li><p>本地仓库与远程仓库建立联系(确保SSH Key已经配置好)</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git remote add origin https://github.com/YOUR_NAME/REPO_NAME.git</span><br></pre></td></tr></table></figure></li><li><p>将远程分支拉到工作区(出现冲突时需要进行这一步,因为要在本地进行合并)</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git pull</span><br></pre></td></tr></table></figure></li><li><p>提交到远程仓库</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git push origin <branch></span><br></pre></td></tr></table></figure><p>如果远程仓库为空,则</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git push -u origin <branch></span><br></pre></td></tr></table></figure></li><li><p>从远程仓库克隆(会自动把本地仓库与远程仓库关联起来)</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git <span class="built_in">clone</span> https://github.com/YOUR_NAME/REPO_NAME.git</span><br></pre></td></tr></table></figure></li><li><p>创建并进入分支</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git checkout -b <branch></span><br></pre></td></tr></table></figure><p>或者</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git switch -c <branch></span><br></pre></td></tr></table></figure></li><li><p>切换分支</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git switch <branch></span><br></pre></td></tr></table></figure></li><li><p>将目标分支合并到当前分支</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git merge <branch></span><br></pre></td></tr></table></figure></li><li><p>删除分支</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git branch -d <branch></span><br></pre></td></tr></table></figure></li><li><p>查看分支情况</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git <span class="built_in">log</span> --pretty=oneline --graph</span><br></pre></td></tr></table></figure></li><li><p>查看以往提交的版本</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git reflog</span><br></pre></td></tr></table></figure></li><li><p>版本回退<br>回退指定次数</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git reset --hard HEAD~<<span class="built_in">times</span>></span><br></pre></td></tr></table></figure><p>回退指定commit_id的版本(id可在log或reflog中找到)</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git reset --hard <commit_id></span><br></pre></td></tr></table></figure></li><li><p>改乱了工作区内的内容,想丢弃工作区的修改(返回到版本库的状态)</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git checkout -- <filename></span><br></pre></td></tr></table></figure></li><li><p>改乱了工作区的内容并且提交到暂存区,以下命令可将修改回退到工作区,此时就回到了上一个场景的情况</p><pre><code class="bash">git reset HEAD <filename></code></pre></li></ul>]]></content>
<categories>
<category> Git </category>
</categories>
<tags>
<tag> Git </tag>
</tags>
</entry>
<entry>
<title>Win10 + Ubuntu 16.04 双系统安装步骤以及问题的解决</title>
<link href="/2020/01/19/windows-ubuntu/"/>
<url>/2020/01/19/windows-ubuntu/</url>
<content type="html"><![CDATA[<h1 id="Win10-Ubuntu-16-04-双系统安装步骤以及问题的解决"><a href="#Win10-Ubuntu-16-04-双系统安装步骤以及问题的解决" class="headerlink" title="Win10 + Ubuntu 16.04 双系统安装步骤以及问题的解决"></a>Win10 + Ubuntu 16.04 双系统安装步骤以及问题的解决</h1><h2 id="双系统安装步骤"><a href="#双系统安装步骤" class="headerlink" title="双系统安装步骤"></a>双系统安装步骤</h2><h3 id="步骤一:Ubuntu-16-04系统盘的制作"><a href="#步骤一:Ubuntu-16-04系统盘的制作" class="headerlink" title="步骤一:Ubuntu 16.04系统盘的制作"></a>步骤一:Ubuntu 16.04系统盘的制作</h3><ol><li>下载Ubuntu镜像<br> 推荐使用<a href="https://mirrors.tuna.tsinghua.edu.cn/ubuntu-releases/16.04/" target="_blank" rel="noopener">清华大学开源软件镜像站</a>,选择ubuntu-16.04.6-desktop-amd64.iso下载即可</li><li>下载UltraSO,使用U盘制作启动盘<ol><li>选择<em>文件->打开</em>,打开Ubuntu的iso镜像。</li><li>在左下角区域选择U盘,点击<em>启动->写入硬盘映像</em>。</li><li>在弹出的对话框中选择写入方式为hdd或hdd+,对硬盘格式化后再点击写入。</li><li>写入完毕后启动盘制作完成。</li></ol></li></ol><h3 id="步骤二:分区与安装"><a href="#步骤二:分区与安装" class="headerlink" title="步骤二:分区与安装"></a>步骤二:分区与安装</h3><ol><li>使用Windows磁盘管理进行分区。<br> 通过<em>WIN + X</em>进入磁盘管理,右键点击有空余空间的磁盘,选择<em>压缩卷</em>,一般大小可设置为50G。此后的Ubuntu系统便安装在此区域。</li><li>关闭Win10系统的快速启动和安全启动。<br> 具体操作可参考<a href="https://www.jianshu.com/p/38e6be8efecf" target="_blank" rel="noopener">win10安装ubuntu18.04 LTS双系统</a></li><li>将启动盘插入电脑,重启电脑,进入BIOS,将U盘设置为第一启动项。</li><li>按照提示信息进行安装。<ul><li><strong>注意</strong>:在分区时,选择自定义模式。一般来说,只需要划分两个区,即swap区(充当虚拟内存,分区格式为swap)和主分区(挂载点/,安装系统和软件,分区格式为ext4)</li></ul></li><li>安装完成后,拔下U盘,重启电脑,选择进入Ubuntu系统。至此,双系统安装已经完成。</li></ol><a id="more"></a><hr><h2 id="遇到的问题以及解决方案"><a href="#遇到的问题以及解决方案" class="headerlink" title="遇到的问题以及解决方案"></a>遇到的问题以及解决方案</h2><h3 id="双系统安装在logo界面卡死"><a href="#双系统安装在logo界面卡死" class="headerlink" title="双系统安装在logo界面卡死"></a>双系统安装在logo界面卡死</h3><p><strong>解决方案:</strong> 问题的原因可能是Ubuntu系统对双显卡支持不友好,可以在安装过程中选中install ubuntu选项,按e键进入编辑</p><p>将<code>quite splash ---</code>改为<code>quite splash nomodeset</code></p><p>显卡被禁止后默认分辨率为800x600,所以窗口可能显示不全,此时只需要按住<em>alt + s</em>(或者<em>alt + F7</em>)拖动窗口即可。</p><p>重启电脑后,进入Ubuntu系统仍需要进行上述修改。</p><p>进入Ubuntu系统后,如果无线网络连接正常(如果无法连接无线网,则需要接网线使用有线网),可以在<em>系统设置->软件和更新->附加驱动</em>中找到NVDIA相关选项,选择<em>应用更改</em>后重新启动即可解决显卡问题。</p><h3 id="无线网卡问题"><a href="#无线网卡问题" class="headerlink" title="无线网卡问题"></a>无线网卡问题</h3><p> 进入Ubuntu系统后,可能无法找到无线网络。原因可能是系统对硬件支持不友好,无法直接找到无线网卡驱动。</p><p> <strong>解决方案:</strong> 网络上最常用的解决方法是在终端输入以下命令(当然需要确保有线网络已连接):</p> <figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">$ sudo apt-get update</span><br><span class="line">$ sudo apt-get install bcmwl-kernal-source</span><br></pre></td></tr></table></figure><p> 如果问题仍然无法解决,那么就需要手动下载对应你的无线网卡型号的驱动软件。</p><p> 以我为例,我的网卡型号为rtl8821ce。</p><ol><li><p>下载网卡驱动。</p> <figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git <span class="built_in">clone</span> https://github.com/endlessm/linux/tree/master/drivers/net/wireless/rtl8821ce</span><br></pre></td></tr></table></figure></li><li><p>修改Makefile文件<br> 将<code>export TopDIR ?=</code>之后的内容改为当前目录地址,例如:</p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">export TopDIR ?= /home/chenqiming/rtl8821ce/</span><br></pre></td></tr></table></figure></li><li><p>在当前目录下打开终端,进行:</p> <figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$ make</span><br><span class="line">$ sudo make install</span><br><span class="line">$ sudo modprobe -a 8821ce</span><br></pre></td></tr></table></figure><p>至此,驱动已经安装完成,重新启动后即可寻找到无线wifi。</p></li></ol>]]></content>
<categories>
<category> Linux </category>
</categories>
<tags>
<tag> Linux </tag>
<tag> Windows </tag>
<tag> Ubuntu </tag>
</tags>
</entry>
</search>