-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatom.xml
220 lines (112 loc) · 513 KB
/
atom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>发呆小屋</title>
<subtitle>WLH</subtitle>
<link href="http://wlh55.github.io/atom.xml" rel="self"/>
<link href="http://wlh55.github.io/"/>
<updated>2022-11-10T13:31:49.960Z</updated>
<id>http://wlh55.github.io/</id>
<author>
<name>WLH</name>
</author>
<generator uri="https://hexo.io/">Hexo</generator>
<entry>
<title></title>
<link href="http://wlh55.github.io/2022/11/07/Spring%E7%AC%AC%E4%B8%89%E5%A4%A9/"/>
<id>http://wlh55.github.io/2022/11/07/Spring%E7%AC%AC%E4%B8%89%E5%A4%A9/</id>
<published>2022-11-06T16:40:29.756Z</published>
<updated>2022-11-10T13:31:49.960Z</updated>
<content type="html"><![CDATA[<hr><p>title: Spring第三天</p><p>author: WLH</p><p>tags: [Spring]</p><p>categories: [Spring]</p><hr><h2 id="今日目标"><a href="#今日目标" class="headerlink" title="今日目标"></a>今日目标</h2><ul><li><input disabled="" type="checkbox"> 能够理解AOP的作用</li><li><input disabled="" type="checkbox"> 能够完成AOP的入门案例</li><li><input disabled="" type="checkbox"> 能够理解AOP的工作流程</li><li><input disabled="" type="checkbox"> 能够说出AOP的五种通知类型</li><li><input disabled="" type="checkbox"> 能够完成”测量业务层接口万次执行效率”案例</li><li><input disabled="" type="checkbox"> 能够掌握Spring事务配置</li></ul><h2 id="一、AOP"><a href="#一、AOP" class="headerlink" title="一、AOP"></a>一、AOP</h2><h3 id="1-AOP简介"><a href="#1-AOP简介" class="headerlink" title="1 AOP简介"></a>1 AOP简介</h3><h4 id="问题导入"><a href="#问题导入" class="headerlink" title="问题导入"></a>问题导入</h4><p>问题1:AOP的作用是什么?</p><p>问题2:连接点和切入点有什么区别,二者谁的范围大?</p><p>问题3:请描述什么是切面?</p><h4 id="1-1-AOP简介和作用【理解】"><a href="#1-1-AOP简介和作用【理解】" class="headerlink" title="1.1 AOP简介和作用【理解】"></a>1.1 AOP简介和作用【理解】</h4><ul><li>AOP(Aspect Oriented Programming)面向切面编程,一种编程范式,指导开发者如何组织程序结构<ul><li>OOP(Object Oriented Programming)面向对象编程</li></ul></li><li>作用:在不惊动原始设计的基础上为其进行功能增强。简单的说就是在不改变方法源代码的基础上对方法进行功能增强。</li><li>Spring理念:无入侵式/无侵入式</li></ul><h4 id="1-2-AOP中的核心概念【理解】"><a href="#1-2-AOP中的核心概念【理解】" class="headerlink" title="1.2 AOP中的核心概念【理解】"></a>1.2 AOP中的核心概念【理解】</h4><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210730144903209.png" alt="image-20210730144903209"></p><ul><li>连接点(JoinPoint):正在执行的方法,例如:update()、delete()、select()等都是连接点。</li><li>切入点(Pointcut):进行功能增强了的方法,例如:update()、delete()方法,select()方法没有被增强所以不是切入点,但是是连接点。<ul><li>在SpringAOP中,一个切入点可以只描述一个具体方法,也可以匹配多个方法<ul><li>一个具体方法:com.itheima.dao包下的BookDao接口中的无形参无返回值的save方法</li><li>匹配多个方法:所有的save方法,所有的get开头的方法,所有以Dao结尾的接口中的任意方法,所有带有一个参数的方法</li></ul></li></ul></li><li>通知(Advice):在切入点前后执行的操作,也就是增强的共性功能<ul><li>在SpringAOP中,功能最终以方法的形式呈现</li></ul></li><li>通知类:通知方法所在的类叫做通知类</li><li>切面(Aspect):描述通知与切入点的对应关系,也就是哪些通知方法对应哪些切入点方法。</li></ul><h3 id="2-AOP入门案例【重点】"><a href="#2-AOP入门案例【重点】" class="headerlink" title="2 AOP入门案例【重点】"></a>2 AOP入门案例【重点】</h3><h4 id="问题导入-1"><a href="#问题导入-1" class="headerlink" title="问题导入"></a>问题导入</h4><p>问题1:在通知方法中如何定义切入点表达式?</p><p>问题2:如何配置切面?</p><p>问题3:在配置类上如何开启AOP注解功能?</p><h4 id="2-1-AOP入门案例思路分析"><a href="#2-1-AOP入门案例思路分析" class="headerlink" title="2.1 AOP入门案例思路分析"></a>2.1 AOP入门案例思路分析</h4><ul><li>案例设定:测定接口执行效率</li><li>简化设定:在接口执行前输出当前系统时间</li><li>开发模式:XML or <strong>==注解==</strong></li><li>思路分析:<ol><li>导入坐标(pom.xml)</li><li>制作连接点方法(原始操作,dao接口与实现类)</li><li>制作共性功能(通知类与通知)</li><li>定义切入点</li><li>绑定切入点与通知关系(切面)</li></ol></li></ul><h4 id="2-2-AOP入门案例实现"><a href="#2-2-AOP入门案例实现" class="headerlink" title="2.2 AOP入门案例实现"></a>2.2 AOP入门案例实现</h4><h5 id="【第一步】导入aop相关坐标"><a href="#【第一步】导入aop相关坐标" class="headerlink" title="【第一步】导入aop相关坐标"></a>【第一步】导入aop相关坐标</h5><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>dependencies</span><span class="token punctuation">></span></span> <span class="token comment"><!--spring核心依赖,会将spring-aop传递进来--></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>dependency</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>groupId</span><span class="token punctuation">></span></span>org.springframework<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>groupId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>artifactId</span><span class="token punctuation">></span></span>spring-context<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>artifactId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>version</span><span class="token punctuation">></span></span>5.2.10.RELEASE<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>version</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>dependency</span><span class="token punctuation">></span></span> <span class="token comment"><!--切入点表达式依赖,目的是找到切入点方法,也就是找到要增强的方法--></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>dependency</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>groupId</span><span class="token punctuation">></span></span>org.aspectj<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>groupId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>artifactId</span><span class="token punctuation">></span></span>aspectjweaver<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>artifactId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>version</span><span class="token punctuation">></span></span>1.9.4<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>version</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>dependency</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>dependencies</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210730150138448.png" alt="image-20210730150138448"></p><h5 id="【第二步】定义dao接口与实现类"><a href="#【第二步】定义dao接口与实现类" class="headerlink" title="【第二步】定义dao接口与实现类"></a>【第二步】定义dao接口与实现类</h5><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">BookDao</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">save</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">update</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token annotation punctuation">@Repository</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">BookDaoImpl</span> <span class="token keyword">implements</span> <span class="token class-name">BookDao</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">save</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token class-name">System</span><span class="token punctuation">.</span><span class="token function">currentTimeMillis</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"book dao save ..."</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">update</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"book dao update ..."</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h5 id="【第三步】定义通知类,制作通知方法"><a href="#【第三步】定义通知类,制作通知方法" class="headerlink" title="【第三步】定义通知类,制作通知方法"></a>【第三步】定义通知类,制作通知方法</h5><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">//通知类必须配置成Spring管理的bean</span><span class="token annotation punctuation">@Component</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">MyAdvice</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">method</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token class-name">System</span><span class="token punctuation">.</span><span class="token function">currentTimeMillis</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h5 id="【第四步】定义切入点表达式、配置切面-绑定切入点与通知关系"><a href="#【第四步】定义切入点表达式、配置切面-绑定切入点与通知关系" class="headerlink" title="【第四步】定义切入点表达式、配置切面(绑定切入点与通知关系)"></a>【第四步】定义切入点表达式、配置切面(绑定切入点与通知关系)</h5><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">//通知类必须配置成Spring管理的bean</span><span class="token annotation punctuation">@Component</span><span class="token comment">//设置当前类为切面类类</span><span class="token annotation punctuation">@Aspect</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">MyAdvice</span> <span class="token punctuation">{</span> <span class="token comment">//设置切入点,@Pointcut注解要求配置在方法上方</span> <span class="token annotation punctuation">@Pointcut</span><span class="token punctuation">(</span><span class="token string">"execution(void com.itheima.dao.BookDao.update())"</span><span class="token punctuation">)</span> <span class="token keyword">private</span> <span class="token keyword">void</span> <span class="token function">pt</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span> <span class="token comment">//设置在切入点pt()的前面运行当前操作(前置通知)</span> <span class="token annotation punctuation">@Before</span><span class="token punctuation">(</span><span class="token string">"pt()"</span><span class="token punctuation">)</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">method</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token class-name">System</span><span class="token punctuation">.</span><span class="token function">currentTimeMillis</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210730154740528.png" alt="image-20210730154740528"></p><h5 id="【第五步】在配置类中进行Spring注解包扫描和开启AOP功能"><a href="#【第五步】在配置类中进行Spring注解包扫描和开启AOP功能" class="headerlink" title="【第五步】在配置类中进行Spring注解包扫描和开启AOP功能"></a>【第五步】在配置类中进行Spring注解包扫描和开启AOP功能</h5><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token annotation punctuation">@Configuration</span><span class="token annotation punctuation">@ComponentScan</span><span class="token punctuation">(</span><span class="token string">"com.itheima"</span><span class="token punctuation">)</span><span class="token comment">//开启注解开发AOP功能</span><span class="token annotation punctuation">@EnableAspectJAutoProxy</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">SpringConfig</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h5 id="测试类和运行结果"><a href="#测试类和运行结果" class="headerlink" title="测试类和运行结果"></a>测试类和运行结果</h5><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">App</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">ApplicationContext</span> ctx <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">AnnotationConfigApplicationContext</span><span class="token punctuation">(</span><span class="token class-name">SpringConfig</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">BookDao</span> bookDao <span class="token operator">=</span> ctx<span class="token punctuation">.</span><span class="token function">getBean</span><span class="token punctuation">(</span><span class="token class-name">BookDao</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span> bookDao<span class="token punctuation">.</span><span class="token function">update</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210730151347354.png" alt="image-20210730151347354"></p><h3 id="3-AOP工作流程【理解】"><a href="#3-AOP工作流程【理解】" class="headerlink" title="3 AOP工作流程【理解】"></a>3 AOP工作流程【理解】</h3><h4 id="问题导入-2"><a href="#问题导入-2" class="headerlink" title="问题导入"></a>问题导入</h4><p>什么是目标对象?什么是代理对象?</p><h4 id="3-1-AOP工作流程"><a href="#3-1-AOP工作流程" class="headerlink" title="3.1 AOP工作流程"></a>3.1 AOP工作流程</h4><ol><li>Spring容器启动</li><li>读取所有切面配置中的切入点</li><li>初始化bean,判定bean对应的类中的方法是否匹配到任意切入点<ul><li>匹配失败,创建原始对象</li><li>匹配成功,创建原始对象(目标对象)的代理对象</li></ul></li><li>获取bean执行方法<ul><li>获取的bean是原始对象时,调用方法并执行,完成操作</li><li>获取的bean是代理对象时,根据代理对象的运行模式运行原始方法与增强的内容,完成操作</li></ul></li></ol><h4 id="3-2-AOP核心概念"><a href="#3-2-AOP核心概念" class="headerlink" title="3.2 AOP核心概念"></a>3.2 AOP核心概念</h4><p>目标对象(Target):被代理的对象,也叫原始对象,该对象中的方法没有任何功能增强。<br>代理对象(Proxy):代理后生成的对象,由Spring帮我们创建代理对象。</p><h4 id="3-3-在测试类中验证代理对象"><a href="#3-3-在测试类中验证代理对象" class="headerlink" title="3.3 在测试类中验证代理对象"></a>3.3 在测试类中验证代理对象</h4><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">App</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">ApplicationContext</span> ctx <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">AnnotationConfigApplicationContext</span><span class="token punctuation">(</span><span class="token class-name">SpringConfig</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">BookDao</span> bookDao <span class="token operator">=</span> ctx<span class="token punctuation">.</span><span class="token function">getBean</span><span class="token punctuation">(</span><span class="token class-name">BookDao</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span> bookDao<span class="token punctuation">.</span><span class="token function">update</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//打印对象的类名</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>bookDao<span class="token punctuation">.</span><span class="token function">getClass</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210730152631741.png" alt="image-20210730152631741"></p><h3 id="4-AOP切入点表达式"><a href="#4-AOP切入点表达式" class="headerlink" title="4 AOP切入点表达式"></a>4 AOP切入点表达式</h3><h4 id="问题导入-3"><a href="#问题导入-3" class="headerlink" title="问题导入"></a>问题导入</h4><p>在切入点表达式中如何简化包名和参数类型书写?</p><h4 id="4-1-语法格式"><a href="#4-1-语法格式" class="headerlink" title="4.1 语法格式"></a>4.1 语法格式</h4><ul><li><p>切入点:要进行增强的方法</p></li><li><p>切入点表达式:要进行增强的方法的描述方式</p><ul><li>描述方式一:执行com.itheima.dao包下的BookDao接口中的无参数update方法</li></ul><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token function">execution</span><span class="token punctuation">(</span><span class="token keyword">void</span> <span class="token class-name"><span class="token namespace">com<span class="token punctuation">.</span>itheima<span class="token punctuation">.</span>dao<span class="token punctuation">.</span></span>BookDao</span><span class="token punctuation">.</span><span class="token function">update</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><ul><li>描述方式二:执行com.itheima.dao.impl包下的BookDaoImpl类中的无参数update方法</li></ul><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token function">execution</span><span class="token punctuation">(</span><span class="token keyword">void</span> <span class="token class-name"><span class="token namespace">com<span class="token punctuation">.</span>itheima<span class="token punctuation">.</span>dao<span class="token punctuation">.</span>impl<span class="token punctuation">.</span></span>BookDaoImpl</span><span class="token punctuation">.</span><span class="token function">update</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></li><li><p>切入点表达式标准格式:动作关键字(访问修饰符 返回值 包名.类/接口名.方法名(参数) 异常名)</p><pre class="line-numbers language-none"><code class="language-none">execution(public User com.itheima.service.UserService.findById(int))<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><ul><li>动作关键字:描述切入点的行为动作,例如execution表示执行到指定切入点</li><li>访问修饰符:public,private等,可以省略</li><li>返回值:写返回值类型</li><li>包名:多级包使用点连接</li><li>类/接口名:</li><li>方法名:</li><li>参数:直接写参数的类型,多个类型用逗号隔开</li><li>异常名:方法定义中抛出指定异常,可以省略</li></ul></li></ul><h4 id="4-2-通配符"><a href="#4-2-通配符" class="headerlink" title="4.2 通配符"></a>4.2 通配符</h4><blockquote><p>目的:可以使用通配符描述切入点,快速描述。</p></blockquote><ul><li>:单个独立的任意符号,可以独立出现,也可以作为前缀或者后缀的匹配符出现</li></ul><blockquote><p>匹配com.itheima包下的任意包中的UserService类或接口中所有find开头的带有一个参数的方法</p></blockquote><pre class="line-numbers language-java" data-language="java"><code class="language-java">execution(<span class="token keyword">public</span> <span class="token operator">*</span> com<span class="token punctuation">.</span>itheima<span class="token punctuation">.</span>*<span class="token punctuation">.</span>UserService<span class="token punctuation">.</span>find<span class="token operator">*</span><span class="token punctuation">(</span><span class="token operator">*</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><ul><li>.. :多个连续的任意符号,可以独立出现,常用于简化包名与参数的书写</li></ul><blockquote><p>匹配com包下的任意包中的UserService类或接口中所有名称为findById的方法</p></blockquote><pre class="line-numbers language-java" data-language="java"><code class="language-java">execution(<span class="token keyword">public</span> <span class="token class-name">User</span> com<span class="token punctuation">.</span><span class="token punctuation">.</span>UserService<span class="token punctuation">.</span><span class="token function">findById</span><span class="token punctuation">(</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><ul><li>+:专用于匹配子类类型</li></ul><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token function">execution</span><span class="token punctuation">(</span><span class="token operator">*</span> <span class="token operator">*</span><span class="token punctuation">.</span><span class="token punctuation">.</span>*<span class="token class-name">Service</span><span class="token operator">+</span><span class="token punctuation">.</span>*<span class="token punctuation">(</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p><img src="/assets/image-20210730153824117.png" alt="image-20210730153824117"></p><h4 id="4-3-书写技巧"><a href="#4-3-书写技巧" class="headerlink" title="4.3 书写技巧"></a>4.3 书写技巧</h4><ul><li>所有代码按照标准规范开发,否则以下技巧全部失效</li><li>描述切入点通**==常描述接口==**,而不描述实现类</li><li>访问控制修饰符针对接口开发均采用public描述(**==可省略访问控制修饰符描述==**)</li><li>返回值类型对于增删改类使用精准类型加速匹配,对于查询类使用*通配快速描述</li><li>**==包名==<strong>书写</strong>==尽量不使用..匹配==**,效率过低,常用*做单个包描述匹配,或精准匹配</li><li>**==接口名/类名==<strong>书写名称与模块相关的</strong>==采用*匹配==**,例如UserService书写成*Service,绑定业务层接口名</li><li>**==方法名==<strong>书写以</strong>==动词==<strong>进行</strong>==精准匹配==*<em>,名词采用</em>匹配,例如getById书写成getBy*,selectAll书写成selectAll</li><li>参数规则较为复杂,根据业务方法灵活调整</li><li>通常**==不使用异常==<strong>作为</strong>==匹配==**规则</li></ul><h3 id="5-AOP通知类型【重点】"><a href="#5-AOP通知类型【重点】" class="headerlink" title="5 AOP通知类型【重点】"></a>5 AOP通知类型【重点】</h3><h4 id="问题导入-4"><a href="#问题导入-4" class="headerlink" title="问题导入"></a>问题导入</h4><p>请描述一下如何定义环绕通知方法?</p><h4 id="5-1-AOP通知分类"><a href="#5-1-AOP通知分类" class="headerlink" title="5.1 AOP通知分类"></a>5.1 AOP通知分类</h4><ul><li>AOP通知描述了抽取的共性功能,根据共性功能抽取的位置不同,最终运行代码时要将其加入到合理的位置</li><li>AOP通知共分为5种类型<ul><li>前置通知:在切入点方法执行之前执行</li><li>后置通知:在切入点方法执行之后执行,无论切入点方法内部是否出现异常,后置通知都会执行。</li><li>**==环绕通知(重点):==**手动调用切入点方法并对其进行增强的通知方式。</li><li>返回后通知(了解):在切入点方法执行之后执行,如果切入点方法内部出现异常将不会执行。</li><li>抛出异常后通知(了解):在切入点方法执行之后执行,只有当切入点方法内部出现异常之后才执行。</li></ul></li></ul><h4 id="5-2-AOP通知详解"><a href="#5-2-AOP通知详解" class="headerlink" title="5.2 AOP通知详解"></a>5.2 AOP通知详解</h4><h5 id="5-2-1-前置通知"><a href="#5-2-1-前置通知" class="headerlink" title="5.2.1 前置通知"></a>5.2.1 前置通知</h5><ul><li>名称:@Before</li><li>类型:**==方法注解==**</li><li>位置:通知方法定义上方</li><li>作用:设置当前通知方法与切入点之间的绑定关系,当前通知方法在原始切入点方法前运行</li><li>范例:</li></ul><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token annotation punctuation">@Before</span><span class="token punctuation">(</span><span class="token string">"pt()"</span><span class="token punctuation">)</span><span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">before</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"before advice ..."</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre><h5 id="5-2-2-后置通知"><a href="#5-2-2-后置通知" class="headerlink" title="5.2.2 后置通知"></a>5.2.2 后置通知</h5><ul><li>名称:@After</li><li>类型:==<strong>方法注解</strong>==</li><li>位置:通知方法定义上方</li><li>作用:设置当前通知方法与切入点之间的绑定关系,当前通知方法在原始切入点方法后运行</li><li>范例:</li></ul><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token annotation punctuation">@After</span><span class="token punctuation">(</span><span class="token string">"pt()"</span><span class="token punctuation">)</span><span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">after</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"after advice ..."</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre><h5 id="5-2-3-返回后通知"><a href="#5-2-3-返回后通知" class="headerlink" title="5.2.3 返回后通知"></a>5.2.3 返回后通知</h5><ul><li>名称:@AfterReturning(了解)</li><li>类型:**==方法注解==**</li><li>位置:通知方法定义上方</li><li>作用:设置当前通知方法与切入点之间的绑定关系,当前通知方法在原始切入点方法正常执行完毕后运行</li><li>范例:</li></ul><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token annotation punctuation">@AfterReturning</span><span class="token punctuation">(</span><span class="token string">"pt()"</span><span class="token punctuation">)</span><span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">afterReturning</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"afterReturning advice ..."</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre><h5 id="5-2-4-抛出异常后通知"><a href="#5-2-4-抛出异常后通知" class="headerlink" title="5.2.4 抛出异常后通知"></a>5.2.4 抛出异常后通知</h5><ul><li>名称:@AfterThrowing(了解)</li><li>类型:**==方法注解==**</li><li>位置:通知方法定义上方</li><li>作用:设置当前通知方法与切入点之间的绑定关系,当前通知方法在原始切入点方法运行抛出异常后执行</li><li>范例:</li></ul><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token annotation punctuation">@AfterThrowing</span><span class="token punctuation">(</span><span class="token string">"pt()"</span><span class="token punctuation">)</span><span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">afterThrowing</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"afterThrowing advice ..."</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre><h5 id="5-2-5-环绕通知"><a href="#5-2-5-环绕通知" class="headerlink" title="5.2.5 环绕通知"></a>5.2.5 环绕通知</h5><ul><li>名称:@Around(重点,常用)</li><li>类型:**==方法注解==**</li><li>位置:通知方法定义上方</li><li>作用:设置当前通知方法与切入点之间的绑定关系,当前通知方法在原始切入点方法前后运行</li><li>范例::</li></ul><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token annotation punctuation">@Around</span><span class="token punctuation">(</span><span class="token string">"pt()"</span><span class="token punctuation">)</span><span class="token keyword">public</span> <span class="token class-name">Object</span> <span class="token function">around</span><span class="token punctuation">(</span><span class="token class-name">ProceedingJoinPoint</span> pjp<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Throwable</span> <span class="token punctuation">{</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"around before advice ..."</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">Object</span> ret <span class="token operator">=</span> pjp<span class="token punctuation">.</span><span class="token function">proceed</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"around after advice ..."</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> ret<span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>==<strong>环绕通知注意事项</strong>==</p><ol><li>环绕通知方法形参必须是ProceedingJoinPoint,表示正在执行的连接点,使用该对象的proceed()方法表示对原始对象方法进行调用,返回值为原始对象方法的返回值。</li><li>环绕通知方法的返回值建议写成Object类型,用于将原始对象方法的返回值进行返回,哪里使用代理对象就返回到哪里。</li></ol><h2 id="二、AOP案例"><a href="#二、AOP案例" class="headerlink" title="二、AOP案例"></a>二、AOP案例</h2><h3 id="1-案例-测量业务层接口万次执行效率"><a href="#1-案例-测量业务层接口万次执行效率" class="headerlink" title="1 案例-测量业务层接口万次执行效率"></a>1 案例-测量业务层接口万次执行效率</h3><h4 id="问题导入-5"><a href="#问题导入-5" class="headerlink" title="问题导入"></a>问题导入</h4><p>能不能描述一下环绕通知里面的实现步骤?</p><h4 id="1-1-需求和分析"><a href="#1-1-需求和分析" class="headerlink" title="1.1 需求和分析"></a>1.1 需求和分析</h4><p>需求:任意业务层接口执行均可显示其执行效率(执行时长)</p><p>分析:</p><p>①:业务功能:业务层接口执行前后分别记录时间,求差值得到执行效率<br>②:通知类型选择前后均可以增强的类型——环绕通知</p><h4 id="1-2-代码实现"><a href="#1-2-代码实现" class="headerlink" title="1.2 代码实现"></a>1.2 代码实现</h4><h5 id="【前置工作】环境准备"><a href="#【前置工作】环境准备" class="headerlink" title="【前置工作】环境准备"></a>【前置工作】环境准备</h5><ol><li><p>Spring整合mybatis对spring_db数据库中的Account进行CRUD操作</p></li><li><p>Spring整合Junit测试CRUD是否OK。</p></li><li><p>在pom.xml中添加aspectjweaver切入点表达式依赖</p></li><li><p>… …</p></li></ol><h5 id="【第一步】编写通知类"><a href="#【第一步】编写通知类" class="headerlink" title="【第一步】编写通知类"></a>【第一步】编写通知类</h5><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token annotation punctuation">@Component</span><span class="token annotation punctuation">@Aspect</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ProjectAdvice</span> <span class="token punctuation">{</span> <span class="token comment">//匹配业务层的所有方法</span> <span class="token annotation punctuation">@Pointcut</span><span class="token punctuation">(</span><span class="token string">"execution(* com.itheima.service.*Service.*(..))"</span><span class="token punctuation">)</span> <span class="token keyword">private</span> <span class="token keyword">void</span> <span class="token function">servicePt</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span> <span class="token comment">//设置环绕通知,在原始操作的运行前后记录执行时间</span> <span class="token annotation punctuation">@Around</span><span class="token punctuation">(</span><span class="token string">"ProjectAdvice.servicePt()"</span><span class="token punctuation">)</span> <span class="token comment">//本类类名可以省略不写</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">runSpeed</span><span class="token punctuation">(</span><span class="token class-name">ProceedingJoinPoint</span> pjp<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Throwable</span> <span class="token punctuation">{</span> <span class="token comment">//获取执行的签名对象</span> <span class="token class-name">Signature</span> signature <span class="token operator">=</span> pjp<span class="token punctuation">.</span><span class="token function">getSignature</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//获取接口/类全限定名</span> <span class="token class-name">String</span> className <span class="token operator">=</span> signature<span class="token punctuation">.</span><span class="token function">getDeclaringTypeName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//获取方法名</span> <span class="token class-name">String</span> methodName <span class="token operator">=</span> signature<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//记录开始时间</span> <span class="token keyword">long</span> start <span class="token operator">=</span> <span class="token class-name">System</span><span class="token punctuation">.</span><span class="token function">currentTimeMillis</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//执行万次操作</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> <span class="token number">10000</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> pjp<span class="token punctuation">.</span><span class="token function">proceed</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">//记录结束时间</span> <span class="token keyword">long</span> end <span class="token operator">=</span> <span class="token class-name">System</span><span class="token punctuation">.</span><span class="token function">currentTimeMillis</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//打印执行结果</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"万次执行:"</span><span class="token operator">+</span> className<span class="token operator">+</span><span class="token string">"."</span><span class="token operator">+</span>methodName<span class="token operator">+</span><span class="token string">"---->"</span> <span class="token operator">+</span><span class="token punctuation">(</span>end<span class="token operator">-</span>start<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">"ms"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h5 id="【第二步】在SpringConfig配置类上开启AOP注解功能"><a href="#【第二步】在SpringConfig配置类上开启AOP注解功能" class="headerlink" title="【第二步】在SpringConfig配置类上开启AOP注解功能"></a>【第二步】在SpringConfig配置类上开启AOP注解功能</h5><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token annotation punctuation">@Configuration</span><span class="token annotation punctuation">@ComponentScan</span><span class="token punctuation">(</span><span class="token string">"com.itheima"</span><span class="token punctuation">)</span><span class="token annotation punctuation">@PropertySource</span><span class="token punctuation">(</span><span class="token string">"classpath:jdbc.properties"</span><span class="token punctuation">)</span><span class="token annotation punctuation">@Import</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token class-name">JdbcConfig</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">,</span><span class="token class-name">MybatisConfig</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token annotation punctuation">@EnableAspectJAutoProxy</span> <span class="token comment">//开启AOP注解功能</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">SpringConfig</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h5 id="【第三步】运行测试类,查看结果"><a href="#【第三步】运行测试类,查看结果" class="headerlink" title="【第三步】运行测试类,查看结果"></a>【第三步】运行测试类,查看结果</h5><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token annotation punctuation">@RunWith</span><span class="token punctuation">(</span><span class="token class-name">SpringJUnit4ClassRunner</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token annotation punctuation">@ContextConfiguration</span><span class="token punctuation">(</span>classes <span class="token operator">=</span> <span class="token class-name">SpringConfig</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">AccountServiceTestCase</span> <span class="token punctuation">{</span> <span class="token annotation punctuation">@Autowired</span> <span class="token keyword">private</span> <span class="token class-name">AccountService</span> accountService<span class="token punctuation">;</span> <span class="token annotation punctuation">@Test</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testFindById</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token class-name">Account</span> account <span class="token operator">=</span> accountService<span class="token punctuation">.</span><span class="token function">findById</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token annotation punctuation">@Test</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testFindAll</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">Account</span><span class="token punctuation">></span></span> list <span class="token operator">=</span> accountService<span class="token punctuation">.</span><span class="token function">findAll</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210730164219555.png" alt="image-20210730164219555"></p><h3 id="2-AOP切入点数据获取"><a href="#2-AOP切入点数据获取" class="headerlink" title="2 AOP切入点数据获取"></a>2 AOP切入点数据获取</h3><h4 id="问题导入-6"><a href="#问题导入-6" class="headerlink" title="问题导入"></a>问题导入</h4><p>在环绕通知中可以获取到哪些数据?</p><h4 id="2-1-获取参数"><a href="#2-1-获取参数" class="headerlink" title="2.1 获取参数"></a>2.1 获取参数</h4><blockquote><p>说明:在前置通知和环绕通知中都可以获取到连接点方法的参数们</p></blockquote><ul><li>JoinPoint对象描述了连接点方法的运行状态,可以获取到原始方法的调用参数</li></ul><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token annotation punctuation">@Before</span><span class="token punctuation">(</span><span class="token string">"pt()"</span><span class="token punctuation">)</span><span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">before</span><span class="token punctuation">(</span><span class="token class-name">JoinPoint</span> jp<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">Object</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args <span class="token operator">=</span> jp<span class="token punctuation">.</span><span class="token function">getArgs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//获取连接点方法的参数们</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token class-name">Arrays</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span>args<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre><ul><li>ProccedJointPoint是JoinPoint的子类</li></ul><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token annotation punctuation">@Around</span><span class="token punctuation">(</span><span class="token string">"pt()"</span><span class="token punctuation">)</span><span class="token keyword">public</span> <span class="token class-name">Object</span> <span class="token function">around</span><span class="token punctuation">(</span><span class="token class-name">ProceedingJoinPoint</span> pjp<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Throwable</span> <span class="token punctuation">{</span> <span class="token class-name">Object</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args <span class="token operator">=</span> pjp<span class="token punctuation">.</span><span class="token function">getArgs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//获取连接点方法的参数们</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token class-name">Arrays</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span>args<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">Object</span> ret <span class="token operator">=</span> pjp<span class="token punctuation">.</span><span class="token function">proceed</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> ret<span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h4 id="2-2-获取返回值"><a href="#2-2-获取返回值" class="headerlink" title="2.2 获取返回值"></a>2.2 获取返回值</h4><blockquote><p>说明:在返回后通知和环绕通知中都可以获取到连接点方法的返回值</p></blockquote><ul><li>抛出异常后通知可以获取切入点方法中出现的异常信息,使用形参可以接收对应的异常对象</li></ul><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token annotation punctuation">@AfterReturning</span><span class="token punctuation">(</span>value <span class="token operator">=</span> <span class="token string">"pt()"</span><span class="token punctuation">,</span>returning <span class="token operator">=</span> <span class="token string">"ret"</span><span class="token punctuation">)</span><span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">afterReturning</span><span class="token punctuation">(</span><span class="token class-name">String</span> ret<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">//变量名要和returning="ret"的属性值一致</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"afterReturning advice ..."</span><span class="token operator">+</span>ret<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre><ul><li>环绕通知中可以手工书写对原始方法的调用,得到的结果即为原始方法的返回值</li></ul><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token annotation punctuation">@Around</span><span class="token punctuation">(</span><span class="token string">"pt()"</span><span class="token punctuation">)</span><span class="token keyword">public</span> <span class="token class-name">Object</span> <span class="token function">around</span><span class="token punctuation">(</span><span class="token class-name">ProceedingJoinPoint</span> pjp<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Throwable</span> <span class="token punctuation">{</span> <span class="token comment">// 手动调用连接点方法,返回值就是连接点方法的返回值</span> <span class="token class-name">Object</span> ret <span class="token operator">=</span> pjp<span class="token punctuation">.</span><span class="token function">proceed</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> ret<span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h4 id="2-3-获取异常"><a href="#2-3-获取异常" class="headerlink" title="2.3 获取异常"></a>2.3 获取异常</h4><blockquote><p>说明:在抛出异常后通知和环绕通知中都可以获取到连接点方法中出现的异常</p></blockquote><ul><li>抛出异常后通知可以获取切入点方法中出现的异常信息,使用形参可以接收对应的异常对象</li></ul><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token annotation punctuation">@AfterThrowing</span><span class="token punctuation">(</span>value <span class="token operator">=</span> <span class="token string">"pt()"</span><span class="token punctuation">,</span>throwing <span class="token operator">=</span> <span class="token string">"t"</span><span class="token punctuation">)</span><span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">afterThrowing</span><span class="token punctuation">(</span><span class="token class-name">Throwable</span> t<span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token comment">//变量名要和throwing = "t"的属性值一致</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"afterThrowing advice ..."</span><span class="token operator">+</span> t<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre><ul><li>抛出异常后通知可以获取切入点方法运行的异常信息,使用形参可以接收运行时抛出的异常对象</li></ul><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token annotation punctuation">@Around</span><span class="token punctuation">(</span><span class="token string">"pt()"</span><span class="token punctuation">)</span><span class="token keyword">public</span> <span class="token class-name">Object</span> <span class="token function">around</span><span class="token punctuation">(</span><span class="token class-name">ProceedingJoinPoint</span> pjp<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">Object</span> ret <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span> <span class="token comment">//此处需要try...catch处理,catch中捕获到的异常就是连接点方法中抛出的异常</span> <span class="token keyword">try</span> <span class="token punctuation">{</span> ret <span class="token operator">=</span> pjp<span class="token punctuation">.</span><span class="token function">proceed</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">Throwable</span> t<span class="token punctuation">)</span> <span class="token punctuation">{</span> t<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> ret<span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h3 id="3-案例-百度网盘密码数据兼容处理"><a href="#3-案例-百度网盘密码数据兼容处理" class="headerlink" title="3 案例-百度网盘密码数据兼容处理"></a>3 案例-百度网盘密码数据兼容处理</h3><h4 id="问题导入-7"><a href="#问题导入-7" class="headerlink" title="问题导入"></a>问题导入</h4><p>请说出我们该使用什么类型的通知来完成这个需求?</p><h4 id="3-1-需求和分析"><a href="#3-1-需求和分析" class="headerlink" title="3.1 需求和分析"></a>3.1 需求和分析</h4><p>需求:对百度网盘分享链接输入密码时尾部多输入的空格做兼容处理</p><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210731193059709.png" alt="image-20210731193059709"></p><p>分析:<br>①:在业务方法执行之前对所有的输入参数进行格式处理——trim()<br>②:使用处理后的参数调用原始方法——环绕通知中存在对原始方法的调用</p><h4 id="3-2-代码实现"><a href="#3-2-代码实现" class="headerlink" title="3.2 代码实现"></a>3.2 代码实现</h4><h5 id="【前置工作】环境准备-1"><a href="#【前置工作】环境准备-1" class="headerlink" title="【前置工作】环境准备"></a>【前置工作】环境准备</h5><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">//-------------service层代码-----------------------</span><span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">ResourcesService</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">boolean</span> <span class="token function">openURL</span><span class="token punctuation">(</span><span class="token class-name">String</span> url <span class="token punctuation">,</span><span class="token class-name">String</span> password<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token annotation punctuation">@Service</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ResourcesServiceImpl</span> <span class="token keyword">implements</span> <span class="token class-name">ResourcesService</span> <span class="token punctuation">{</span> <span class="token annotation punctuation">@Autowired</span> <span class="token keyword">private</span> <span class="token class-name">ResourcesDao</span> resourcesDao<span class="token punctuation">;</span> <span class="token keyword">public</span> <span class="token keyword">boolean</span> <span class="token function">openURL</span><span class="token punctuation">(</span><span class="token class-name">String</span> url<span class="token punctuation">,</span> <span class="token class-name">String</span> password<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> resourcesDao<span class="token punctuation">.</span><span class="token function">readResources</span><span class="token punctuation">(</span>url<span class="token punctuation">,</span>password<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token comment">//-------------dao层代码-----------------------</span><span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">ResourcesDao</span> <span class="token punctuation">{</span> <span class="token keyword">boolean</span> <span class="token function">readResources</span><span class="token punctuation">(</span><span class="token class-name">String</span> url<span class="token punctuation">,</span> <span class="token class-name">String</span> password<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token annotation punctuation">@Repository</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ResourcesDaoImpl</span> <span class="token keyword">implements</span> <span class="token class-name">ResourcesDao</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">boolean</span> <span class="token function">readResources</span><span class="token punctuation">(</span><span class="token class-name">String</span> url<span class="token punctuation">,</span> <span class="token class-name">String</span> password<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>password<span class="token punctuation">.</span><span class="token function">length</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//模拟校验</span> <span class="token keyword">return</span> password<span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span><span class="token string">"root"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h5 id="【第一步】编写通知类-1"><a href="#【第一步】编写通知类-1" class="headerlink" title="【第一步】编写通知类"></a>【第一步】编写通知类</h5><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token annotation punctuation">@Component</span><span class="token annotation punctuation">@Aspect</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">DataAdvice</span> <span class="token punctuation">{</span> <span class="token annotation punctuation">@Pointcut</span><span class="token punctuation">(</span><span class="token string">"execution(boolean com.itheima.service.*Service.*(*,*))"</span><span class="token punctuation">)</span> <span class="token keyword">private</span> <span class="token keyword">void</span> <span class="token function">servicePt</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span> <span class="token annotation punctuation">@Around</span><span class="token punctuation">(</span><span class="token string">"DataAdvice.servicePt()"</span><span class="token punctuation">)</span> <span class="token keyword">public</span> <span class="token class-name">Object</span> <span class="token function">trimStr</span><span class="token punctuation">(</span><span class="token class-name">ProceedingJoinPoint</span> pjp<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Throwable</span> <span class="token punctuation">{</span> <span class="token class-name">Object</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args <span class="token operator">=</span> pjp<span class="token punctuation">.</span><span class="token function">getArgs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> args<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">//判断参数是不是字符串</span> <span class="token keyword">if</span><span class="token punctuation">(</span>args<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">getClass</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> args<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> args<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">trim</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token class-name">Object</span> ret <span class="token operator">=</span> pjp<span class="token punctuation">.</span><span class="token function">proceed</span><span class="token punctuation">(</span>args<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> ret<span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h5 id="【第二步】在SpringConfig配置类上开启AOP注解功能-1"><a href="#【第二步】在SpringConfig配置类上开启AOP注解功能-1" class="headerlink" title="【第二步】在SpringConfig配置类上开启AOP注解功能"></a>【第二步】在SpringConfig配置类上开启AOP注解功能</h5><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token annotation punctuation">@Configuration</span><span class="token annotation punctuation">@ComponentScan</span><span class="token punctuation">(</span><span class="token string">"com.itheima"</span><span class="token punctuation">)</span><span class="token annotation punctuation">@EnableAspectJAutoProxy</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">SpringConfig</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre><h5 id="【第三步】运行测试类,查看结果-1"><a href="#【第三步】运行测试类,查看结果-1" class="headerlink" title="【第三步】运行测试类,查看结果"></a>【第三步】运行测试类,查看结果</h5><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">App</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">ApplicationContext</span> ctx <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">AnnotationConfigApplicationContext</span><span class="token punctuation">(</span><span class="token class-name">SpringConfig</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">ResourcesService</span> resourcesService <span class="token operator">=</span> ctx<span class="token punctuation">.</span><span class="token function">getBean</span><span class="token punctuation">(</span><span class="token class-name">ResourcesService</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">boolean</span> flag <span class="token operator">=</span> resourcesService<span class="token punctuation">.</span><span class="token function">openURL</span><span class="token punctuation">(</span><span class="token string">"http://pan.baidu.com/haha"</span><span class="token punctuation">,</span> <span class="token string">"root "</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>flag<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h3 id="4-AOP开发总结"><a href="#4-AOP开发总结" class="headerlink" title="4 AOP开发总结"></a>4 AOP开发总结</h3><h4 id="4-1-AOP的核心概念"><a href="#4-1-AOP的核心概念" class="headerlink" title="4.1 AOP的核心概念"></a>4.1 AOP的核心概念</h4><ul><li>概念:AOP(Aspect Oriented Programming)面向切面编程,一种编程范式</li><li>作用:在不惊动原始设计的基础上为方法进行功能增强</li><li>核心概念<ul><li>代理(Proxy):SpringAOP的核心本质是采用代理模式实现的</li><li>连接点(JoinPoint):在SpringAOP中,理解为任意方法的执行</li><li>切入点(Pointcut):匹配连接点的式子,也是具有共性功能的方法描述</li><li>通知(Advice):若干个方法的共性功能,在切入点处执行,最终体现为一个方法</li><li>切面(Aspect):描述通知与切入点的对应关系</li><li>目标对象(Target):被代理的原始对象成为目标对象</li></ul></li></ul><h4 id="4-2-切入点表达式语法"><a href="#4-2-切入点表达式语法" class="headerlink" title="4.2 切入点表达式语法"></a>4.2 切入点表达式语法</h4><ul><li><p>切入点表达式标准格式:动作关键字(访问修饰符 返回值 包名.类/接口名.方法名(参数)异常名)</p><ul><li>execution(* com.itheima.service.<em>Service.</em>(..))</li></ul></li><li><p>切入点表达式描述通配符:</p><ul><li>作用:用于快速描述,范围描述</li><li>*:匹配任意符号(常用)</li><li>.. :匹配多个连续的任意符号(常用)</li><li>+:匹配子类类型</li></ul></li><li><p>切入点表达式书写技巧</p><p>1.按标准规范开发<br>2.查询操作的返回值建议使用*匹配<br>3.减少使用..的形式描述包<br>4.对接口进行描述,使用*表示模块名,例如UserService的匹配描述为*Service<br>5.方法名书写保留动词,例如get,使用*表示名词,例如getById匹配描述为getBy*<br>6.参数根据实际情况灵活调整</p></li></ul><h4 id="4-3-五种通知类型"><a href="#4-3-五种通知类型" class="headerlink" title="4.3 五种通知类型"></a>4.3 五种通知类型</h4><ul><li>前置通知</li><li>后置通知<ul><li>环绕通知(重点)</li><li>环绕通知依赖形参ProceedingJoinPoint才能实现对原始方法的调用</li><li>环绕通知可以隔离原始方法的调用执行</li><li>环绕通知返回值设置为Object类型</li><li>环绕通知中可以对原始方法调用过程中出现的异常进行处理</li></ul></li><li>返回后通知</li><li>抛出异常后通知</li></ul><h2 id="三、Spring事务管理"><a href="#三、Spring事务管理" class="headerlink" title="三、Spring事务管理"></a>三、Spring事务管理</h2><h3 id="1-Spring事务简介【重点】"><a href="#1-Spring事务简介【重点】" class="headerlink" title="1 Spring事务简介【重点】"></a>1 Spring事务简介【重点】</h3><h4 id="问题导入-8"><a href="#问题导入-8" class="headerlink" title="问题导入"></a>问题导入</h4><p>Spring提供的事务管理是数据层的事务还是业务层的事务?</p><h4 id="1-1-Spring事务作用"><a href="#1-1-Spring事务作用" class="headerlink" title="1.1 Spring事务作用"></a>1.1 Spring事务作用</h4><ul><li>事务作用:在数据层保障一系列的数据库操作同成功同失败</li><li>Spring事务作用:在数据层或**==业务层==**保障一系列的数据库操作同成功同失败</li></ul><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210801190820853.png" alt="image-20210801190820853"></p><h4 id="1-2-需求和分析"><a href="#1-2-需求和分析" class="headerlink" title="1.2 需求和分析"></a>1.2 需求和分析</h4><ul><li>需求:实现任意两个账户间转账操作</li><li>需求微缩:A账户减钱,B账户加钱</li><li>分析:<br>①:数据层提供基础操作,指定账户减钱(outMoney),指定账户加钱(inMoney)<br>②:业务层提供转账操作(transfer),调用减钱与加钱的操作<br>③:提供2个账号和操作金额执行转账操作<br>④:基于Spring整合MyBatis环境搭建上述操作</li><li>结果分析:<br>①:程序正常执行时,账户金额A减B加,没有问题<br>②:程序出现异常后,转账失败,但是异常之前操作成功,异常之后操作失败,整体业务失败</li></ul><h4 id="1-3-代码实现"><a href="#1-3-代码实现" class="headerlink" title="1.3 代码实现"></a>1.3 代码实现</h4><h5 id="【前置工作】环境准备-2"><a href="#【前置工作】环境准备-2" class="headerlink" title="【前置工作】环境准备"></a>【前置工作】环境准备</h5><blockquote><p>Spring整合Mybatis相关代码(依赖、JdbcConfig、MybatisConfig、SpringConfig)省略。</p></blockquote><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">AccountDao</span> <span class="token punctuation">{</span> <span class="token annotation punctuation">@Update</span><span class="token punctuation">(</span><span class="token string">"update tbl_account set money = money + #{money} where name = #{name}"</span><span class="token punctuation">)</span> <span class="token keyword">void</span> <span class="token function">inMoney</span><span class="token punctuation">(</span><span class="token annotation punctuation">@Param</span><span class="token punctuation">(</span><span class="token string">"name"</span><span class="token punctuation">)</span> <span class="token class-name">String</span> name<span class="token punctuation">,</span> <span class="token annotation punctuation">@Param</span><span class="token punctuation">(</span><span class="token string">"money"</span><span class="token punctuation">)</span> <span class="token class-name">Double</span> money<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token annotation punctuation">@Update</span><span class="token punctuation">(</span><span class="token string">"update tbl_account set money = money - #{money} where name = #{name}"</span><span class="token punctuation">)</span> <span class="token keyword">void</span> <span class="token function">outMoney</span><span class="token punctuation">(</span><span class="token annotation punctuation">@Param</span><span class="token punctuation">(</span><span class="token string">"name"</span><span class="token punctuation">)</span> <span class="token class-name">String</span> name<span class="token punctuation">,</span> <span class="token annotation punctuation">@Param</span><span class="token punctuation">(</span><span class="token string">"money"</span><span class="token punctuation">)</span> <span class="token class-name">Double</span> money<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">AccountService</span> <span class="token punctuation">{</span> <span class="token comment">/** * 转账操作 * @param out 传出方 * @param in 转入方 * @param money 金额 */</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">transfer</span><span class="token punctuation">(</span><span class="token class-name">String</span> out<span class="token punctuation">,</span><span class="token class-name">String</span> in <span class="token punctuation">,</span><span class="token class-name">Double</span> money<span class="token punctuation">)</span> <span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token annotation punctuation">@Service</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">AccountServiceImpl</span> <span class="token keyword">implements</span> <span class="token class-name">AccountService</span> <span class="token punctuation">{</span> <span class="token annotation punctuation">@Autowired</span> <span class="token keyword">private</span> <span class="token class-name">AccountDao</span> accountDao<span class="token punctuation">;</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">transfer</span><span class="token punctuation">(</span><span class="token class-name">String</span> out<span class="token punctuation">,</span><span class="token class-name">String</span> in <span class="token punctuation">,</span><span class="token class-name">Double</span> money<span class="token punctuation">)</span> <span class="token punctuation">{</span> accountDao<span class="token punctuation">.</span><span class="token function">outMoney</span><span class="token punctuation">(</span>out<span class="token punctuation">,</span>money<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token operator">/</span><span class="token number">0</span><span class="token punctuation">;</span> accountDao<span class="token punctuation">.</span><span class="token function">inMoney</span><span class="token punctuation">(</span>in<span class="token punctuation">,</span>money<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h5 id="【第一步】在业务层接口上添加Spring事务管理"><a href="#【第一步】在业务层接口上添加Spring事务管理" class="headerlink" title="【第一步】在业务层接口上添加Spring事务管理"></a>【第一步】在业务层接口上添加Spring事务管理</h5><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">AccountService</span> <span class="token punctuation">{</span> <span class="token comment">//配置当前接口方法具有事务</span> <span class="token annotation punctuation">@Transactional</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">transfer</span><span class="token punctuation">(</span><span class="token class-name">String</span> out<span class="token punctuation">,</span><span class="token class-name">String</span> in <span class="token punctuation">,</span><span class="token class-name">Double</span> money<span class="token punctuation">)</span> <span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>注意事项</p><ol><li>Spring注解式事务通常添加在业务层接口中而不会添加到业务层实现类中,降低耦合</li><li>注解式事务可以添加到业务方法上表示当前方法开启事务,也可以添加到接口上表示当前接口所有方法开启事务</li></ol><h5 id="【第二步】设置事务管理器-将事务管理器添加到IOC容器中"><a href="#【第二步】设置事务管理器-将事务管理器添加到IOC容器中" class="headerlink" title="【第二步】设置事务管理器(将事务管理器添加到IOC容器中)"></a>【第二步】设置事务管理器(将事务管理器添加到IOC容器中)</h5><blockquote><p>说明:可以在JdbcConfig中配置事务管理器</p></blockquote><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">//配置事务管理器,mybatis使用的是jdbc事务</span><span class="token annotation punctuation">@Bean</span><span class="token keyword">public</span> <span class="token class-name">PlatformTransactionManager</span> <span class="token function">transactionManager</span><span class="token punctuation">(</span><span class="token class-name">DataSource</span> dataSource<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token class-name">DataSourceTransactionManager</span> dtm <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">DataSourceTransactionManager</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> transactionManager<span class="token punctuation">.</span><span class="token function">setDataSource</span><span class="token punctuation">(</span>dataSource<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> transactionManager<span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>注意事项</p><ol><li>事务管理器要根据实现技术进行选择</li><li>MyBatis框架使用的是JDBC事务</li></ol><h5 id="【第三步】开启注解式事务驱动"><a href="#【第三步】开启注解式事务驱动" class="headerlink" title="【第三步】开启注解式事务驱动"></a>【第三步】开启注解式事务驱动</h5><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token annotation punctuation">@Configuration</span><span class="token annotation punctuation">@ComponentScan</span><span class="token punctuation">(</span><span class="token string">"com.itheima"</span><span class="token punctuation">)</span><span class="token annotation punctuation">@PropertySource</span><span class="token punctuation">(</span><span class="token string">"classpath:jdbc.properties"</span><span class="token punctuation">)</span><span class="token annotation punctuation">@Import</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token class-name">JdbcConfig</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">,</span><span class="token class-name">MybatisConfig</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token comment">//开启注解式事务驱动</span><span class="token annotation punctuation">@EnableTransactionManagement</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">SpringConfig</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h5 id="【第四步】运行测试类,查看结果"><a href="#【第四步】运行测试类,查看结果" class="headerlink" title="【第四步】运行测试类,查看结果"></a>【第四步】运行测试类,查看结果</h5><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token annotation punctuation">@RunWith</span><span class="token punctuation">(</span><span class="token class-name">SpringJUnit4ClassRunner</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token annotation punctuation">@ContextConfiguration</span><span class="token punctuation">(</span>classes <span class="token operator">=</span> <span class="token class-name">SpringConfig</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">AccountServiceTest</span> <span class="token punctuation">{</span> <span class="token annotation punctuation">@Autowired</span> <span class="token keyword">private</span> <span class="token class-name">AccountService</span> accountService<span class="token punctuation">;</span> <span class="token annotation punctuation">@Test</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testTransfer</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">IOException</span> <span class="token punctuation">{</span> accountService<span class="token punctuation">.</span><span class="token function">transfer</span><span class="token punctuation">(</span><span class="token string">"Tom"</span><span class="token punctuation">,</span><span class="token string">"Jerry"</span><span class="token punctuation">,</span><span class="token number">100D</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h3 id="2-Spring事务角色【理解】"><a href="#2-Spring事务角色【理解】" class="headerlink" title="2 Spring事务角色【理解】"></a>2 Spring事务角色【理解】</h3><h4 id="问题导入-9"><a href="#问题导入-9" class="headerlink" title="问题导入"></a>问题导入</h4><p>什么是事务管理员,什么是事务协调员?</p><h4 id="2-1-Spring事务角色"><a href="#2-1-Spring事务角色" class="headerlink" title="2.1 Spring事务角色"></a>2.1 Spring事务角色</h4><ul><li>事务管理员:发起事务方,在Spring中通常指代业务层开启事务的方法</li><li>事务协调员:加入事务方,在Spring中通常指代数据层方法,也可以是业务层方法</li></ul><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210801192453227.png" alt="image-20210801192453227"></p><h3 id="3-Spring事务相关配置"><a href="#3-Spring事务相关配置" class="headerlink" title="3 Spring事务相关配置"></a>3 Spring事务相关配置</h3><h4 id="问题导入-10"><a href="#问题导入-10" class="headerlink" title="问题导入"></a>问题导入</h4><p>什么样的异常,Spring事务默认是不进行回滚的?</p><h4 id="3-1-事务配置"><a href="#3-1-事务配置" class="headerlink" title="3.1 事务配置"></a>3.1 事务配置</h4><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210802151553053.png" alt="image-20210802151553053"></p><blockquote><p>说明:对于RuntimeException类型异常或者Error错误,Spring事务能够进行回滚操作。但是对于编译器异常,Spring事务是不进行回滚的,所以需要使用rollbackFor来设置要回滚的异常。</p></blockquote><h4 id="3-2-案例:转账业务追加日志"><a href="#3-2-案例:转账业务追加日志" class="headerlink" title="3.2 案例:转账业务追加日志"></a>3.2 案例:转账业务追加日志</h4><h5 id="需求和分析"><a href="#需求和分析" class="headerlink" title="需求和分析"></a>需求和分析</h5><ul><li>需求:实现任意两个账户间转账操作,并对每次转账操作在数据库进行留痕</li><li>需求微缩:A账户减钱,B账户加钱,数据库记录日志</li><li>分析:<br>①:基于转账操作案例添加日志模块,实现数据库中记录日志<br>②:业务层转账操作(transfer),调用减钱、加钱与记录日志功能</li><li>实现效果预期:<br> 无论转账操作是否成功,均进行转账操作的日志留痕</li><li>存在的问题:<br> 日志的记录与转账操作隶属同一个事务,同成功同失败</li><li>实现效果预期改进:<br> 无论转账操作是否成功,日志必须保留</li><li>事务传播行为:事务协调员对事务管理员所携带事务的处理态度</li></ul><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210802153216460.png" alt="image-20210802153216460"></p><h5 id="【准备工作】环境整备"><a href="#【准备工作】环境整备" class="headerlink" title="【准备工作】环境整备"></a>【准备工作】环境整备</h5><pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">USE</span> spring_db<span class="token punctuation">;</span><span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> tbl_log<span class="token punctuation">(</span>id <span class="token keyword">INT</span> <span class="token keyword">PRIMARY</span> <span class="token keyword">KEY</span> <span class="token keyword">AUTO_INCREMENT</span><span class="token punctuation">,</span>info <span class="token keyword">VARCHAR</span><span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">)</span><span class="token punctuation">,</span>createDate <span class="token keyword">DATE</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">LogService</span> <span class="token punctuation">{</span> <span class="token comment">//propagation设置事务属性:传播行为设置为当前操作需要新事务</span> <span class="token annotation punctuation">@Transactional</span> <span class="token keyword">void</span> <span class="token function">log</span><span class="token punctuation">(</span><span class="token class-name">String</span> out<span class="token punctuation">,</span> <span class="token class-name">String</span> in<span class="token punctuation">,</span> <span class="token class-name">Double</span> money<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token annotation punctuation">@Service</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">LogServiceImpl</span> <span class="token keyword">implements</span> <span class="token class-name">LogService</span> <span class="token punctuation">{</span> <span class="token annotation punctuation">@Autowired</span> <span class="token keyword">private</span> <span class="token class-name">LogDao</span> logDao<span class="token punctuation">;</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">log</span><span class="token punctuation">(</span><span class="token class-name">String</span> out<span class="token punctuation">,</span><span class="token class-name">String</span> in<span class="token punctuation">,</span><span class="token class-name">Double</span> money <span class="token punctuation">)</span> <span class="token punctuation">{</span> logDao<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"转账操作由"</span><span class="token operator">+</span>out<span class="token operator">+</span><span class="token string">"到"</span><span class="token operator">+</span>in<span class="token operator">+</span><span class="token string">",金额:"</span><span class="token operator">+</span>money<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">LogDao</span> <span class="token punctuation">{</span> <span class="token annotation punctuation">@Insert</span><span class="token punctuation">(</span><span class="token string">"insert into tbl_log (info,createDate) values(#{info},now())"</span><span class="token punctuation">)</span> <span class="token keyword">void</span> <span class="token function">log</span><span class="token punctuation">(</span><span class="token class-name">String</span> info<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h5 id="【第一步】在AccountServiceImpl中调用logService中添加日志的方法"><a href="#【第一步】在AccountServiceImpl中调用logService中添加日志的方法" class="headerlink" title="【第一步】在AccountServiceImpl中调用logService中添加日志的方法"></a>【第一步】在AccountServiceImpl中调用logService中添加日志的方法</h5><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token annotation punctuation">@Service</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">AccountServiceImpl</span> <span class="token keyword">implements</span> <span class="token class-name">AccountService</span> <span class="token punctuation">{</span> <span class="token annotation punctuation">@Autowired</span> <span class="token keyword">private</span> <span class="token class-name">AccountDao</span> accountDao<span class="token punctuation">;</span> <span class="token annotation punctuation">@Autowired</span> <span class="token keyword">private</span> <span class="token class-name">LogService</span> logService<span class="token punctuation">;</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">transfer</span><span class="token punctuation">(</span><span class="token class-name">String</span> out<span class="token punctuation">,</span><span class="token class-name">String</span> in <span class="token punctuation">,</span><span class="token class-name">Double</span> money<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">try</span><span class="token punctuation">{</span> accountDao<span class="token punctuation">.</span><span class="token function">outMoney</span><span class="token punctuation">(</span>out<span class="token punctuation">,</span>money<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token operator">/</span><span class="token number">0</span><span class="token punctuation">;</span> accountDao<span class="token punctuation">.</span><span class="token function">inMoney</span><span class="token punctuation">(</span>in<span class="token punctuation">,</span>money<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token keyword">finally</span> <span class="token punctuation">{</span> logService<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>out<span class="token punctuation">,</span>in<span class="token punctuation">,</span>money<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h5 id="【第二步】在LogService的log-方法上设置事务的传播行为"><a href="#【第二步】在LogService的log-方法上设置事务的传播行为" class="headerlink" title="【第二步】在LogService的log()方法上设置事务的传播行为"></a>【第二步】在LogService的log()方法上设置事务的传播行为</h5><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">LogService</span> <span class="token punctuation">{</span> <span class="token comment">//propagation设置事务属性:传播行为设置为当前操作需要新事务</span> <span class="token annotation punctuation">@Transactional</span><span class="token punctuation">(</span>propagation <span class="token operator">=</span> <span class="token class-name">Propagation</span><span class="token punctuation">.</span>REQUIRES_NEW<span class="token punctuation">)</span> <span class="token keyword">void</span> <span class="token function">log</span><span class="token punctuation">(</span><span class="token class-name">String</span> out<span class="token punctuation">,</span> <span class="token class-name">String</span> in<span class="token punctuation">,</span> <span class="token class-name">Double</span> money<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre><h5 id="【第三步】运行测试类,查看结果-2"><a href="#【第三步】运行测试类,查看结果-2" class="headerlink" title="【第三步】运行测试类,查看结果"></a>【第三步】运行测试类,查看结果</h5><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token annotation punctuation">@RunWith</span><span class="token punctuation">(</span><span class="token class-name">SpringJUnit4ClassRunner</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token annotation punctuation">@ContextConfiguration</span><span class="token punctuation">(</span>classes <span class="token operator">=</span> <span class="token class-name">SpringConfig</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">AccountServiceTest</span> <span class="token punctuation">{</span> <span class="token annotation punctuation">@Autowired</span> <span class="token keyword">private</span> <span class="token class-name">AccountService</span> accountService<span class="token punctuation">;</span> <span class="token annotation punctuation">@Test</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testTransfer</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">IOException</span> <span class="token punctuation">{</span> accountService<span class="token punctuation">.</span><span class="token function">transfer</span><span class="token punctuation">(</span><span class="token string">"Tom"</span><span class="token punctuation">,</span><span class="token string">"Jerry"</span><span class="token punctuation">,</span><span class="token number">50D</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h4 id="3-3-事务传播行为"><a href="#3-3-事务传播行为" class="headerlink" title="3.3 事务传播行为"></a>3.3 事务传播行为</h4><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210802153014296.png" alt="image-20210802153014296"></p>]]></content>
<summary type="html"><hr>
<p>title: Spring第三天</p>
<p>author: WLH</p>
<p>tags: [Spring]</p>
<p>categories: [Spring]</p>
<hr>
<h2 id="今日目标"><a href="#今日目标" class="</summary>
</entry>
<entry>
<title></title>
<link href="http://wlh55.github.io/2022/11/07/Spring%E7%AC%AC%E4%BA%8C%E5%A4%A9/"/>
<id>http://wlh55.github.io/2022/11/07/Spring%E7%AC%AC%E4%BA%8C%E5%A4%A9/</id>
<published>2022-11-06T16:39:51.303Z</published>
<updated>2022-11-06T16:30:01.590Z</updated>
<content type="html"><![CDATA[<h2 id="今日目标"><a href="#今日目标" class="headerlink" title="今日目标"></a>今日目标</h2><ul><li><input disabled="" type="checkbox"> 能够掌握注解开发定义Bean对象</li><li><input disabled="" type="checkbox"> 能够掌握纯注解开发模式</li><li><input disabled="" type="checkbox"> 能够配置注解开发依赖注入</li><li><input disabled="" type="checkbox"> 能够配置注解开发管理第三方Bean</li><li><input disabled="" type="checkbox"> 能够配置注解开发为第三方Bean注入资源</li><li><input disabled="" type="checkbox"> 能够使用Spring整合Mybatis</li><li><input disabled="" type="checkbox"> 能够使用Spring整合Junit</li></ul><h2 id="一、第三方资源配置管理"><a href="#一、第三方资源配置管理" class="headerlink" title="一、第三方资源配置管理"></a>一、第三方资源配置管理</h2><blockquote><p>说明:以管理DataSource连接池对象为例讲解第三方资源配置管理</p></blockquote><h3 id="1-管理DataSource连接池对象"><a href="#1-管理DataSource连接池对象" class="headerlink" title="1 管理DataSource连接池对象"></a>1 管理DataSource连接池对象</h3><h4 id="问题导入"><a href="#问题导入" class="headerlink" title="问题导入"></a>问题导入</h4><p>配置数据库连接参数时,注入驱动类名是用driverClassName还是driver?</p><h4 id="1-1-管理Druid连接池【重点】"><a href="#1-1-管理Druid连接池【重点】" class="headerlink" title="1.1 管理Druid连接池【重点】"></a>1.1 管理Druid连接池【重点】</h4><p><strong>数据库准备</strong></p><pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">create</span> <span class="token keyword">database</span> <span class="token keyword">if</span> <span class="token operator">not</span> <span class="token keyword">exists</span> spring_db <span class="token keyword">character</span> <span class="token keyword">set</span> utf8<span class="token punctuation">;</span><span class="token keyword">use</span> spring_db<span class="token punctuation">;</span><span class="token keyword">create</span> <span class="token keyword">table</span> <span class="token keyword">if</span> <span class="token operator">not</span> <span class="token keyword">exists</span> tbl_account<span class="token punctuation">(</span> id <span class="token keyword">int</span> <span class="token keyword">primary</span> <span class="token keyword">key</span> <span class="token keyword">auto_increment</span><span class="token punctuation">,</span> name <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">20</span><span class="token punctuation">)</span><span class="token punctuation">,</span> money <span class="token keyword">double</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">insert</span> <span class="token keyword">into</span> tbl_account <span class="token keyword">values</span><span class="token punctuation">(</span><span class="token boolean">null</span><span class="token punctuation">,</span><span class="token string">'Tom'</span><span class="token punctuation">,</span><span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">insert</span> <span class="token keyword">into</span> tbl_account <span class="token keyword">values</span><span class="token punctuation">(</span><span class="token boolean">null</span><span class="token punctuation">,</span><span class="token string">'Jerry'</span><span class="token punctuation">,</span><span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p><strong>【第一步】添加Druid连接池依赖</strong></p><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>dependency</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>groupId</span><span class="token punctuation">></span></span>com.alibaba<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>groupId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>artifactId</span><span class="token punctuation">></span></span>druid<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>artifactId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>version</span><span class="token punctuation">></span></span>1.1.16<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>version</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>dependency</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>dependency</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>groupId</span><span class="token punctuation">></span></span>mysql<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>groupId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>artifactId</span><span class="token punctuation">></span></span>mysql-connector-java<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>artifactId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>version</span><span class="token punctuation">></span></span>5.1.47<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>version</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>dependency</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><blockquote><p>注意:除了添加以上两个依赖之外,别忘了添加spring-context依赖。</p></blockquote><p><strong>【第二步】配置DruidDataSource连接池Bean对象</strong></p><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>bean</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>dataSource<span class="token punctuation">"</span></span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>com.alibaba.druid.pool.DruidDataSource<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>property</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>driverClassName<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>com.mysql.jdbc.Driver<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>property</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>url<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>jdbc:mysql://localhost:3306/spring_db<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>property</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>username<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>root<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>property</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>password<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>root<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>bean</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p><strong>【第三步】在测试类中从IOC容器中获取连接池对象并打印</strong></p><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">App</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">ApplicationContext</span> ctx <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ClassPathXmlApplicationContext</span><span class="token punctuation">(</span><span class="token string">"applicationContext.xml"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">DataSource</span> dataSource <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token class-name">DataSource</span><span class="token punctuation">)</span> ctx<span class="token punctuation">.</span><span class="token function">getBean</span><span class="token punctuation">(</span><span class="token string">"dataSource"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>dataSource<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h4 id="1-2-管理c3p0连接池"><a href="#1-2-管理c3p0连接池" class="headerlink" title="1.2 管理c3p0连接池"></a>1.2 管理c3p0连接池</h4><p><strong>【第一步】添加c3p0连接池依赖</strong></p><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>dependency</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>groupId</span><span class="token punctuation">></span></span>c3p0<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>groupId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>artifactId</span><span class="token punctuation">></span></span>c3p0<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>artifactId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>version</span><span class="token punctuation">></span></span>0.9.1.2<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>version</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>dependency</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre><p><strong>【第二步】配置c3p0连接池Bean对象</strong></p><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>bean</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>dataSource<span class="token punctuation">"</span></span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>com.mchange.v2.c3p0.ComboPooledDataSource<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>property</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>driverClass<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>com.mysql.jdbc.Driver<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>property</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>jdbcUrl<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>jdbc:mysql://localhost:3306/spring_db<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>property</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>user<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>root<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>property</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>password<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>root<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>property</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>maxPoolSize<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>1000<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>bean</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><blockquote><p>注意:同一个Spring容器中不能有两个id=”dataSource”的连接池。</p></blockquote><p><strong>【第三步】在测试类中从IOC容器中获取连接池对象并打印</strong></p><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">App</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">ApplicationContext</span> ctx <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ClassPathXmlApplicationContext</span><span class="token punctuation">(</span><span class="token string">"applicationContext.xml"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">DataSource</span> dataSource <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token class-name">DataSource</span><span class="token punctuation">)</span> ctx<span class="token punctuation">.</span><span class="token function">getBean</span><span class="token punctuation">(</span><span class="token string">"dataSource"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>dataSource<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h3 id="2-加载properties属性文件【重点】"><a href="#2-加载properties属性文件【重点】" class="headerlink" title="2 加载properties属性文件【重点】"></a>2 加载properties属性文件【重点】</h3><blockquote><p>目的:将数据库的连接参数抽取到一个单独的文件中,与Spring配置文件解耦。</p></blockquote><h4 id="问题导入-1"><a href="#问题导入-1" class="headerlink" title="问题导入"></a>问题导入</h4><p>问题1:如何解决使用EL表达式读取属性文件中的值结果读取到了系统属性问题?</p><p>问题2:加载properties文件写法标准写法该怎么写?</p><h4 id="2-1-基本用法"><a href="#2-1-基本用法" class="headerlink" title="2.1 基本用法"></a>2.1 基本用法</h4><p><strong>【第一步】编写jdbc.properties属性文件</strong></p><pre class="line-numbers language-properties" data-language="properties"><code class="language-properties"><span class="token key attr-name">jdbc.driver</span><span class="token punctuation">=</span><span class="token value attr-value">com.mysql.jdbc.Driver</span><span class="token key attr-name">jdbc.url</span><span class="token punctuation">=</span><span class="token value attr-value">jdbc:mysql://127.0.0.1:3306/spring_db</span><span class="token key attr-name">jdbc.username</span><span class="token punctuation">=</span><span class="token value attr-value">root</span><span class="token key attr-name">jdbc.password</span><span class="token punctuation">=</span><span class="token value attr-value">root</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre><p><strong>【第二步】在applicationContext.xml中开启开启context命名空间,加载jdbc.properties属性文件</strong></p><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210730101826913.png" alt="image-20210730101826913"></p><p><strong>==小技巧:如果同学们觉得上述复制粘贴方式不好改或者容易改错,其实idea是有提示功能的,注意不要选错就行了。有些版本的idea没有这个提示,那么就按照上面复制粘贴的方式改,改完之后可以做成live template模板,后期直接用。==</strong></p><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210730102053281.png" alt="image-20210730102053281"></p><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token namespace">context:</span>property-placeholder</span> <span class="token attr-name">location</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>jdbc.properties<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p><strong>【第三步】在配置连接池Bean的地方使用EL表达式获取jdbc.properties属性文件中的值</strong></p><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>bean</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>com.alibaba.druid.pool.DruidDataSource<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>property</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>driverClassName<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>${jdbc.driver}<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>property</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>url<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>${jdbc.url}<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>property</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>username<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>${jdbc.username}<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>property</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>password<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>${jdbc.password}<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>bean</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><blockquote><p>配置完成之后,运行之前的获取Druid连接池代码,可以获取到连接池对象就表示配置成功。</p></blockquote><h4 id="2-2-配置不加载系统属性"><a href="#2-2-配置不加载系统属性" class="headerlink" title="2.2 配置不加载系统属性"></a>2.2 配置不加载系统属性</h4><p><strong>问题</strong></p><p>如果属性文件中配置的不是jdbc.username,而是username=root666,那么使用${username}获取到的不是root666,而是计算机的名称。</p><p><strong>原因</strong></p><p>系统属性的优先级比我们属性文件中的高,替换了我们的username=root666。</p><p><strong>解决</strong></p><p>解决1:换一个名称,例如不叫username,叫jdbc.username。</p><p>解决2:使用system-properties-mode=”NEVER”属性表示不使用系统属性。</p><pre class="line-numbers language-properties" data-language="properties"><code class="language-properties"><span class="token key attr-name"><context</span><span class="token punctuation">:</span><span class="token value attr-value">property-placeholder location="jdbc.properties" system-properties-mode="NEVER"/></span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><h4 id="2-3-加载properties文件写法"><a href="#2-3-加载properties文件写法" class="headerlink" title="2.3 加载properties文件写法"></a>2.3 加载properties文件写法</h4><ul><li>不加载系统属性</li></ul><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token namespace">context:</span>property-placeholder</span> <span class="token attr-name">location</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>jdbc.properties<span class="token punctuation">"</span></span> <span class="token attr-name">system-properties-mode</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>NEVER<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><ul><li>加载多个properties文件<pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token namespace">context:</span>property-placeholder</span> <span class="token attr-name">location</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>jdbc.properties,msg.properties<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></li><li>加载所有properties文件<pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token namespace">context:</span>property-placeholder</span> <span class="token attr-name">location</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>*.properties<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></li><li>加载properties文件**==标准格式==**<pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token namespace">context:</span>property-placeholder</span> <span class="token attr-name">location</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>classpath:*.properties<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></li><li>加载properties文件标准格式<pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token namespace">context:</span>property-placeholder</span> <span class="token attr-name">location</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>classpath*:*.properties<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></li></ul><h2 id="二、Spring容器"><a href="#二、Spring容器" class="headerlink" title="二、Spring容器"></a>二、Spring容器</h2><h3 id="1-Spring核心容器介绍"><a href="#1-Spring核心容器介绍" class="headerlink" title="1 Spring核心容器介绍"></a>1 Spring核心容器介绍</h3><h4 id="问题导入-2"><a href="#问题导入-2" class="headerlink" title="问题导入"></a>问题导入</h4><p>问题:按照Bean名称获取Bean有什么弊端,按照Bean类型获取Bean有什么弊端?</p><h4 id="1-1-创建容器"><a href="#1-1-创建容器" class="headerlink" title="1.1 创建容器"></a>1.1 创建容器</h4><ul><li>方式一:类路径加载配置文件</li></ul><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">ApplicationContext</span> ctx <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ClassPathXmlApplicationContext</span><span class="token punctuation">(</span><span class="token string">"applicationContext.xml"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><ul><li>方式二:文件路径加载配置文件</li></ul><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">ApplicationContext</span> ctx <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">FileSystemXmlApplicationContext</span><span class="token punctuation">(</span><span class="token string">"D:\\applicationContext.xml"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><ul><li>加载多个配置文件</li></ul><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">ApplicationContext</span> ctx <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ClassPathXmlApplicationContext</span><span class="token punctuation">(</span><span class="token string">"bean1.xml"</span><span class="token punctuation">,</span> <span class="token string">"bean2.xml"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><h4 id="1-2-获取bean对象"><a href="#1-2-获取bean对象" class="headerlink" title="1.2 获取bean对象"></a>1.2 获取bean对象</h4><ul><li>方式一:使用bean名称获取</li></ul><blockquote><p>弊端:需要自己强制类型转换</p></blockquote><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">BookDao</span> bookDao <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token class-name">BookDao</span><span class="token punctuation">)</span> ctx<span class="token punctuation">.</span><span class="token function">getBean</span><span class="token punctuation">(</span><span class="token string">"bookDao"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><ul><li><strong>==方式二:使用bean名称获取并指定类型==</strong></li></ul><blockquote><p>弊端:推荐使用</p></blockquote><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">BookDao</span> bookDao <span class="token operator">=</span> ctx<span class="token punctuation">.</span><span class="token function">getBean</span><span class="token punctuation">(</span><span class="token string">"bookDao"</span><span class="token punctuation">,</span> <span class="token class-name">BookDao</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><ul><li>方式三:使用bean类型获取</li></ul><blockquote><p>弊端:如果IOC容器中同类型的Bean对象有多个,此处获取会报错</p></blockquote><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">BookDao</span> bookDao <span class="token operator">=</span> ctx<span class="token punctuation">.</span><span class="token function">getBean</span><span class="token punctuation">(</span><span class="token class-name">BookDao</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><h4 id="1-3-容器类层次结构"><a href="#1-3-容器类层次结构" class="headerlink" title="1.3 容器类层次结构"></a>1.3 容器类层次结构</h4><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210730102842030.png" alt="image-20210730102842030"></p><h4 id="1-4-BeanFactory"><a href="#1-4-BeanFactory" class="headerlink" title="1.4 BeanFactory"></a>1.4 BeanFactory</h4><ul><li>类路径加载配置文件</li></ul><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">Resource</span> resources <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ClassPathResource</span><span class="token punctuation">(</span><span class="token string">"applicationContext.xml"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token class-name">BeanFactory</span> bf <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">XmlBeanFactory</span><span class="token punctuation">(</span>resources<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token class-name">BookDao</span> bookDao <span class="token operator">=</span> bf<span class="token punctuation">.</span><span class="token function">getBean</span><span class="token punctuation">(</span><span class="token string">"bookDao"</span><span class="token punctuation">,</span> <span class="token class-name">BookDao</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>bookDao<span class="token punctuation">.</span><span class="token function">save</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre><ul><li>BeanFactory创建完毕后,所有的Bean均为延迟加载,也就是说我们调用getBean()方法获取Bean对象时才创建Bean对象并返回给我们</li></ul><h3 id="2-Spring核心容器总结"><a href="#2-Spring核心容器总结" class="headerlink" title="2 Spring核心容器总结"></a>2 Spring核心容器总结</h3><h4 id="2-1-容器相关"><a href="#2-1-容器相关" class="headerlink" title="2.1 容器相关"></a>2.1 容器相关</h4><ul><li>BeanFactory是IoC容器的顶层接口,初始化BeanFactory对象时,加载的bean延迟加载</li><li>ApplicationContext接口是Spring容器的核心接口,初始化时bean立即加载</li><li>ApplicationContext接口提供基础的bean操作相关方法,通过其他接口扩展其功能</li><li>ApplicationContext接口常用初始化类<ul><li><strong>==ClassPathXmlApplicationContext(常用)==</strong></li><li>FileSystemXmlApplicationContext</li></ul></li></ul><h4 id="2-2-bean相关"><a href="#2-2-bean相关" class="headerlink" title="2.2 bean相关"></a>2.2 bean相关</h4><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210730103438742.png" alt="image-20210730103438742"></p><h4 id="2-3-依赖注入相关"><a href="#2-3-依赖注入相关" class="headerlink" title="2.3 依赖注入相关"></a>2.3 依赖注入相关</h4><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210730103701525.png" alt="image-20210730103701525"></p><h2 id="三、Spring注解开发"><a href="#三、Spring注解开发" class="headerlink" title="三、Spring注解开发"></a>三、Spring注解开发</h2><h3 id="1-注解开发定义Bean对象【重点】"><a href="#1-注解开发定义Bean对象【重点】" class="headerlink" title="1 注解开发定义Bean对象【重点】"></a>1 注解开发定义Bean对象【重点】</h3><blockquote><p>目的:xml配置Bean对象有些繁琐,使用注解简化Bean对象的定义</p></blockquote><h4 id="问题导入-3"><a href="#问题导入-3" class="headerlink" title="问题导入"></a>问题导入</h4><p>问题1:使用什么标签进行Spring注解包扫描?</p><p>问题2:@Component注解和@Controller、@Service、@Repository三个衍生注解有什么区别?</p><h4 id="1-1-基本使用"><a href="#1-1-基本使用" class="headerlink" title="1.1 基本使用"></a>1.1 基本使用</h4><p><strong>【第一步】在applicationContext.xml中开启Spring注解包扫描</strong></p><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token prolog"><?xml version="1.0" encoding="UTF-8"?></span><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>beans</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>http://www.springframework.org/schema/beans<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">xmlns:</span>context</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>http://www.springframework.org/schema/context<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">xmlns:</span>xsi</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>http://www.w3.org/2001/XMLSchema-instance<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">xsi:</span>schemaLocation</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span> http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token comment"><!--扫描com.itheima包及其子包下的类中注解--></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token namespace">context:</span>component-scan</span> <span class="token attr-name">base-package</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>com.itheima<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>beans</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p><strong>【第二步】在类上使用@Component注解定义Bean。</strong></p><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">//@Component定义bean</span><span class="token annotation punctuation">@Component</span><span class="token punctuation">(</span><span class="token string">"bookDao"</span><span class="token punctuation">)</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">BookDaoImpl</span> <span class="token keyword">implements</span> <span class="token class-name">BookDao</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">save</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"book dao save ..."</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token annotation punctuation">@Component</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">BookServiceImpl</span> <span class="token keyword">implements</span> <span class="token class-name">BookService</span> <span class="token punctuation">{</span> <span class="token keyword">private</span> <span class="token class-name">BookDao</span> bookDao<span class="token punctuation">;</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setBookDao</span><span class="token punctuation">(</span><span class="token class-name">BookDao</span> bookDao<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>bookDao <span class="token operator">=</span> bookDao<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">save</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"book service save ..."</span><span class="token punctuation">)</span><span class="token punctuation">;</span> bookDao<span class="token punctuation">.</span><span class="token function">save</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><blockquote><p>补充说明:如果@Component注解没有使用参数指定Bean的名称,那么类名首字母小写就是Bean在IOC容器中的默认名称。例如:BookServiceImpl对象在IOC容器中的名称是bookServiceImpl。</p></blockquote><p><strong>【第三步】在测试类中获取Bean对象</strong></p><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">AppForAnnotation</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">ApplicationContext</span> ctx <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ClassPathXmlApplicationContext</span><span class="token punctuation">(</span><span class="token string">"applicationContext.xml"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">BookDao</span> bookDao <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token class-name">BookDao</span><span class="token punctuation">)</span> ctx<span class="token punctuation">.</span><span class="token function">getBean</span><span class="token punctuation">(</span><span class="token string">"bookDao"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>bookDao<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//按类型获取bean</span> <span class="token class-name">BookService</span> bookService <span class="token operator">=</span> ctx<span class="token punctuation">.</span><span class="token function">getBean</span><span class="token punctuation">(</span><span class="token class-name">BookService</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>bookService<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><blockquote><p>注意:在测试类中不要调用bookService的save方法,因为还没有给BookServiceImpl中的bookDao赋值,调用bookService的save方法会出现空指针异常。</p></blockquote><p><strong>运行结果</strong></p><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210730104835746.png" alt="image-20210730104835746"></p><h4 id="1-2-Component三个衍生注解"><a href="#1-2-Component三个衍生注解" class="headerlink" title="1.2 @Component三个衍生注解"></a>1.2 @Component三个衍生注解</h4><blockquote><p>说明:加粗的注解为常用注解</p></blockquote><ul><li>Spring提供**<code>@Component</code>**注解的三个衍生注解<ul><li>**<code>@Controller</code>**:用于表现层bean定义</li><li>**<code>@Service</code>**:用于业务层bean定义</li><li><code>@Repository</code>:用于数据层bean定义</li></ul></li></ul><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token annotation punctuation">@Repository</span><span class="token punctuation">(</span><span class="token string">"bookDao"</span><span class="token punctuation">)</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">BookDaoImpl</span> <span class="token keyword">implements</span> <span class="token class-name">BookDao</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token annotation punctuation">@Service</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">BookServiceImpl</span> <span class="token keyword">implements</span> <span class="token class-name">BookService</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h3 id="2-纯注解开发模式【重点】"><a href="#2-纯注解开发模式【重点】" class="headerlink" title="2 纯注解开发模式【重点】"></a>2 纯注解开发模式【重点】</h3><h4 id="问题导入-4"><a href="#问题导入-4" class="headerlink" title="问题导入"></a>问题导入</h4><p>问题1:配置类上使用什么注解表示该类是一个配置类?</p><p>问题2:配置类上使用什么注解进行Spring注解包扫描?</p><h4 id="2-1-纯注解开发模式介绍"><a href="#2-1-纯注解开发模式介绍" class="headerlink" title="2.1 纯注解开发模式介绍"></a>2.1 纯注解开发模式介绍</h4><ul><li>Spring3.0开启了纯注解开发模式,使用Java类替代配置文件,开启了Spring快速开发赛道</li><li>Java类代替Spring核心配置文件</li></ul><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210803192052811.png" alt="image-20210803192052811"></p><ul><li>@Configuration注解用于设定当前类为配置类</li><li>@ComponentScan注解用于设定扫描路径,此注解只能添加一次,多个数据请用数组格式</li></ul><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token annotation punctuation">@ComponentScan</span><span class="token punctuation">(</span><span class="token punctuation">{</span>com<span class="token punctuation">.</span>itheima<span class="token punctuation">.</span>service<span class="token string">","</span>com<span class="token punctuation">.</span>itheima<span class="token punctuation">.</span>dao"<span class="token punctuation">}</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><ul><li>读取Spring核心配置文件初始化容器对象切换为读取Java配置类初始化容器对象</li></ul><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">//加载配置文件初始化容器</span><span class="token class-name">ApplicationContext</span> ctx <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ClassPathXmlApplicationContext</span><span class="token punctuation">(</span><span class="token string">"applicationContext.xml"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//加载配置类初始化容器</span><span class="token class-name">ApplicationContext</span> ctx <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">AnnotationConfigApplicationContext</span><span class="token punctuation">(</span><span class="token class-name">SpringConfig</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre><h4 id="2-2-代码演示"><a href="#2-2-代码演示" class="headerlink" title="2.2 代码演示"></a>2.2 代码演示</h4><p><strong>【第一步】定义配置类代替配置文件</strong></p><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">//声明当前类为Spring配置类</span><span class="token annotation punctuation">@Configuration</span><span class="token comment">//Spring注解扫描,相当于<context:component-scan base-package="com.itheima"/></span><span class="token annotation punctuation">@ComponentScan</span><span class="token punctuation">(</span><span class="token string">"com.itheima"</span><span class="token punctuation">)</span><span class="token comment">//设置bean扫描路径,多个路径书写为字符串数组格式</span><span class="token comment">//@ComponentScan({"com.itheima.service","com.itheima.dao"})</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">SpringConfig</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p><strong>【第二步】在测试类中加载配置类,获取Bean对象并使用</strong></p><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">AppForAnnotation</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">//AnnotationConfigApplicationContext加载Spring配置类初始化Spring容器</span> <span class="token class-name">ApplicationContext</span> ctx <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">AnnotationConfigApplicationContext</span><span class="token punctuation">(</span><span class="token class-name">SpringConfig</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">BookDao</span> bookDao <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token class-name">BookDao</span><span class="token punctuation">)</span> ctx<span class="token punctuation">.</span><span class="token function">getBean</span><span class="token punctuation">(</span><span class="token string">"bookDao"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>bookDao<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//按类型获取bean</span> <span class="token class-name">BookService</span> bookService <span class="token operator">=</span> ctx<span class="token punctuation">.</span><span class="token function">getBean</span><span class="token punctuation">(</span><span class="token class-name">BookService</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>bookService<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h3 id="3-注解开发Bean作用范围和生命周期管理"><a href="#3-注解开发Bean作用范围和生命周期管理" class="headerlink" title="3 注解开发Bean作用范围和生命周期管理"></a>3 注解开发Bean作用范围和生命周期管理</h3><h4 id="问题导入-5"><a href="#问题导入-5" class="headerlink" title="问题导入"></a>问题导入</h4><p>在类上使用什么注解定义Bean的作用范围?</p><h4 id="3-1-bean作用范围注解配置"><a href="#3-1-bean作用范围注解配置" class="headerlink" title="3.1 bean作用范围注解配置"></a>3.1 bean作用范围注解配置</h4><ul><li>使用@Scope定义bean作用范围</li></ul><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token annotation punctuation">@Repository</span><span class="token annotation punctuation">@Scope</span><span class="token punctuation">(</span><span class="token string">"singleton"</span><span class="token punctuation">)</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">BookDaoImpl</span> <span class="token keyword">implements</span> <span class="token class-name">BookDao</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre><h4 id="3-2-bean生命周期注解配置"><a href="#3-2-bean生命周期注解配置" class="headerlink" title="3.2 bean生命周期注解配置"></a>3.2 bean生命周期注解配置</h4><ul><li>使用@PostConstruct、@PreDestroy定义bean生命周期</li></ul><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token annotation punctuation">@Repository</span><span class="token annotation punctuation">@Scope</span><span class="token punctuation">(</span><span class="token string">"singleton"</span><span class="token punctuation">)</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">BookDaoImpl</span> <span class="token keyword">implements</span> <span class="token class-name">BookDao</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token class-name">BookDaoImpl</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"book dao constructor ..."</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token annotation punctuation">@PostConstruct</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"book init ..."</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token annotation punctuation">@PreDestroy</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">destroy</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"book destory ..."</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>==<strong>注意:@PostConstruct和@PreDestroy注解是jdk中提供的注解,从jdk9开始,jdk中的javax.annotation包被移除了,也就是说这两个注解就用不了了,可以额外导入一下依赖解决这个问题。</strong>==</p><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>dependency</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>groupId</span><span class="token punctuation">></span></span>javax.annotation<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>groupId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>artifactId</span><span class="token punctuation">></span></span>javax.annotation-api<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>artifactId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>version</span><span class="token punctuation">></span></span>1.3.2<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>version</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>dependency</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre><h3 id="4-注解开发依赖注入【重点】"><a href="#4-注解开发依赖注入【重点】" class="headerlink" title="4 注解开发依赖注入【重点】"></a>4 注解开发依赖注入【重点】</h3><h4 id="问题导入-6"><a href="#问题导入-6" class="headerlink" title="问题导入"></a>问题导入</h4><p>问题1:请描述@Autowired注解是如何进行自动装配的?</p><p>问题2:请描述@Qualifier注解的作用</p><h4 id="4-1-使用-Autowired注解开启自动装配模式(按类型)"><a href="#4-1-使用-Autowired注解开启自动装配模式(按类型)" class="headerlink" title="4.1 使用@Autowired注解开启自动装配模式(按类型)"></a>4.1 使用@Autowired注解开启自动装配模式(按类型)</h4><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token annotation punctuation">@Service</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">BookServiceImpl</span> <span class="token keyword">implements</span> <span class="token class-name">BookService</span> <span class="token punctuation">{</span> <span class="token comment">//@Autowired:注入引用类型,自动装配模式,默认按类型装配</span> <span class="token annotation punctuation">@Autowired</span> <span class="token keyword">private</span> <span class="token class-name">BookDao</span> bookDao<span class="token punctuation">;</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">save</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"book service save ..."</span><span class="token punctuation">)</span><span class="token punctuation">;</span> bookDao<span class="token punctuation">.</span><span class="token function">save</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><blockquote><p>说明:不管是使用配置文件还是配置类,都必须进行对应的Spring注解包扫描才可以使用。@Autowired默认按照类型自动装配,如果IOC容器中同类的Bean有多个,那么默认按照变量名和Bean的名称匹配,建议使用@Qualifier注解指定要装配的bean名称</p></blockquote><p>==注意:自动装配基于反射设计创建对象并暴力反射对应属性为私有属性初始化数据,因此无需提供setter方法。==</p><h4 id="4-2-使用-Qualifier注解指定要装配的bean名称"><a href="#4-2-使用-Qualifier注解指定要装配的bean名称" class="headerlink" title="4.2 使用@Qualifier注解指定要装配的bean名称"></a>4.2 使用@Qualifier注解指定要装配的bean名称</h4><blockquote><p>目的:解决IOC容器中同类型Bean有多个装配哪一个的问题</p></blockquote><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token annotation punctuation">@Service</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">BookServiceImpl</span> <span class="token keyword">implements</span> <span class="token class-name">BookService</span> <span class="token punctuation">{</span> <span class="token comment">//@Autowired:注入引用类型,自动装配模式,默认按类型装配</span> <span class="token annotation punctuation">@Autowired</span> <span class="token comment">//@Qualifier:自动装配bean时按bean名称装配</span> <span class="token annotation punctuation">@Qualifier</span><span class="token punctuation">(</span><span class="token string">"bookDao"</span><span class="token punctuation">)</span> <span class="token keyword">private</span> <span class="token class-name">BookDao</span> bookDao<span class="token punctuation">;</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">save</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"book service save ..."</span><span class="token punctuation">)</span><span class="token punctuation">;</span> bookDao<span class="token punctuation">.</span><span class="token function">save</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>==注意:@Qualifier注解无法单独使用,必须配合@Autowired注解使用==</p><h4 id="4-3-使用-Value实现简单类型注入"><a href="#4-3-使用-Value实现简单类型注入" class="headerlink" title="4.3 使用@Value实现简单类型注入"></a>4.3 使用@Value实现简单类型注入</h4><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token annotation punctuation">@Repository</span><span class="token punctuation">(</span><span class="token string">"bookDao"</span><span class="token punctuation">)</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">BookDaoImpl</span> <span class="token keyword">implements</span> <span class="token class-name">BookDao</span> <span class="token punctuation">{</span> <span class="token comment">//@Value:注入简单类型(无需提供set方法)</span> <span class="token annotation punctuation">@Value</span><span class="token punctuation">(</span><span class="token string">"${name}"</span><span class="token punctuation">)</span> <span class="token keyword">private</span> <span class="token class-name">String</span> name<span class="token punctuation">;</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">save</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"book dao save ..."</span> <span class="token operator">+</span> name<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>以上@Value注解中使用${name}从属性文件中读取name值,那么就需要在配置类或者配置文件中加载属性文件。</p><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token annotation punctuation">@Configuration</span><span class="token annotation punctuation">@ComponentScan</span><span class="token punctuation">(</span><span class="token string">"com.itheima"</span><span class="token punctuation">)</span><span class="token comment">//@PropertySource加载properties配置文件</span><span class="token annotation punctuation">@PropertySource</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string">"classpath:jdbc.properties"</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token comment">//{}可以省略不写</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">SpringConfig</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>==注意:@PropertySource()中加载多文件请使用数组格式配置,不允许使用通配符*==</p><h3 id="5-注解开发管理第三方Bean【重点】"><a href="#5-注解开发管理第三方Bean【重点】" class="headerlink" title="5 注解开发管理第三方Bean【重点】"></a>5 注解开发管理第三方Bean【重点】</h3><h4 id="问题导入-7"><a href="#问题导入-7" class="headerlink" title="问题导入"></a>问题导入</h4><p>导入自己定义的配置类有几种方式?</p><h4 id="【第一步】单独定义配置类"><a href="#【第一步】单独定义配置类" class="headerlink" title="【第一步】单独定义配置类"></a><strong>【第一步】单独定义配置类</strong></h4><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">JdbcConfig</span> <span class="token punctuation">{</span> <span class="token comment">//@Bean:表示当前方法的返回值是一个bean对象,添加到IOC容器中</span> <span class="token annotation punctuation">@Bean</span> <span class="token keyword">public</span> <span class="token class-name">DataSource</span> <span class="token function">dataSource</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token class-name">DruidDataSource</span> ds <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">DruidDataSource</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> ds<span class="token punctuation">.</span><span class="token function">setDriverClassName</span><span class="token punctuation">(</span><span class="token string">"com.mysql.jdbc.Driver"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> ds<span class="token punctuation">.</span><span class="token function">setUrl</span><span class="token punctuation">(</span><span class="token string">"jdbc:mysql://localhost:3306/spring_db"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> ds<span class="token punctuation">.</span><span class="token function">setUsername</span><span class="token punctuation">(</span><span class="token string">"root"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> ds<span class="token punctuation">.</span><span class="token function">setPassword</span><span class="token punctuation">(</span><span class="token string">"root"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> ds<span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h4 id="【第二步】将独立的配置类加入核心配置"><a href="#【第二步】将独立的配置类加入核心配置" class="headerlink" title="【第二步】将独立的配置类加入核心配置"></a><strong>【第二步】将独立的配置类加入核心配置</strong></h4><h5 id="方式1:-Import注解导入式"><a href="#方式1:-Import注解导入式" class="headerlink" title="方式1:@Import注解导入式"></a>方式1:@Import注解导入式</h5><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token annotation punctuation">@Configuration</span><span class="token annotation punctuation">@ComponentScan</span><span class="token punctuation">(</span><span class="token string">"com.itheima"</span><span class="token punctuation">)</span><span class="token comment">//@Import:导入配置信息</span><span class="token annotation punctuation">@Import</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token class-name">JdbcConfig</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">SpringConfig</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h5 id="方式2:-ComponentScan扫描式"><a href="#方式2:-ComponentScan扫描式" class="headerlink" title="方式2:@ComponentScan扫描式"></a>方式2:@ComponentScan扫描式</h5><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token annotation punctuation">@Configuration</span><span class="token annotation punctuation">@ComponentScan</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string">"com.itheima.config"</span><span class="token punctuation">,</span><span class="token string">"com.itheima.service"</span><span class="token punctuation">,</span><span class="token string">"com.itheima.dao"</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token comment">//只要com.itheima.config包扫到了就行,三个包可以合并写成com.itheima</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">SpringConfig</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre><h3 id="6-注解开发为第三方Bean注入资源【重点】"><a href="#6-注解开发为第三方Bean注入资源【重点】" class="headerlink" title="6 注解开发为第三方Bean注入资源【重点】"></a>6 注解开发为第三方Bean注入资源【重点】</h3><h4 id="问题导入-8"><a href="#问题导入-8" class="headerlink" title="问题导入"></a>问题导入</h4><p>配置类中如何注入简单类型数据,如何注入引用类型数据?</p><h4 id="6-1-简单类型依赖注入"><a href="#6-1-简单类型依赖注入" class="headerlink" title="6.1 简单类型依赖注入"></a>6.1 简单类型依赖注入</h4><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">JdbcConfig</span> <span class="token punctuation">{</span> <span class="token comment">//1.定义一个方法获得要管理的对象</span> <span class="token annotation punctuation">@Value</span><span class="token punctuation">(</span><span class="token string">"com.mysql.jdbc.Driver"</span><span class="token punctuation">)</span> <span class="token keyword">private</span> <span class="token class-name">String</span> driver<span class="token punctuation">;</span> <span class="token annotation punctuation">@Value</span><span class="token punctuation">(</span><span class="token string">"jdbc:mysql://localhost:3306/spring_db"</span><span class="token punctuation">)</span> <span class="token keyword">private</span> <span class="token class-name">String</span> url<span class="token punctuation">;</span> <span class="token annotation punctuation">@Value</span><span class="token punctuation">(</span><span class="token string">"root"</span><span class="token punctuation">)</span> <span class="token keyword">private</span> <span class="token class-name">String</span> userName<span class="token punctuation">;</span> <span class="token annotation punctuation">@Value</span><span class="token punctuation">(</span><span class="token string">"root"</span><span class="token punctuation">)</span> <span class="token keyword">private</span> <span class="token class-name">String</span> password<span class="token punctuation">;</span> <span class="token comment">//2.@Bean:表示当前方法的返回值是一个bean对象,添加到IOC容器中</span> <span class="token annotation punctuation">@Bean</span> <span class="token keyword">public</span> <span class="token class-name">DataSource</span> <span class="token function">dataSource</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token class-name">DruidDataSource</span> ds <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">DruidDataSource</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> ds<span class="token punctuation">.</span><span class="token function">setDriverClassName</span><span class="token punctuation">(</span>driver<span class="token punctuation">)</span><span class="token punctuation">;</span> ds<span class="token punctuation">.</span><span class="token function">setUrl</span><span class="token punctuation">(</span>url<span class="token punctuation">)</span><span class="token punctuation">;</span> ds<span class="token punctuation">.</span><span class="token function">setUsername</span><span class="token punctuation">(</span>userName<span class="token punctuation">)</span><span class="token punctuation">;</span> ds<span class="token punctuation">.</span><span class="token function">setPassword</span><span class="token punctuation">(</span>password<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> ds<span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><blockquote><p>说明:如果@Value()中使用了EL表达式读取properties属性文件中的内容,那么就需要加载properties属性文件。</p></blockquote><h4 id="6-2-引用类型依赖注入"><a href="#6-2-引用类型依赖注入" class="headerlink" title="6.2 引用类型依赖注入"></a>6.2 引用类型依赖注入</h4><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">//Spring会自动从IOC容器中找到BookDao对象赋值给参数bookDao变量,如果没有就会报错。</span><span class="token annotation punctuation">@Bean</span> <span class="token keyword">public</span> <span class="token class-name">DataSource</span> <span class="token function">dataSource</span><span class="token punctuation">(</span><span class="token class-name">BookDao</span> bookDao<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>bookDao<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">DruidDataSource</span> ds <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">DruidDataSource</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> ds<span class="token punctuation">.</span><span class="token function">setDriverClassName</span><span class="token punctuation">(</span>driver<span class="token punctuation">)</span><span class="token punctuation">;</span> ds<span class="token punctuation">.</span><span class="token function">setUrl</span><span class="token punctuation">(</span>url<span class="token punctuation">)</span><span class="token punctuation">;</span> ds<span class="token punctuation">.</span><span class="token function">setUsername</span><span class="token punctuation">(</span>userName<span class="token punctuation">)</span><span class="token punctuation">;</span> ds<span class="token punctuation">.</span><span class="token function">setPassword</span><span class="token punctuation">(</span>password<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> ds<span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><blockquote><p>说明:引用类型注入只需要为bean定义方法设置形参即可,容器会根据类型自动装配对象</p></blockquote><h3 id="7-注解开发总结"><a href="#7-注解开发总结" class="headerlink" title="7 注解开发总结"></a>7 注解开发总结</h3><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210730113548708.png" alt="image-20210730113548708"></p><h2 id="四、Spring整合其他技术【重点】"><a href="#四、Spring整合其他技术【重点】" class="headerlink" title="四、Spring整合其他技术【重点】"></a>四、Spring整合其他技术【重点】</h2><h3 id="1-Spring整合mybatis【重点】"><a href="#1-Spring整合mybatis【重点】" class="headerlink" title="1 Spring整合mybatis【重点】"></a>1 Spring整合mybatis【重点】</h3><h4 id="1-1-思路分析"><a href="#1-1-思路分析" class="headerlink" title="1.1 思路分析"></a>1.1 思路分析</h4><h5 id="问题导入-9"><a href="#问题导入-9" class="headerlink" title="问题导入"></a>问题导入</h5><p>mybatis进行数据层操作的核心对象是谁?</p><h5 id="1-1-1-MyBatis程序核心对象分析"><a href="#1-1-1-MyBatis程序核心对象分析" class="headerlink" title="1.1.1 MyBatis程序核心对象分析"></a>1.1.1 MyBatis程序核心对象分析</h5><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210730114303147.png" alt="image-20210730114303147"></p><h5 id="1-1-2-整合MyBatis"><a href="#1-1-2-整合MyBatis" class="headerlink" title="1.1.2 整合MyBatis"></a>1.1.2 整合MyBatis</h5><ul><li>使用SqlSessionFactoryBean封装SqlSessionFactory需要的环境信息</li></ul><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210730114342060.png" alt="image-20210730114342060"></p><ul><li>使用MapperScannerConfigurer加载Dao接口,创建代理对象保存到IOC容器中</li></ul><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210730114356175.png" alt="image-20210730114356175"></p><h4 id="1-2-代码实现"><a href="#1-2-代码实现" class="headerlink" title="1.2 代码实现"></a>1.2 代码实现</h4><h5 id="问题导入-10"><a href="#问题导入-10" class="headerlink" title="问题导入"></a>问题导入</h5><p>问题1:Spring整合mybatis的依赖叫什么?</p><p>问题2:Spring整合mybatis需要管理配置哪两个Bean,这两个Bean作用分别是什么?</p><h5 id="【前置工作】"><a href="#【前置工作】" class="headerlink" title="【前置工作】"></a>【前置工作】</h5><ol><li>在pom.xml中添加spring-context、druid、mybatis、mysql-connector-java等基础依赖。</li><li>准备service和dao层基础代码</li></ol><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">AccountService</span> <span class="token punctuation">{</span> <span class="token keyword">void</span> <span class="token function">save</span><span class="token punctuation">(</span><span class="token class-name">Account</span> account<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">void</span> <span class="token function">delete</span><span class="token punctuation">(</span><span class="token class-name">Integer</span> id<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">void</span> <span class="token function">update</span><span class="token punctuation">(</span><span class="token class-name">Account</span> account<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">Account</span><span class="token punctuation">></span></span> <span class="token function">findAll</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">Account</span> <span class="token function">findById</span><span class="token punctuation">(</span><span class="token class-name">Integer</span> id<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token annotation punctuation">@Service</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">AccountServiceImpl</span> <span class="token keyword">implements</span> <span class="token class-name">AccountService</span> <span class="token punctuation">{</span> <span class="token annotation punctuation">@Autowired</span> <span class="token keyword">private</span> <span class="token class-name">AccountDao</span> accountDao<span class="token punctuation">;</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">save</span><span class="token punctuation">(</span><span class="token class-name">Account</span> account<span class="token punctuation">)</span> <span class="token punctuation">{</span> accountDao<span class="token punctuation">.</span><span class="token function">save</span><span class="token punctuation">(</span>account<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">update</span><span class="token punctuation">(</span><span class="token class-name">Account</span> account<span class="token punctuation">)</span><span class="token punctuation">{</span> accountDao<span class="token punctuation">.</span><span class="token function">update</span><span class="token punctuation">(</span>account<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">delete</span><span class="token punctuation">(</span><span class="token class-name">Integer</span> id<span class="token punctuation">)</span> <span class="token punctuation">{</span> accountDao<span class="token punctuation">.</span><span class="token function">delete</span><span class="token punctuation">(</span>id<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token class-name">Account</span> <span class="token function">findById</span><span class="token punctuation">(</span><span class="token class-name">Integer</span> id<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> accountDao<span class="token punctuation">.</span><span class="token function">findById</span><span class="token punctuation">(</span>id<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">Account</span><span class="token punctuation">></span></span> <span class="token function">findAll</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> accountDao<span class="token punctuation">.</span><span class="token function">findAll</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">AccountDao</span> <span class="token punctuation">{</span> <span class="token annotation punctuation">@Insert</span><span class="token punctuation">(</span><span class="token string">"insert into tbl_account(name,money)values(#{name},#{money})"</span><span class="token punctuation">)</span> <span class="token keyword">void</span> <span class="token function">save</span><span class="token punctuation">(</span><span class="token class-name">Account</span> account<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token annotation punctuation">@Delete</span><span class="token punctuation">(</span><span class="token string">"delete from tbl_account where id = #{id} "</span><span class="token punctuation">)</span> <span class="token keyword">void</span> <span class="token function">delete</span><span class="token punctuation">(</span><span class="token class-name">Integer</span> id<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token annotation punctuation">@Update</span><span class="token punctuation">(</span><span class="token string">"update tbl_account set name = #{name} , money = #{money} where id = #{id} "</span><span class="token punctuation">)</span> <span class="token keyword">void</span> <span class="token function">update</span><span class="token punctuation">(</span><span class="token class-name">Account</span> account<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token annotation punctuation">@Select</span><span class="token punctuation">(</span><span class="token string">"select * from tbl_account"</span><span class="token punctuation">)</span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">Account</span><span class="token punctuation">></span></span> <span class="token function">findAll</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token annotation punctuation">@Select</span><span class="token punctuation">(</span><span class="token string">"select * from tbl_account where id = #{id} "</span><span class="token punctuation">)</span> <span class="token class-name">Account</span> <span class="token function">findById</span><span class="token punctuation">(</span><span class="token class-name">Integer</span> id<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h5 id="【第一步】导入Spring整合Mybatis依赖"><a href="#【第一步】导入Spring整合Mybatis依赖" class="headerlink" title="【第一步】导入Spring整合Mybatis依赖"></a><strong>【第一步】导入Spring整合Mybatis依赖</strong></h5><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>dependency</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>groupId</span><span class="token punctuation">></span></span>org.springframework<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>groupId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>artifactId</span><span class="token punctuation">></span></span>spring-jdbc<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>artifactId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>version</span><span class="token punctuation">></span></span>5.2.10.RELEASE<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>version</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>dependency</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>dependency</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>groupId</span><span class="token punctuation">></span></span>org.mybatis<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>groupId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>artifactId</span><span class="token punctuation">></span></span>mybatis-spring<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>artifactId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>version</span><span class="token punctuation">></span></span>1.3.0<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>version</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>dependency</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h5 id="【第二步】创建JdbcConfig配置DataSource数据源"><a href="#【第二步】创建JdbcConfig配置DataSource数据源" class="headerlink" title="【第二步】创建JdbcConfig配置DataSource数据源"></a><strong>【第二步】创建JdbcConfig配置DataSource数据源</strong></h5><pre class="line-numbers language-properties" data-language="properties"><code class="language-properties"><span class="token key attr-name">jdbc.driver</span><span class="token punctuation">=</span><span class="token value attr-value">com.mysql.jdbc.Driver</span><span class="token key attr-name">jdbc.url</span><span class="token punctuation">=</span><span class="token value attr-value">jdbc:mysql://localhost:3306/spring_db?useSSL=false</span><span class="token key attr-name">jdbc.username</span><span class="token punctuation">=</span><span class="token value attr-value">root</span><span class="token key attr-name">jdbc.password</span><span class="token punctuation">=</span><span class="token value attr-value">root</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">JdbcConfig</span> <span class="token punctuation">{</span> <span class="token annotation punctuation">@Value</span><span class="token punctuation">(</span><span class="token string">"${jdbc.driver}"</span><span class="token punctuation">)</span> <span class="token keyword">private</span> <span class="token class-name">String</span> driver<span class="token punctuation">;</span> <span class="token annotation punctuation">@Value</span><span class="token punctuation">(</span><span class="token string">"${jdbc.url}"</span><span class="token punctuation">)</span> <span class="token keyword">private</span> <span class="token class-name">String</span> url<span class="token punctuation">;</span> <span class="token annotation punctuation">@Value</span><span class="token punctuation">(</span><span class="token string">"${jdbc.username}"</span><span class="token punctuation">)</span> <span class="token keyword">private</span> <span class="token class-name">String</span> userName<span class="token punctuation">;</span> <span class="token annotation punctuation">@Value</span><span class="token punctuation">(</span><span class="token string">"${jdbc.password}"</span><span class="token punctuation">)</span> <span class="token keyword">private</span> <span class="token class-name">String</span> password<span class="token punctuation">;</span> <span class="token annotation punctuation">@Bean</span> <span class="token keyword">public</span> <span class="token class-name">DataSource</span> <span class="token function">dataSource</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token class-name">DruidDataSource</span> ds <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">DruidDataSource</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> ds<span class="token punctuation">.</span><span class="token function">setDriverClassName</span><span class="token punctuation">(</span>driver<span class="token punctuation">)</span><span class="token punctuation">;</span> ds<span class="token punctuation">.</span><span class="token function">setUrl</span><span class="token punctuation">(</span>url<span class="token punctuation">)</span><span class="token punctuation">;</span> ds<span class="token punctuation">.</span><span class="token function">setUsername</span><span class="token punctuation">(</span>userName<span class="token punctuation">)</span><span class="token punctuation">;</span> ds<span class="token punctuation">.</span><span class="token function">setPassword</span><span class="token punctuation">(</span>password<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> ds<span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h5 id="【第三步】创建MybatisConfig整合mybatis"><a href="#【第三步】创建MybatisConfig整合mybatis" class="headerlink" title="【第三步】创建MybatisConfig整合mybatis"></a><strong>【第三步】创建MybatisConfig整合mybatis</strong></h5><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">MybatisConfig</span> <span class="token punctuation">{</span> <span class="token comment">//定义bean,SqlSessionFactoryBean,用于产生SqlSessionFactory对象</span> <span class="token annotation punctuation">@Bean</span> <span class="token keyword">public</span> <span class="token class-name">SqlSessionFactoryBean</span> <span class="token function">sqlSessionFactory</span><span class="token punctuation">(</span><span class="token class-name">DataSource</span> dataSource<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token class-name">SqlSessionFactoryBean</span> ssfb <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SqlSessionFactoryBean</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> ssfb<span class="token punctuation">.</span><span class="token function">setTypeAliasesPackage</span><span class="token punctuation">(</span><span class="token string">"com.itheima.domain"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> ssfb<span class="token punctuation">.</span><span class="token function">setDataSource</span><span class="token punctuation">(</span>dataSource<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> ssfb<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">//定义bean,返回MapperScannerConfigurer对象</span> <span class="token annotation punctuation">@Bean</span> <span class="token keyword">public</span> <span class="token class-name">MapperScannerConfigurer</span> <span class="token function">mapperScannerConfigurer</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token class-name">MapperScannerConfigurer</span> msc <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">MapperScannerConfigurer</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> msc<span class="token punctuation">.</span><span class="token function">setBasePackage</span><span class="token punctuation">(</span><span class="token string">"com.itheima.dao"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> msc<span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h5 id="【第四步】创建SpringConfig主配置类进行包扫描和加载其他配置类"><a href="#【第四步】创建SpringConfig主配置类进行包扫描和加载其他配置类" class="headerlink" title="【第四步】创建SpringConfig主配置类进行包扫描和加载其他配置类"></a><strong>【第四步】创建SpringConfig主配置类进行包扫描和加载其他配置类</strong></h5><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token annotation punctuation">@Configuration</span><span class="token annotation punctuation">@ComponentScan</span><span class="token punctuation">(</span><span class="token string">"com.itheima"</span><span class="token punctuation">)</span><span class="token comment">//@PropertySource:加载类路径jdbc.properties文件</span><span class="token annotation punctuation">@PropertySource</span><span class="token punctuation">(</span><span class="token string">"classpath:jdbc.properties"</span><span class="token punctuation">)</span><span class="token annotation punctuation">@Import</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token class-name">JdbcConfig</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">,</span><span class="token class-name">MybatisConfig</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">SpringConfig</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h5 id="【第五步】定义测试类进行测试"><a href="#【第五步】定义测试类进行测试" class="headerlink" title="【第五步】定义测试类进行测试"></a><strong>【第五步】定义测试类进行测试</strong></h5><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">App</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">ApplicationContext</span> ctx <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">AnnotationConfigApplicationContext</span><span class="token punctuation">(</span><span class="token class-name">SpringConfig</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">AccountService</span> accountService <span class="token operator">=</span> ctx<span class="token punctuation">.</span><span class="token function">getBean</span><span class="token punctuation">(</span><span class="token class-name">AccountService</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">Account</span> ac <span class="token operator">=</span> accountService<span class="token punctuation">.</span><span class="token function">findById</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>ac<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h3 id="2-Spring整合Junit单元测试【重点】"><a href="#2-Spring整合Junit单元测试【重点】" class="headerlink" title="2 Spring整合Junit单元测试【重点】"></a>2 Spring整合Junit单元测试【重点】</h3><h4 id="问题导入-11"><a href="#问题导入-11" class="headerlink" title="问题导入"></a>问题导入</h4><p>Spring整合Junit的两个注解作用分别是什么?</p><h4 id="【第一步】导入整合的依赖坐标spring-test"><a href="#【第一步】导入整合的依赖坐标spring-test" class="headerlink" title="【第一步】导入整合的依赖坐标spring-test"></a>【第一步】导入整合的依赖坐标spring-test</h4><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token comment"><!--junit--></span><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>dependency</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>groupId</span><span class="token punctuation">></span></span>junit<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>groupId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>artifactId</span><span class="token punctuation">></span></span>junit<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>artifactId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>version</span><span class="token punctuation">></span></span>4.12<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>version</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>dependency</span><span class="token punctuation">></span></span><span class="token comment"><!--spring整合junit--></span><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>dependency</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>groupId</span><span class="token punctuation">></span></span>org.springframework<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>groupId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>artifactId</span><span class="token punctuation">></span></span>spring-test<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>artifactId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>version</span><span class="token punctuation">></span></span>5.1.9.RELEASE<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>version</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>dependency</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h4 id="【第二步】使用Spring整合Junit专用的类加载器"><a href="#【第二步】使用Spring整合Junit专用的类加载器" class="headerlink" title="【第二步】使用Spring整合Junit专用的类加载器"></a>【第二步】使用Spring整合Junit专用的类加载器</h4><h4 id="【第三步】加载配置文件或者配置类"><a href="#【第三步】加载配置文件或者配置类" class="headerlink" title="【第三步】加载配置文件或者配置类"></a>【第三步】加载配置文件或者配置类</h4><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">//【第二步】使用Spring整合Junit专用的类加载器</span><span class="token annotation punctuation">@RunWith</span><span class="token punctuation">(</span><span class="token class-name">SpringJUnit4ClassRunner</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token comment">//【第三步】加载配置文件或者配置类</span><span class="token annotation punctuation">@ContextConfiguration</span><span class="token punctuation">(</span>classes <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token class-name">SpringConfiguration</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token comment">//加载配置类</span><span class="token comment">//@ContextConfiguration(locations={"classpath:applicationContext.xml"})//加载配置文件</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">AccountServiceTest</span> <span class="token punctuation">{</span> <span class="token comment">//支持自动装配注入bean</span> <span class="token annotation punctuation">@Autowired</span> <span class="token keyword">private</span> <span class="token class-name">AccountService</span> accountService<span class="token punctuation">;</span> <span class="token annotation punctuation">@Test</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testFindById</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>accountService<span class="token punctuation">.</span><span class="token function">findById</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token annotation punctuation">@Test</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testFindAll</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>accountService<span class="token punctuation">.</span><span class="token function">findAll</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p><strong>==注意:junit的依赖至少要是4.12版本,可以是4.13等版本,否则出现如下异常:==</strong></p><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20200831155517797.png" alt="image-20200831155517797"></p>]]></content>
<summary type="html"><h2 id="今日目标"><a href="#今日目标" class="headerlink" title="今日目标"></a>今日目标</h2><ul>
<li><input disabled="" type="checkbox"> 能够掌握注解开发定义Bean对象</li</summary>
</entry>
<entry>
<title>java基础小点</title>
<link href="http://wlh55.github.io/2022/09/16/java%E5%9F%BA%E7%A1%80%E5%B0%8F%E7%82%B9/"/>
<id>http://wlh55.github.io/2022/09/16/java%E5%9F%BA%E7%A1%80%E5%B0%8F%E7%82%B9/</id>
<published>2022-09-16T11:24:44.000Z</published>
<updated>2022-09-16T11:37:57.110Z</updated>
<content type="html"><![CDATA[<h2 id="java基础小点"><a href="#java基础小点" class="headerlink" title="java基础小点"></a>java基础小点</h2><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLH/image-20220830165525340.png" alt="image-20220830165525340"></p><ul><li><strong>4.4.5&& 和 & 使用区别</strong></li></ul><p>\1) &&短路与:如果第一个条件为 false,则第二个条件不会判断,最终结果为 false,效率高 </p><p>\2) & 逻辑与:不管第一个条件是否为 false,第二个条件都要判断,效率低 </p><p>\3) 开发中, 我们使用的基本是使用短路与&&, 效率高 </p><p><strong>单个或与符号效率低,都要全部条件判断完</strong></p><ul><li><strong>//表达式 1 和表达式 2 要为可以赋给接收变量的类型</strong></li></ul><p>//(或可以自动转换/或者强制转换) </p><p>int a = 3; </p><p>int b = 8; </p><p>int c = a > b ? (int)1.1 : (int)3.4;//可以的 </p><p>double d = a > b ? a : b + 3;//可以的,满足 int -> double</p><ul><li><p><strong>4.7 运算符优先级</strong> </p><p>\1) 运算符有不同的优先级,所谓优先级就是表达式运算中的运算顺序。如右表,上一行运算符总优先于下一行。 </p><p>\2) 只有单目运算符、赋值运算符是从右向左运算的。 </p><p>\3) 一览表, 不要背,使用多了,就熟悉了</p></li></ul><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLH/image-20220830170306976.png" alt="image-20220830170306976"></p><ul><li><strong>4.8.2标识符命名规范[更加专业]</strong></li></ul><p>\1) 包名:多单词组成时所有字母都小写:aaa.bbb.ccc //比如 com.hsp.crm </p><p>\2) 类名、接口名:多单词组成时,所有单词的首字母大写:XxxYyyZzz [大驼峰] </p><p>比如: TankShotGame </p><p>\3) 变量名、方法名:多单词组成时,第一个单词首字母小写,第二个单词开始每个单词首字母大写:xxxYyyZzz [小 </p><p>驼峰, 简称 驼峰法] </p><p>比如: tankShotGame </p><p>\4) 常量名:所有字母都大写。多单词时每个单词用下划线连接:XXX_YYY_ZZZ </p><p>比如 :定义一个所得税率 TAX_RATE </p><p>\5) 后面我们学习到 类,包,接口,等时,我们的命名规范要这样遵守,更加详细的看文档</p><ul><li><strong>4.10 保留字</strong></li></ul><p>4.10.1 介绍 </p><p>Java 保留字:现有 Java 版本<strong>尚未使用</strong>,但<strong>以后版本可能会作为关键字使用</strong>。自己命名标识符时要避免使用这些保留 </p><p>字 byValue、cast、future、 generic、 inner、 operator、 outer、 rest、 var 、 goto 、const </p><p>4.11 <strong>键盘输入语句</strong> </p><p><strong>4.11.1 介绍</strong> </p><p>在编程中,需要接收用户输入的数据,就可以使用键盘输入语句来获取。Input.java , 需要一个 扫描器(对象), 就是 </p><h2 id="Scanner"><a href="#Scanner" class="headerlink" title="Scanner"></a><strong>Scanner</strong></h2><p>4.11.2 步骤 : </p><p>\1) 导入该类的所在包, java.util.* </p><p>\2) 创建该类对象(声明变量) </p><p>\3) 调用里面的功能 </p><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>util<span class="token punctuation">.</span></span><span class="token class-name">Scanner</span></span><span class="token punctuation">;</span><span class="token class-name">Scanner</span> scanner <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Scanner</span><span class="token punctuation">(</span><span class="token class-name">System</span><span class="token punctuation">.</span>in<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"请输入年龄"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">int</span> age <span class="token operator">=</span> scanner<span class="token punctuation">.</span><span class="token function">nextInt</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>age<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre><h2 id="4-12-1-进制介绍"><a href="#4-12-1-进制介绍" class="headerlink" title="4.12.1 进制介绍"></a><strong>4.12.1</strong> 进制介绍</h2><p>对于整数,有四种表示方式: </p><p>二进制:0,1 ,满 2 进 1.以 0b 或 0B 开头。 </p><p>十进制:0-9 ,满 10 进 1。 </p><p>八进制:0-7 ,满 8 进 1. 以数字 0 开头表示。 </p><p>十六进制:0-9 及 A(10)-F(15),满 16 进 1. 以 <strong>0x</strong> <strong>或</strong> <strong>0X</strong> 开头表示。此处的 A-F 不区分大小写</p><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLH/image-20220830171628509.png" alt="image-20220830171628509"></p><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLH/image-20220830200912473.png" alt="image-20220830200912473"></p><ul><li>遇到break才会停</li><li><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLH/image-20220830201850485.png" alt="image-20220830201850485"></li></ul><p>5.7.4注意事项和细节说明 </p><p>ForDetail.java </p><p>\1) 循环条件是返回一个布尔值的表达式 </p><p>\2) for(;循环判断条件;) 中的初始化和变量迭代可以写到其它地方,但是两边的分号不能省略。 </p><p>\3) 循环初始值可以有多条初始化语句,但要求类型一样,并且中间用逗号隔开,循环变量迭代也可以有多条变量迭代 </p><p>语句,中间用逗号隔开</p><h2 id="数组"><a href="#数组" class="headerlink" title="数组"></a>数组</h2><p><strong>6.2.1使用方式 2-动态初始化</strong> </p><p><strong> 先声明数组</strong> </p><p>语法:数据类型 数组名[]; 也可以 数据类型[] 数组名; </p><p>int a[]; 或者 int[] a; </p><p> 创建数组 </p><p>语法: 数组名=new 数据类型[大小]; </p><p>a=new int[10]; </p><p> 案例演示【前面修改即可】</p><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLH/image-20220830205949033.png" alt="image-20220830205949033"></p><p><strong>6.3 数组使用注意事项和细节</strong> </p><p>\1) 数组是多个相同类型数据的组合,实现对这些数据的统一管理 </p><p>\2) 数组中的元素可以是任何数据类型,包括基本类型和引用类型,但是不能混用。 </p><p>\3) 数组创建后,如果没有赋值,有默认值 </p><p>int 0,short 0, byte 0, long 0, float 0.0,double 0.0,char \u0000,boolean false,String null </p><p>\4) 使用数组的步骤 1. 声明数组并开辟空间 2 给数组各个元素赋值 3 使用数组 </p><p>\5) 数组的<strong>下标是从</strong> <strong>0</strong> <strong>开始的</strong>。 </p><p>\6) 数组下标必须在指定范围内使用,否则报:下标越界异常,比如 </p><p>) 数组属引用类型,数组型数据是对象(object)</p><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLH/image-20220830210835104.png" alt="image-20220830210835104"></p><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLH/image-20220830211014736.png" alt="image-20220830211014736"></p><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLH/image-20220830211154790.png" alt="image-20220830211154790"></p><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLH/image-20220830211445244.png" alt="image-20220830211445244"></p><ul><li>查找</li></ul><p>findName.equals(names[i])</p><ul><li>二维数组</li><li><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLH/image-20220830212449011.png" alt="image-20220830212449011"></li></ul><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLH/image-20220830212705391.png" alt="image-20220830212705391"></p><ul><li><p>静态初始化</p></li><li><pre><code class="java">- int[][] arr = {{1,1,1}, {8,8,9}, {100}};- </code></pre></li><li><p><strong>6.16 二维数组使用细节和注意事项</strong> </p><p>\1) 一维数组的声明方式有: </p><p>int[] x 或者 int x[] </p><p>\2) 二维数组的声明方式有: </p><p><strong>int[][] y</strong> <strong>或者</strong> <strong>int[] y[]</strong> <strong>或者</strong> <strong>int y[][]</strong> </p><p>\3) 二维数组实际上是由多个一维数组组成的,它的各个一维数组的长度可以相同,也可以不相同。比如: map[][] 是 </p><p>一个二维数组 </p><p>int map [][] = “{“{1,2},{3,4,5}”}”//因为上传有报错,所以才改成这样</p><p>由 map[0] 是一个含有两个元素的一维数组 ,map[1] 是一个含有三个元素的一维数组构成,我们也称为列数不等 </p><p>的二维数组</p></li></ul><h2 id="类与对象"><a href="#类与对象" class="headerlink" title="类与对象"></a>类与对象</h2><p>\3) 属性如果不赋值,有默认值,规则和数组一致。具体说: int 0,</p><p>short 0, byte 0, long 0, float 0.0,double 0.0,</p><p>char \u0000, </p><p>boolean false,String null</p><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20220902235040084.png" alt="image-20220902235040084"></p><p><strong>7.1.13 类和对象的内存分配机制</strong> </p><p> Java 内存的结构分析 </p><p>\1) 栈: 一般存放基本数据类型(局部变量) </p><p>\2) 堆: 存放对象(Cat cat , 数组等) </p><p>\3) 方法区:常量池(常量,比如字符串), 类加载信息 </p><p>\4) 示意图 [Cat (name, age, price)] </p><p> Java 创建对象的流程简单分析 </p><p>Person p = new Person(); </p><p>p.name = “jack”; </p><p>p.age = 10 </p><p>\1) 先加载 Person 类信息(属性和方法信息, 只会加载一次) </p><p>\2) 在堆中分配空间, 进行默认初始化(看规则) </p><p>\3) 把地址赋给 p , p 就指向对象 </p><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20220902235700230.png" alt="image-20220902235700230"></p><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20220902235943444.png" alt="image-20220902235943444"></p><ul><li><p><strong>引用类型传递的是地址(传递也是值,但是值是地址),可以通过形参影响实参!</strong>***</p></li><li><p><strong>Java中只有基本数据类型,比如int、double、boolean等是值传递,其他一律是引用传递。在Java中数组(如:int [])被认为是对象,也是引用传递,即2个名称指向同一内存地址。</strong></p></li></ul><h2 id="递归"><a href="#递归" class="headerlink" title="递归"></a>递归</h2><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20220903112307463.png" alt="image-20220903112307463"></p><ul><li><p><strong>如果把输出语句放在if后面的else中,则最后只会输出n = 2</strong></p></li><li><p>阶层递归</p></li><li><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20220903113238484.png" alt="image-20220903113238484"></p></li></ul><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20220903113308749.png" alt="image-20220903113308749"></p><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20220903163222983.png" alt="image-20220903163222983"></p><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20220903163256500.png" alt="image-20220903163256500"></p><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20220903163314696.png" alt="image-20220903163314696"></p><h2 id="方法重载"><a href="#方法重载" class="headerlink" title="方法重载"></a>方法重载</h2><p>7.5 方法重载(OverLoad) </p><p>7.5.1基本介绍 </p><p>java 中允许同一个类中,多个同名方法的存在,但要求 形参列表不一致!</p><p>7.5.3快速入门案例 </p><p>OverLoad01.java </p><p>案例:类:MyCalculator 方法:calculate </p><p>\1) calculate(int n1, int n2) //两个整数的和 </p><p>\2) calculate(int n1, double n2) //一个整数,一个 double 的和 </p><p>\3) calculate(double n2, int n1)//一个 double ,一个 Int 和 </p><p>\4) calculate(int n1, int n2,int n3)//三个 int 的和</p><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20220905131425173.png" alt="image-20220905131425173"></p><h2 id="可变参数"><a href="#可变参数" class="headerlink" title="可变参数"></a>可变参数</h2><ul><li>示范</li></ul><p>//1. int… 表示接受的是可变参数,类型是 int ,即可以接收多个 int(0-多) </p><p>//2. 使用可变参数时,可以当做数组来使用 即 nums 可以当做数组 </p><p>//3. 遍历 nums 求和即可 </p><p>public int sum(int… nums) { </p><p>//System.out.println(“接收的参数个数=” + nums.length); </p><p>int res = 0; </p><p>for(int i = 0; i < nums.length; i++) { </p><p>res += nums[i]; </p><p>}</p><p>return res; </p><p>} </p><p>} </p><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20220905132217893.png" alt="image-20220905132217893"></p><h2 id="变量作用域"><a href="#变量作用域" class="headerlink" title="变量作用域"></a>变量作用域</h2><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20220905133225183.png" alt="image-20220905133225183"></p><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20220905133546827.png" alt="image-20220905133546827"></p><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20220905134015572.png" alt="image-20220905134015572"></p><h2 id="构造器"><a href="#构造器" class="headerlink" title="构造器"></a>构造器</h2><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20220907093408492.png" alt="image-20220907093408492"></p><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20220907095133125.png" alt="image-20220907095133125"></p><h2 id="this关键字"><a href="#this关键字" class="headerlink" title="this关键字"></a>this关键字</h2><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20220907095556159.png" alt="image-20220907095556159"></p><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20220907095654921.png" alt="image-20220907095654921"></p><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20220907102554759.png" alt="image-20220907102554759"></p><p>7.10.3 this 的注意事项和使用细节 </p><p>ThisDetail.java </p><p>\1) this 关键字可以用来访问本类的属性、方法、构造器 </p><p>\2) this 用于区分当前类的属性和局部变量 </p><p>\3) 访问成员方法的语法:this.方法名(参数列表); </p><p>\4) 访问构造器语法:this(参数列表); 注意只能在构造器中使用(即只能在构造器中访问另外一个构造器, 必须放在第一 </p><p>条语句) </p><p>\5) this 不能在类定义的外部使用,只能在类定义的方法中使用。</p><h2 id="8-4-7常用的包"><a href="#8-4-7常用的包" class="headerlink" title="8.4.7常用的包"></a>8.4.7常用的包</h2><p>一个包下,包含很多的类,java 中常用的包有: </p><p>\1) java.lang.* </p><p>//lang 包是基本包,默认引入,不需要再引入. </p><p>\2) java.util.* </p><p>//util 包,系统提供的工具包, 工具类,使用 Scanner </p><p>\3) java.net.* </p><p>//网络包,网络开发 </p><p>\4) java.awt.* //是做 java 的界面开发,GUI</p><h2 id="8-5-访问修饰符"><a href="#8-5-访问修饰符" class="headerlink" title="8.5 访问修饰符"></a>8.5 访问修饰符</h2><p>8.5.1基本介绍 </p><p>java 提供四种访问控制修饰符号,用于控制方法和属性(成员变量)的访问权限(范围): </p><p>\1) 公开级别:用 public 修饰,对外公开 </p><p>\2) 受保护级别:用 protected 修饰,对子类和同一个包中的类公开 </p><p>\3) 默认级别:没有修饰符号,向同一个包的类公开. </p><p>\4) 私有级别:用 private 修饰,只有类本身可以访问,不对外公开.</p><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20220907165259387.png" alt="image-20220907165259387"></p><h2 id="封装"><a href="#封装" class="headerlink" title="封装"></a>封装</h2><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20220907165851671.png" alt="image-20220907165851671"></p><h2 id="继承"><a href="#继承" class="headerlink" title="继承"></a>继承</h2><p><strong>8.8.5继承给编程带来的便利</strong> </p><p><strong>\1) 代码的复用性提高了</strong> </p><p><strong>\2) 代码的扩展性和维护性提高了</strong> </p><p><strong>8.8.6继承的深入讨论/细节问题</strong> </p><p><strong>\1) 子类继承了所有的属性和方法,非私有的属性和方法可以在子类直接访问, 但是私有属性和方法不能在子类直接访</strong> </p><p><strong>问,要通过父类提供公共的方法去访问</strong> </p><p><strong>\2) 子类必须调用父类的构造器, 完成父类的初始化</strong> </p><p><strong>\3) 当创建子类对象时,不管使用子类的哪个构造器,默认情况下总会去调用父类的无参构造器,如果父类没有提供无</strong> </p><p><strong>参构造器,则必须在子类的构造器中用 super 去指定使用父类的哪个构造器完成对父类的初始化工作,否则,编译</strong> </p><p><strong>不会通过(怎么理解。) [举例说明]</strong> </p><p><strong>\4) 如果希望指定去调用父类的某个构造器,则显式的调用一下 : super(参数列表)</strong> </p><p><strong>\5) super 在使用时,必须放在构造器第一行(super 只能在构造器中使用)</strong> </p><p><strong>\6) super() 和 this() 都只能放在构造器第一行,因此这两个方法不能共存在一个构造器</strong> </p><p><strong>\7) java 所有类都是 Object 类的子类, Object 是所有类的基类.</strong> </p><p><strong>\8) 父类构造器的调用不限于直接父类!将一直往上追溯直到 Object 类(顶级父类)</strong> </p><p><strong>\9) 子类最多只能继承一个父类(指直接继承),即 java 中是单继承机制。</strong> </p><p><strong>思考:如何让 A 类继承 B 类和 C 类? 【A 继承 B, B 继承 C】</strong> </p><p><strong>\10) 不能滥用继承,子类和父类之间必须满足 is-a 的逻辑关系</strong> </p><h2 id="super关键字"><a href="#super关键字" class="headerlink" title="super关键字"></a>super关键字</h2><ul><li>super 代表父类的引用,用于<strong>访问父类的属性、方法、构造</strong>器</li></ul><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20220907224650600.png" alt="image-20220907224650600"></p><p>//super 的访问不限于直接父类,如果爷爷类和本类中有同名的成员,也可以使用 super 去访问爷爷类的成员; </p><p>// 如果多个基类(上级类)中都有同名的成员,使用 super 访问遵循就近原则。A->B->C </p><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20220907224815321.png" alt="image-20220907224815321"></p><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20220907224830074.png" alt="image-20220907224830074"></p><h2 id="方法重写-x2F-覆盖-override"><a href="#方法重写-x2F-覆盖-override" class="headerlink" title="方法重写/覆盖(override)"></a>方法重写/覆盖(override)</h2><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20220907224938371.png" alt="image-20220907224938371"></p><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20220907224950139.png" alt="image-20220907224950139"></p><h2 id="面向对象编程-多态"><a href="#面向对象编程-多态" class="headerlink" title="面向对象编程-多态"></a>面向对象编程-多态</h2><ul><li><p>方法的多态</p></li><li><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20220907225128965.png" alt="image-20220907225128965"></p></li><li><p>对象多态</p></li></ul><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20220907225200510.png" alt="image-20220907225200510"></p><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20220907225251989.png" alt="image-20220907225251989"></p><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20220907225324771.png" alt="image-20220907225324771"></p><p><strong>public static void main(String[] args) {</strong> </p><p><strong>//向上转型: 父类的引用指向了子类的对象</strong> </p><p><strong>//语法:父类类型引用名 = new 子类类型();</strong> </p><p><strong>Animal animal = new Cat();</strong> </p><p><strong>Object obj = new Cat();//可以吗? 可以 Object 也是 Cat 的父类</strong> </p><p><strong>//向上转型调用方法的规则如下:</strong> </p><p><strong>//(1)可以调用父类中的所有成员(需遵守访问权限)</strong> </p><p><strong>//(2)但是不能调用子类的特有的成员</strong> </p><p><strong>//(#)因为在编译阶段,能调用哪些成员,是由编译类型来决定的</strong> </p><p><strong>//animal.catchMouse();错误</strong> </p><p><strong>//(4)最终运行效果看子类(运行类型)的具体实现, 即调用方法时,按照从子类(运行类型)开始查找方法</strong> </p><p><strong>//,然后调用,规则我前面我们讲的方法调用规则一致。</strong> </p><p><strong>animal.eat();//猫吃鱼..</strong> </p><p><strong>animal.run();//跑</strong> </p><p><strong>animal.show();//hello,你好</strong> </p><p><strong>animal.sleep();//睡</strong> </p><p><strong>//老师希望,可以调用 Cat 的 catchMouse 方法</strong> </p><p><strong>//多态的向下转型</strong> </p><p><strong>//(1)语法:子类类型 引用名 =(子类类型)父类引用;</strong> </p><p><strong>//问一个问题? cat 的编译类型 Cat,运行类型是 Cat</strong> </p><p><strong>Cat cat = (Cat) animal;</strong> </p><p><strong>cat.catchMouse();//猫抓老鼠</strong> </p><p><strong>//(2)要求父类的引用必须指向的是当前目标类型的对象</strong> </p><p><strong>Dog dog = (Dog) animal; //可以吗?</strong> </p><p><strong>System.out.println(“ok~~”);</strong> </p><p><strong>}</strong> </p><p><strong>}</strong></p><ul><li> instanceOf 比较操作符,用于判断对象的运行类型是否为 XX 类型或 XX 类型的子类型【举例说明】PolyDetail03.java</li></ul><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20220907225612042.png" alt="image-20220907225612042"></p><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20220907225730884.png" alt="image-20220907225730884"></p><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20220907225817821.png" alt="image-20220907225817821"></p><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20220909160451471.png" alt="image-20220909160451471"></p><ul><li><p>当把子类B的sum注释掉后,最后输出那个a.sum()会调用父类的sum(),但父类的sum()里面有getI(),这个函数子类和父类都有(动态绑定),所以他会调用子类的getI(),geti()里面有一个属性I,但是属性没有动态绑定 ,所以调用的是父类的I。</p><h2 id="多态数组"><a href="#多态数组" class="headerlink" title="多态数组"></a>多态数组</h2><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20220909171059710.png" alt="image-20220909171059710"></p><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20220909170942177.png" alt="image-20220909170942177"></p></li><li><p><strong>多态参数</strong></p></li><li><p>使用向下转型调用子类特有的方法</p></li></ul><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20220909174732163.png" alt="image-20220909174732163"></p><h2 id="Object-类详解"><a href="#Object-类详解" class="headerlink" title="Object 类详解"></a>Object 类详解</h2><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20220909181632419.png" alt="image-20220909181632419"></p><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20220909184234902.png" alt="image-20220909184234902"></p><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20220909184616411.png" alt="image-20220909184616411"></p><ul><li><strong>hashCode 方法</strong></li></ul><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20220909185606436.png" alt="image-20220909185606436"></p><p> 老韩的 6 个小结: </p><p>\1) 提高具有哈希结构的容器的效率! </p><p>\2) 两个引用,如果指向的是同一个对象,则哈希值肯定是一样的! </p><p>\3) 两个引用,如果指向的是不同对象,则哈希值是不一样的 </p><p>\4) 哈希值主要根据地址号来的!, 不能完全将哈希值等价于地址。</p><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20220909190251232.png" alt="image-20220909190251232"></p><ul><li><p><strong>toString 方法</strong></p></li><li><p>\1) 基本介绍 </p><p>默认返回:全类名+@+哈希值的十六进制,【查看 Object 的 toString 方法】 </p><p>子类往往重写 toString 方法,用于返回对象的属性信息 </p><p>\2) 重写 toString 方法,打印对象或拼接对象时,都会自动调用该对象的 toString 形式.</p></li></ul><p>\3) 当直接输出一个对象时,toString 方法会被默认的调用, 比如 System.out.println(monster); 就会默认调用 </p><p>monster.toString() </p><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20220909191058732.png" alt="image-20220909191058732"></p><ul><li><strong>finalize 方法</strong></li></ul><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20220909191241991.png" alt="image-20220909191241991"></p><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20220909191819206.png" alt="image-20220909191819206"></p>]]></content>
<summary type="html"><h2 id="java基础小点"><a href="#java基础小点" class="headerlink" title="java基础小点"></a>java基础小点</h2><p><img src="https://xingqiu-tuchuang-1256524210.</summary>
<category term="java" scheme="http://wlh55.github.io/categories/java/"/>
<category term="java" scheme="http://wlh55.github.io/tags/java/"/>
</entry>
<entry>
<title>小程序入门</title>
<link href="http://wlh55.github.io/2022/08/17/%E5%B0%8F%E7%A8%8B%E5%BA%8F%E7%AE%80%E5%8D%95%E5%85%A5%E9%97%A8/"/>
<id>http://wlh55.github.io/2022/08/17/%E5%B0%8F%E7%A8%8B%E5%BA%8F%E7%AE%80%E5%8D%95%E5%85%A5%E9%97%A8/</id>
<published>2022-08-17T15:06:02.278Z</published>
<updated>2022-08-25T13:17:01.693Z</updated>
<content type="html"><![CDATA[<h2 id="小程序简单入门"><a href="#小程序简单入门" class="headerlink" title="小程序简单入门"></a>小程序简单入门</h2><p><strong>一、工具准备</strong></p><ul><li>1、阅读微信小程序开发文档,下载微信小程序开发工具<a href="https://developers.weixin.qq.com/miniprogram/dev/devtools/download.html">稳定版 Stable Build | 微信开放文档 (qq.com)</a></li></ul><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLH/image-20220817163932507.png" alt="image-20220817163932507"></p><ul><li>2、运行微信小程序开发工具,新建项目==>选择小程序==>填写信息(获取appid)</li></ul><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLH/image-20220817164247164.png" alt="image-20220817164247164"></p><ul><li>3、项目结构和工具界面</li></ul><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLH/image-20220817164458189.png" alt="image-20220817164458189"></p><p><strong>二、页面配置</strong></p><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLH/image-20220817164828569.png" alt="image-20220817164828569"></p><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLH/image-20220817171843173.png" alt="image-20220817171843173"></p><ul><li>在app.json中配置显示的页面</li><li>删掉自动生成的模板页面重写pages里的页面</li><li>写完基本页面配置后端云函数</li></ul>]]></content>
<summary type="html"><h2 id="小程序简单入门"><a href="#小程序简单入门" class="headerlink" title="小程序简单入门"></a>小程序简单入门</h2><p><strong>一、工具准备</strong></p>
<ul>
<li>1、阅读微信小程序开发文档</summary>
<category term="小程序" scheme="http://wlh55.github.io/categories/%E5%B0%8F%E7%A8%8B%E5%BA%8F/"/>
<category term="小程序" scheme="http://wlh55.github.io/tags/%E5%B0%8F%E7%A8%8B%E5%BA%8F/"/>
<category term="wxss" scheme="http://wlh55.github.io/tags/wxss/"/>
<category term="js" scheme="http://wlh55.github.io/tags/js/"/>
<category term="wxml" scheme="http://wlh55.github.io/tags/wxml/"/>
</entry>
<entry>
<title>书城项目第三阶段</title>
<link href="http://wlh55.github.io/2022/08/11/%E4%B9%A6%E5%9F%8E%E9%A1%B9%E7%9B%AE%E7%AC%AC%E4%B8%89%E9%98%B6%E6%AE%B5/"/>
<id>http://wlh55.github.io/2022/08/11/%E4%B9%A6%E5%9F%8E%E9%A1%B9%E7%9B%AE%E7%AC%AC%E4%B8%89%E9%98%B6%E6%AE%B5/</id>
<published>2022-08-11T15:11:31.433Z</published>
<updated>2022-08-25T13:17:01.693Z</updated>
<content type="html"><![CDATA[<h2 id="书城项目第三阶段"><a href="#书城项目第三阶段" class="headerlink" title="书城项目第三阶段"></a>书城项目第三阶段</h2><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLH/image-20220810111412038.png" alt="image-20220810111412038"></p><ul><li><p>搜索替换快快捷键(ctrl+shift+r)</p></li><li><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLH/image-20220810113641351.png" alt="image-20220810113641351"></p></li><li><p>代码优化,去除多余冗余,使用jsp的静态包含标签</p></li></ul><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLH/image-20220810161640607.png" alt="image-20220810161640607"></p><ul><li><p>登录和注册页面表单信息回显,将表单信息保存在requestyu中</p></li><li><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLH/image-20220810161941509.png" alt="image-20220810161941509"></p></li><li><p>然后在html页面中使用jsp标签输出回显信息</p></li><li><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLH/image-20220810162113713.png" alt="image-20220810162113713"></p></li></ul><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLH/image-20220810162131452.png" alt="image-20220810162131452"></p><ul><li><p>代码优化1:合并loginservlet程序和registservelt程序为Userservlet程序,使用隐藏标签,判断使用哪个方法</p></li><li><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLH/image-20220810170934751.png" alt="image-20220810170934751"></p></li><li><p>```<br> String action=req.getParameter(“action”);//获取action的值判断使用哪个方法<br> if(“regist”.equals(action)){<br> regist(req,resp)<br> }</p><pre class="line-numbers language-none"><code class="language-none">- 代码优化2:使用反射优化大量if else语句- ![image-20220810171337048](https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLH/image-20220810171337048.png)- 代码优化3- ![image-20220810171633205](https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLH/image-20220810171633205.png)### BeanUtils工具类的使用![image-20220811224210038](https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLH/image-20220811224210038.png)- 使用HttpServletRequest可能导致代码耦合度过高 ### 泛型 泛型类型在逻辑上看以看成是多个不同的类型,实际上都是相同的基本类型。<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></li></ul><p>/此处T可以随便写为任意标识,常见的如T、E、K、V等形式的参数常用于表示泛型<br>//在实例化泛型类时,必须指定T的具体类型<br>public class Generic<t>{<br> //key这个成员变量的类型为T,T的类型由外部指定<br> private T key;</t></p><pre><code>public Generic(T key) { //泛型构造方法形参key的类型也为T,T的类型由外部指定 this.key = key;}public T getKey(){ //泛型方法getKey的返回值类型为T,T的类型由外部指定 return key;}</code></pre><p>}</p><pre class="line-numbers language-none"><code class="language-none">[![复制代码](https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLH/copycode.gif)](javascript:void(0);) [![复制代码](https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLH/copycode.gif)](javascript:void(0);)<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>//泛型的类型参数只能是类类型(包括自定义类),不能是简单类型<br>//传入的实参类型需与泛型的类型参数类型相同,即为Integer.<br>Generic<integer> genericInteger = new Generic<integer>(123456);</integer></integer></p><p>//传入的实参类型需与泛型的类型参数类型相同,即为String.<br>Generic<string> genericString = new Generic<string>(“key_vlaue”);<br>Log.d(“泛型测试”,”key is “ + genericInteger.getKey());<br>Log.d(“泛型测试”,”key is “ + genericString.getKey());</string></string></p><p>```</p><p><a href="javascript:void(0);"><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLH/copycode.gif" alt="复制代码"></a></p><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLH/image-20220811230944556.png" alt="image-20220811230944556"></p><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLH/image-20220811231002559.png" alt="image-20220811231002559"></p><ul><li>不用转类型了</li></ul>]]></content>
<summary type="html"><h2 id="书城项目第三阶段"><a href="#书城项目第三阶段" class="headerlink" title="书城项目第三阶段"></a>书城项目第三阶段</h2><p><img src="https://xingqiu-tuchuang-1256524210.</summary>
<category term="javaweb" scheme="http://wlh55.github.io/categories/javaweb/"/>
<category term="servelt" scheme="http://wlh55.github.io/tags/servelt/"/>
<category term="BeanUtils" scheme="http://wlh55.github.io/tags/BeanUtils/"/>
<category term="jsp" scheme="http://wlh55.github.io/tags/jsp/"/>
</entry>
<entry>
<title>月明星稀</title>
<link href="http://wlh55.github.io/2022/08/07/%E6%9C%88%E6%98%8E%E6%98%9F%E7%A8%80/"/>
<id>http://wlh55.github.io/2022/08/07/%E6%9C%88%E6%98%8E%E6%98%9F%E7%A8%80/</id>
<published>2022-08-07T13:54:28.000Z</published>
<updated>2022-08-07T14:06:41.036Z</updated>
<content type="html"><![CDATA[<h3 id="行路难"><a href="#行路难" class="headerlink" title="行路难"></a>行路难</h3><p>金樽清酒斗十千,玉盘珍羞直万钱。(羞 同:馐;直 同:值)<br>停杯投箸不能食,拔剑四顾心茫然。<br>欲渡黄河冰塞川,将登太行雪满山。(雪满山 一作:雪暗天)<br>闲来垂钓碧溪上,忽复乘舟梦日边。(碧 一作:坐)<br>行路难,行路难,多歧路,今安在?<br>长风破浪会有时,直挂云帆济沧海。</p><h2 id="译文及注释"><a href="#译文及注释" class="headerlink" title="译文及注释"></a>译文及注释</h2><p><strong>译文</strong><br>金杯中的美酒一斗价十千,玉盘里的菜肴珍贵值万钱。<br>心中郁闷,我放下杯筷不愿进餐;拔出宝剑环顾四周,心里一片茫然。<br>想渡黄河,冰雪却冻封了河川;想登太行山,莽莽风雪早已封山。<br>像姜尚垂钓溪,闲待东山再起;又像伊尹做梦,他乘船经过日边。<br>人生道路多么艰难,多么艰难;歧路纷杂,如今又身在何处?<br>相信乘风破浪的时机总会到来,到时定要扬起征帆,横渡沧海!</p><p><strong>注释</strong><br>行路难:选自《<a href="https://so.gushiwen.cn/authorv_b90660e3e492.aspx">李白</a>集校注》,乐府旧题。<br>金樽(zūn):古代盛酒的器具,以金为饰。<br>清酒:清醇的美酒。<br>斗十千:一斗值十千钱(即万钱),形容酒美价高。<br>玉盘:精美的食具。<br>珍羞:珍贵的菜肴。羞:同“馐”,美味的食物。<br>直:通“值”,价值。<br>投箸:丢下筷子。<br>箸(zhù):筷子。不能食:咽不下。<br>茫然:无所适从。<br>太行:太行山。<br>碧:一作“坐”。<br>忽复:忽然又。<br>多歧路,今安在:岔道这么多,如今身在何处?<br>歧:一作“岐”,岔路。<br>安:哪里。<br>长风破浪:比喻实现政治理想。<br>会:终将。<br>云帆:高高的船帆。船在海里航行,因天水相连,船帆好像出没在云雾之中。</p><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLH/R-C.12d344efd4201d111019484e5c01cd43" alt="查看源图像"></p><ul><li>晚安!!!</li></ul>]]></content>
<summary type="html"><h3 id="行路难"><a href="#行路难" class="headerlink" title="行路难"></a>行路难</h3><p>金樽清酒斗十千,玉盘珍羞直万钱。(羞 同:馐;直 同:值)<br>停杯投箸不能食,拔剑四顾心茫然。<br>欲渡黄河冰塞川,将登太行雪</summary>
<category term="js" scheme="http://wlh55.github.io/tags/js/"/>
<category term="古诗" scheme="http://wlh55.github.io/tags/%E5%8F%A4%E8%AF%97/"/>
<category term="css" scheme="http://wlh55.github.io/tags/css/"/>
</entry>
<entry>
<title></title>
<link href="http://wlh55.github.io/2022/02/28/Mybatis/"/>
<id>http://wlh55.github.io/2022/02/28/Mybatis/</id>
<published>2022-02-28T06:48:49.727Z</published>
<updated>2022-11-06T16:33:14.545Z</updated>
<content type="html"><![CDATA[<h2 id="Mybatis练习"><a href="#Mybatis练习" class="headerlink" title="Mybatis练习"></a>Mybatis练习</h2><p><strong>目标</strong></p><blockquote><ul><li>能够使用映射配置文件实现CRUD操作</li><li>能够使用注解实现CRUD操作</li></ul></blockquote><h2 id="1,配置文件实现CRUD"><a href="#1,配置文件实现CRUD" class="headerlink" title="1,配置文件实现CRUD"></a>1,配置文件实现CRUD</h2><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210729111159534.png" alt="image-20210729111159534"></p><p>如上图所示产品原型,里面包含了品牌数据的 <code>查询</code> 、<code>按条件查询</code>、<code>添加</code>、<code>删除</code>、<code>批量删除</code>、<code>修改</code> 等功能,而这些功能其实就是对数据库表中的数据进行CRUD操作。接下来我们就使用Mybatis完成品牌数据的增删改查操作。以下是我们要完成功能列表:</p><blockquote><ul><li>查询<ul><li>查询所有数据</li><li>查询详情</li><li>条件查询</li></ul></li><li>添加</li><li>修改<ul><li>修改全部字段</li><li>修改动态字段</li></ul></li><li>删除<ul><li>删除一个</li><li>批量删除</li></ul></li></ul></blockquote><p>我们先将必要的环境准备一下。</p><h3 id="1-1-环境准备"><a href="#1-1-环境准备" class="headerlink" title="1.1 环境准备"></a>1.1 环境准备</h3><ul><li><p>数据库表(tb_brand)及数据准备</p><pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token comment">-- 删除tb_brand表</span><span class="token keyword">drop</span> <span class="token keyword">table</span> <span class="token keyword">if</span> <span class="token keyword">exists</span> tb_brand<span class="token punctuation">;</span><span class="token comment">-- 创建tb_brand表</span><span class="token keyword">create</span> <span class="token keyword">table</span> tb_brand<span class="token punctuation">(</span> <span class="token comment">-- id 主键</span> id <span class="token keyword">int</span> <span class="token keyword">primary</span> <span class="token keyword">key</span> <span class="token keyword">auto_increment</span><span class="token punctuation">,</span> <span class="token comment">-- 品牌名称</span> brand_name <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">20</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token comment">-- 企业名称</span> company_name <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">20</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token comment">-- 排序字段</span> ordered <span class="token keyword">int</span><span class="token punctuation">,</span> <span class="token comment">-- 描述信息</span> description <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token comment">-- 状态:0:禁用 1:启用</span> <span class="token keyword">status</span> <span class="token keyword">int</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 添加数据</span><span class="token keyword">insert</span> <span class="token keyword">into</span> tb_brand <span class="token punctuation">(</span>brand_name<span class="token punctuation">,</span> company_name<span class="token punctuation">,</span> ordered<span class="token punctuation">,</span> description<span class="token punctuation">,</span> <span class="token keyword">status</span><span class="token punctuation">)</span><span class="token keyword">values</span> <span class="token punctuation">(</span><span class="token string">'三只松鼠'</span><span class="token punctuation">,</span> <span class="token string">'三只松鼠股份有限公司'</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token string">'好吃不上火'</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token string">'华为'</span><span class="token punctuation">,</span> <span class="token string">'华为技术有限公司'</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">,</span> <span class="token string">'华为致力于把数字世界带入每个人、每个家庭、每个组织,构建万物互联的智能世界'</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token string">'小米'</span><span class="token punctuation">,</span> <span class="token string">'小米科技有限公司'</span><span class="token punctuation">,</span> <span class="token number">50</span><span class="token punctuation">,</span> <span class="token string">'are you ok'</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></li><li><p>实体类 Brand</p><p>在 <code>com.itheima.pojo</code> 包下创建 Brand 实体类。</p><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Brand</span> <span class="token punctuation">{</span> <span class="token comment">// id 主键</span> <span class="token keyword">private</span> <span class="token class-name">Integer</span> id<span class="token punctuation">;</span> <span class="token comment">// 品牌名称</span> <span class="token keyword">private</span> <span class="token class-name">String</span> brandName<span class="token punctuation">;</span> <span class="token comment">// 企业名称</span> <span class="token keyword">private</span> <span class="token class-name">String</span> companyName<span class="token punctuation">;</span> <span class="token comment">// 排序字段</span> <span class="token keyword">private</span> <span class="token class-name">Integer</span> ordered<span class="token punctuation">;</span> <span class="token comment">// 描述信息</span> <span class="token keyword">private</span> <span class="token class-name">String</span> description<span class="token punctuation">;</span> <span class="token comment">// 状态:0:禁用 1:启用</span> <span class="token keyword">private</span> <span class="token class-name">Integer</span> status<span class="token punctuation">;</span> <span class="token comment">//省略 setter and getter。自己写时要补全这部分代码</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></li><li><p>编写测试用例</p><p>测试代码需要在 <code>test/java</code> 目录下创建包及测试用例。项目结构如下:</p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210729112907106.png" alt="image-20210729112907106" style="zoom:80%;"></li><li><p>安装 MyBatisX 插件</p><ul><li><p>MybatisX 是一款基于 IDEA 的快速开发插件,为效率而生。</p></li><li><p>主要功能</p><ul><li>XML映射配置文件 和 接口方法 间相互跳转</li><li>根据接口方法生成 statement</li></ul></li><li><p>安装方式</p><p>点击 <code>file</code> ,选择 <code>settings</code> ,就能看到如下图所示界面</p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210729113304743.png" alt="image-20210729113304743" style="zoom:80%;"><blockquote><p>注意:安装完毕后需要重启IDEA</p></blockquote></li><li><p>插件效果</p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210729164450524.png" alt="image-20210729164450524" style="zoom:70%;"><p>红色头绳的表示映射配置文件,蓝色头绳的表示mapper接口。在mapper接口点击红色头绳的小鸟图标会自动跳转到对应的映射配置文件,在映射配置文件中点击蓝色头绳的小鸟图标会自动跳转到对应的mapper接口。也可以在mapper接口中定义方法,自动生成映射配置文件中的 <code>statement</code> ,如图所示</p><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210729165337223.png" alt="image-20210729165337223"></p></li></ul></li></ul><h3 id="1-2-查询所有数据"><a href="#1-2-查询所有数据" class="headerlink" title="1.2 查询所有数据"></a>1.2 查询所有数据</h3><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210729165724838.png" alt="image-20210729165724838" style="zoom:80%;"><p>如上图所示就页面上展示的数据,而这些数据需要从数据库进行查询。接下来我们就来讲查询所有数据功能,而实现该功能我们分以下步骤进行实现:</p><ul><li><p>编写接口方法:Mapper接口</p><ul><li><p>参数:无</p><p>查询所有数据功能是不需要根据任何条件进行查询的,所以此方法不需要参数。</p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210729171208737.png" alt="image-20210729171208737" style="zoom:80%;"></li><li><p>结果:List<brand></brand></p><p>我们会将查询出来的每一条数据封装成一个 <code>Brand</code> 对象,而多条数据封装多个 <code>Brand</code> 对象,需要将这些对象封装到List集合中返回。</p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210729171146911.png" alt="image-20210729171146911" style="zoom:80%;"></li><li><p>执行方法、测试</p></li></ul></li></ul><h4 id="1-2-1-编写接口方法"><a href="#1-2-1-编写接口方法" class="headerlink" title="1.2.1 编写接口方法"></a>1.2.1 编写接口方法</h4><p>在 <code>com.itheima.mapper</code> 包写创建名为 <code>BrandMapper</code> 的接口。并在该接口中定义 <code>List<Brand> selectAll()</code> 方法。</p><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">BrandMapper</span> <span class="token punctuation">{</span> <span class="token comment">/** * 查询所有 */</span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">Brand</span><span class="token punctuation">></span></span> <span class="token function">selectAll</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h4 id="1-2-2-编写SQL语句"><a href="#1-2-2-编写SQL语句" class="headerlink" title="1.2.2 编写SQL语句"></a>1.2.2 编写SQL语句</h4><p>在 <code>reources</code> 下创建 <code>com/itheima/mapper</code> 目录结构,并在该目录下创建名为 <code>BrandMapper.xml</code> 的映射配置文件</p><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token prolog"><?xml version="1.0" encoding="UTF-8" ?></span><span class="token doctype"><span class="token punctuation"><!</span><span class="token doctype-tag">DOCTYPE</span> <span class="token name">mapper</span> <span class="token name">PUBLIC</span> <span class="token string">"-//mybatis.org//DTD Mapper 3.0//EN"</span> <span class="token string">"http://mybatis.org/dtd/mybatis-3-mapper.dtd"</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>mapper</span> <span class="token attr-name">namespace</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>com.itheima.mapper.BrandMapper<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>select</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>selectAll<span class="token punctuation">"</span></span> <span class="token attr-name">resultType</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>brand<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> select * from tb_brand; <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>select</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>mapper</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h4 id="1-2-3-编写测试方法"><a href="#1-2-3-编写测试方法" class="headerlink" title="1.2.3 编写测试方法"></a>1.2.3 编写测试方法</h4><p>在 <code>MybatisTest</code> 类中编写测试查询所有的方法</p><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token annotation punctuation">@Test</span><span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testSelectAll</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">IOException</span> <span class="token punctuation">{</span> <span class="token comment">//1. 获取SqlSessionFactory</span> <span class="token class-name">String</span> resource <span class="token operator">=</span> <span class="token string">"mybatis-config.xml"</span><span class="token punctuation">;</span> <span class="token class-name">InputStream</span> inputStream <span class="token operator">=</span> <span class="token class-name">Resources</span><span class="token punctuation">.</span><span class="token function">getResourceAsStream</span><span class="token punctuation">(</span>resource<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">SqlSessionFactory</span> sqlSessionFactory <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SqlSessionFactoryBuilder</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">build</span><span class="token punctuation">(</span>inputStream<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//2. 获取SqlSession对象</span> <span class="token class-name">SqlSession</span> sqlSession <span class="token operator">=</span> sqlSessionFactory<span class="token punctuation">.</span><span class="token function">openSession</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//3. 获取Mapper接口的代理对象</span> <span class="token class-name">BrandMapper</span> brandMapper <span class="token operator">=</span> sqlSession<span class="token punctuation">.</span><span class="token function">getMapper</span><span class="token punctuation">(</span><span class="token class-name">BrandMapper</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//4. 执行方法</span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">Brand</span><span class="token punctuation">></span></span> brands <span class="token operator">=</span> brandMapper<span class="token punctuation">.</span><span class="token function">selectAll</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>brands<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//5. 释放资源</span> sqlSession<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><blockquote><p>注意:现在我们感觉测试这部分代码写起来特别麻烦,我们可以先忍忍。以后我们只会写上面的第3步的代码,其他的都不需要我们来完成。</p></blockquote><p>执行测试方法结果如下:</p><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210729172544230.png" alt="image-20210729172544230"></p><p>从上面结果我们看到了问题,有些数据封装成功了,而有些数据并没有封装成功。为什么这样呢?</p><p>这个问题可以通过两种方式进行解决:</p><ul><li>给字段起别名</li><li>使用resultMap定义字段和属性的映射关系</li></ul><h4 id="1-2-4-起别名解决上述问题"><a href="#1-2-4-起别名解决上述问题" class="headerlink" title="1.2.4 起别名解决上述问题"></a>1.2.4 起别名解决上述问题</h4><p>从上面结果可以看到 <code>brandName</code> 和 <code>companyName</code> 这两个属性的数据没有封装成功,查询 实体类 和 表中的字段 发现,在实体类中属性名是 <code>brandName</code> 和 <code>companyName</code> ,而表中的字段名为 <code>brand_name</code> 和 <code>company_name</code>,如下图所示 。那么我们只需要保持这两部分的名称一致这个问题就迎刃而解。</p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210729173210433.png" alt="image-20210729173210433" style="zoom:80%;"><p>我们可以在写sql语句时给这两个字段起别名,将别名定义成和属性名一致即可。</p><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>select</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>selectAll<span class="token punctuation">"</span></span> <span class="token attr-name">resultType</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>brand<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> select id, brand_name as brandName, company_name as companyName, ordered, description, status from tb_brand;<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>select</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>而上面的SQL语句中的字段列表书写麻烦,如果表中还有更多的字段,同时其他的功能也需要查询这些字段时就显得我们的代码不够精炼。Mybatis提供了<code>sql</code> 片段可以提高sql的复用性。</p><p><strong>SQL片段:</strong></p><ul><li><p>将需要复用的SQL片段抽取到 <code>sql</code> 标签中</p><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>sql</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>brand_column<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>id, brand_name as brandName, company_name as companyName, ordered, description, status<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>sql</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre><p>id属性值是唯一标识,引用时也是通过该值进行引用。</p></li><li><p>在原sql语句中进行引用</p><p>使用 <code>include</code> 标签引用上述的 SQL 片段,而 <code>refid</code> 指定上述 SQL 片段的id值。</p><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>select</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>selectAll<span class="token punctuation">"</span></span> <span class="token attr-name">resultType</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>brand<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> select <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>include</span> <span class="token attr-name">refid</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>brand_column<span class="token punctuation">"</span></span> <span class="token punctuation">/></span></span> from tb_brand;<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>select</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre></li></ul><h4 id="1-2-5-使用resultMap解决上述问题"><a href="#1-2-5-使用resultMap解决上述问题" class="headerlink" title="1.2.5 使用resultMap解决上述问题"></a>1.2.5 使用resultMap解决上述问题</h4><p>起别名 + sql片段的方式可以解决上述问题,但是它也存在问题。如果还有功能只需要查询部分字段,而不是查询所有字段,那么我们就需要再定义一个 SQL 片段,这就显得不是那么灵活。</p><p>那么我们也可以使用resultMap来定义字段和属性的映射关系的方式解决上述问题。</p><ul><li><p>在映射配置文件中使用resultMap定义 字段 和 属性 的映射关系</p><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>resultMap</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>brandResultMap<span class="token punctuation">"</span></span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>brand<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token comment"><!-- id:完成主键字段的映射 column:表的列名 property:实体类的属性名 result:完成一般字段的映射 column:表的列名 property:实体类的属性名 --></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>result</span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>brand_name<span class="token punctuation">"</span></span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>brandName<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>result</span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>company_name<span class="token punctuation">"</span></span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>companyName<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>resultMap</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><blockquote><p>注意:在上面只需要定义 字段名 和 属性名 不一样的映射,而一样的则不需要专门定义出来。</p></blockquote></li><li><p>SQL语句正常编写</p><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>select</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>selectAll<span class="token punctuation">"</span></span> <span class="token attr-name">resultMap</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>brandResultMap<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> select * from tb_brand;<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>select</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre></li></ul><h4 id="1-2-6-小结"><a href="#1-2-6-小结" class="headerlink" title="1.2.6 小结"></a>1.2.6 小结</h4><p>实体类属性名 和 数据库表列名 不一致,不能自动封装数据</p><ul><li>==起别名:==在SQL语句中,对不一样的列名起别名,别名和实体类属性名一样<ul><li>可以定义 <sql>片段,提升复用性</sql></li></ul></li><li>==resultMap:==定义<resultmap> 完成不一致的属性名和列名的映射</resultmap></li></ul><p>而我们最终选择使用 resultMap的方式。查询映射配置文件中查询所有的 statement 书写如下:</p><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>resultMap</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>brandResultMap<span class="token punctuation">"</span></span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>brand<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token comment"><!-- id:完成主键字段的映射 column:表的列名 property:实体类的属性名 result:完成一般字段的映射 column:表的列名 property:实体类的属性名 --></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>result</span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>brand_name<span class="token punctuation">"</span></span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>brandName<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>result</span> <span class="token attr-name">column</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>company_name<span class="token punctuation">"</span></span> <span class="token attr-name">property</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>companyName<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>resultMap</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>select</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>selectAll<span class="token punctuation">"</span></span> <span class="token attr-name">resultMap</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>brandResultMap<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> select * from tb_brand;<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>select</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h3 id="1-3-查询详情"><a href="#1-3-查询详情" class="headerlink" title="1.3 查询详情"></a>1.3 查询详情</h3><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210729180118287.png" alt="image-20210729180118287" style="zoom:80%;"><p>有些数据的属性比较多,在页面表格中无法全部实现,而只会显示部分,而其他属性数据的查询可以通过 <code>查看详情</code> 来进行查询,如上图所示。</p><p>查看详情功能实现步骤:</p><ul><li><p>编写接口方法:Mapper接口</p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210729180604529.png" alt="image-20210729180604529" style="zoom:80%;"><ul><li><p>参数:id</p><p>查看详情就是查询某一行数据,所以需要根据id进行查询。而id以后是由页面传递过来。</p></li><li><p>结果:Brand</p><p>根据id查询出来的数据只要一条,而将一条数据封装成一个Brand对象即可</p></li></ul></li><li><p>编写SQL语句:SQL映射文件</p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210729180709318.png" alt="image-20210729180709318" style="zoom:80%;"></li><li><p>执行方法、进行测试</p></li></ul><h4 id="1-3-1-编写接口方法"><a href="#1-3-1-编写接口方法" class="headerlink" title="1.3.1 编写接口方法"></a>1.3.1 编写接口方法</h4><p>在 <code>BrandMapper</code> 接口中定义根据id查询数据的方法 </p><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">/** * 查看详情:根据Id查询 */</span><span class="token class-name">Brand</span> <span class="token function">selectById</span><span class="token punctuation">(</span><span class="token keyword">int</span> id<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre><h4 id="1-3-2-编写SQL语句"><a href="#1-3-2-编写SQL语句" class="headerlink" title="1.3.2 编写SQL语句"></a>1.3.2 编写SQL语句</h4><p>在 <code>BrandMapper.xml</code> 映射配置文件中编写 <code>statement</code>,使用 <code>resultMap</code> 而不是使用 <code>resultType</code></p><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>select</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>selectById<span class="token punctuation">"</span></span> <span class="token attr-name">resultMap</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>brandResultMap<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> select * from tb_brand where id = #{id};<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>select</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre><blockquote><p>注意:上述SQL中的 #{id}先这样写,一会我们再详细讲解</p></blockquote><h4 id="1-3-3-编写测试方法"><a href="#1-3-3-编写测试方法" class="headerlink" title="1.3.3 编写测试方法"></a>1.3.3 编写测试方法</h4><p>在 <code>test/java</code> 下的 <code>com.itheima.mapper</code> 包下的 <code>MybatisTest类中</code> 定义测试方法</p><pre class="line-numbers language-java" data-language="java"><code class="language-java"> <span class="token annotation punctuation">@Test</span><span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testSelectById</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">IOException</span> <span class="token punctuation">{</span> <span class="token comment">//接收参数,该id以后需要传递过来</span> <span class="token keyword">int</span> id <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token comment">//1. 获取SqlSessionFactory</span> <span class="token class-name">String</span> resource <span class="token operator">=</span> <span class="token string">"mybatis-config.xml"</span><span class="token punctuation">;</span> <span class="token class-name">InputStream</span> inputStream <span class="token operator">=</span> <span class="token class-name">Resources</span><span class="token punctuation">.</span><span class="token function">getResourceAsStream</span><span class="token punctuation">(</span>resource<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">SqlSessionFactory</span> sqlSessionFactory <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SqlSessionFactoryBuilder</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">build</span><span class="token punctuation">(</span>inputStream<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//2. 获取SqlSession对象</span> <span class="token class-name">SqlSession</span> sqlSession <span class="token operator">=</span> sqlSessionFactory<span class="token punctuation">.</span><span class="token function">openSession</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//3. 获取Mapper接口的代理对象</span> <span class="token class-name">BrandMapper</span> brandMapper <span class="token operator">=</span> sqlSession<span class="token punctuation">.</span><span class="token function">getMapper</span><span class="token punctuation">(</span><span class="token class-name">BrandMapper</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//4. 执行方法</span> <span class="token class-name">Brand</span> brand <span class="token operator">=</span> brandMapper<span class="token punctuation">.</span><span class="token function">selectById</span><span class="token punctuation">(</span>id<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>brand<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//5. 释放资源</span> sqlSession<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>执行测试方法结果如下:</p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210729182223137.png" alt="image-20210729182223137" style="zoom:70%;"><h4 id="1-3-4-参数占位符"><a href="#1-3-4-参数占位符" class="headerlink" title="1.3.4 参数占位符"></a>1.3.4 参数占位符</h4><p>查询到的结果很好理解就是id为1的这行数据。而这里我们需要看控制台显示的SQL语句,能看到使用?进行占位。说明我们在映射配置文件中的写的 <code>#{id}</code> 最终会被?进行占位。接下来我们就聊聊映射配置文件中的参数占位符。</p><p>mybatis提供了两种参数占位符:</p><ul><li><p>#{} :执行SQL时,会将 #{} 占位符替换为?,将来自动设置参数值。从上述例子可以看出使用#{} 底层使用的是 <code>PreparedStatement</code></p></li><li><p>${} :拼接SQL。底层使用的是 <code>Statement</code>,会存在SQL注入问题。如下图将 映射配置文件中的 #{} 替换成 ${} 来看效果</p><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>select</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>selectById<span class="token punctuation">"</span></span> <span class="token attr-name">resultMap</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>brandResultMap<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> select * from tb_brand where id = ${id};<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>select</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre><p>重新运行查看结果如下:</p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210729184156019.png" alt="image-20210729184156019" style="zoom:70%;"></li></ul><blockquote><p>==注意:==从上面两个例子可以看出,以后开发我们使用 #{} 参数占位符。</p></blockquote><h4 id="1-3-5-parameterType使用"><a href="#1-3-5-parameterType使用" class="headerlink" title="1.3.5 parameterType使用"></a>1.3.5 parameterType使用</h4><p>对于有参数的mapper接口方法,我们在映射配置文件中应该配置 <code>ParameterType</code> 来指定参数类型。只不过该属性都可以省略。如下图:</p><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>select</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>selectById<span class="token punctuation">"</span></span> <span class="token attr-name">parameterType</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>int<span class="token punctuation">"</span></span> <span class="token attr-name">resultMap</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>brandResultMap<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> select * from tb_brand where id = ${id};<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>select</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre><h4 id="1-3-6-SQL语句中特殊字段处理"><a href="#1-3-6-SQL语句中特殊字段处理" class="headerlink" title="1.3.6 SQL语句中特殊字段处理"></a>1.3.6 SQL语句中特殊字段处理</h4><p>以后肯定会在SQL语句中写一下特殊字符,比如某一个字段大于某个值,如下图</p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210729184756094.png" alt="image-20210729184756094" style="zoom:80%;"><p>可以看出报错了,因为映射配置文件是xml类型的问题,而 > < 等这些字符在xml中有特殊含义,所以此时我们需要将这些符号进行转义,可以使用以下两种方式进行转义</p><ul><li><p>转义字符</p><p>下图的 <code>&lt;</code> 就是 <code><</code> 的转义字符。</p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210729185128686.png" alt="image-20210729185128686" style="zoom:60%;"></li><li><!--[CDATA[内容]]--><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210729185030318.png" alt="image-20210729185030318" style="zoom:60%;"></li></ul><h3 id="1-4-多条件查询"><a href="#1-4-多条件查询" class="headerlink" title="1.4 多条件查询"></a>1.4 多条件查询</h3><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210729203804276.png" alt="image-20210729203804276"></p><p>我们经常会遇到如上图所示的多条件查询,将多条件查询的结果展示在下方的数据列表中。而我们做这个功能需要分析最终的SQL语句应该是什么样,思考两个问题</p><ul><li>条件表达式</li><li>如何连接</li></ul><p>条件字段 <code>企业名称</code> 和 <code>品牌名称</code> 需要进行模糊查询,所以条件应该是:</p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210729204458815.png" alt="image-20210729204458815" style="zoom:70%;"><p>简单的分析后,我们来看功能实现的步骤:</p><ul><li><p>编写接口方法</p><ul><li>参数:所有查询条件</li><li>结果:List<brand></brand></li></ul></li><li><p>在映射配置文件中编写SQL语句</p></li><li><p>编写测试方法并执行</p></li></ul><h4 id="1-4-1-编写接口方法"><a href="#1-4-1-编写接口方法" class="headerlink" title="1.4.1 编写接口方法"></a>1.4.1 编写接口方法</h4><p>在 <code>BrandMapper</code> 接口中定义多条件查询的方法。</p><p>而该功能有三个参数,我们就需要考虑定义接口时,参数应该如何定义。Mybatis针对多参数有多种实现</p><ul><li><p>使用 <code>@Param("参数名称")</code> 标记每一个参数,在映射配置文件中就需要使用 <code>#{参数名称}</code> 进行占位</p><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">List</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">Brand</span><span class="token punctuation">></span></span> <span class="token function">selectByCondition</span><span class="token punctuation">(</span><span class="token annotation punctuation">@Param</span><span class="token punctuation">(</span><span class="token string">"status"</span><span class="token punctuation">)</span> <span class="token keyword">int</span> status<span class="token punctuation">,</span> <span class="token annotation punctuation">@Param</span><span class="token punctuation">(</span><span class="token string">"companyName"</span><span class="token punctuation">)</span> <span class="token class-name">String</span> companyName<span class="token punctuation">,</span><span class="token annotation punctuation">@Param</span><span class="token punctuation">(</span><span class="token string">"brandName"</span><span class="token punctuation">)</span> <span class="token class-name">String</span> brandName<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></li><li><p>将多个参数封装成一个 实体对象 ,将该实体对象作为接口的方法参数。该方式要求在映射配置文件的SQL中使用 <code>#{内容}</code> 时,里面的内容必须和实体类属性名保持一致。</p><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">List</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">Brand</span><span class="token punctuation">></span></span> <span class="token function">selectByCondition</span><span class="token punctuation">(</span><span class="token class-name">Brand</span> brand<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></li><li><p>将多个参数封装到map集合中,将map集合作为接口的方法参数。该方式要求在映射配置文件的SQL中使用 <code>#{内容}</code> 时,里面的内容必须和map集合中键的名称一致。</p><pre class="line-numbers language-none"><code class="language-none">List<Brand> selectByCondition(Map map);<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></li></ul><h4 id="1-4-2-编写SQL语句"><a href="#1-4-2-编写SQL语句" class="headerlink" title="1.4.2 编写SQL语句"></a>1.4.2 编写SQL语句</h4><p>在 <code>BrandMapper.xml</code> 映射配置文件中编写 <code>statement</code>,使用 <code>resultMap</code> 而不是使用 <code>resultType</code></p><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>select</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>selectByCondition<span class="token punctuation">"</span></span> <span class="token attr-name">resultMap</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>brandResultMap<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> select * from tb_brand where status = #{status} and company_name like #{companyName} and brand_name like #{brandName}<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>select</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h4 id="1-4-3-编写测试方法"><a href="#1-4-3-编写测试方法" class="headerlink" title="1.4.3 编写测试方法"></a>1.4.3 编写测试方法</h4><p>在 <code>test/java</code> 下的 <code>com.itheima.mapper</code> 包下的 <code>MybatisTest类中</code> 定义测试方法</p><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token annotation punctuation">@Test</span><span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testSelectByCondition</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">IOException</span> <span class="token punctuation">{</span> <span class="token comment">//接收参数</span> <span class="token keyword">int</span> status <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token class-name">String</span> companyName <span class="token operator">=</span> <span class="token string">"华为"</span><span class="token punctuation">;</span> <span class="token class-name">String</span> brandName <span class="token operator">=</span> <span class="token string">"华为"</span><span class="token punctuation">;</span> <span class="token comment">// 处理参数</span> companyName <span class="token operator">=</span> <span class="token string">"%"</span> <span class="token operator">+</span> companyName <span class="token operator">+</span> <span class="token string">"%"</span><span class="token punctuation">;</span> brandName <span class="token operator">=</span> <span class="token string">"%"</span> <span class="token operator">+</span> brandName <span class="token operator">+</span> <span class="token string">"%"</span><span class="token punctuation">;</span> <span class="token comment">//1. 获取SqlSessionFactory</span> <span class="token class-name">String</span> resource <span class="token operator">=</span> <span class="token string">"mybatis-config.xml"</span><span class="token punctuation">;</span> <span class="token class-name">InputStream</span> inputStream <span class="token operator">=</span> <span class="token class-name">Resources</span><span class="token punctuation">.</span><span class="token function">getResourceAsStream</span><span class="token punctuation">(</span>resource<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">SqlSessionFactory</span> sqlSessionFactory <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SqlSessionFactoryBuilder</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">build</span><span class="token punctuation">(</span>inputStream<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//2. 获取SqlSession对象</span> <span class="token class-name">SqlSession</span> sqlSession <span class="token operator">=</span> sqlSessionFactory<span class="token punctuation">.</span><span class="token function">openSession</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//3. 获取Mapper接口的代理对象</span> <span class="token class-name">BrandMapper</span> brandMapper <span class="token operator">=</span> sqlSession<span class="token punctuation">.</span><span class="token function">getMapper</span><span class="token punctuation">(</span><span class="token class-name">BrandMapper</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//4. 执行方法</span><span class="token comment">//方式一 :接口方法参数使用 @Param 方式调用的方法</span> <span class="token comment">//List<Brand> brands = brandMapper.selectByCondition(status, companyName, brandName);</span> <span class="token comment">//方式二 :接口方法参数是 实体类对象 方式调用的方法</span> <span class="token comment">//封装对象</span> <span class="token comment">/* Brand brand = new Brand(); brand.setStatus(status); brand.setCompanyName(companyName); brand.setBrandName(brandName);*/</span> <span class="token comment">//List<Brand> brands = brandMapper.selectByCondition(brand);</span> <span class="token comment">//方式三 :接口方法参数是 map集合对象 方式调用的方法</span> <span class="token class-name">Map</span> map <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashMap</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"status"</span> <span class="token punctuation">,</span> status<span class="token punctuation">)</span><span class="token punctuation">;</span> map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"companyName"</span><span class="token punctuation">,</span> companyName<span class="token punctuation">)</span><span class="token punctuation">;</span> map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"brandName"</span> <span class="token punctuation">,</span> brandName<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">Brand</span><span class="token punctuation">></span></span> brands <span class="token operator">=</span> brandMapper<span class="token punctuation">.</span><span class="token function">selectByCondition</span><span class="token punctuation">(</span>map<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>brands<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//5. 释放资源</span> sqlSession<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h4 id="1-4-4-动态SQL"><a href="#1-4-4-动态SQL" class="headerlink" title="1.4.4 动态SQL"></a>1.4.4 动态SQL</h4><p>上述功能实现存在很大的问题。用户在输入条件时,肯定不会所有的条件都填写,这个时候我们的SQL语句就不能那样写的</p><p>例如用户只输入 当前状态 时,SQL语句就是</p><pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> <span class="token operator">*</span> <span class="token keyword">from</span> tb_brand <span class="token keyword">where</span> <span class="token keyword">status</span> <span class="token operator">=</span> <span class="token comment">#{status}</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>而用户如果只输入企业名称时,SQL语句就是</p><pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> <span class="token operator">*</span> <span class="token keyword">from</span> tb_brand <span class="token keyword">where</span> company_name <span class="token operator">like</span> <span class="token comment">#{companName}</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>而用户如果输入了 <code>当前状态</code> 和 <code>企业名称 </code> 时,SQL语句又不一样</p><pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> <span class="token operator">*</span> <span class="token keyword">from</span> tb_brand <span class="token keyword">where</span> <span class="token keyword">status</span> <span class="token operator">=</span> <span class="token comment">#{status} and company_name like #{companName}</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>针对上述的需要,Mybatis对动态SQL有很强大的支撑:</p><blockquote><ul><li><p>if</p></li><li><p>choose (when, otherwise)</p></li><li><p>trim (where, set)</p></li><li><p>foreach</p></li></ul></blockquote><p>我们先学习 if 标签和 where 标签:</p><ul><li><p>if 标签:条件判断</p><ul><li>test 属性:逻辑表达式</li></ul><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>select</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>selectByCondition<span class="token punctuation">"</span></span> <span class="token attr-name">resultMap</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>brandResultMap<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> select * from tb_brand where <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>if</span> <span class="token attr-name">test</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>status != null<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> and status = #{status} <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>if</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>if</span> <span class="token attr-name">test</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>companyName != null and companyName != <span class="token punctuation">'</span><span class="token punctuation">'</span> <span class="token punctuation">"</span></span><span class="token punctuation">></span></span> and company_name like #{companyName} <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>if</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>if</span> <span class="token attr-name">test</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>brandName != null and brandName != <span class="token punctuation">'</span><span class="token punctuation">'</span> <span class="token punctuation">"</span></span><span class="token punctuation">></span></span> and brand_name like #{brandName} <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>if</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>select</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>如上的这种SQL语句就会根据传递的参数值进行动态的拼接。如果此时status和companyName有值那么就会值拼接这两个条件。</p><p>执行结果如下:</p><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210729212510291.png" alt="image-20210729212510291"></p><p>但是它也存在问题,如果此时给的参数值是</p><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">Map</span> map <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashMap</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// map.put("status" , status);</span>map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"companyName"</span><span class="token punctuation">,</span> companyName<span class="token punctuation">)</span><span class="token punctuation">;</span>map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"brandName"</span> <span class="token punctuation">,</span> brandName<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre><p>拼接的SQL语句就变成了</p><pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> <span class="token operator">*</span> <span class="token keyword">from</span> tb_brand <span class="token keyword">where</span> <span class="token operator">and</span> company_name <span class="token operator">like</span> ? <span class="token operator">and</span> brand_name <span class="token operator">like</span> ?<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>而上面的语句中 where 关键后直接跟 and 关键字,这就是一条错误的SQL语句。这个就可以使用 where 标签解决</p></li><li><p>where 标签</p><ul><li>作用:<ul><li>替换where关键字</li><li>会动态的去掉第一个条件前的 and </li><li>如果所有的参数没有值则不加where关键字</li></ul></li></ul><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>select</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>selectByCondition<span class="token punctuation">"</span></span> <span class="token attr-name">resultMap</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>brandResultMap<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> select * from tb_brand <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>where</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>if</span> <span class="token attr-name">test</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>status != null<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> and status = #{status} <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>if</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>if</span> <span class="token attr-name">test</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>companyName != null and companyName != <span class="token punctuation">'</span><span class="token punctuation">'</span> <span class="token punctuation">"</span></span><span class="token punctuation">></span></span> and company_name like #{companyName} <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>if</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>if</span> <span class="token attr-name">test</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>brandName != null and brandName != <span class="token punctuation">'</span><span class="token punctuation">'</span> <span class="token punctuation">"</span></span><span class="token punctuation">></span></span> and brand_name like #{brandName} <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>if</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>where</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>select</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><blockquote><p>注意:需要给每个条件前都加上 and 关键字。</p></blockquote></li></ul><h3 id="1-5-单个条件(动态SQL)"><a href="#1-5-单个条件(动态SQL)" class="headerlink" title="1.5 单个条件(动态SQL)"></a>1.5 单个条件(动态SQL)</h3><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210729213613029.png" alt="image-20210729213613029" style="zoom:80%;"><p>如上图所示,在查询时只能选择 <code>品牌名称</code>、<code>当前状态</code>、<code>企业名称</code> 这三个条件中的一个,但是用户到底选择哪儿一个,我们并不能确定。这种就属于单个条件的动态SQL语句。 </p><p>这种需求需要使用到 <code>choose(when,otherwise)标签</code> 实现, 而 <code>choose</code> 标签类似于Java 中的switch语句。</p><p>通过一个案例来使用这些标签</p><h4 id="1-5-1-编写接口方法"><a href="#1-5-1-编写接口方法" class="headerlink" title="1.5.1 编写接口方法"></a>1.5.1 编写接口方法</h4><p>在 <code>BrandMapper</code> 接口中定义单条件查询的方法。</p><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">/** * 单条件动态查询 * @param brand * @return */</span><span class="token class-name">List</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">Brand</span><span class="token punctuation">></span></span> <span class="token function">selectByConditionSingle</span><span class="token punctuation">(</span><span class="token class-name">Brand</span> brand<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h4 id="1-5-2-编写SQL语句"><a href="#1-5-2-编写SQL语句" class="headerlink" title="1.5.2 编写SQL语句"></a>1.5.2 编写SQL语句</h4><p>在 <code>BrandMapper.xml</code> 映射配置文件中编写 <code>statement</code>,使用 <code>resultMap</code> 而不是使用 <code>resultType</code></p><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>select</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>selectByConditionSingle<span class="token punctuation">"</span></span> <span class="token attr-name">resultMap</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>brandResultMap<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> select * from tb_brand <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>where</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>choose</span><span class="token punctuation">></span></span><span class="token comment"><!--相当于switch--></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>when</span> <span class="token attr-name">test</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>status != null<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token comment"><!--相当于case--></span> status = #{status} <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>when</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>when</span> <span class="token attr-name">test</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>companyName != null and companyName != <span class="token punctuation">'</span><span class="token punctuation">'</span> <span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token comment"><!--相当于case--></span> company_name like #{companyName} <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>when</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>when</span> <span class="token attr-name">test</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>brandName != null and brandName != <span class="token punctuation">'</span><span class="token punctuation">'</span><span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token comment"><!--相当于case--></span> brand_name like #{brandName} <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>when</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>choose</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>where</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>select</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h4 id="1-5-3-编写测试方法"><a href="#1-5-3-编写测试方法" class="headerlink" title="1.5.3 编写测试方法"></a>1.5.3 编写测试方法</h4><p>在 <code>test/java</code> 下的 <code>com.itheima.mapper</code> 包下的 <code>MybatisTest类中</code> 定义测试方法</p><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token annotation punctuation">@Test</span><span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testSelectByConditionSingle</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">IOException</span> <span class="token punctuation">{</span> <span class="token comment">//接收参数</span> <span class="token keyword">int</span> status <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token class-name">String</span> companyName <span class="token operator">=</span> <span class="token string">"华为"</span><span class="token punctuation">;</span> <span class="token class-name">String</span> brandName <span class="token operator">=</span> <span class="token string">"华为"</span><span class="token punctuation">;</span> <span class="token comment">// 处理参数</span> companyName <span class="token operator">=</span> <span class="token string">"%"</span> <span class="token operator">+</span> companyName <span class="token operator">+</span> <span class="token string">"%"</span><span class="token punctuation">;</span> brandName <span class="token operator">=</span> <span class="token string">"%"</span> <span class="token operator">+</span> brandName <span class="token operator">+</span> <span class="token string">"%"</span><span class="token punctuation">;</span> <span class="token comment">//封装对象</span> <span class="token class-name">Brand</span> brand <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Brand</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//brand.setStatus(status);</span> brand<span class="token punctuation">.</span><span class="token function">setCompanyName</span><span class="token punctuation">(</span>companyName<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//brand.setBrandName(brandName);</span> <span class="token comment">//1. 获取SqlSessionFactory</span> <span class="token class-name">String</span> resource <span class="token operator">=</span> <span class="token string">"mybatis-config.xml"</span><span class="token punctuation">;</span> <span class="token class-name">InputStream</span> inputStream <span class="token operator">=</span> <span class="token class-name">Resources</span><span class="token punctuation">.</span><span class="token function">getResourceAsStream</span><span class="token punctuation">(</span>resource<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">SqlSessionFactory</span> sqlSessionFactory <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SqlSessionFactoryBuilder</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">build</span><span class="token punctuation">(</span>inputStream<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//2. 获取SqlSession对象</span> <span class="token class-name">SqlSession</span> sqlSession <span class="token operator">=</span> sqlSessionFactory<span class="token punctuation">.</span><span class="token function">openSession</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//3. 获取Mapper接口的代理对象</span> <span class="token class-name">BrandMapper</span> brandMapper <span class="token operator">=</span> sqlSession<span class="token punctuation">.</span><span class="token function">getMapper</span><span class="token punctuation">(</span><span class="token class-name">BrandMapper</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//4. 执行方法</span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">Brand</span><span class="token punctuation">></span></span> brands <span class="token operator">=</span> brandMapper<span class="token punctuation">.</span><span class="token function">selectByConditionSingle</span><span class="token punctuation">(</span>brand<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>brands<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//5. 释放资源</span> sqlSession<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>执行测试方法结果如下:</p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210729214548756.png" alt="image-20210729214548756" style="zoom:70%;"><h3 id="1-6-添加数据"><a href="#1-6-添加数据" class="headerlink" title="1.6 添加数据"></a>1.6 添加数据</h3><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210729214917317.png" alt="image-20210729214917317" style="zoom:70%;"><p>如上图是我们平时在添加数据时展示的页面,而我们在该页面输入想要的数据后添加 <code>提交</code> 按钮,就会将这些数据添加到数据库中。接下来我们就来实现添加数据的操作。</p><ul><li><p>编写接口方法</p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210729215351651.png" alt="image-20210729215351651" style="zoom:80%;"><p>参数:除了id之外的所有的数据。id对应的是表中主键值,而主键我们是 ==自动增长== 生成的。</p></li><li><p>编写SQL语句</p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210729215537167.png" alt="image-20210729215537167" style="zoom:80%;"></li><li><p>编写测试方法并执行</p></li></ul><p>明确了该功能实现的步骤后,接下来我们进行具体的操作。</p><h4 id="1-6-1-编写接口方法"><a href="#1-6-1-编写接口方法" class="headerlink" title="1.6.1 编写接口方法"></a>1.6.1 编写接口方法</h4><p>在 <code>BrandMapper</code> 接口中定义添加方法。</p><pre class="line-numbers language-java" data-language="java"><code class="language-java"> <span class="token comment">/** * 添加 */</span><span class="token keyword">void</span> <span class="token function">add</span><span class="token punctuation">(</span><span class="token class-name">Brand</span> brand<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre><h4 id="1-6-2-编写SQL语句"><a href="#1-6-2-编写SQL语句" class="headerlink" title="1.6.2 编写SQL语句"></a>1.6.2 编写SQL语句</h4><p>在 <code>BrandMapper.xml</code> 映射配置文件中编写添加数据的 <code>statement</code></p><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>insert</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>add<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> insert into tb_brand (brand_name, company_name, ordered, description, status) values (#{brandName}, #{companyName}, #{ordered}, #{description}, #{status});<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>insert</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre><h4 id="1-6-3-编写测试方法"><a href="#1-6-3-编写测试方法" class="headerlink" title="1.6.3 编写测试方法"></a>1.6.3 编写测试方法</h4><p>在 <code>test/java</code> 下的 <code>com.itheima.mapper</code> 包下的 <code>MybatisTest类中</code> 定义测试方法</p><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token annotation punctuation">@Test</span><span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testAdd</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">IOException</span> <span class="token punctuation">{</span> <span class="token comment">//接收参数</span> <span class="token keyword">int</span> status <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token class-name">String</span> companyName <span class="token operator">=</span> <span class="token string">"波导手机"</span><span class="token punctuation">;</span> <span class="token class-name">String</span> brandName <span class="token operator">=</span> <span class="token string">"波导"</span><span class="token punctuation">;</span> <span class="token class-name">String</span> description <span class="token operator">=</span> <span class="token string">"手机中的战斗机"</span><span class="token punctuation">;</span> <span class="token keyword">int</span> ordered <span class="token operator">=</span> <span class="token number">100</span><span class="token punctuation">;</span> <span class="token comment">//封装对象</span> <span class="token class-name">Brand</span> brand <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Brand</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> brand<span class="token punctuation">.</span><span class="token function">setStatus</span><span class="token punctuation">(</span>status<span class="token punctuation">)</span><span class="token punctuation">;</span> brand<span class="token punctuation">.</span><span class="token function">setCompanyName</span><span class="token punctuation">(</span>companyName<span class="token punctuation">)</span><span class="token punctuation">;</span> brand<span class="token punctuation">.</span><span class="token function">setBrandName</span><span class="token punctuation">(</span>brandName<span class="token punctuation">)</span><span class="token punctuation">;</span> brand<span class="token punctuation">.</span><span class="token function">setDescription</span><span class="token punctuation">(</span>description<span class="token punctuation">)</span><span class="token punctuation">;</span> brand<span class="token punctuation">.</span><span class="token function">setOrdered</span><span class="token punctuation">(</span>ordered<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//1. 获取SqlSessionFactory</span> <span class="token class-name">String</span> resource <span class="token operator">=</span> <span class="token string">"mybatis-config.xml"</span><span class="token punctuation">;</span> <span class="token class-name">InputStream</span> inputStream <span class="token operator">=</span> <span class="token class-name">Resources</span><span class="token punctuation">.</span><span class="token function">getResourceAsStream</span><span class="token punctuation">(</span>resource<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">SqlSessionFactory</span> sqlSessionFactory <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SqlSessionFactoryBuilder</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">build</span><span class="token punctuation">(</span>inputStream<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//2. 获取SqlSession对象</span> <span class="token class-name">SqlSession</span> sqlSession <span class="token operator">=</span> sqlSessionFactory<span class="token punctuation">.</span><span class="token function">openSession</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//SqlSession sqlSession = sqlSessionFactory.openSession(true); //设置自动提交事务,这种情况不需要手动提交事务了</span> <span class="token comment">//3. 获取Mapper接口的代理对象</span> <span class="token class-name">BrandMapper</span> brandMapper <span class="token operator">=</span> sqlSession<span class="token punctuation">.</span><span class="token function">getMapper</span><span class="token punctuation">(</span><span class="token class-name">BrandMapper</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//4. 执行方法</span> brandMapper<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>brand<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//提交事务</span> sqlSession<span class="token punctuation">.</span><span class="token function">commit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//5. 释放资源</span> sqlSession<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>执行结果如下:</p><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210729220348255.png" alt="image-20210729220348255"></p><h4 id="1-6-4-添加-主键返回"><a href="#1-6-4-添加-主键返回" class="headerlink" title="1.6.4 添加-主键返回"></a>1.6.4 添加-主键返回</h4><p>在数据添加成功后,有时候需要获取插入数据库数据的主键(主键是自增长)。</p><p>比如:添加订单和订单项,如下图就是京东上的订单</p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210729221207962.png" alt="image-20210729221207962" style="zoom:80%;"><p>订单数据存储在订单表中,订单项存储在订单项表中。</p><ul><li><p>添加订单数据</p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210729221049462.png" alt="image-20210729221049462" style="zoom:80%;"></li><li><p>添加订单项数据,订单项中需要设置所属订单的id</p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210729221058898.png" alt="image-20210729221058898" style="zoom:80%;"></li></ul><p>明白了什么时候 <code>主键返回</code> 。接下来我们简单模拟一下,在添加完数据后打印id属性值,能打印出来说明已经获取到了。</p><p>我们将上面添加品牌数据的案例中映射配置文件里 <code>statement</code> 进行修改,如下</p><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>insert</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>add<span class="token punctuation">"</span></span> <span class="token attr-name">useGeneratedKeys</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>true<span class="token punctuation">"</span></span> <span class="token attr-name">keyProperty</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>id<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> insert into tb_brand (brand_name, company_name, ordered, description, status) values (#{brandName}, #{companyName}, #{ordered}, #{description}, #{status});<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>insert</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre><blockquote><p>在 insert 标签上添加如下属性:</p><ul><li>useGeneratedKeys:是够获取自动增长的主键值。true表示获取</li><li>keyProperty :指定将获取到的主键值封装到哪儿个属性里</li></ul></blockquote><h3 id="1-7-修改"><a href="#1-7-修改" class="headerlink" title="1.7 修改"></a>1.7 修改</h3><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210729222642700.png" alt="image-20210729222642700" style="zoom:80%;"><p>如图所示是修改页面,用户在该页面书写需要修改的数据,点击 <code>提交</code> 按钮,就会将数据库中对应的数据进行修改。注意一点,如果哪儿个输入框没有输入内容,我们是将表中数据对应字段值替换为空白还是保留字段之前的值?答案肯定是保留之前的数据。</p><p>接下来我们就具体来实现</p><h4 id="1-7-1-编写接口方法"><a href="#1-7-1-编写接口方法" class="headerlink" title="1.7.1 编写接口方法"></a>1.7.1 编写接口方法</h4><p>在 <code>BrandMapper</code> 接口中定义修改方法。</p><pre class="line-numbers language-java" data-language="java"><code class="language-java"> <span class="token comment">/** * 修改 */</span><span class="token keyword">void</span> <span class="token function">update</span><span class="token punctuation">(</span><span class="token class-name">Brand</span> brand<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre><blockquote><p>上述方法参数 Brand 就是封装了需要修改的数据,而id肯定是有数据的,这也是和添加方法的区别。</p></blockquote><h4 id="1-7-2-编写SQL语句"><a href="#1-7-2-编写SQL语句" class="headerlink" title="1.7.2 编写SQL语句"></a>1.7.2 编写SQL语句</h4><p>在 <code>BrandMapper.xml</code> 映射配置文件中编写修改数据的 <code>statement</code>。</p><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>update</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>update<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> update tb_brand <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>set</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>if</span> <span class="token attr-name">test</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>brandName != null and brandName != <span class="token punctuation">'</span><span class="token punctuation">'</span><span class="token punctuation">"</span></span><span class="token punctuation">></span></span> brand_name = #{brandName}, <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>if</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>if</span> <span class="token attr-name">test</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>companyName != null and companyName != <span class="token punctuation">'</span><span class="token punctuation">'</span><span class="token punctuation">"</span></span><span class="token punctuation">></span></span> company_name = #{companyName}, <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>if</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>if</span> <span class="token attr-name">test</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>ordered != null<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> ordered = #{ordered}, <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>if</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>if</span> <span class="token attr-name">test</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>description != null and description != <span class="token punctuation">'</span><span class="token punctuation">'</span><span class="token punctuation">"</span></span><span class="token punctuation">></span></span> description = #{description}, <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>if</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>if</span> <span class="token attr-name">test</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>status != null<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> status = #{status} <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>if</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>set</span><span class="token punctuation">></span></span> where id = #{id};<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>update</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><blockquote><p><em>set</em> 标签可以用于动态包含需要更新的列,忽略其它不更新的列。</p></blockquote><h4 id="1-7-3-编写测试方法"><a href="#1-7-3-编写测试方法" class="headerlink" title="1.7.3 编写测试方法"></a>1.7.3 编写测试方法</h4><p>在 <code>test/java</code> 下的 <code>com.itheima.mapper</code> 包下的 <code>MybatisTest类中</code> 定义测试方法</p><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token annotation punctuation">@Test</span><span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testUpdate</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">IOException</span> <span class="token punctuation">{</span> <span class="token comment">//接收参数</span> <span class="token keyword">int</span> status <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token class-name">String</span> companyName <span class="token operator">=</span> <span class="token string">"波导手机"</span><span class="token punctuation">;</span> <span class="token class-name">String</span> brandName <span class="token operator">=</span> <span class="token string">"波导"</span><span class="token punctuation">;</span> <span class="token class-name">String</span> description <span class="token operator">=</span> <span class="token string">"波导手机,手机中的战斗机"</span><span class="token punctuation">;</span> <span class="token keyword">int</span> ordered <span class="token operator">=</span> <span class="token number">200</span><span class="token punctuation">;</span> <span class="token keyword">int</span> id <span class="token operator">=</span> <span class="token number">6</span><span class="token punctuation">;</span> <span class="token comment">//封装对象</span> <span class="token class-name">Brand</span> brand <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Brand</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> brand<span class="token punctuation">.</span><span class="token function">setStatus</span><span class="token punctuation">(</span>status<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// brand.setCompanyName(companyName);</span> <span class="token comment">// brand.setBrandName(brandName);</span> <span class="token comment">// brand.setDescription(description);</span> <span class="token comment">// brand.setOrdered(ordered);</span> brand<span class="token punctuation">.</span><span class="token function">setId</span><span class="token punctuation">(</span>id<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//1. 获取SqlSessionFactory</span> <span class="token class-name">String</span> resource <span class="token operator">=</span> <span class="token string">"mybatis-config.xml"</span><span class="token punctuation">;</span> <span class="token class-name">InputStream</span> inputStream <span class="token operator">=</span> <span class="token class-name">Resources</span><span class="token punctuation">.</span><span class="token function">getResourceAsStream</span><span class="token punctuation">(</span>resource<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">SqlSessionFactory</span> sqlSessionFactory <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SqlSessionFactoryBuilder</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">build</span><span class="token punctuation">(</span>inputStream<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//2. 获取SqlSession对象</span> <span class="token class-name">SqlSession</span> sqlSession <span class="token operator">=</span> sqlSessionFactory<span class="token punctuation">.</span><span class="token function">openSession</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//SqlSession sqlSession = sqlSessionFactory.openSession(true);</span> <span class="token comment">//3. 获取Mapper接口的代理对象</span> <span class="token class-name">BrandMapper</span> brandMapper <span class="token operator">=</span> sqlSession<span class="token punctuation">.</span><span class="token function">getMapper</span><span class="token punctuation">(</span><span class="token class-name">BrandMapper</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//4. 执行方法</span> <span class="token keyword">int</span> count <span class="token operator">=</span> brandMapper<span class="token punctuation">.</span><span class="token function">update</span><span class="token punctuation">(</span>brand<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>count<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//提交事务</span> sqlSession<span class="token punctuation">.</span><span class="token function">commit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//5. 释放资源</span> sqlSession<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>执行测试方法结果如下:</p><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210729224205522.png" alt="image-20210729224205522"></p><p>从结果中SQL语句可以看出,只修改了 <code>status</code> 字段值,因为我们给的数据中只给Brand实体对象的 <code>status</code> 属性设置值了。这就是 <code>set</code> 标签的作用。</p><h3 id="1-8-删除一行数据"><a href="#1-8-删除一行数据" class="headerlink" title="1.8 删除一行数据"></a>1.8 删除一行数据</h3><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210729224549305.png" alt="image-20210729224549305"></p><p>如上图所示,每行数据后面都有一个 <code>删除</code> 按钮,当用户点击了该按钮,就会将改行数据删除掉。那我们就需要思考,这种删除是根据什么进行删除呢?是通过主键id删除,因为id是表中数据的唯一标识。</p><p>接下来就来实现该功能。</p><h4 id="1-8-1-编写接口方法"><a href="#1-8-1-编写接口方法" class="headerlink" title="1.8.1 编写接口方法"></a>1.8.1 编写接口方法</h4><p>在 <code>BrandMapper</code> 接口中定义根据id删除方法。</p><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">/** * 根据id删除 */</span><span class="token keyword">void</span> <span class="token function">deleteById</span><span class="token punctuation">(</span><span class="token keyword">int</span> id<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre><h4 id="1-8-2-编写SQL语句"><a href="#1-8-2-编写SQL语句" class="headerlink" title="1.8.2 编写SQL语句"></a>1.8.2 编写SQL语句</h4><p>在 <code>BrandMapper.xml</code> 映射配置文件中编写删除一行数据的 <code>statement</code></p><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>delete</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>deleteById<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> delete from tb_brand where id = #{id};<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>delete</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre><h4 id="1-8-3-编写测试方法"><a href="#1-8-3-编写测试方法" class="headerlink" title="1.8.3 编写测试方法"></a>1.8.3 编写测试方法</h4><p>在 <code>test/java</code> 下的 <code>com.itheima.mapper</code> 包下的 <code>MybatisTest类中</code> 定义测试方法</p><pre class="line-numbers language-java" data-language="java"><code class="language-java"> <span class="token annotation punctuation">@Test</span><span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testDeleteById</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">IOException</span> <span class="token punctuation">{</span> <span class="token comment">//接收参数</span> <span class="token keyword">int</span> id <span class="token operator">=</span> <span class="token number">6</span><span class="token punctuation">;</span> <span class="token comment">//1. 获取SqlSessionFactory</span> <span class="token class-name">String</span> resource <span class="token operator">=</span> <span class="token string">"mybatis-config.xml"</span><span class="token punctuation">;</span> <span class="token class-name">InputStream</span> inputStream <span class="token operator">=</span> <span class="token class-name">Resources</span><span class="token punctuation">.</span><span class="token function">getResourceAsStream</span><span class="token punctuation">(</span>resource<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">SqlSessionFactory</span> sqlSessionFactory <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SqlSessionFactoryBuilder</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">build</span><span class="token punctuation">(</span>inputStream<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//2. 获取SqlSession对象</span> <span class="token class-name">SqlSession</span> sqlSession <span class="token operator">=</span> sqlSessionFactory<span class="token punctuation">.</span><span class="token function">openSession</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//SqlSession sqlSession = sqlSessionFactory.openSession(true);</span> <span class="token comment">//3. 获取Mapper接口的代理对象</span> <span class="token class-name">BrandMapper</span> brandMapper <span class="token operator">=</span> sqlSession<span class="token punctuation">.</span><span class="token function">getMapper</span><span class="token punctuation">(</span><span class="token class-name">BrandMapper</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//4. 执行方法</span> brandMapper<span class="token punctuation">.</span><span class="token function">deleteById</span><span class="token punctuation">(</span>id<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//提交事务</span> sqlSession<span class="token punctuation">.</span><span class="token function">commit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//5. 释放资源</span> sqlSession<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>运行过程只要没报错,直接到数据库查询数据是否还存在。</p><h3 id="1-9-批量删除"><a href="#1-9-批量删除" class="headerlink" title="1.9 批量删除"></a>1.9 批量删除</h3><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210729225713894.png" alt="image-20210729225713894" style="zoom:70%;"><p>如上图所示,用户可以选择多条数据,然后点击上面的 <code>删除</code> 按钮,就会删除数据库中对应的多行数据。</p><h4 id="1-9-1-编写接口方法"><a href="#1-9-1-编写接口方法" class="headerlink" title="1.9.1 编写接口方法"></a>1.9.1 编写接口方法</h4><p>在 <code>BrandMapper</code> 接口中定义删除多行数据的方法。</p><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">/** * 批量删除 */</span><span class="token keyword">void</span> <span class="token function">deleteByIds</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> ids<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre><blockquote><p>参数是一个数组,数组中存储的是多条数据的id</p></blockquote><h4 id="1-9-2-编写SQL语句"><a href="#1-9-2-编写SQL语句" class="headerlink" title="1.9.2 编写SQL语句"></a>1.9.2 编写SQL语句</h4><p>在 <code>BrandMapper.xml</code> 映射配置文件中编写删除多条数据的 <code>statement</code>。</p><p>编写SQL时需要遍历数组来拼接SQL语句。Mybatis 提供了 <code>foreach</code> 标签供我们使用</p><p><strong>foreach 标签</strong></p><p>用来迭代任何可迭代的对象(如数组,集合)。</p><ul><li>collection 属性:<ul><li>mybatis会将数组参数,封装为一个Map集合。<ul><li>默认:array = 数组</li><li>使用@Param注解改变map集合的默认key的名称</li></ul></li></ul></li><li>item 属性:本次迭代获取到的元素。</li><li>separator 属性:集合项迭代之间的分隔符。<code>foreach</code> 标签不会错误地添加多余的分隔符。也就是最后一次迭代不会加分隔符。</li><li>open 属性:该属性值是在拼接SQL语句之前拼接的语句,只会拼接一次</li><li>close 属性:该属性值是在拼接SQL语句拼接后拼接的语句,只会拼接一次</li></ul><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>delete</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>deleteByIds<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> delete from tb_brand where id in <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>foreach</span> <span class="token attr-name">collection</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>array<span class="token punctuation">"</span></span> <span class="token attr-name">item</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>id<span class="token punctuation">"</span></span> <span class="token attr-name">separator</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>,<span class="token punctuation">"</span></span> <span class="token attr-name">open</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>(<span class="token punctuation">"</span></span> <span class="token attr-name">close</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>)<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> #{id} <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>foreach</span><span class="token punctuation">></span></span> ;<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>delete</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><blockquote><p>假如数组中的id数据是{1,2,3},那么拼接后的sql语句就是:</p><pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">delete</span> <span class="token keyword">from</span> tb_brand <span class="token keyword">where</span> id <span class="token operator">in</span> <span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></blockquote><h4 id="1-9-3-编写测试方法"><a href="#1-9-3-编写测试方法" class="headerlink" title="1.9.3 编写测试方法"></a>1.9.3 编写测试方法</h4><p>在 <code>test/java</code> 下的 <code>com.itheima.mapper</code> 包下的 <code>MybatisTest类中</code> 定义测试方法</p><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token annotation punctuation">@Test</span><span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testDeleteByIds</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">IOException</span> <span class="token punctuation">{</span> <span class="token comment">//接收参数</span> <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> ids <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">,</span><span class="token number">8</span><span class="token punctuation">}</span><span class="token punctuation">;</span> <span class="token comment">//1. 获取SqlSessionFactory</span> <span class="token class-name">String</span> resource <span class="token operator">=</span> <span class="token string">"mybatis-config.xml"</span><span class="token punctuation">;</span> <span class="token class-name">InputStream</span> inputStream <span class="token operator">=</span> <span class="token class-name">Resources</span><span class="token punctuation">.</span><span class="token function">getResourceAsStream</span><span class="token punctuation">(</span>resource<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">SqlSessionFactory</span> sqlSessionFactory <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SqlSessionFactoryBuilder</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">build</span><span class="token punctuation">(</span>inputStream<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//2. 获取SqlSession对象</span> <span class="token class-name">SqlSession</span> sqlSession <span class="token operator">=</span> sqlSessionFactory<span class="token punctuation">.</span><span class="token function">openSession</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//SqlSession sqlSession = sqlSessionFactory.openSession(true);</span> <span class="token comment">//3. 获取Mapper接口的代理对象</span> <span class="token class-name">BrandMapper</span> brandMapper <span class="token operator">=</span> sqlSession<span class="token punctuation">.</span><span class="token function">getMapper</span><span class="token punctuation">(</span><span class="token class-name">BrandMapper</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//4. 执行方法</span> brandMapper<span class="token punctuation">.</span><span class="token function">deleteByIds</span><span class="token punctuation">(</span>ids<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//提交事务</span> sqlSession<span class="token punctuation">.</span><span class="token function">commit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//5. 释放资源</span> sqlSession<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h3 id="1-10-Mybatis参数传递"><a href="#1-10-Mybatis参数传递" class="headerlink" title="1.10 Mybatis参数传递"></a>1.10 Mybatis参数传递</h3><p>Mybatis 接口方法中可以接收各种各样的参数,如下:</p><ul><li>多个参数</li><li>单个参数:单个参数又可以是如下类型<ul><li>POJO 类型</li><li>Map 集合类型</li><li>Collection 集合类型</li><li>List 集合类型</li><li>Array 类型</li><li>其他类型</li></ul></li></ul><h4 id="1-10-1-多个参数"><a href="#1-10-1-多个参数" class="headerlink" title="1.10.1 多个参数"></a>1.10.1 多个参数</h4><p>如下面的代码,就是接收两个参数,而接收多个参数需要使用 <code>@Param</code> 注解,那么为什么要加该注解呢?这个问题要弄明白就必须来研究Mybatis 底层对于这些参数是如何处理的。</p><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">User</span> <span class="token function">select</span><span class="token punctuation">(</span><span class="token annotation punctuation">@Param</span><span class="token punctuation">(</span><span class="token string">"username"</span><span class="token punctuation">)</span> <span class="token class-name">String</span> username<span class="token punctuation">,</span><span class="token annotation punctuation">@Param</span><span class="token punctuation">(</span><span class="token string">"password"</span><span class="token punctuation">)</span> <span class="token class-name">String</span> password<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>select</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>select<span class="token punctuation">"</span></span> <span class="token attr-name">resultType</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>user<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>select * from tb_user where username=#{username} and password=#{password}<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>select</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>我们在接口方法中定义多个参数,Mybatis 会将这些参数封装成 Map 集合对象,值就是参数值,而键在没有使用 <code>@Param</code> 注解时有以下命名规则:</p><ul><li><p>以 arg 开头 :第一个参数就叫 arg0,第二个参数就叫 arg1,以此类推。如:</p><blockquote><p>map.put(“arg0”,参数值1);</p><p>map.put(“arg1”,参数值2);</p></blockquote></li><li><p>以 param 开头 : 第一个参数就叫 param1,第二个参数就叫 param2,依次类推。如:</p><blockquote><p>map.put(“param1”,参数值1);</p><p>map.put(“param2”,参数值2);</p></blockquote></li></ul><p><strong>代码验证:</strong></p><ul><li><p>在 <code>UserMapper</code> 接口中定义如下方法</p><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">User</span> <span class="token function">select</span><span class="token punctuation">(</span><span class="token class-name">String</span> username<span class="token punctuation">,</span><span class="token class-name">String</span> password<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></li><li><p>在 <code>UserMapper.xml</code> 映射配置文件中定义SQL</p><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>select</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>select<span class="token punctuation">"</span></span> <span class="token attr-name">resultType</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>user<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>select * from tb_user where username=#{arg0} and password=#{arg1}<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>select</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>或者</p><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>select</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>select<span class="token punctuation">"</span></span> <span class="token attr-name">resultType</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>user<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>select * from tb_user where username=#{param1} and password=#{param2}<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>select</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></li><li><p>运行代码结果如下</p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210805230303461.png" alt="image-20210805230303461" style="zoom:80%;"><p>在映射配合文件的SQL语句中使用用 <code>arg</code> 开头的和 <code>param</code> 书写,代码的可读性会变的特别差,此时可以使用 <code>@Param</code> 注解。</p></li></ul><p>在接口方法参数上使用 <code>@Param</code> 注解,Mybatis 会将 <code>arg</code> 开头的键名替换为对应注解的属性值。</p><p><strong>代码验证:</strong></p><ul><li><p>在 <code>UserMapper</code> 接口中定义如下方法,在 <code>username</code> 参数前加上 <code>@Param</code> 注解</p><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">User</span> <span class="token function">select</span><span class="token punctuation">(</span><span class="token annotation punctuation">@Param</span><span class="token punctuation">(</span><span class="token string">"username"</span><span class="token punctuation">)</span> <span class="token class-name">String</span> username<span class="token punctuation">,</span> <span class="token class-name">String</span> password<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>Mybatis 在封装 Map 集合时,键名就会变成如下:</p><blockquote><p>map.put(“username”,参数值1);</p><p>map.put(“arg1”,参数值2);</p><p>map.put(“param1”,参数值1);</p><p>map.put(“param2”,参数值2);</p></blockquote></li><li><p>在 <code>UserMapper.xml</code> 映射配置文件中定义SQL</p><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>select</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>select<span class="token punctuation">"</span></span> <span class="token attr-name">resultType</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>user<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>select * from tb_user where username=#{username} and password=#{param2}<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>select</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></li><li><p>运行程序结果没有报错。而如果将 <code>#{}</code> 中的 <code>username</code> 还是写成 <code>arg0</code> </p><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>select</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>select<span class="token punctuation">"</span></span> <span class="token attr-name">resultType</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>user<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>select * from tb_user where username=#{arg0} and password=#{param2}<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>select</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></li><li><p>运行程序则可以看到错误</p><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210805231727206.png" alt="image-20210805231727206"></p></li></ul><p>==结论:以后接口参数是多个时,在每个参数上都使用 <code>@Param</code> 注解。这样代码的可读性更高。==</p><h4 id="1-10-2-单个参数"><a href="#1-10-2-单个参数" class="headerlink" title="1.10.2 单个参数"></a>1.10.2 单个参数</h4><ul><li><p>POJO 类型</p><p>直接使用。要求 <code>属性名</code> 和 <code>参数占位符名称</code> 一致</p></li><li><p>Map 集合类型</p><p>直接使用。要求 <code>map集合的键名</code> 和 <code>参数占位符名称</code> 一致</p></li><li><p>Collection 集合类型</p><p>Mybatis 会将集合封装到 map 集合中,如下:</p><blockquote><p>map.put(“arg0”,collection集合);</p><p>map.put(“collection”,collection集合;</p></blockquote><p>==可以使用 <code>@Param</code> 注解替换map集合中默认的 arg 键名。==</p></li><li><p>List 集合类型</p><p>Mybatis 会将集合封装到 map 集合中,如下:</p><blockquote><p>map.put(“arg0”,list集合);</p><p>map.put(“collection”,list集合);</p><p>map.put(“list”,list集合);</p></blockquote><p>==可以使用 <code>@Param</code> 注解替换map集合中默认的 arg 键名。==</p></li><li><p>Array 类型</p><p>Mybatis 会将集合封装到 map 集合中,如下:</p><blockquote><p>map.put(“arg0”,数组);</p><p>map.put(“array”,数组);</p></blockquote><p>==可以使用 <code>@Param</code> 注解替换map集合中默认的 arg 键名。==</p></li><li><p>其他类型</p><p>比如int类型,<code>参数占位符名称</code> 叫什么都可以。尽量做到见名知意</p></li></ul><h2 id="2,注解实现CRUD"><a href="#2,注解实现CRUD" class="headerlink" title="2,注解实现CRUD"></a>2,注解实现CRUD</h2><p>使用注解开发会比配置文件开发更加方便。如下就是使用注解进行开发</p><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token annotation punctuation">@Select</span><span class="token punctuation">(</span>value <span class="token operator">=</span> <span class="token string">"select * from tb_user where id = #{id}"</span><span class="token punctuation">)</span><span class="token keyword">public</span> <span class="token class-name">User</span> <span class="token function">select</span><span class="token punctuation">(</span><span class="token keyword">int</span> id<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre><blockquote><p>==注意:==</p><ul><li>注解是用来替换映射配置文件方式配置的,所以使用了注解,就不需要再映射配置文件中书写对应的 <code>statement</code></li></ul></blockquote><p>Mybatis 针对 CURD 操作都提供了对应的注解,已经做到见名知意。如下:</p><ul><li>查询 :@Select</li><li>添加 :@Insert</li><li>修改 :@Update</li><li>删除 :@Delete</li></ul><p>接下来我们做一个案例来使用 Mybatis 的注解开发</p><p><strong>代码实现:</strong></p><ul><li><p>将之前案例中 <code>UserMapper.xml</code> 中的 根据id查询数据 的 <code>statement</code> 注释掉</p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210805235229938.png" alt="image-20210805235229938" style="zoom:70%;"></li><li><p>在 <code>UserMapper</code> 接口的 <code>selectById</code> 方法上添加注解</p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210805235405070.png" alt="image-20210805235405070" style="zoom:70%;"></li><li><p>运行测试程序也能正常查询到数据</p></li></ul><p>我们课程上只演示这一个查询的注解开发,其他的同学们下来可以自己实现,都是比较简单。</p><p>==注意:==在官方文档中 <code>入门</code> 中有这样的一段话:</p><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210805234302849.png" alt="image-20210805234302849"></p><p>所以,==注解完成简单功能,配置文件完成复杂功能。==</p><p>而我们之前写的动态 SQL 就是复杂的功能,如果用注解使用的话,就需要使用到 Mybatis 提供的SQL构建器来完成,而对应的代码如下:</p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210805234842497.png" alt="image-20210805234842497" style="zoom:70%;"><p>上述代码将java代码和SQL语句融到了一块,使得代码的可读性大幅度降低。</p>]]></content>
<summary type="html"><h2 id="Mybatis练习"><a href="#Mybatis练习" class="headerlink" title="Mybatis练习"></a>Mybatis练习</h2><p><strong>目标</strong></p>
<blockquote>
<ul>
</summary>
</entry>
<entry>
<title></title>
<link href="http://wlh55.github.io/2022/02/28/Spring%E7%AC%AC%E4%B8%80%E5%A4%A9/"/>
<id>http://wlh55.github.io/2022/02/28/Spring%E7%AC%AC%E4%B8%80%E5%A4%A9/</id>
<published>2022-02-28T02:45:29.787Z</published>
<updated>2022-11-06T16:30:05.912Z</updated>
<content type="html"><![CDATA[<h2 id="学习目标"><a href="#学习目标" class="headerlink" title="学习目标"></a>学习目标</h2><ul><li><input disabled="" type="checkbox"> 能够说出Spring的体系结构</li><li><input disabled="" type="checkbox"> 能够编写IOC入门案例</li><li><input disabled="" type="checkbox"> 能够编写DI入门案例</li><li><input disabled="" type="checkbox"> 能够配置setter方式注入属性值</li><li><input disabled="" type="checkbox"> 能够配置构造方式注入属性值</li><li><input disabled="" type="checkbox"> 能够理解什么是自动装配</li></ul><h2 id="一、Spring简介"><a href="#一、Spring简介" class="headerlink" title="一、Spring简介"></a>一、Spring简介</h2><h3 id="1-Spring课程介绍"><a href="#1-Spring课程介绍" class="headerlink" title="1 Spring课程介绍"></a>1 Spring课程介绍</h3><h4 id="问题导入"><a href="#问题导入" class="headerlink" title="问题导入"></a>问题导入</h4><p>我们为什么要学习Spring框架?</p><h4 id="1-1-为什么要学"><a href="#1-1-为什么要学" class="headerlink" title="1.1 为什么要学"></a>1.1 为什么要学</h4><ul><li><p>Spring技术是JavaEE开发必备技能,企业开发技术选型命中率>==90%==</p></li><li><p>专业角度</p><ul><li>==<strong>简化开发</strong>==,降低企业级开发的复杂性</li><li>**==框架整合==**,高效整合其他技术,提高企业级应用开发与运行效率</li></ul></li></ul><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210729171139088.png" alt="image-20210729171139088"></p><h4 id="1-2-学什么"><a href="#1-2-学什么" class="headerlink" title="1.2 学什么"></a>1.2 学什么</h4><ul><li><p>简化开发</p><ul><li>==IOC(反转控制)==</li><li>==AOP(面向切面编程)==<ul><li>==事务处理==</li></ul></li></ul></li><li><p>框架整合</p><ul><li>MyBatis</li><li>MyBatis-plus</li><li>Struts</li><li>Struts2</li><li>Hibernate</li><li>……</li></ul></li></ul><h4 id="1-3-怎么学"><a href="#1-3-怎么学" class="headerlink" title="1.3 怎么学"></a>1.3 怎么学</h4><ul><li>学习Spring框架设计思想</li><li>学习基础操作,思考操作与思想间的联系</li><li>学习案例,熟练应用操作的同时,体会思想</li></ul><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210729171346022.png" alt="image-20210729171346022"></p><h3 id="2-初识Spring"><a href="#2-初识Spring" class="headerlink" title="2 初识Spring"></a>2 初识Spring</h3><h4 id="问题导入-1"><a href="#问题导入-1" class="headerlink" title="问题导入"></a>问题导入</h4><p>目前我们使用的是Spring几版本?</p><h4 id="2-1-Spring家族"><a href="#2-1-Spring家族" class="headerlink" title="2.1 Spring家族"></a>2.1 Spring家族</h4><ul><li>官网:<a href="https://spring.io/">https://spring.io</a></li><li>Spring发展到今天已经形成了一种开发的生态圈,Spring提供了若干个项目,每个项目用于完成特定的功能。</li></ul><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210729171850181.png" alt="image-20210729171850181"></p><h4 id="2-2-Spring发展史"><a href="#2-2-Spring发展史" class="headerlink" title="2.2 Spring发展史"></a>2.2 Spring发展史</h4><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210729171926576.png" alt="image-20210729171926576"></p><h3 id="3-Spring体系结构"><a href="#3-Spring体系结构" class="headerlink" title="3 Spring体系结构"></a>3 Spring体系结构</h3><h4 id="问题导入-2"><a href="#问题导入-2" class="headerlink" title="问题导入"></a>问题导入</h4><p>通过系统架构图,Spring能不能进行数据层开发?Spring能不能进行web层开发?</p><h4 id="3-1-Spring-Framework系统架构图"><a href="#3-1-Spring-Framework系统架构图" class="headerlink" title="3.1 Spring Framework系统架构图"></a>3.1 Spring Framework系统架构图</h4><ul><li>Spring Framework是Spring生态圈中最基础的项目,是其他项目的根基</li></ul><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210729172153796.png" alt="image-20210729172153796"></p><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210729172352627.png" alt="image-20210729172352627"></p><h4 id="3-2-Spring-Framework课程学习路线"><a href="#3-2-Spring-Framework课程学习路线" class="headerlink" title="3.2 Spring Framework课程学习路线"></a>3.2 Spring Framework课程学习路线</h4><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210729172513669.png" alt="image-20210729172513669"></p><h3 id="4-Spring核心概念"><a href="#4-Spring核心概念" class="headerlink" title="4 Spring核心概念"></a>4 Spring核心概念</h3><h4 id="问题导入-3"><a href="#问题导入-3" class="headerlink" title="问题导入"></a>问题导入</h4><p>问题1:目前我们的代码存在什么问题以及怎么解决这些问题?</p><p>问题2:请描述什么是IOC,什么是DI?</p><h4 id="4-1-目前我们代码存在的问题"><a href="#4-1-目前我们代码存在的问题" class="headerlink" title="4.1 目前我们代码存在的问题"></a>4.1 目前我们代码存在的问题</h4><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210729173516149.png" alt="image-20210729173516149"></p><ul><li>代码书写现状<ul><li>耦合度偏高</li></ul></li><li>解决方案<ul><li>使用对象时,在程序中不要主动使用new产生对象,转换为由外部提供对象</li></ul></li></ul><h4 id="4-2-核心概念"><a href="#4-2-核心概念" class="headerlink" title="4.2 核心概念"></a>4.2 核心概念</h4><ul><li><p>==IOC(Inversion of Control)控制反转==</p><p>使用对象时,由主动new产生对象转换为由==外部==提供对象,此过程中对象创建控制权由程序转移到外部,此思想称为控制反转。通俗的讲就是“==将new对象的权利交给Spring,我们从Spring中获取对象使用即可==”</p></li><li><p>Spring技术对IoC思想进行了实现</p><ul><li>Spring提供了一个容器,称为==IOC容器==,用来充当IoC思想中的“外部”</li><li>IOC容器负责对象的创建、初始化等一系列工作,被创建或被管理的对象在IoC容器中统称为==Bean==</li></ul></li><li><p>==DI(Dependency Injection)依赖注入==</p><ul><li>在容器中建立bean与bean之间的依赖关系的整个过程,称为依赖注入。</li></ul></li></ul><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210729174148134.png" alt="image-20210729174148134"></p><ul><li>目标:充分解耦<ul><li>使用IoC容器管理bean(IOC)</li><li>在IoC容器内将有依赖关系的bean进行关系绑定(DI)</li></ul></li><li>最终效果<ul><li>使用对象时不仅可以直接从IoC容器中获取,并且获取到的bean已经绑定了所有的依赖关系</li></ul></li></ul><h2 id="二、IOC和DI入门案例【重点】"><a href="#二、IOC和DI入门案例【重点】" class="headerlink" title="二、IOC和DI入门案例【重点】"></a>二、IOC和DI入门案例【重点】</h2><h3 id="1-IOC入门案例【重点】"><a href="#1-IOC入门案例【重点】" class="headerlink" title="1 IOC入门案例【重点】"></a>1 IOC入门案例【重点】</h3><h4 id="问题导入-4"><a href="#问题导入-4" class="headerlink" title="问题导入"></a>问题导入</h4><p><bean>标签中id属性和class属性的作用是什么?</p><h4 id="1-1-门案例思路分析"><a href="#1-1-门案例思路分析" class="headerlink" title="1.1 门案例思路分析"></a>1.1 门案例思路分析</h4><ol><li>管理什么?(Service与Dao)</li><li>如何将被管理的对象告知IOC容器?(配置文件)</li><li>被管理的对象交给IOC容器,如何获取到IoC容器?(接口)</li><li>IOC容器得到后,如何从容器中获取bean?(接口方法)</li><li>使用Spring导入哪些坐标?(pom.xml)</li></ol><h4 id="1-2-实现步骤"><a href="#1-2-实现步骤" class="headerlink" title="1.2 实现步骤"></a>1.2 实现步骤</h4><pre class="line-numbers language-none"><code class="language-none">【第一步】导入Spring坐标【第二步】定义Spring管理的类(接口)【第三步】创建Spring配置文件,配置对应类作为Spring管理的bean对象【第四步】初始化IOC容器(Spring核心容器/Spring容器),通过容器获取bean对象<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre><h4 id="1-3-实现代码"><a href="#1-3-实现代码" class="headerlink" title="1.3 实现代码"></a>1.3 实现代码</h4><p><strong>【第一步】导入Spring坐标</strong></p><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>dependencies</span><span class="token punctuation">></span></span> <span class="token comment"><!--导入spring的坐标spring-context,对应版本是5.2.10.RELEASE--></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>dependency</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>groupId</span><span class="token punctuation">></span></span>org.springframework<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>groupId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>artifactId</span><span class="token punctuation">></span></span>spring-context<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>artifactId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>version</span><span class="token punctuation">></span></span>5.2.10.RELEASE<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>version</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>dependency</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>dependencies</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p><strong>【第二步】定义Spring管理的类(接口)</strong></p><ul><li>BookDao接口和BookDaoImpl实现类</li></ul><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">BookDao</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">save</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">BookDaoImpl</span> <span class="token keyword">implements</span> <span class="token class-name">BookDao</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">save</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"book dao save ..."</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><ul><li>BookService接口和BookServiceImpl实现类</li></ul><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">BookService</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">save</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">BookServiceImpl</span> <span class="token keyword">implements</span> <span class="token class-name">BookService</span> <span class="token punctuation">{</span> <span class="token keyword">private</span> <span class="token class-name">BookDao</span> bookDao <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BookDaoImpl</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">save</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"book service save ..."</span><span class="token punctuation">)</span><span class="token punctuation">;</span> bookDao<span class="token punctuation">.</span><span class="token function">save</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p><strong>【第三步】创建Spring配置文件,配置对应类作为Spring管理的bean对象</strong></p><ul><li>定义applicationContext.xml配置文件并配置BookServiceImpl</li></ul><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token prolog"><?xml version="1.0" encoding="UTF-8"?></span><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>beans</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>http://www.springframework.org/schema/beans<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">xmlns:</span>xsi</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>http://www.w3.org/2001/XMLSchema-instance<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">xsi:</span>schemaLocation</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token comment"><!--bean标签:表示配置bean id属性:表示给bean起名字 class属性:表示给bean定义类型--></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>bean</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>bookService<span class="token punctuation">"</span></span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>com.itheima.service.impl.BookServiceImpl<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>bean</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>beans</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p><strong>==注意事项:bean定义时id属性在同一个上下文中(IOC容器中)不能重复==</strong></p><p><strong>【第四步】初始化IOC容器(Spring核心容器/Spring容器),通过容器获取Bean对象</strong></p><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">App</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">//1.创建IoC容器对象,加载spring核心配置文件</span> <span class="token class-name">ApplicationContext</span> ctx <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ClassPathXmlApplicationContext</span><span class="token punctuation">(</span><span class="token string">"applicationContext.xml"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//2 从IOC容器中获取Bean对象(BookService对象)</span> <span class="token class-name">BookService</span> bookService<span class="token operator">=</span> <span class="token punctuation">(</span><span class="token class-name">BookService</span><span class="token punctuation">)</span>ctx<span class="token punctuation">.</span><span class="token function">getBean</span><span class="token punctuation">(</span><span class="token string">"bookService"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//3 调用Bean对象(BookService对象)的方法</span> bookService<span class="token punctuation">.</span><span class="token function">save</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h4 id="1-4-运行结果"><a href="#1-4-运行结果" class="headerlink" title="1.4 运行结果"></a>1.4 运行结果</h4><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210729184337603.png" alt="image-20210729184337603"></p><h3 id="2-DI入门案例【重点】"><a href="#2-DI入门案例【重点】" class="headerlink" title="2 DI入门案例【重点】"></a>2 DI入门案例【重点】</h3><h4 id="问题导入-5"><a href="#问题导入-5" class="headerlink" title="问题导入"></a>问题导入</h4><p><property>标签中name属性和ref属性的作用是什么?</p><h4 id="2-1-DI入门案例思路分析"><a href="#2-1-DI入门案例思路分析" class="headerlink" title="2.1 DI入门案例思路分析"></a>2.1 DI入门案例思路分析</h4><ol><li>基于IOC管理bean</li><li>Service中使用new形式创建的Dao对象是否保留?(否)</li><li>Service中需要的Dao对象如何进入到Service中?(提供方法)</li><li>Service与Dao间的关系如何描述?(配置)</li></ol><h4 id="2-2-实现步骤"><a href="#2-2-实现步骤" class="headerlink" title="2.2 实现步骤"></a>2.2 实现步骤</h4><pre class="line-numbers language-none"><code class="language-none">【第一步】删除使用new的形式创建对象的代码【第二步】提供依赖对象对应的setter方法【第三步】配置service与dao之间的关系<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre><h4 id="2-3-实现代码"><a href="#2-3-实现代码" class="headerlink" title="2.3 实现代码"></a>2.3 实现代码</h4><p><strong>【第一步】删除使用new的形式创建对象的代码</strong></p><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">BookServiceImpl</span> <span class="token keyword">implements</span> <span class="token class-name">BookService</span> <span class="token punctuation">{</span> <span class="token keyword">private</span> <span class="token class-name">BookDao</span> bookDao<span class="token punctuation">;</span> <span class="token comment">//【第一步】删除使用new的形式创建对象的代码</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">save</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"book service save ..."</span><span class="token punctuation">)</span><span class="token punctuation">;</span> bookDao<span class="token punctuation">.</span><span class="token function">save</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p><strong>【第二步】提供依赖对象对应的setter方法</strong></p><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">BookServiceImpl</span> <span class="token keyword">implements</span> <span class="token class-name">BookService</span> <span class="token punctuation">{</span> <span class="token keyword">private</span> <span class="token class-name">BookDao</span> bookDao<span class="token punctuation">;</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">save</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"book service save ..."</span><span class="token punctuation">)</span><span class="token punctuation">;</span> bookDao<span class="token punctuation">.</span><span class="token function">save</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">//【第二步】提供依赖对象对应的setter方法</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setBookDao</span><span class="token punctuation">(</span><span class="token class-name">BookDao</span> bookDao<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>bookDao <span class="token operator">=</span> bookDao<span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p><strong>【第三步】配置service与dao之间的关系</strong></p><blockquote><p>在applicationContext.xml中配置</p></blockquote><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token prolog"><?xml version="1.0" encoding="UTF-8"?></span><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>beans</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>http://www.springframework.org/schema/beans<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">xmlns:</span>xsi</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>http://www.w3.org/2001/XMLSchema-instance<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">xsi:</span>schemaLocation</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token comment"><!--bean标签:表示配置bean id属性:表示给bean起名字 class属性:表示给bean定义类型--></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>bean</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>bookDao<span class="token punctuation">"</span></span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>com.itheima.dao.impl.BookDaoImpl<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>bean</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>bookService<span class="token punctuation">"</span></span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>com.itheima.service.impl.BookServiceImpl<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token comment"><!--配置server与dao的关系property标签:表示配置当前bean的属性 name属性:表示配置哪一个具体的属性 ref属性:表示参照哪一个bean--></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>property</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>bookDao<span class="token punctuation">"</span></span> <span class="token attr-name">ref</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>bookDao<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>bean</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>beans</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h4 id="2-4-图解演示"><a href="#2-4-图解演示" class="headerlink" title="2.4 图解演示"></a>2.4 图解演示</h4><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210729183104748.png" alt="image-20210729183104748"></p><h2 id="三、Bean的基础配置"><a href="#三、Bean的基础配置" class="headerlink" title="三、Bean的基础配置"></a>三、Bean的基础配置</h2><h3 id="问题导入-6"><a href="#问题导入-6" class="headerlink" title="问题导入"></a>问题导入</h3><p>问题1:在<bean>标签上如何配置别名?</p><p>问题2:Bean的默认作用范围是什么?如何修改?</p><h3 id="1-Bean基础配置【重点】"><a href="#1-Bean基础配置【重点】" class="headerlink" title="1 Bean基础配置【重点】"></a>1 Bean基础配置【重点】</h3><h4 id="配置说明"><a href="#配置说明" class="headerlink" title="配置说明"></a>配置说明</h4><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210729183500978.png" alt="image-20210729183500978"></p><h4 id="代码演示"><a href="#代码演示" class="headerlink" title="代码演示"></a>代码演示</h4><blockquote><p>见《IOC入门案例》applicationContext.xml配置</p></blockquote><h4 id="运行结果"><a href="#运行结果" class="headerlink" title="运行结果"></a>运行结果</h4><blockquote><p>见《IOC入门案例》运行结果</p></blockquote><h3 id="2-Bean别名配置"><a href="#2-Bean别名配置" class="headerlink" title="2 Bean别名配置"></a>2 Bean别名配置</h3><h4 id="配置说明-1"><a href="#配置说明-1" class="headerlink" title="配置说明"></a>配置说明</h4><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210729183558051.png" alt="image-20210729183558051"></p><h4 id="代码演示-1"><a href="#代码演示-1" class="headerlink" title="代码演示"></a>代码演示</h4><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210729191924626.png" alt="image-20210729191924626"></p><h4 id="打印结果"><a href="#打印结果" class="headerlink" title="打印结果"></a>打印结果</h4><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210729191954870.png" alt="image-20210729191954870"></p><h3 id="3-Bean作用范围配置【重点】"><a href="#3-Bean作用范围配置【重点】" class="headerlink" title="3 Bean作用范围配置【重点】"></a>3 Bean作用范围配置【重点】</h3><h4 id="配置说明-2"><a href="#配置说明-2" class="headerlink" title="配置说明"></a>配置说明</h4><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210729183628138.png" alt="image-20210729183628138"></p><blockquote><p>扩展:scope的取值不仅仅只有singleton和prototype,还有request、session、application、 websocket ,表示创建出的对象放置在web容器(tomcat)对应的位置。比如:request表示保存到request域中。</p></blockquote><h4 id="代码演示-2"><a href="#代码演示-2" class="headerlink" title="代码演示"></a>代码演示</h4><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210729192420048.png" alt="image-20210729192420048"></p><h4 id="打印结果-1"><a href="#打印结果-1" class="headerlink" title="打印结果"></a>打印结果</h4><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210729192730871.png" alt="image-20210729192730871"></p><blockquote><p>最后给大家说明一下:在我们的实际开发当中,绝大部分的Bean是单例的,也就是说绝大部分Bean不需要配置scope属性。</p></blockquote><h2 id="四、Bean的实例化"><a href="#四、Bean的实例化" class="headerlink" title="四、Bean的实例化"></a>四、Bean的实例化</h2><h3 id="问题导入-7"><a href="#问题导入-7" class="headerlink" title="问题导入"></a>问题导入</h3><p>Bean的实例化方式有几种?</p><h3 id="1-Bean是如何创建的【理解】"><a href="#1-Bean是如何创建的【理解】" class="headerlink" title="1 Bean是如何创建的【理解】"></a>1 Bean是如何创建的【理解】</h3><p>bean本质上就是对象,创建bean使用构造方法完成</p><h3 id="2-实例化Bean的三种方式"><a href="#2-实例化Bean的三种方式" class="headerlink" title="2 实例化Bean的三种方式"></a>2 实例化Bean的三种方式</h3><h4 id="2-1-构造方法方式【重点】"><a href="#2-1-构造方法方式【重点】" class="headerlink" title="2.1 构造方法方式【重点】"></a>2.1 构造方法方式【重点】</h4><ul><li>BookDaoImpl实现类</li></ul><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">BookDaoImpl</span> <span class="token keyword">implements</span> <span class="token class-name">BookDao</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token class-name">BookDaoImpl</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"book dao constructor is running ...."</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">save</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"book dao save ..."</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><ul><li>applicationContext.xml配置</li></ul><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token comment"><!--方式一:构造方法实例化bean--></span><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>bean</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>bookDao<span class="token punctuation">"</span></span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>com.itheima.dao.impl.BookDaoImpl<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre><ul><li>AppForInstanceBook测试类</li></ul><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">AppForInstanceBook</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">ApplicationContext</span> ctx <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ClassPathXmlApplicationContext</span><span class="token punctuation">(</span><span class="token string">"applicationContext.xml"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">BookDao</span> bookDao <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token class-name">BookDao</span><span class="token punctuation">)</span> ctx<span class="token punctuation">.</span><span class="token function">getBean</span><span class="token punctuation">(</span><span class="token string">"bookDao"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> bookDao<span class="token punctuation">.</span><span class="token function">save</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><ul><li>运行结果</li></ul><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210729194137151.png" alt="image-20210729194137151"></p><p>==注意:无参构造方法如果不存在,将抛出异常<code>BeanCreationException</code>==</p><h4 id="2-2-静态工厂方式"><a href="#2-2-静态工厂方式" class="headerlink" title="2.2 静态工厂方式"></a>2.2 静态工厂方式</h4><ul><li>OrderDao接口和OrderDaoImpl实现类</li></ul><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">OrderDao</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">save</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">OrderDaoImpl</span> <span class="token keyword">implements</span> <span class="token class-name">OrderDao</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">save</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"order dao save ..."</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><ul><li>OrderDaoFatory工厂类</li></ul><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">//静态工厂创建对象</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">OrderDaoFactory</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token class-name">OrderDao</span> <span class="token function">getOrderDao</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"factory setup...."</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">OrderDaoImpl</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><ul><li>applicationContext.xml配置</li></ul><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token comment"><!--方式二:使用静态工厂实例化bean--></span><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>bean</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>orderDao<span class="token punctuation">"</span></span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>com.itheima.factory.OrderDaoFactory<span class="token punctuation">"</span></span> <span class="token attr-name">factory-method</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>getOrderDao<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210729195248948.png" alt="image-20210729195248948"></p><ul><li>AppForInstanceOrder测试类</li></ul><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">AppForInstanceOrder</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">ApplicationContext</span> ctx <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ClassPathXmlApplicationContext</span><span class="token punctuation">(</span><span class="token string">"applicationContext.xml"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">OrderDao</span> orderDao <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token class-name">OrderDao</span><span class="token punctuation">)</span> ctx<span class="token punctuation">.</span><span class="token function">getBean</span><span class="token punctuation">(</span><span class="token string">"orderDao"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> orderDao<span class="token punctuation">.</span><span class="token function">save</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><ul><li>运行结果</li></ul><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210729195009198.png" alt="image-20210729195009198"></p><h4 id="2-3-实例工厂方式"><a href="#2-3-实例工厂方式" class="headerlink" title="2.3 实例工厂方式"></a>2.3 实例工厂方式</h4><ul><li>UserDao接口和UserDaoImpl实现类</li></ul><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">UserDao</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">save</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">UserDaoImpl</span> <span class="token keyword">implements</span> <span class="token class-name">UserDao</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">save</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"user dao save ..."</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><ul><li>UserDaoFactory工厂类</li></ul><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">//实例工厂创建对象</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">UserDaoFactory</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token class-name">UserDao</span> <span class="token function">getUserDao</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">UserDaoImpl</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><ul><li>applicationContext.xml配置</li></ul><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token comment"><!--方式三:使用实例工厂实例化bean--></span><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>bean</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>userFactory<span class="token punctuation">"</span></span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>com.itheima.factory.UserDaoFactory<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>bean</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>userDao<span class="token punctuation">"</span></span> <span class="token attr-name">factory-method</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>getUserDao<span class="token punctuation">"</span></span> <span class="token attr-name">factory-bean</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>userFactory<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210729200203249.png" alt="image-20210729200203249"></p><ul><li>AppForInstanceUser测试类</li></ul><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">AppForInstanceUser</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// //创建实例工厂对象</span> <span class="token comment">// UserDaoFactory userDaoFactory = new UserDaoFactory();</span> <span class="token comment">// //通过实例工厂对象创建对象</span> <span class="token comment">// UserDao userDao = userDaoFactory.getUserDao();</span> <span class="token comment">// userDao.save();</span> <span class="token class-name">ApplicationContext</span> ctx <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ClassPathXmlApplicationContext</span><span class="token punctuation">(</span><span class="token string">"applicationContext.xml"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">UserDao</span> userDao <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token class-name">UserDao</span><span class="token punctuation">)</span> ctx<span class="token punctuation">.</span><span class="token function">getBean</span><span class="token punctuation">(</span><span class="token string">"userDao"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> userDao<span class="token punctuation">.</span><span class="token function">save</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><ul><li>运行结果</li></ul><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210729200240820.png" alt="image-20210729200240820"></p><h4 id="2-4-实现FactoryBean-lt-T-gt-方式【扩展-了解】"><a href="#2-4-实现FactoryBean-lt-T-gt-方式【扩展-了解】" class="headerlink" title="2.4 实现FactoryBean<T>方式【扩展,了解】"></a>2.4 实现FactoryBean<T>方式【扩展,了解】</h4><ul><li>定义UserDaoFactoryBean实现FactoryBean<UserDao></li></ul><blockquote><p>UserDaoFactoryBean中实例化什么类型的对象泛型就是该类型。</p></blockquote><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">//FactoryBean创建对象</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">UserDaoFactoryBean</span> <span class="token keyword">implements</span> <span class="token class-name">FactoryBean</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">UserDao</span><span class="token punctuation">></span></span> <span class="token punctuation">{</span> <span class="token comment">//代替原始实例工厂中创建对象的方法</span> <span class="token keyword">public</span> <span class="token class-name">UserDao</span> <span class="token function">getObject</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Exception</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">UserDaoImpl</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token class-name">Class</span><span class="token generics"><span class="token punctuation"><</span><span class="token operator">?</span><span class="token punctuation">></span></span> <span class="token function">getObjectType</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token class-name">UserDao</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><ul><li>applicationContext.xml配置</li></ul><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token comment"><!--方式四:使用FactoryBean实例化bean--></span><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>bean</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>userDao<span class="token punctuation">"</span></span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>com.itheima.factory.UserDaoFactoryBean<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre><blockquote><p>使用之前的AppForInstanceUser测试类去运行看结果就行了。注意配置文件中id=”userDao”是否重复。</p></blockquote><h2 id="五、Bean的生命周期【了解】"><a href="#五、Bean的生命周期【了解】" class="headerlink" title="五、Bean的生命周期【了解】"></a>五、Bean的生命周期【了解】</h2><h3 id="问题导入-8"><a href="#问题导入-8" class="headerlink" title="问题导入"></a>问题导入</h3><p>问题1:多例的Bean能够配置并执行销毁的方法?</p><p>问题2:如何做才执行Bean销毁的方法?</p><h3 id="1-生命周期相关概念介绍"><a href="#1-生命周期相关概念介绍" class="headerlink" title="1 生命周期相关概念介绍"></a>1 生命周期相关概念介绍</h3><ul><li>生命周期:从创建到消亡的完整过程</li><li>bean生命周期:bean从创建到销毁的整体过程</li><li>bean生命周期控制:在bean创建后到销毁前做一些事情</li></ul><h3 id="2-代码演示"><a href="#2-代码演示" class="headerlink" title="2 代码演示"></a>2 代码演示</h3><h4 id="2-1-Bean生命周期控制"><a href="#2-1-Bean生命周期控制" class="headerlink" title="2.1 Bean生命周期控制"></a>2.1 Bean生命周期控制</h4><ul><li>提供生命周期控制方法</li></ul><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">BookDaoImpl</span> <span class="token keyword">implements</span> <span class="token class-name">BookDao</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">save</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"book dao save ..."</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">//表示bean初始化对应的操作</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"init..."</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">//表示bean销毁前对应的操作</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">destory</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"destory..."</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><ul><li>applicationContext.xml配置</li></ul><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token comment"><!--init-method:设置bean初始化生命周期回调函数,此处填写init方法名--></span><span class="token comment"><!--destroy-method:设置bean销毁生命周期回调函数,仅适用于单例对象,此处填写destory方法名--></span><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>bean</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>bookDao<span class="token punctuation">"</span></span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>com.itheima.dao.impl.BookDaoImpl<span class="token punctuation">"</span></span> <span class="token attr-name">init-method</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>init<span class="token punctuation">"</span></span> <span class="token attr-name">destroy-method</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>destory<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre><ul><li>测试类</li></ul><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">AppForLifeCycle</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span> <span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">//此处需要使用实现类类型,接口类型没有close方法</span> <span class="token class-name">ClassPathXmlApplicationContext</span> ctx <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ClassPathXmlApplicationContext</span><span class="token punctuation">(</span><span class="token string">"applicationContext.xml"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">BookDao</span> bookDao <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token class-name">BookDao</span><span class="token punctuation">)</span> ctx<span class="token punctuation">.</span><span class="token function">getBean</span><span class="token punctuation">(</span><span class="token string">"bookDao"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> bookDao<span class="token punctuation">.</span><span class="token function">save</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//关闭容器,执行销毁的方法</span> ctx<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h4 id="2-2-Bean生命周期控制"><a href="#2-2-Bean生命周期控制" class="headerlink" title="2.2 Bean生命周期控制"></a>2.2 Bean生命周期控制</h4><ul><li>实现InitializingBean, DisposableBean接口</li></ul><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">BookServiceImpl</span> <span class="token keyword">implements</span> <span class="token class-name">BookService</span><span class="token punctuation">,</span> <span class="token class-name">InitializingBean</span><span class="token punctuation">,</span> <span class="token class-name">DisposableBean</span> <span class="token punctuation">{</span> <span class="token keyword">private</span> <span class="token class-name">BookDao</span> bookDao<span class="token punctuation">;</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setBookDao</span><span class="token punctuation">(</span><span class="token class-name">BookDao</span> bookDao<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"set ....."</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">this</span><span class="token punctuation">.</span>bookDao <span class="token operator">=</span> bookDao<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">save</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"book service save ..."</span><span class="token punctuation">)</span><span class="token punctuation">;</span> bookDao<span class="token punctuation">.</span><span class="token function">save</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">destroy</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Exception</span> <span class="token punctuation">{</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"service destroy"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">afterPropertiesSet</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Exception</span> <span class="token punctuation">{</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"service init"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><blockquote><p>测试类代码同《3.2.1 Bean生命周期控制》中的测试代码</p></blockquote><h3 id="3-Bean销毁时机"><a href="#3-Bean销毁时机" class="headerlink" title="3 Bean销毁时机"></a>3 Bean销毁时机</h3><ul><li>容器关闭前触发bean的销毁</li><li>关闭容器方式:<ul><li>手工关闭容器<br><code>ConfigurableApplicationContext</code>接口<code>close()</code>操作</li><li>注册关闭钩子,在虚拟机退出前先关闭容器再退出虚拟机<br><code>ConfigurableApplicationContext</code>接口<code>registerShutdownHook()</code>操作</li></ul></li></ul><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">AppForLifeCycle</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span> <span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">//此处需要使用实现类类型,接口类型没有close方法</span> <span class="token class-name">ClassPathXmlApplicationContext</span> ctx <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ClassPathXmlApplicationContext</span><span class="token punctuation">(</span><span class="token string">"applicationContext.xml"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">BookDao</span> bookDao <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token class-name">BookDao</span><span class="token punctuation">)</span> ctx<span class="token punctuation">.</span><span class="token function">getBean</span><span class="token punctuation">(</span><span class="token string">"bookDao"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> bookDao<span class="token punctuation">.</span><span class="token function">save</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//注册关闭钩子函数,在虚拟机退出之前回调此函数,关闭容器</span> ctx<span class="token punctuation">.</span><span class="token function">registerShutdownHook</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//关闭容器</span> <span class="token comment">//ctx.close();</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h2 id="六、依赖注入-DI配置"><a href="#六、依赖注入-DI配置" class="headerlink" title="六、依赖注入(DI配置)"></a>六、依赖注入(DI配置)</h2><h3 id="1-依赖注入方式【重点】"><a href="#1-依赖注入方式【重点】" class="headerlink" title="1 依赖注入方式【重点】"></a>1 依赖注入方式【重点】</h3><h4 id="问题导入-9"><a href="#问题导入-9" class="headerlink" title="问题导入"></a>问题导入</h4><p>依赖注入有几种方式?</p><h4 id="1-1-依赖注入的两种方式"><a href="#1-1-依赖注入的两种方式" class="headerlink" title="1.1 依赖注入的两种方式"></a>1.1 依赖注入的两种方式</h4><ul><li>setter注入<br>简单类型<br>==<strong>引用类型(很常用)</strong>==</li><li>构造器注入<br>简单类型<br>引用类型</li></ul><h4 id="1-2-setter方式注入"><a href="#1-2-setter方式注入" class="headerlink" title="1.2 setter方式注入"></a>1.2 setter方式注入</h4><h5 id="问题导入-10"><a href="#问题导入-10" class="headerlink" title="问题导入"></a>问题导入</h5><p>setter方式注入使用什么子标签?</p><h5 id="引用类型"><a href="#引用类型" class="headerlink" title="引用类型"></a>引用类型</h5><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210729203626540.png" alt="image-20210729203626540"></p><h5 id="简单类型"><a href="#简单类型" class="headerlink" title="简单类型"></a>简单类型</h5><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210729203728173.png" alt="image-20210729203728173"></p><h4 id="1-3-构造方式注入"><a href="#1-3-构造方式注入" class="headerlink" title="1.3 构造方式注入"></a>1.3 构造方式注入</h4><h5 id="问题导入-11"><a href="#问题导入-11" class="headerlink" title="问题导入"></a>问题导入</h5><p>构造方式注入使用什么子标签?</p><h5 id="引用类型-1"><a href="#引用类型-1" class="headerlink" title="引用类型"></a>引用类型</h5><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210729203859855.png" alt="image-20210729203859855"></p><h5 id="简单类型-1"><a href="#简单类型-1" class="headerlink" title="简单类型"></a>简单类型</h5><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210729204006542.png" alt="image-20210729204006542"></p><h5 id="参数适配【了解】"><a href="#参数适配【了解】" class="headerlink" title="参数适配【了解】"></a>参数适配【了解】</h5><p><img src="https://xingqiu-tuchuang-1256524210.cos.ap-shanghai.myqcloud.com/7856/WLHimage-20210729204117697.png" alt="image-20210729204117697"></p><h4 id="1-4-依赖注入方式选择"><a href="#1-4-依赖注入方式选择" class="headerlink" title="1.4 依赖注入方式选择"></a>1.4 依赖注入方式选择</h4><ol><li>强制依赖使用构造器进行,使用setter注入有概率不进行注入导致null对象出现</li><li>可选依赖使用setter注入进行,灵活性强</li><li>Spring框架倡导使用构造器,第三方框架内部大多数采用构造器注入的形式进行数据初始化,相对严谨</li><li>如果有必要可以两者同时使用,使用构造器注入完成强制依赖的注入,使用setter注入完成可选依赖的注入</li><li>实际开发过程中还要根据实际情况分析,如果受控对象没有提供setter方法就必须使用构造器注入</li><li><strong>==自己开发的模块推荐使用setter注入==</strong></li></ol><h3 id="2-依赖自动装配【理解】"><a href="#2-依赖自动装配【理解】" class="headerlink" title="2 依赖自动装配【理解】"></a>2 依赖自动装配【理解】</h3><h4 id="问题导入-12"><a href="#问题导入-12" class="headerlink" title="问题导入"></a>问题导入</h4><p>如何配置按照类型自动装配?</p><h4 id="2-1-自动装配概念"><a href="#2-1-自动装配概念" class="headerlink" title="2.1 自动装配概念"></a>2.1 自动装配概念</h4><ul><li>IoC容器根据bean所依赖的资源在容器中自动查找并注入到bean中的过程称为自动装配</li><li>自动装配方式<br>==按类型(常用)==<br>按名称<br>按构造方法<br>不启用自动装配</li></ul><h4 id="2-2-自动装配类型"><a href="#2-2-自动装配类型" class="headerlink" title="2.2 自动装配类型"></a>2.2 自动装配类型</h4><h5 id="依赖自动装配"><a href="#依赖自动装配" class="headerlink" title="依赖自动装配"></a>依赖自动装配</h5><blockquote><p>配置中使用bean标签autowire属性设置自动装配的类型</p></blockquote><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>bean</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>bookDao<span class="token punctuation">"</span></span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>com.itheima.dao.impl.BookDaoImpl<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>bean</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>bookService<span class="token punctuation">"</span></span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>com.itheima.service.impl.BookServiceImpl<span class="token punctuation">"</span></span> <span class="token attr-name">autowire</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>byType<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre><h5 id="依赖自动装配特征"><a href="#依赖自动装配特征" class="headerlink" title="依赖自动装配特征"></a>依赖自动装配特征</h5><ol><li>自动装配用于引用类型依赖注入,不能对简单类型进行操作</li><li>使用按类型装配时(byType)必须保障容器中相同类型的bean唯一,推荐使用</li><li>使用按名称装配时(byName)必须保障容器中具有指定名称的bean,因变量名与配置耦合,不推荐使用</li><li>自动装配优先级低于setter注入与构造器注入,同时出现时自动装配配置失效</li></ol><h3 id="3-集合注入"><a href="#3-集合注入" class="headerlink" title="3 集合注入"></a>3 集合注入</h3><h4 id="3-1-注入数组类型数据"><a href="#3-1-注入数组类型数据" class="headerlink" title="3.1 注入数组类型数据"></a>3.1 注入数组类型数据</h4><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>property</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>array<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>array</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>value</span><span class="token punctuation">></span></span>100<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>value</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>value</span><span class="token punctuation">></span></span>200<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>value</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>value</span><span class="token punctuation">></span></span>300<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>value</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>array</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>property</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h4 id="3-2-注入List类型数据"><a href="#3-2-注入List类型数据" class="headerlink" title="3.2 注入List类型数据"></a>3.2 注入List类型数据</h4><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>property</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>list<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>list</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>value</span><span class="token punctuation">></span></span>itcast<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>value</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>value</span><span class="token punctuation">></span></span>itheima<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>value</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>value</span><span class="token punctuation">></span></span>boxuegu<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>value</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>value</span><span class="token punctuation">></span></span>chuanzhihui<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>value</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>list</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>property</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h4 id="3-3-注入Set类型数据"><a href="#3-3-注入Set类型数据" class="headerlink" title="3.3 注入Set类型数据"></a>3.3 注入Set类型数据</h4><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>property</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>set<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>set</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>value</span><span class="token punctuation">></span></span>itcast<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>value</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>value</span><span class="token punctuation">></span></span>itheima<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>value</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>value</span><span class="token punctuation">></span></span>boxuegu<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>value</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>value</span><span class="token punctuation">></span></span>boxuegu<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>value</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>set</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>property</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h4 id="3-4-注入Map类型数据"><a href="#3-4-注入Map类型数据" class="headerlink" title="3.4 注入Map类型数据"></a>3.4 注入Map类型数据</h4><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>property</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>map<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>map</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>entry</span> <span class="token attr-name">key</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>country<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>china<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>entry</span> <span class="token attr-name">key</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>province<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>henan<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>entry</span> <span class="token attr-name">key</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>city<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>kaifeng<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>map</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>property</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h4 id="3-5-注入Properties类型数据"><a href="#3-5-注入Properties类型数据" class="headerlink" title="3.5 注入Properties类型数据"></a>3.5 注入Properties类型数据</h4><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>property</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>properties<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>props</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>prop</span> <span class="token attr-name">key</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>country<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>china<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>prop</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>prop</span> <span class="token attr-name">key</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>province<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>henan<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>prop</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>prop</span> <span class="token attr-name">key</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>city<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>kaifeng<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>prop</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>props</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>property</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><blockquote><p>说明:property标签表示setter方式注入,构造方式注入constructor-arg标签内部也可以写<array>、<list>、<set>、<map>、<props>标签</p></blockquote>]]></content>
<summary type="html"><h2 id="学习目标"><a href="#学习目标" class="headerlink" title="学习目标"></a>学习目标</h2><ul>
<li><input disabled="" type="checkbox"> 能够说出Spring的体系结构</li></summary>
</entry>
</feed>