-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatom.xml
180 lines (91 loc) · 211 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
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>Jiangsh's Blog</title>
<subtitle>write code and life</subtitle>
<link href="http://example.com/atom.xml" rel="self"/>
<link href="http://example.com/"/>
<updated>2023-05-15T02:06:23.378Z</updated>
<id>http://example.com/</id>
<author>
<name>jiangsh</name>
</author>
<generator uri="https://hexo.io/">Hexo</generator>
<entry>
<title>如何实现商品二级分类</title>
<link href="http://example.com/2023/05/15/%E5%A6%82%E4%BD%95%E5%AE%9E%E7%8E%B0%E5%95%86%E5%93%81%E4%BA%8C%E7%BA%A7%E5%88%86%E7%B1%BB/"/>
<id>http://example.com/2023/05/15/%E5%A6%82%E4%BD%95%E5%AE%9E%E7%8E%B0%E5%95%86%E5%93%81%E4%BA%8C%E7%BA%A7%E5%88%86%E7%B1%BB/</id>
<published>2023-05-15T02:05:14.000Z</published>
<updated>2023-05-15T02:06:23.378Z</updated>
<content type="html"><![CDATA[<p>如何实现商品二级分类</p><p>首先必须需要id,parent_id字段,其他根据需求扩展。</p><p>1.新建Category实体类</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Category</span> {</span><br><span class="line"> <span class="keyword">private</span> Long id;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">private</span> Long parent;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">private</span> String name;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">private</span> <span class="type">int</span> sort;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">private</span> String photoUrl;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">private</span> <span class="type">int</span> state;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">private</span> LocalDateTime startTime;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">public</span> Long <span class="title function_">getId</span><span class="params">()</span> {</span><br><span class="line"> <span class="keyword">return</span> id;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setId</span><span class="params">(Long id)</span> {</span><br><span class="line"> <span class="built_in">this</span>.id = id;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">public</span> Long <span class="title function_">getParent</span><span class="params">()</span> {</span><br><span class="line"> <span class="keyword">return</span> parent;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setParent</span><span class="params">(Long parent)</span> {</span><br><span class="line"> <span class="built_in">this</span>.parent = parent;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">public</span> String <span class="title function_">getName</span><span class="params">()</span> {</span><br><span class="line"> <span class="keyword">return</span> name;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setName</span><span class="params">(String name)</span> {</span><br><span class="line"> <span class="built_in">this</span>.name = name;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">getSort</span><span class="params">()</span> {</span><br><span class="line"> <span class="keyword">return</span> sort;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setSort</span><span class="params">(<span class="type">int</span> sort)</span> {</span><br><span class="line"> <span class="built_in">this</span>.sort = sort;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">public</span> String <span class="title function_">getPhotoUrl</span><span class="params">()</span> {</span><br><span class="line"> <span class="keyword">return</span> photoUrl;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setPhotoUrl</span><span class="params">(String photoUrl)</span> {</span><br><span class="line"> <span class="built_in">this</span>.photoUrl = photoUrl;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">getState</span><span class="params">()</span> {</span><br><span class="line"> <span class="keyword">return</span> state;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setState</span><span class="params">(<span class="type">int</span> state)</span> {</span><br><span class="line"> <span class="built_in">this</span>.state = state;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">public</span> LocalDateTime <span class="title function_">getStartTime</span><span class="params">()</span> {</span><br><span class="line"> <span class="keyword">return</span> startTime;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setStartTime</span><span class="params">(LocalDateTime startTime)</span> {</span><br><span class="line"> <span class="built_in">this</span>.startTime = startTime;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>2.创建数据库表sp_category</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">#分类</span><br><span class="line"><span class="keyword">drop</span> <span class="keyword">table</span> if <span class="keyword">exists</span> `sp_category` ;</span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> `sp_category` (</span><br><span class="line"> `id` <span class="type">bigint</span> <span class="keyword">not</span> <span class="keyword">null</span> comment <span class="string">'id'</span>,</span><br><span class="line"> `parent` <span class="type">bigint</span> <span class="keyword">not</span> <span class="keyword">null</span> <span class="keyword">default</span> <span class="number">0</span> comment <span class="string">'父id'</span>,</span><br><span class="line"> `name` <span class="type">varchar</span>(<span class="number">50</span>) <span class="keyword">not</span> <span class="keyword">null</span> comment <span class="string">'名称'</span>,</span><br><span class="line"> `sort` <span class="type">int</span> comment <span class="string">'分类顺序'</span>,</span><br><span class="line"> `photo_url` <span class="type">varchar</span>(<span class="number">50</span>) comment <span class="string">'分类图片'</span>,</span><br><span class="line"> `state` <span class="type">int</span> comment <span class="string">'状态'</span>,</span><br><span class="line"> `start_time` datetime comment <span class="string">'创建时间'</span>,</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">primary</span> key (`id`)</span><br><span class="line">) engine <span class="operator">=</span>innodb <span class="keyword">default</span> charset<span class="operator">=</span>utf8mb4 comment<span class="operator">=</span><span class="string">'分类'</span>;</span><br></pre></td></tr></table></figure><p>3.通过父id和子id使他们联系在一起,若商品分类为一级目录,没有父目录,则父id为0 ,若为二级目录,则父id指向一级目录的id</p><p>4.若要实现tree树形结构,可以选择前端处理或者后端处理,后端处理有多种方式,我这里选择使用的是使用hutool工具实现树形结构。</p><p>5.首先导入hutool工具依赖</p><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">dependency</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">groupId</span>></span>cn.hutool<span class="tag"></<span class="name">groupId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">artifactId</span>></span>hutool-all<span class="tag"></<span class="name">artifactId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">version</span>></span>5.8.15<span class="tag"></<span class="name">version</span>></span></span><br><span class="line"><span class="tag"></<span class="name">dependency</span>></span></span><br></pre></td></tr></table></figure><p>6.实现tree树形结构</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">List<CategoryDO> nodeList=categoryMapper.all();<span class="comment">//这里指从数据库获取的数据</span></span><br><span class="line">List<Tree<Long>> trees = treeUtil(nodeList);</span><br></pre></td></tr></table></figure><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span> List<Tree<Long>> <span class="title function_">treeUtil</span><span class="params">(List<CategoryDO> nodeList)</span>{</span><br><span class="line"> <span class="comment">//配置 这里根据自己需求设置</span></span><br><span class="line"> <span class="type">TreeNodeConfig</span> <span class="variable">treeNodeConfig</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">TreeNodeConfig</span>();</span><br><span class="line"> treeNodeConfig.setIdKey(<span class="string">"id"</span>);</span><br><span class="line"> treeNodeConfig.setParentIdKey(<span class="string">"parent"</span>);</span><br><span class="line"> treeNodeConfig.setNameKey(<span class="string">"name"</span>);</span><br><span class="line"> <span class="comment">//商品分类权重</span></span><br><span class="line"> treeNodeConfig.setWeightKey(<span class="string">"sort"</span>);</span><br><span class="line"> <span class="comment">//这里设置递归深度</span></span><br><span class="line"> treeNodeConfig.setDeep(<span class="number">2</span>);</span><br><span class="line"></span><br><span class="line"> <span class="comment">//转换器 注意这里的List<Tree<Long>>,Tree内中的泛型类型根据自己的id类型填写</span></span><br><span class="line"> List<Tree<Long>> treeNodes = TreeUtil.build(nodeList, <span class="number">0L</span>, treeNodeConfig,</span><br><span class="line"> (categoryDO, tree) -> {</span><br><span class="line"> <span class="comment">//Tree中不存在你类中的属性时候,你可以根据需求使用putExtra扩展</span></span><br><span class="line"> tree.putExtra(<span class="string">"id"</span>,categoryDO.getId());</span><br><span class="line"> tree.putExtra(<span class="string">"parent"</span>,categoryDO.getParent());</span><br><span class="line"> tree.putExtra(<span class="string">"name"</span>,categoryDO.getName());</span><br><span class="line"> tree.putExtra(<span class="string">"sort"</span>,categoryDO.getSort());</span><br><span class="line"> tree.putExtra(<span class="string">"photoUrl"</span>,categoryDO.getPhotoUrl());</span><br><span class="line"> tree.putExtra(<span class="string">"startTime"</span>,categoryDO.getStartTime());</span><br><span class="line"> tree.put(<span class="string">"state"</span>,categoryDO.getState());</span><br><span class="line"> });</span><br><span class="line"> <span class="keyword">return</span> treeNodes;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>7.实现效果</p><p><img src="https://img-blog.csdnimg.cn/ee2936a9195b4aa199ad4e733ad5e189.png" alt="img"><img src="/" alt="点击并拖拽以移动"></p>]]></content>
<summary type="html"><p>如何实现商品二级分类</p>
<p>首先必须需要id,parent_id字段,其他根据需求扩展。</p>
<p>1.新建Category实体类</p>
<figure class="highlight java"><table><tr><td class="gutter"></summary>
</entry>
<entry>
<title>spring cloud config 源码分析(二)</title>
<link href="http://example.com/2023/04/21/spring-cloud-config-%E6%BA%90%E7%A0%81%E5%88%86%E6%9E%90-%E4%BA%8C/"/>
<id>http://example.com/2023/04/21/spring-cloud-config-%E6%BA%90%E7%A0%81%E5%88%86%E6%9E%90-%E4%BA%8C/</id>
<published>2023-04-21T06:33:36.000Z</published>
<updated>2023-04-21T09:28:56.909Z</updated>
<content type="html"><![CDATA[<h1 id="config源码分析"><a href="#config源码分析" class="headerlink" title="config源码分析"></a>config源码分析</h1><meta name="referrer" content="no-referrer" /><h2 id="如果config-server从数据库中读取数据,config-server如何去获取数据。"><a href="#如果config-server从数据库中读取数据,config-server如何去获取数据。" class="headerlink" title="如果config server从数据库中读取数据,config server如何去获取数据。"></a>如果config server从数据库中读取数据,config server如何去获取数据。</h2><p>1.在org.springframework.cloud.config.server包中存在EnviormentController类中,也是通过rest风格去访问。</p><p><img src="https://img-blog.csdnimg.cn/35d00fd481704e65bb760127bd77a643.png" alt="img"></p><p>2.我们从游览器访问对应的url,然后再去debug</p><p><img src="https://img-blog.csdnimg.cn/a29479fca0b24ba581ff8fa1b2743774.png" alt="img"></p><p>3.在这个位置打个断点,进行debug</p><p><img src="https://img-blog.csdnimg.cn/9ec8217eff1c426d9be76158b6b81537.png" alt="img"></p><p>4.然后会从findOne方法去查找配置,根据对应的label,profiles,name</p><p><img src="https://img-blog.csdnimg.cn/5e1867be16da4c0da304a54913dd5c29.png" alt="img"></p><p> 5.进行findOne方法后,继续进行debug</p><p><img src="https://img-blog.csdnimg.cn/7bfa7fbb75fb4fd894df773377ab71c3.png" alt="[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-LMjdQjNX-1682067675568)(C:\Users\20471\AppData\Roaming\Typora\typora-user-images\image-20230421145724150.png)]"></p><p>6.继续debug后,我们发现它会将相关信息 注入到Environment对象中。</p><p><img src="https://img-blog.csdnimg.cn/0fd65906b4f74357bb70eed50e0c3c3a.png" alt="在这里插入图片描述"></p><p>7.最重要的一步就是EnvironmentRepositories这个接口,这是一个接口,我们可以通过这个接口去实现config server如何去获取数据,在findOne方法中,this.environmentRepositories是一个集合,因为有多个类实现该接口,所以Spring在启动的时候会去获取实现该接口的所有类,那么它就是一个接口。</p><p><img src="https://img-blog.csdnimg.cn/259975eec2be40f2b4008651acf492fb.png" alt="在这里插入图片描述"></p><p>8.这个类在config server中十分重要,我们可以通过继承这个类去扩展config server通过git,数据库获取数据等其他方法</p><p><img src="https://img-blog.csdnimg.cn/d23384025b7448a8b1ae9d45f2d85028.png" alt="[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-zqEE83Iy-1682067675569)(C:\Users\20471\AppData\Roaming\Typora\typora-user-images\image-20230421150815669.png)]"><br>git和数据库便是通过这两个类去查找配置信息的</p><p><img src="https://img-blog.csdnimg.cn/a28d321e67834f65b3f0689cbe2f2828.png" alt="[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xgeRvWfL-1682067675569)(C:\Users\20471\AppData\Roaming\Typora\typora-user-images\image-20230421151714688.png)]"></p><p>9.然后再去判断Spring中实现EnvironmentRepositories的接口有多少个类,然后再去遍历</p><p><img src="https://img-blog.csdnimg.cn/6cdafb51f7ae4561a0945dccd1275b5a.png" alt="[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-lp6b6d10-1682067675569)(C:\Users\20471\AppData\Roaming\Typora\typora-user-images\image-20230421151958951.png)]"></p><p>如果是多个实现的话,它会去遍历查询然后将获取的配置全部加入到env中</p><p><img src="https://img-blog.csdnimg.cn/e41f01fd822f45bd8d390cb4e352372d.png" alt="[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9KpXY7FK-1682067675569)(C:\Users\20471\AppData\Roaming\Typora\typora-user-images\image-20230421152059324.png)]"></p><p>至于它如何去控制Bean加载到spring中仍需要研究</p><p>10.我们这里debug只查出this.environmentRepositories.size() == 1,那么我们进入if分支,他去获取第一个Bean然后调用方法</p><p><img src="https://img-blog.csdnimg.cn/439f69b9c3824c33a0d397abf2039370.png" alt="[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Fj7MgbZA-1682067675570)(C:\Users\20471\AppData\Roaming\Typora\typora-user-images\image-20230421153424694.png)]"></p><p>这里会新建一个NativeEnvironmentRepository对象,并且获取配置文件和相关环境信息</p><p>这里会获取一些元数据</p><p><img src="https://img-blog.csdnimg.cn/43a656fa67914920a1a636b2c8db7bed.png" alt="[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-DbyuIk9K-1682067675570)(C:\Users\20471\AppData\Roaming\Typora\typora-user-images\image-20230421153737281.png)]"></p><p>11.这里有个getLocaltions方法会去获取,当地文件的位置</p><p><img src="https://img-blog.csdnimg.cn/ab75202021604581a2af60989eceea57.png" alt="[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9AWZDhVj-1682067675570)(C:\Users\20471\AppData\Roaming\Typora\typora-user-images\image-20230421153917772.png)]"></p><p>它会获取你从数据库读取放在本地,然后文件的地址</p><p><img src="https://img-blog.csdnimg.cn/6eca1abc2666451fb8c3c14ff2bd2019.png" alt="[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ACE18Be4-1682067675570)(C:\Users\20471\AppData\Roaming\Typora\typora-user-images\image-20230421154228412.png)]"></p><p>12.接着debug,label为空所以,它进入if分支,将label进行赋值,这里是自己实现的,具体分析根据自己debug所得。</p><p><img src="https://img-blog.csdnimg.cn/4cf5cf66153048d3841ea258beaa1233.png" alt="[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-OLvBJQjb-1682067675571)(C:\Users\20471\AppData\Roaming\Typora\typora-user-images\image-20230421154318281.png)]"></p><p>我们这里自己实现了使config server分支默认为master</p><p><img src="https://img-blog.csdnimg.cn/9f2889f2b20343e9bb10687c6d6a0f43.png" alt="[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-NxuIPXXt-1682067675571)(C:\Users\20471\AppData\Roaming\Typora\typora-user-images\image-20230421154741042.png)]"></p><p>13.我们接着debug,看看部门大佬如何写的,下面我们来到refresh方法</p><p><img src="https://img-blog.csdnimg.cn/244cd84ff05042929434d0bd47ee0e50.png" alt="[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9vUtaHs2-1682067675571)(C:\Users\20471\AppData\Roaming\Typora\typora-user-images\image-20230421155151675.png)]"></p><p>14.我们进入refresh方法,我们来看看它是如何判断的</p><p><img src="https://img-blog.csdnimg.cn/720e38775aaf41a3bc26a7191286a493.png" alt="[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4YfRwTXN-1682067675571)(C:\Users\20471\AppData\Roaming\Typora\typora-user-images\image-20230421155316071.png)]"></p><p>这里配置文件可以配置refreshRate刷新频率,这里设置了refreshRate = 600,经过计算十分钟刷新一次</p><p><img src="https://img-blog.csdnimg.cn/8095f15903bf4fa4999d2f94bee5cdc4.png" alt="[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-fUKvc6z4-1682067675571)(C:\Users\20471\AppData\Roaming\Typora\typora-user-images\image-20230421155434857.png)]"></p><p><img src="https://img-blog.csdnimg.cn/4c4f391d43614141959f75a81946c167.png" alt="[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8Cwk7s46-1682067675572)(C:\Users\20471\AppData\Roaming\Typora\typora-user-images\image-20230421155515014.png)]"></p><p>15.然后我们在进行debug可以看见getWorkingDirectory方法,紧接着cleanDirectory方法会去清理你文件夹中是否含有其他文件,如果含有它就会清理,如何再将查询的数据加入到文件夹中。</p><p><img src="https://img-blog.csdnimg.cn/4c1aaea445d14611af739ff846f2145e.png" alt="[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0q2lpqfx-1682067675572)(C:\Users\20471\AppData\Roaming\Typora\typora-user-images\image-20230421161437897.png)]"></p><p>16.然后我们会去数据库中读取数据然后利用writeConfig方法将配置文件写入到文件夹中。</p><p><img src="https://img-blog.csdnimg.cn/b2d65080a8b6423c8c9e650e1a42a040.png" alt="[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-SofCrezM-1682067675573)(C:\Users\20471\AppData\Roaming\Typora\typora-user-images\image-20230421161837244.png)]"></p><p>17.它通过jdbcTemplate去获取数据,这里的getsql会去获取表中的所有数据,这里有个existLabels对象,它是一个set数据结构,它用来保存label数据并且将label唯一,然后最后去判断是否存在label标签,如果不存在会抛出一个异常。(用set是为了方便去检索是否存在)</p><p><img src="https://img-blog.csdnimg.cn/20e64309760d468584f9f58e03a31de5.png" alt="[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vVF5pEpc-1682067675573)(C:\Users\20471\AppData\Roaming\Typora\typora-user-images\image-20230421162552782.png)]"></p><p><img src="https://img-blog.csdnimg.cn/db8bd89f792b41cebbf7f98c378c95d8.png" alt="[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tazdbO0U-1682067675573)(C:\Users\20471\AppData\Roaming\Typora\typora-user-images\image-20230421162431754.png)]"></p><p><img src="https://img-blog.csdnimg.cn/2b8895dc1ef14c71a0f363fd61ba4d08.png" alt="[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4fY9EEPx-1682067675573)(C:\Users\20471\AppData\Roaming\Typora\typora-user-images\image-20230421163030483.png)]"></p><p>17.下面我们debug一下writeConfig方法,这个writeStringToFile会将数据创建放入之前的文件夹中。</p><p><img src="https://img-blog.csdnimg.cn/43b42f10ca5e4b05aed580a75ac98840.png" alt="[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-RXB6ZnYm-1682067675573)(C:\Users\20471\AppData\Roaming\Typora\typora-user-images\image-20230421163305190.png)]"></p><p><img src="https://img-blog.csdnimg.cn/435ba56a79ad44a19fbd9b9c17dd0c16.png" alt="[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-O9OfxZfT-1682067675574)(C:\Users\20471\AppData\Roaming\Typora\typora-user-images\image-20230421163426385.png)]"></p><p>18.最后更新一下刷新时间。</p><p><img src="https://img-blog.csdnimg.cn/6d1f55fdd4054915ba1f6b35fc6c7931.png" alt="[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-glOkSoVg-1682067675574)(C:\Users\20471\AppData\Roaming\Typora\typora-user-images\image-20230421163529594.png)]"></p><p>19,返回之后 我们达到refresh方法,检查label类型,返回一个location对象</p><p><img src="https://img-blog.csdnimg.cn/3f0d5c43711f41c7af77a229b04ece5c.png" alt="[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-sRrBFBKa-1682067675574)(C:\Users\20471\AppData\Roaming\Typora\typora-user-images\image-20230421163842662.png)]"></p><p>20,debug之后,来到下一步</p><p><img src="https://img-blog.csdnimg.cn/29844bdb10b84e7c9bb8d6859305cbd6.png" alt="[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0uMkjGlJ-1682067675574)(C:\Users\20471\AppData\Roaming\Typora\typora-user-images\image-20230421164439077.png)]"></p><p>我们可以看到findOne方法会去获取相关配置信息</p><p><img src="https://img-blog.csdnimg.cn/6a9048a5695f425296a519e724f6b2bf.png" alt="[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0wkhIUSd-1682067675574)(C:\Users\20471\AppData\Roaming\Typora\typora-user-images\image-20230421164526336.png)]"></p><p>21,我们对findOne方法进行debug会发现这个方法会将配置文件放入它里面(核心)<img src="https://img-blog.csdnimg.cn/61c3e8ffa2134b759adeebba7752dd10.png" alt="在这里插入图片描述"></p><p>22.我们来到clean方法,它会将刚刚获取的配置信息放入到PropertySource,然后再放入Environment对象中,最后将它返回</p><p><img src="https://img-blog.csdnimg.cn/6f1918bc9b13494e939f13897081e986.png" alt="[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Kbj3jNVP-1682067675575)(C:\Users\20471\AppData\Roaming\Typora\typora-user-images\image-20230421165100367.png)]"></p><p><img src="https://img-blog.csdnimg.cn/f7529f5a3e1b41e7b33f18daa3938b4a.png" alt="[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-uGlyBief-1682067675575)(C:\Users\20471\AppData\Roaming\Typora\typora-user-images\image-20230421165231536.png)]"></p><p>23回调之后我们来到,它将配置文件内容赋值给envRepo,最后依次set进去</p><p><img src="https://img-blog.csdnimg.cn/1a502b3249e84bbfa03ba7b2760b07b3.png" alt="[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-RHEXHEVh-1682067675575)(C:\Users\20471\AppData\Roaming\Typora\typora-user-images\image-20230421165400298.png)]"></p><p>24 再继续回调,我们会来到对环境配置进行解密</p><p><img src="https://img-blog.csdnimg.cn/cd9ec1cac48f4ad0adf80df5659cb149.png" alt="[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-P8vfekCG-1682067675575)(C:\Users\20471\AppData\Roaming\Typora\typora-user-images\image-20230421165656959.png)]"></p><p>这里会判断它是否设置了加解密,如果设置则进行解密操作</p><p><img src="https://img-blog.csdnimg.cn/c491a25752704e9bb58235c1c2077a0a.png" alt="[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-loRQ8JYT-1682067675575)(C:\Users\20471\AppData\Roaming\Typora\typora-user-images\image-20230421165736752.png)]"></p><p>25,再继续回调我们就会发现,它将配置文件返回给客户端</p><p><img src="https://img-blog.csdnimg.cn/c82cab5995df4a598fb357fd0f669a6e.png" alt="[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-p2pemU8m-1682067675575)(C:\Users\20471\AppData\Roaming\Typora\typora-user-images\image-20230421165931386.png)]"></p>]]></content>
<summary type="html"><h1 id="config源码分析"><a href="#config源码分析" class="headerlink" title="config源码分析"></a>config源码分析</h1><meta name="referrer" content="no-referre</summary>
<category term="spring cloud" scheme="http://example.com/tags/spring-cloud/"/>
<category term="config" scheme="http://example.com/tags/config/"/>
</entry>
<entry>
<title>config源码分析</title>
<link href="http://example.com/2023/04/21/spring-cloud-config-%E6%BA%90%E7%A0%81%E9%98%85%E8%AF%BB/"/>
<id>http://example.com/2023/04/21/spring-cloud-config-%E6%BA%90%E7%A0%81%E9%98%85%E8%AF%BB/</id>
<published>2023-04-21T04:18:20.000Z</published>
<updated>2023-04-21T06:21:03.277Z</updated>
<content type="html"><![CDATA[<h1 id="config源码分析"><a href="#config源码分析" class="headerlink" title="config源码分析"></a>config源码分析</h1><meta name="referrer" content="no-referrer" /><h2 id="config-client如何从config-server获取配置文件"><a href="#config-client如何从config-server获取配置文件" class="headerlink" title="config-client如何从config-server获取配置文件"></a>config-client如何从config-server获取配置文件</h2><p>在spring-cloud-config-client包中存在ConfigServicePropertySourceLocator类,在spring启动加载的时候首先会从该类通过RestTemplate远程调用接口获取信息。</p><p>1.首先在ConfigServicePropertySourceLocator类的locate方法打个断点。</p><p><img src="https://img-blog.csdnimg.cn/75242d99a5884114b12bc55e60cb6462.png" alt="在这里插入图片描述"></p><p>2.在进入defaultProperties.override方法中获取配置文件</p><p><img src="https://img-blog.csdnimg.cn/5b02919e5eff40f798218dca55e97a2d.png" alt="在这里插入图片描述"></p><p>3.ConfigClientProperties,该类会去获取config-client中配置文件信息,该类是一个获取配置文件的类</p><p><img src="https://img-blog.csdnimg.cn/8b5d022804e847889a6aed9099370e31.png" alt="在这里插入图片描述"></p><p>获取的配置文件如下以及注册中心的地址(该配置文件不在下展示)</p><figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">spring:</span></span><br><span class="line"> <span class="attr">cloud:</span></span><br><span class="line"> <span class="attr">config:</span></span><br><span class="line"> <span class="attr">username:</span> </span><br><span class="line"> <span class="attr">password:</span> </span><br><span class="line"> <span class="attr">discovery:</span></span><br><span class="line"> <span class="attr">enabled:</span> <span class="literal">true</span></span><br><span class="line"> <span class="attr">serviceId:</span> <span class="string">rise-config-server</span></span><br><span class="line"> <span class="attr">fail-fast:</span> <span class="literal">false</span></span><br></pre></td></tr></table></figure><p>4.BeanUtils.copyProperties会进行数据交换,将this数据赋给override对象</p><p><img src="https://img-blog.csdnimg.cn/fb566e9fb7944883818a541123c77566.png" alt="在这里插入图片描述"></p><p>5.后面会对override进行赋值,将它返回</p><p><img src="https://img-blog.csdnimg.cn/ad67d4bba5b94bc6b9fd9f805549aead.png" alt="在这里插入图片描述"></p><p>6.返回数据后,来到ConfigServicePropretySourceLocator类中的lcoate方法中的getRemoteEnvironment()方法,获取远程数据。</p><p><img src="https://img-blog.csdnimg.cn/d78b47533dfb430da7658e6ef436da59.png" alt="在这里插入图片描述"></p><p>7.这里会去创建一个RestTemplate去远程获取数据。<br><img src="https://img-blog.csdnimg.cn/fd58f6a4760341e9b999b7bd523d2372.png" alt="在这里插入图片描述"></p><p>8,获取url的拼接路径</p><p><img src="https://img-blog.csdnimg.cn/d7720e5065754092a10cd8ba26307e91.png" alt="在这里插入图片描述"></p><p><img src="https://img-blog.csdnimg.cn/981baedabb2b440cb92ef4c96b6602af.png" alt="在这里插入图片描述"></p><p>9.通过RestTemplate去获取数据,返回对象为 Environment类<br><img src="https://img-blog.csdnimg.cn/08be9a467fa44173af0b6144413213c1.png" alt="在这里插入图片描述"></p><p>uri+path的路径如下</p><p><img src="https://img-blog.csdnimg.cn/c4d6afdd83604c068c77700e53afc6f2.png" alt="在这里插入图片描述"></p><p>该路径就相当于从config-server中获取信息,config-server路径就是url+path路径</p><p>10.最后将数据返回,通过上面方法我们便能获取config-server中配置文件,然后将数据读取到该应用spring配置文件</p><p><img src="https://img-blog.csdnimg.cn/cb6095c80cd146c6abad655753b99c6c.png" alt="在这里插入图片描述"></p><p>所有断点位置</p><p><img src="https://img-blog.csdnimg.cn/1c23dd53541e42189eefefd1a827d7de.png" alt="在这里插入图片描述"></p>]]></content>
<summary type="html"><h1 id="config源码分析"><a href="#config源码分析" class="headerlink" title="config源码分析"></a>config源码分析</h1><meta name="referrer" content="no-referre</summary>
<category term="spring cloud" scheme="http://example.com/tags/spring-cloud/"/>
<category term="config" scheme="http://example.com/tags/config/"/>
</entry>
<entry>
<title>干掉mapper.xml!MyBatis新特性动态SQL真香!</title>
<link href="http://example.com/2023/03/18/%E5%B9%B2%E6%8E%89mapper-xml%EF%BC%81MyBatis%E6%96%B0%E7%89%B9%E6%80%A7%E5%8A%A8%E6%80%81SQL%E7%9C%9F%E9%A6%99%EF%BC%81/"/>
<id>http://example.com/2023/03/18/%E5%B9%B2%E6%8E%89mapper-xml%EF%BC%81MyBatis%E6%96%B0%E7%89%B9%E6%80%A7%E5%8A%A8%E6%80%81SQL%E7%9C%9F%E9%A6%99%EF%BC%81/</id>
<published>2023-03-18T05:58:13.000Z</published>
<updated>2023-04-21T02:22:52.682Z</updated>
<content type="html"><![CDATA[<h1 id="干掉mapper-xml!MyBatis新特性动态SQL真香!"><a href="#干掉mapper-xml!MyBatis新特性动态SQL真香!" class="headerlink" title="干掉mapper.xml!MyBatis新特性动态SQL真香!"></a>干掉mapper.xml!MyBatis新特性动态SQL真香!</h1><blockquote><p>当我们使用MyBatis的时候,需要在mapper.xml中书写大量的SQL语句。当我们使用MyBatis Generator(MBG)作为代码生成器时,也会生成大量的mapper.xml文件。其实从MBG 1.3.6版本以后,MyBatis官方已经推荐使用Dynamic SQL,使用这一新特性基本就不用写mapper.xml文件了,使用起来非常方便,推荐给大家!</p></blockquote><h2 id="Dynamic-SQL简介"><a href="#Dynamic-SQL简介" class="headerlink" title="Dynamic SQL简介"></a><strong>Dynamic SQL简介</strong></h2><p>在我们使用Spring的时候,有XML和Java两种配置方式。在使用SpringBoot时,已经推荐使用Java配置,基本不用xml配置了。使用Dynamic SQL就好比是使用Java的方式来操作MyBatis。Dynamic SQL是用于生成动态SQL语句的框架,提倡使用Java API的方式来实现SQL操作,支持复杂查询和多表查询。</p><p>Dynamic SQL具有如下特性:</p><ul><li>类型安全:可以确保参数类型和<a href="https://cloud.tencent.com/solution/database?from=10680">数据库</a>字段类型相匹配;</li><li>富有表现力:语句的构建方式可以清楚地传达其含义;</li><li>使用灵活:可以使用and,or和nested条件的任意组合来构建where子句;</li><li>扩展性强:可以同时为MyBatis3, Spring JDBC和纯JDBC框架生成SQL语句;</li><li>轻量级:只需添加一个小的依赖项,没有传递依赖。</li></ul><h2 id="开始使用"><a href="#开始使用" class="headerlink" title="开始使用"></a><strong>开始使用</strong></h2><blockquote><p>首先我们通过一个入门示例将Dynamic SQL用起来,该示例会包含基础的CRUD操作。对MBG使用不了解的朋友可以先看下之前的文章<a href="https://mp.weixin.qq.com/s?__biz=MzU1Nzg4NjgyMw==&mid=2247488369&idx=1&sn=fbba6578d287c4d0ed767531f38b24c2&scene=21#wechat_redirect"><strong>《解放双手!MyBatis官方代码生成工具给力!》</strong></a></p></blockquote><h3 id="集成Dynamic-SQL"><a href="#集成Dynamic-SQL" class="headerlink" title="集成Dynamic SQL"></a><strong>集成Dynamic SQL</strong></h3><ul><li>在<code>pom.xml</code>中添加如下依赖,对比之前使用MBG,仅仅多添加了MyBatis的动态SQL依赖;</li></ul><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><dependencies></span><br><span class="line"> <!--<span class="title class_">SpringBoot</span>整合<span class="title class_">MyBatis</span>--></span><br><span class="line"> <span class="language-xml"><span class="tag"><<span class="name">dependency</span>></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">groupId</span>></span>org.mybatis.spring.boot<span class="tag"></<span class="name">groupId</span>></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">artifactId</span>></span>mybatis-spring-boot-starter<span class="tag"></<span class="name">artifactId</span>></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">version</span>></span>2.1.3<span class="tag"></<span class="name">version</span>></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"></<span class="name">dependency</span>></span></span></span><br><span class="line"> <!--<span class="title class_">MyBatis</span>分页插件--></span><br><span class="line"> <span class="language-xml"><span class="tag"><<span class="name">dependency</span>></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">groupId</span>></span>com.github.pagehelper<span class="tag"></<span class="name">groupId</span>></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">artifactId</span>></span>pagehelper-spring-boot-starter<span class="tag"></<span class="name">artifactId</span>></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">version</span>></span>1.3.0<span class="tag"></<span class="name">version</span>></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"></<span class="name">dependency</span>></span></span></span><br><span class="line"> <!--集成druid连接池--></span><br><span class="line"> <span class="language-xml"><span class="tag"><<span class="name">dependency</span>></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">groupId</span>></span>com.alibaba<span class="tag"></<span class="name">groupId</span>></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">artifactId</span>></span>druid-spring-boot-starter<span class="tag"></<span class="name">artifactId</span>></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">version</span>></span>1.1.10<span class="tag"></<span class="name">version</span>></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"></<span class="name">dependency</span>></span></span></span><br><span class="line"> <!-- <span class="title class_">MyBatis</span> 生成器 --></span><br><span class="line"> <span class="language-xml"><span class="tag"><<span class="name">dependency</span>></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">groupId</span>></span>org.mybatis.generator<span class="tag"></<span class="name">groupId</span>></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">artifactId</span>></span>mybatis-generator-core<span class="tag"></<span class="name">artifactId</span>></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">version</span>></span>1.4.0<span class="tag"></<span class="name">version</span>></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"></<span class="name">dependency</span>></span></span></span><br><span class="line"> <!-- <span class="title class_">MyBatis</span> 动态<span class="variable constant_">SQL</span>支持 --></span><br><span class="line"> <span class="language-xml"><span class="tag"><<span class="name">dependency</span>></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">groupId</span>></span>org.mybatis.dynamic-sql<span class="tag"></<span class="name">groupId</span>></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">artifactId</span>></span>mybatis-dynamic-sql<span class="tag"></<span class="name">artifactId</span>></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">version</span>></span>1.2.1<span class="tag"></<span class="name">version</span>></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"></<span class="name">dependency</span>></span></span></span><br><span class="line"> <!--<span class="title class_">Mysql</span>数据库驱动--></span><br><span class="line"> <span class="language-xml"><span class="tag"><<span class="name">dependency</span>></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">groupId</span>></span>mysql<span class="tag"></<span class="name">groupId</span>></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">artifactId</span>></span>mysql-connector-java<span class="tag"></<span class="name">artifactId</span>></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">version</span>></span>8.0.15<span class="tag"></<span class="name">version</span>></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"></<span class="name">dependency</span>></span></span></span><br><span class="line"></dependencies></span><br></pre></td></tr></table></figure><ul><li>在<code>application.yml</code>中对数据源和MyBatis的<code>mapper.xml</code>文件路径进行配置,只需配置自定义mapper.xml路径即可;</li></ul><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">spring</span>:</span><br><span class="line"> <span class="attr">datasource</span>:</span><br><span class="line"> <span class="attr">url</span>: <span class="attr">jdbc</span>:<span class="attr">mysql</span>:<span class="comment">//localhost:3306/mall?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai</span></span><br><span class="line"> <span class="attr">username</span>: root</span><br><span class="line"> <span class="attr">password</span>: root</span><br><span class="line"></span><br><span class="line"><span class="attr">mybatis</span>:</span><br><span class="line"> mapper-<span class="attr">locations</span>:</span><br><span class="line"> - <span class="attr">classpath</span>:dao<span class="comment">/*.xml</span></span><br></pre></td></tr></table></figure><ul><li>添加Java配置,用于扫描Mapper接口路径,MBG生成的放在<code>mapper</code>包下,自定义的放在<code>dao</code>包下。</li></ul><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * MyBatis配置类</span></span><br><span class="line"><span class="comment"> * Created by macro on 2019/4/8.</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">@<span class="title class_">Configuration</span></span><br><span class="line">@<span class="title class_">MapperScan</span>({<span class="string">"com.macro.mall.tiny.mbg.mapper"</span>,<span class="string">"com.macro.mall.tiny.dao"</span>})</span><br><span class="line">public <span class="keyword">class</span> <span class="title class_">MyBatisConfig</span> {</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h3 id="使用代码生成器"><a href="#使用代码生成器" class="headerlink" title="使用代码生成器"></a><strong>使用代码生成器</strong></h3><ul><li>在使用MBG生成代码前,我们还需要对其进行一些配置,首先在<code>generator.properties</code>文件中配置好数据库连接信息;</li></ul><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">jdbc.<span class="property">driverClass</span>=com.<span class="property">mysql</span>.<span class="property">cj</span>.<span class="property">jdbc</span>.<span class="property">Driver</span></span><br><span class="line">jdbc.<span class="property">connectionURL</span>=<span class="attr">jdbc</span>:<span class="attr">mysql</span>:<span class="comment">//localhost:3306/mall?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai</span></span><br><span class="line">jdbc.<span class="property">userId</span>=root</span><br><span class="line">jdbc.<span class="property">password</span>=root</span><br></pre></td></tr></table></figure><ul><li>然后在<code>generatorConfig.xml</code>文件中对MBG进行配置,配置属性说明直接参考注释即可;</li></ul><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"><?xml version=<span class="string">"1.0"</span> encoding=<span class="string">"UTF-8"</span>?></span><br><span class="line"><!<span class="variable constant_">DOCTYPE</span> generatorConfiguration</span><br><span class="line"> <span class="variable constant_">PUBLIC</span> <span class="string">"-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"</span></span><br><span class="line"> <span class="string">"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"</span>></span><br><span class="line"></span><br><span class="line"><span class="language-xml"><span class="tag"><<span class="name">generatorConfiguration</span>></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">properties</span> <span class="attr">resource</span>=<span class="string">"generator.properties"</span>/></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">context</span> <span class="attr">id</span>=<span class="string">"MySqlContext"</span> <span class="attr">targetRuntime</span>=<span class="string">"MyBatis3DynamicSQL"</span>></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"beginningDelimiter"</span> <span class="attr">value</span>=<span class="string">"`"</span>/></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"endingDelimiter"</span> <span class="attr">value</span>=<span class="string">"`"</span>/></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"javaFileEncoding"</span> <span class="attr">value</span>=<span class="string">"UTF-8"</span>/></span></span></span><br><span class="line"><span class="language-xml"> <span class="comment"><!-- 为模型生成序列化方法--></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">plugin</span> <span class="attr">type</span>=<span class="string">"org.mybatis.generator.plugins.SerializablePlugin"</span>/></span></span></span><br><span class="line"><span class="language-xml"> <span class="comment"><!-- 为生成的Java模型创建一个toString方法 --></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">plugin</span> <span class="attr">type</span>=<span class="string">"org.mybatis.generator.plugins.ToStringPlugin"</span>/></span></span></span><br><span class="line"><span class="language-xml"> <span class="comment"><!--可以自定义生成model的代码注释--></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">commentGenerator</span> <span class="attr">type</span>=<span class="string">"com.macro.mall.tiny.mbg.CommentGenerator"</span>></span></span></span><br><span class="line"><span class="language-xml"> <span class="comment"><!-- 是否去除自动生成的注释 true:是 :false:否 --></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"suppressAllComments"</span> <span class="attr">value</span>=<span class="string">"true"</span>/></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"suppressDate"</span> <span class="attr">value</span>=<span class="string">"true"</span>/></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"addRemarkComments"</span> <span class="attr">value</span>=<span class="string">"true"</span>/></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"></<span class="name">commentGenerator</span>></span></span></span><br><span class="line"><span class="language-xml"> <span class="comment"><!--配置数据库连接--></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">jdbcConnection</span> <span class="attr">driverClass</span>=<span class="string">"${jdbc.driverClass}"</span></span></span></span><br><span class="line"><span class="tag"><span class="language-xml"> <span class="attr">connectionURL</span>=<span class="string">"${jdbc.connectionURL}"</span></span></span></span><br><span class="line"><span class="tag"><span class="language-xml"> <span class="attr">userId</span>=<span class="string">"${jdbc.userId}"</span></span></span></span><br><span class="line"><span class="tag"><span class="language-xml"> <span class="attr">password</span>=<span class="string">"${jdbc.password}"</span>></span></span></span><br><span class="line"><span class="language-xml"> <span class="comment"><!--解决mysql驱动升级到8.0后不生成指定数据库代码的问题--></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"nullCatalogMeansCurrent"</span> <span class="attr">value</span>=<span class="string">"true"</span> /></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"></<span class="name">jdbcConnection</span>></span></span></span><br><span class="line"><span class="language-xml"> <span class="comment"><!--指定生成model的路径--></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">javaModelGenerator</span> <span class="attr">targetPackage</span>=<span class="string">"com.macro.mall.tiny.mbg.model"</span> <span class="attr">targetProject</span>=<span class="string">"mall-tiny-dynamic-sql\src\main\java"</span>/></span></span></span><br><span class="line"><span class="language-xml"> <span class="comment"><!--指定生成mapper接口的的路径--></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">javaClientGenerator</span> <span class="attr">type</span>=<span class="string">"XMLMAPPER"</span> <span class="attr">targetPackage</span>=<span class="string">"com.macro.mall.tiny.mbg.mapper"</span></span></span></span><br><span class="line"><span class="tag"><span class="language-xml"> <span class="attr">targetProject</span>=<span class="string">"mall-tiny-dynamic-sql\src\main\java"</span>/></span></span></span><br><span class="line"><span class="language-xml"> <span class="comment"><!--生成全部表tableName设为%--></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">table</span> <span class="attr">tableName</span>=<span class="string">"ums_admin"</span>></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">generatedKey</span> <span class="attr">column</span>=<span class="string">"id"</span> <span class="attr">sqlStatement</span>=<span class="string">"MySql"</span> <span class="attr">identity</span>=<span class="string">"true"</span>/></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"></<span class="name">table</span>></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">table</span> <span class="attr">tableName</span>=<span class="string">"ums_role"</span>></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">generatedKey</span> <span class="attr">column</span>=<span class="string">"id"</span> <span class="attr">sqlStatement</span>=<span class="string">"MySql"</span> <span class="attr">identity</span>=<span class="string">"true"</span>/></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"></<span class="name">table</span>></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">table</span> <span class="attr">tableName</span>=<span class="string">"ums_admin_role_relation"</span>></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"><<span class="name">generatedKey</span> <span class="attr">column</span>=<span class="string">"id"</span> <span class="attr">sqlStatement</span>=<span class="string">"MySql"</span> <span class="attr">identity</span>=<span class="string">"true"</span>/></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"></<span class="name">table</span>></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"></<span class="name">context</span>></span></span></span><br><span class="line"><span class="language-xml"><span class="tag"></<span class="name">generatorConfiguration</span>></span></span></span><br></pre></td></tr></table></figure><ul><li>与之前使用MBG有所不同,<code>targetRuntime</code>需要改为<code>MyBatis3DynamicSql</code>,用于配置生成mapper.xml路径的<code>sqlMapGenerator</code>标签也不需要配置了;</li><li>之前使用MBG时自定义了实体类注解的生成,写了个类CommentGenerator继承DefaultCommentGenerator,在<code>addFieldComment</code>方法中将Swagger注解写入到了实体类的属性上;</li></ul><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 自定义注释生成器</span></span><br><span class="line"><span class="comment"> * Created by macro on 2018/4/26.</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">public <span class="keyword">class</span> <span class="title class_">CommentGenerator</span> <span class="keyword">extends</span> <span class="title class_ inherited__">DefaultCommentGenerator</span> {</span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 给字段添加注释</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> @<span class="title class_">Override</span></span><br><span class="line"> public <span class="keyword">void</span> <span class="title function_">addFieldComment</span>(<span class="params">Field field, IntrospectedTable introspectedTable,</span></span><br><span class="line"><span class="params"> IntrospectedColumn introspectedColumn</span>) {</span><br><span class="line"> <span class="title class_">String</span> remarks = introspectedColumn.<span class="title function_">getRemarks</span>();</span><br><span class="line"> <span class="comment">//根据参数和备注信息判断是否添加备注信息</span></span><br><span class="line"> <span class="keyword">if</span>(addRemarkComments&&<span class="title class_">StringUtility</span>.<span class="title function_">stringHasValue</span>(remarks)){</span><br><span class="line"> <span class="comment">//数据库中特殊字符需要转义</span></span><br><span class="line"> <span class="keyword">if</span>(remarks.<span class="title function_">contains</span>(<span class="string">"\""</span>)){</span><br><span class="line"> remarks = remarks.<span class="title function_">replace</span>(<span class="string">"\""</span>,<span class="string">"'"</span>);</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//给model的字段添加swagger注解</span></span><br><span class="line"> field.<span class="title function_">addJavaDocLine</span>(<span class="string">"@ApiModelProperty(value = \""</span>+remarks+<span class="string">"\")"</span>);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><ul><li>在使用Dynamic SQL的时候,这种方法已经无用,需要在<code>addFieldAnnotation</code>中将Swagger注解写入到了实体类的属性上;</li></ul><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 自定义注释生成器</span></span><br><span class="line"><span class="comment"> * Created by macro on 2018/4/26.</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">public <span class="keyword">class</span> <span class="title class_">CommentGenerator</span> <span class="keyword">extends</span> <span class="title class_ inherited__">DefaultCommentGenerator</span> {</span><br><span class="line"></span><br><span class="line"> @<span class="title class_">Override</span></span><br><span class="line"> public <span class="keyword">void</span> <span class="title function_">addFieldAnnotation</span>(<span class="params">Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn, <span class="built_in">Set</span><FullyQualifiedJavaType> imports</span>) {</span><br><span class="line"> <span class="keyword">if</span> (!addRemarkComments || <span class="title class_">CollUtil</span>.<span class="title function_">isEmpty</span>(imports)) <span class="keyword">return</span>;</span><br><span class="line"> long count = imports.<span class="title function_">stream</span>()</span><br><span class="line"> .<span class="title function_">filter</span>(item -> <span class="variable constant_">API_MODEL_PROPERTY_FULL_CLASS_NAME</span>.<span class="title function_">equals</span>(item.<span class="title function_">getFullyQualifiedName</span>()))</span><br><span class="line"> .<span class="title function_">count</span>();</span><br><span class="line"> <span class="keyword">if</span> (count <= 0L) {</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="title class_">String</span> remarks = introspectedColumn.<span class="title function_">getRemarks</span>();</span><br><span class="line"> <span class="comment">//根据参数和备注信息判断是否添加备注信息</span></span><br><span class="line"> <span class="keyword">if</span> (<span class="title class_">StringUtility</span>.<span class="title function_">stringHasValue</span>(remarks)) {</span><br><span class="line"> <span class="comment">//数据库中特殊字符需要转义</span></span><br><span class="line"> <span class="keyword">if</span> (remarks.<span class="title function_">contains</span>(<span class="string">"\""</span>)) {</span><br><span class="line"> remarks = remarks.<span class="title function_">replace</span>(<span class="string">"\""</span>, <span class="string">"'"</span>);</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//给model的字段添加swagger注解</span></span><br><span class="line"> field.<span class="title function_">addJavaDocLine</span>(<span class="string">"@ApiModelProperty(value = \""</span> + remarks + <span class="string">"\")"</span>);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><ul><li>一切准备就绪,执行Generator类的main方法,生成代码结构信息如下,可以发现已经不再生成mapper.xml文件和Example类,取而代之的是生成了DynamicSqlSupport类。</li></ul><p><img src="https://ask.qcloudimg.com/http-save/yehe-5866756/vevtjuik18.png?imageView2/2/w/2560/h/7000" alt="img"></p><h3 id="实现基本的CRUD操作"><a href="#实现基本的CRUD操作" class="headerlink" title="实现基本的CRUD操作"></a><strong>实现基本的CRUD操作</strong></h3><blockquote><p>这里使用的是<code>mall-tiny</code>项目中权限管理功能相关表,具体可以参考<a href="https://mp.weixin.qq.com/s?__biz=MzU1Nzg4NjgyMw==&mid=2247486629&idx=1&sn=42e41b7e55013c960889d9cc0e290187&scene=21#wechat_redirect"><strong>《还在从零开始搭建项目?手撸了款快速开发脚手架!》</strong></a>。</p></blockquote><ul><li>查看下MBG生成的Mapper接口,比之前使用MBG时增加了很多方法,并且有了一些默认的方法实现,可见之前在mapper.xml中的实现都已经转移到Mapper接口中去了,单表CRUD直接调用对应方法即可;</li></ul><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br></pre></td><td class="code"><pre><span class="line">@<span class="title class_">Mapper</span></span><br><span class="line">public interface <span class="title class_">UmsAdminMapper</span> {</span><br><span class="line"> @<span class="title class_">Generated</span>(<span class="string">"org.mybatis.generator.api.MyBatisGenerator"</span>)</span><br><span class="line"> <span class="title class_">BasicColumn</span>[] selectList = <span class="title class_">BasicColumn</span>.<span class="title function_">columnList</span>(id, username, password, icon, email, nickName, note, createTime, loginTime, status);</span><br><span class="line"></span><br><span class="line"> @<span class="title class_">Generated</span>(<span class="string">"org.mybatis.generator.api.MyBatisGenerator"</span>)</span><br><span class="line"> @<span class="title class_">SelectProvider</span>(type=<span class="title class_">SqlProviderAdapter</span>.<span class="property">class</span>, method=<span class="string">"select"</span>)</span><br><span class="line"> long <span class="title function_">count</span>(<span class="title class_">SelectStatementProvider</span> selectStatement);</span><br><span class="line"></span><br><span class="line"> @<span class="title class_">Generated</span>(<span class="string">"org.mybatis.generator.api.MyBatisGenerator"</span>)</span><br><span class="line"> @<span class="title class_">DeleteProvider</span>(type=<span class="title class_">SqlProviderAdapter</span>.<span class="property">class</span>, method=<span class="string">"delete"</span>)</span><br><span class="line"> int <span class="title function_">delete</span>(<span class="title class_">DeleteStatementProvider</span> deleteStatement);</span><br><span class="line"></span><br><span class="line"> @<span class="title class_">Generated</span>(<span class="string">"org.mybatis.generator.api.MyBatisGenerator"</span>)</span><br><span class="line"> @<span class="title class_">InsertProvider</span>(type=<span class="title class_">SqlProviderAdapter</span>.<span class="property">class</span>, method=<span class="string">"insert"</span>)</span><br><span class="line"> @<span class="title class_">SelectKey</span>(statement=<span class="string">"SELECT LAST_INSERT_ID()"</span>, keyProperty=<span class="string">"record.id"</span>, before=<span class="literal">false</span>, resultType=<span class="title class_">Long</span>.<span class="property">class</span>)</span><br><span class="line"> int <span class="title function_">insert</span>(<span class="title class_">InsertStatementProvider</span><<span class="title class_">UmsAdmin</span>> insertStatement);</span><br><span class="line"></span><br><span class="line"> @<span class="title class_">Generated</span>(<span class="string">"org.mybatis.generator.api.MyBatisGenerator"</span>)</span><br><span class="line"> @<span class="title class_">SelectProvider</span>(type=<span class="title class_">SqlProviderAdapter</span>.<span class="property">class</span>, method=<span class="string">"select"</span>)</span><br><span class="line"> @<span class="title class_">ResultMap</span>(<span class="string">"UmsAdminResult"</span>)</span><br><span class="line"> <span class="title class_">Optional</span><<span class="title class_">UmsAdmin</span>> <span class="title function_">selectOne</span>(<span class="title class_">SelectStatementProvider</span> selectStatement);</span><br><span class="line"></span><br><span class="line"> @<span class="title class_">Generated</span>(<span class="string">"org.mybatis.generator.api.MyBatisGenerator"</span>)</span><br><span class="line"> @<span class="title class_">SelectProvider</span>(type=<span class="title class_">SqlProviderAdapter</span>.<span class="property">class</span>, method=<span class="string">"select"</span>)</span><br><span class="line"> @<span class="title class_">Results</span>(id=<span class="string">"UmsAdminResult"</span>, value = {</span><br><span class="line"> @<span class="title class_">Result</span>(column=<span class="string">"id"</span>, property=<span class="string">"id"</span>, jdbcType=<span class="title class_">JdbcType</span>.<span class="property">BIGINT</span>, id=<span class="literal">true</span>),</span><br><span class="line"> @<span class="title class_">Result</span>(column=<span class="string">"username"</span>, property=<span class="string">"username"</span>, jdbcType=<span class="title class_">JdbcType</span>.<span class="property">VARCHAR</span>),</span><br><span class="line"> @<span class="title class_">Result</span>(column=<span class="string">"password"</span>, property=<span class="string">"password"</span>, jdbcType=<span class="title class_">JdbcType</span>.<span class="property">VARCHAR</span>),</span><br><span class="line"> @<span class="title class_">Result</span>(column=<span class="string">"icon"</span>, property=<span class="string">"icon"</span>, jdbcType=<span class="title class_">JdbcType</span>.<span class="property">VARCHAR</span>),</span><br><span class="line"> @<span class="title class_">Result</span>(column=<span class="string">"email"</span>, property=<span class="string">"email"</span>, jdbcType=<span class="title class_">JdbcType</span>.<span class="property">VARCHAR</span>),</span><br><span class="line"> @<span class="title class_">Result</span>(column=<span class="string">"nick_name"</span>, property=<span class="string">"nickName"</span>, jdbcType=<span class="title class_">JdbcType</span>.<span class="property">VARCHAR</span>),</span><br><span class="line"> @<span class="title class_">Result</span>(column=<span class="string">"note"</span>, property=<span class="string">"note"</span>, jdbcType=<span class="title class_">JdbcType</span>.<span class="property">VARCHAR</span>),</span><br><span class="line"> @<span class="title class_">Result</span>(column=<span class="string">"create_time"</span>, property=<span class="string">"createTime"</span>, jdbcType=<span class="title class_">JdbcType</span>.<span class="property">TIMESTAMP</span>),</span><br><span class="line"> @<span class="title class_">Result</span>(column=<span class="string">"login_time"</span>, property=<span class="string">"loginTime"</span>, jdbcType=<span class="title class_">JdbcType</span>.<span class="property">TIMESTAMP</span>),</span><br><span class="line"> @<span class="title class_">Result</span>(column=<span class="string">"status"</span>, property=<span class="string">"status"</span>, jdbcType=<span class="title class_">JdbcType</span>.<span class="property">INTEGER</span>)</span><br><span class="line"> })</span><br><span class="line"> <span class="title class_">List</span><<span class="title class_">UmsAdmin</span>> <span class="title function_">selectMany</span>(<span class="title class_">SelectStatementProvider</span> selectStatement);</span><br><span class="line"></span><br><span class="line"> @<span class="title class_">Generated</span>(<span class="string">"org.mybatis.generator.api.MyBatisGenerator"</span>)</span><br><span class="line"> @<span class="title class_">UpdateProvider</span>(type=<span class="title class_">SqlProviderAdapter</span>.<span class="property">class</span>, method=<span class="string">"update"</span>)</span><br><span class="line"> int <span class="title function_">update</span>(<span class="title class_">UpdateStatementProvider</span> updateStatement);</span><br><span class="line"></span><br><span class="line"> @<span class="title class_">Generated</span>(<span class="string">"org.mybatis.generator.api.MyBatisGenerator"</span>)</span><br><span class="line"> <span class="keyword">default</span> long <span class="title function_">count</span>(<span class="params">CountDSLCompleter completer</span>) {</span><br><span class="line"> <span class="keyword">return</span> <span class="title class_">MyBatis3Utils</span>.<span class="title function_">countFrom</span>(<span class="attr">this</span>::count, umsAdmin, completer);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> @<span class="title class_">Generated</span>(<span class="string">"org.mybatis.generator.api.MyBatisGenerator"</span>)</span><br><span class="line"> <span class="keyword">default</span> int <span class="title function_">delete</span>(<span class="params">DeleteDSLCompleter completer</span>) {</span><br><span class="line"> <span class="keyword">return</span> <span class="title class_">MyBatis3Utils</span>.<span class="title function_">deleteFrom</span>(<span class="attr">this</span>::<span class="keyword">delete</span>, umsAdmin, completer);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> @<span class="title class_">Generated</span>(<span class="string">"org.mybatis.generator.api.MyBatisGenerator"</span>)</span><br><span class="line"> <span class="keyword">default</span> int <span class="title function_">deleteByPrimaryKey</span>(<span class="params">Long id_</span>) {</span><br><span class="line"> <span class="keyword">return</span> <span class="title function_">delete</span>(c -> </span><br><span class="line"> c.<span class="title function_">where</span>(id, <span class="title function_">isEqualTo</span>(id_))</span><br><span class="line"> );</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> @<span class="title class_">Generated</span>(<span class="string">"org.mybatis.generator.api.MyBatisGenerator"</span>)</span><br><span class="line"> <span class="keyword">default</span> int <span class="title function_">insert</span>(<span class="params">UmsAdmin record</span>) {</span><br><span class="line"> <span class="keyword">return</span> <span class="title class_">MyBatis3Utils</span>.<span class="title function_">insert</span>(<span class="attr">this</span>::insert, record, umsAdmin, c -></span><br><span class="line"> c.<span class="title function_">map</span>(username).<span class="title function_">toProperty</span>(<span class="string">"username"</span>)</span><br><span class="line"> .<span class="title function_">map</span>(password).<span class="title function_">toProperty</span>(<span class="string">"password"</span>)</span><br><span class="line"> .<span class="title function_">map</span>(icon).<span class="title function_">toProperty</span>(<span class="string">"icon"</span>)</span><br><span class="line"> .<span class="title function_">map</span>(email).<span class="title function_">toProperty</span>(<span class="string">"email"</span>)</span><br><span class="line"> .<span class="title function_">map</span>(nickName).<span class="title function_">toProperty</span>(<span class="string">"nickName"</span>)</span><br><span class="line"> .<span class="title function_">map</span>(note).<span class="title function_">toProperty</span>(<span class="string">"note"</span>)</span><br><span class="line"> .<span class="title function_">map</span>(createTime).<span class="title function_">toProperty</span>(<span class="string">"createTime"</span>)</span><br><span class="line"> .<span class="title function_">map</span>(loginTime).<span class="title function_">toProperty</span>(<span class="string">"loginTime"</span>)</span><br><span class="line"> .<span class="title function_">map</span>(status).<span class="title function_">toProperty</span>(<span class="string">"status"</span>)</span><br><span class="line"> );</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> @<span class="title class_">Generated</span>(<span class="string">"org.mybatis.generator.api.MyBatisGenerator"</span>)</span><br><span class="line"> <span class="keyword">default</span> int <span class="title function_">insertSelective</span>(<span class="params">UmsAdmin record</span>) {</span><br><span class="line"> <span class="keyword">return</span> <span class="title class_">MyBatis3Utils</span>.<span class="title function_">insert</span>(<span class="attr">this</span>::insert, record, umsAdmin, c -></span><br><span class="line"> c.<span class="title function_">map</span>(username).<span class="title function_">toPropertyWhenPresent</span>(<span class="string">"username"</span>, <span class="attr">record</span>::getUsername)</span><br><span class="line"> .<span class="title function_">map</span>(password).<span class="title function_">toPropertyWhenPresent</span>(<span class="string">"password"</span>, <span class="attr">record</span>::getPassword)</span><br><span class="line"> .<span class="title function_">map</span>(icon).<span class="title function_">toPropertyWhenPresent</span>(<span class="string">"icon"</span>, <span class="attr">record</span>::getIcon)</span><br><span class="line"> .<span class="title function_">map</span>(email).<span class="title function_">toPropertyWhenPresent</span>(<span class="string">"email"</span>, <span class="attr">record</span>::getEmail)</span><br><span class="line"> .<span class="title function_">map</span>(nickName).<span class="title function_">toPropertyWhenPresent</span>(<span class="string">"nickName"</span>, <span class="attr">record</span>::getNickName)</span><br><span class="line"> .<span class="title function_">map</span>(note).<span class="title function_">toPropertyWhenPresent</span>(<span class="string">"note"</span>, <span class="attr">record</span>::getNote)</span><br><span class="line"> .<span class="title function_">map</span>(createTime).<span class="title function_">toPropertyWhenPresent</span>(<span class="string">"createTime"</span>, <span class="attr">record</span>::getCreateTime)</span><br><span class="line"> .<span class="title function_">map</span>(loginTime).<span class="title function_">toPropertyWhenPresent</span>(<span class="string">"loginTime"</span>, <span class="attr">record</span>::getLoginTime)</span><br><span class="line"> .<span class="title function_">map</span>(status).<span class="title function_">toPropertyWhenPresent</span>(<span class="string">"status"</span>, <span class="attr">record</span>::getStatus)</span><br><span class="line"> );</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> @<span class="title class_">Generated</span>(<span class="string">"org.mybatis.generator.api.MyBatisGenerator"</span>)</span><br><span class="line"> <span class="keyword">default</span> <span class="title class_">Optional</span><<span class="title class_">UmsAdmin</span>> <span class="title function_">selectOne</span>(<span class="params">SelectDSLCompleter completer</span>) {</span><br><span class="line"> <span class="keyword">return</span> <span class="title class_">MyBatis3Utils</span>.<span class="title function_">selectOne</span>(<span class="attr">this</span>::selectOne, selectList, umsAdmin, completer);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> @<span class="title class_">Generated</span>(<span class="string">"org.mybatis.generator.api.MyBatisGenerator"</span>)</span><br><span class="line"> <span class="keyword">default</span> <span class="title class_">List</span><<span class="title class_">UmsAdmin</span>> <span class="title function_">select</span>(<span class="params">SelectDSLCompleter completer</span>) {</span><br><span class="line"> <span class="keyword">return</span> <span class="title class_">MyBatis3Utils</span>.<span class="title function_">selectList</span>(<span class="attr">this</span>::selectMany, selectList, umsAdmin, completer);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> @<span class="title class_">Generated</span>(<span class="string">"org.mybatis.generator.api.MyBatisGenerator"</span>)</span><br><span class="line"> <span class="keyword">default</span> <span class="title class_">List</span><<span class="title class_">UmsAdmin</span>> <span class="title function_">selectDistinct</span>(<span class="params">SelectDSLCompleter completer</span>) {</span><br><span class="line"> <span class="keyword">return</span> <span class="title class_">MyBatis3Utils</span>.<span class="title function_">selectDistinct</span>(<span class="attr">this</span>::selectMany, selectList, umsAdmin, completer);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> @<span class="title class_">Generated</span>(<span class="string">"org.mybatis.generator.api.MyBatisGenerator"</span>)</span><br><span class="line"> <span class="keyword">default</span> <span class="title class_">Optional</span><<span class="title class_">UmsAdmin</span>> <span class="title function_">selectByPrimaryKey</span>(<span class="params">Long id_</span>) {</span><br><span class="line"> <span class="keyword">return</span> <span class="title function_">selectOne</span>(c -></span><br><span class="line"> c.<span class="title function_">where</span>(id, <span class="title function_">isEqualTo</span>(id_))</span><br><span class="line"> );</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> @<span class="title class_">Generated</span>(<span class="string">"org.mybatis.generator.api.MyBatisGenerator"</span>)</span><br><span class="line"> <span class="keyword">default</span> int <span class="title function_">update</span>(<span class="params">UpdateDSLCompleter completer</span>) {</span><br><span class="line"> <span class="keyword">return</span> <span class="title class_">MyBatis3Utils</span>.<span class="title function_">update</span>(<span class="attr">this</span>::update, umsAdmin, completer);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> @<span class="title class_">Generated</span>(<span class="string">"org.mybatis.generator.api.MyBatisGenerator"</span>)</span><br><span class="line"> <span class="keyword">static</span> <span class="title class_">UpdateDSL</span><<span class="title class_">UpdateModel</span>> <span class="title function_">updateAllColumns</span>(<span class="params">UmsAdmin record, UpdateDSL<UpdateModel> dsl</span>) {</span><br><span class="line"> <span class="keyword">return</span> dsl.<span class="title function_">set</span>(username).<span class="title function_">equalTo</span>(<span class="attr">record</span>::getUsername)</span><br><span class="line"> .<span class="title function_">set</span>(password).<span class="title function_">equalTo</span>(<span class="attr">record</span>::getPassword)</span><br><span class="line"> .<span class="title function_">set</span>(icon).<span class="title function_">equalTo</span>(<span class="attr">record</span>::getIcon)</span><br><span class="line"> .<span class="title function_">set</span>(email).<span class="title function_">equalTo</span>(<span class="attr">record</span>::getEmail)</span><br><span class="line"> .<span class="title function_">set</span>(nickName).<span class="title function_">equalTo</span>(<span class="attr">record</span>::getNickName)</span><br><span class="line"> .<span class="title function_">set</span>(note).<span class="title function_">equalTo</span>(<span class="attr">record</span>::getNote)</span><br><span class="line"> .<span class="title function_">set</span>(createTime).<span class="title function_">equalTo</span>(<span class="attr">record</span>::getCreateTime)</span><br><span class="line"> .<span class="title function_">set</span>(loginTime).<span class="title function_">equalTo</span>(<span class="attr">record</span>::getLoginTime)</span><br><span class="line"> .<span class="title function_">set</span>(status).<span class="title function_">equalTo</span>(<span class="attr">record</span>::getStatus);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> @<span class="title class_">Generated</span>(<span class="string">"org.mybatis.generator.api.MyBatisGenerator"</span>)</span><br><span class="line"> <span class="keyword">static</span> <span class="title class_">UpdateDSL</span><<span class="title class_">UpdateModel</span>> <span class="title function_">updateSelectiveColumns</span>(<span class="params">UmsAdmin record, UpdateDSL<UpdateModel> dsl</span>) {</span><br><span class="line"> <span class="keyword">return</span> dsl.<span class="title function_">set</span>(username).<span class="title function_">equalToWhenPresent</span>(<span class="attr">record</span>::getUsername)</span><br><span class="line"> .<span class="title function_">set</span>(password).<span class="title function_">equalToWhenPresent</span>(<span class="attr">record</span>::getPassword)</span><br><span class="line"> .<span class="title function_">set</span>(icon).<span class="title function_">equalToWhenPresent</span>(<span class="attr">record</span>::getIcon)</span><br><span class="line"> .<span class="title function_">set</span>(email).<span class="title function_">equalToWhenPresent</span>(<span class="attr">record</span>::getEmail)</span><br><span class="line"> .<span class="title function_">set</span>(nickName).<span class="title function_">equalToWhenPresent</span>(<span class="attr">record</span>::getNickName)</span><br><span class="line"> .<span class="title function_">set</span>(note).<span class="title function_">equalToWhenPresent</span>(<span class="attr">record</span>::getNote)</span><br><span class="line"> .<span class="title function_">set</span>(createTime).<span class="title function_">equalToWhenPresent</span>(<span class="attr">record</span>::getCreateTime)</span><br><span class="line"> .<span class="title function_">set</span>(loginTime).<span class="title function_">equalToWhenPresent</span>(<span class="attr">record</span>::getLoginTime)</span><br><span class="line"> .<span class="title function_">set</span>(status).<span class="title function_">equalToWhenPresent</span>(<span class="attr">record</span>::getStatus);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> @<span class="title class_">Generated</span>(<span class="string">"org.mybatis.generator.api.MyBatisGenerator"</span>)</span><br><span class="line"> <span class="keyword">default</span> int <span class="title function_">updateByPrimaryKey</span>(<span class="params">UmsAdmin record</span>) {</span><br><span class="line"> <span class="keyword">return</span> <span class="title function_">update</span>(c -></span><br><span class="line"> c.<span class="title function_">set</span>(username).<span class="title function_">equalTo</span>(<span class="attr">record</span>::getUsername)</span><br><span class="line"> .<span class="title function_">set</span>(password).<span class="title function_">equalTo</span>(<span class="attr">record</span>::getPassword)</span><br><span class="line"> .<span class="title function_">set</span>(icon).<span class="title function_">equalTo</span>(<span class="attr">record</span>::getIcon)</span><br><span class="line"> .<span class="title function_">set</span>(email).<span class="title function_">equalTo</span>(<span class="attr">record</span>::getEmail)</span><br><span class="line"> .<span class="title function_">set</span>(nickName).<span class="title function_">equalTo</span>(<span class="attr">record</span>::getNickName)</span><br><span class="line"> .<span class="title function_">set</span>(note).<span class="title function_">equalTo</span>(<span class="attr">record</span>::getNote)</span><br><span class="line"> .<span class="title function_">set</span>(createTime).<span class="title function_">equalTo</span>(<span class="attr">record</span>::getCreateTime)</span><br><span class="line"> .<span class="title function_">set</span>(loginTime).<span class="title function_">equalTo</span>(<span class="attr">record</span>::getLoginTime)</span><br><span class="line"> .<span class="title function_">set</span>(status).<span class="title function_">equalTo</span>(<span class="attr">record</span>::getStatus)</span><br><span class="line"> .<span class="title function_">where</span>(id, <span class="title function_">isEqualTo</span>(<span class="attr">record</span>::getId))</span><br><span class="line"> );</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> @<span class="title class_">Generated</span>(<span class="string">"org.mybatis.generator.api.MyBatisGenerator"</span>)</span><br><span class="line"> <span class="keyword">default</span> int <span class="title function_">updateByPrimaryKeySelective</span>(<span class="params">UmsAdmin record</span>) {</span><br><span class="line"> <span class="keyword">return</span> <span class="title function_">update</span>(c -></span><br><span class="line"> c.<span class="title function_">set</span>(username).<span class="title function_">equalToWhenPresent</span>(<span class="attr">record</span>::getUsername)</span><br><span class="line"> .<span class="title function_">set</span>(password).<span class="title function_">equalToWhenPresent</span>(<span class="attr">record</span>::getPassword)</span><br><span class="line"> .<span class="title function_">set</span>(icon).<span class="title function_">equalToWhenPresent</span>(<span class="attr">record</span>::getIcon)</span><br><span class="line"> .<span class="title function_">set</span>(email).<span class="title function_">equalToWhenPresent</span>(<span class="attr">record</span>::getEmail)</span><br><span class="line"> .<span class="title function_">set</span>(nickName).<span class="title function_">equalToWhenPresent</span>(<span class="attr">record</span>::getNickName)</span><br><span class="line"> .<span class="title function_">set</span>(note).<span class="title function_">equalToWhenPresent</span>(<span class="attr">record</span>::getNote)</span><br><span class="line"> .<span class="title function_">set</span>(createTime).<span class="title function_">equalToWhenPresent</span>(<span class="attr">record</span>::getCreateTime)</span><br><span class="line"> .<span class="title function_">set</span>(loginTime).<span class="title function_">equalToWhenPresent</span>(<span class="attr">record</span>::getLoginTime)</span><br><span class="line"> .<span class="title function_">set</span>(status).<span class="title function_">equalToWhenPresent</span>(<span class="attr">record</span>::getStatus)</span><br><span class="line"> .<span class="title function_">where</span>(id, <span class="title function_">isEqualTo</span>(<span class="attr">record</span>::getId))</span><br><span class="line"> );</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><ul><li>生成代码中有一些DynamicSqlSupport类,比如UmsAdminDynamicSqlSupport,主要是把数据库表和字段抽象成了SqlTable和SqlColumn对象,估计是为了防止我们硬编码;</li></ul><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line">public final <span class="keyword">class</span> <span class="title class_">UmsAdminDynamicSqlSupport</span> {</span><br><span class="line"> @<span class="title class_">Generated</span>(<span class="string">"org.mybatis.generator.api.MyBatisGenerator"</span>)</span><br><span class="line"> public <span class="keyword">static</span> final <span class="title class_">UmsAdmin</span> umsAdmin = <span class="keyword">new</span> <span class="title class_">UmsAdmin</span>();</span><br><span class="line"></span><br><span class="line"> public <span class="keyword">static</span> final <span class="title class_">SqlColumn</span><<span class="title class_">Long</span>> id = umsAdmin.<span class="property">id</span>;</span><br><span class="line"></span><br><span class="line"> public <span class="keyword">static</span> final <span class="title class_">SqlColumn</span><<span class="title class_">String</span>> username = umsAdmin.<span class="property">username</span>;</span><br><span class="line"></span><br><span class="line"> public <span class="keyword">static</span> final <span class="title class_">SqlColumn</span><<span class="title class_">String</span>> password = umsAdmin.<span class="property">password</span>;</span><br><span class="line"></span><br><span class="line"> public <span class="keyword">static</span> final <span class="title class_">SqlColumn</span><<span class="title class_">String</span>> icon = umsAdmin.<span class="property">icon</span>;</span><br><span class="line"></span><br><span class="line"> public <span class="keyword">static</span> final <span class="title class_">SqlColumn</span><<span class="title class_">String</span>> email = umsAdmin.<span class="property">email</span>;</span><br><span class="line"></span><br><span class="line"> public <span class="keyword">static</span> final <span class="title class_">SqlColumn</span><<span class="title class_">String</span>> nickName = umsAdmin.<span class="property">nickName</span>;</span><br><span class="line"></span><br><span class="line"> public <span class="keyword">static</span> final <span class="title class_">SqlColumn</span><<span class="title class_">String</span>> note = umsAdmin.<span class="property">note</span>;</span><br><span class="line"></span><br><span class="line"> public <span class="keyword">static</span> final <span class="title class_">SqlColumn</span><<span class="title class_">Date</span>> createTime = umsAdmin.<span class="property">createTime</span>;</span><br><span class="line"></span><br><span class="line"> public <span class="keyword">static</span> final <span class="title class_">SqlColumn</span><<span class="title class_">Date</span>> loginTime = umsAdmin.<span class="property">loginTime</span>;</span><br><span class="line"></span><br><span class="line"> public <span class="keyword">static</span> final <span class="title class_">SqlColumn</span><<span class="title class_">Integer</span>> status = umsAdmin.<span class="property">status</span>;</span><br><span class="line"></span><br><span class="line"> @<span class="title class_">Generated</span>(<span class="string">"org.mybatis.generator.api.MyBatisGenerator"</span>)</span><br><span class="line"> public <span class="keyword">static</span> final <span class="keyword">class</span> <span class="title class_">UmsAdmin</span> <span class="keyword">extends</span> <span class="title class_ inherited__">SqlTable</span> {</span><br><span class="line"> public final <span class="title class_">SqlColumn</span><<span class="title class_">Long</span>> id = <span class="title function_">column</span>(<span class="string">"id"</span>, <span class="title class_">JDBCType</span>.<span class="property">BIGINT</span>);</span><br><span class="line"></span><br><span class="line"> public final <span class="title class_">SqlColumn</span><<span class="title class_">String</span>> username = <span class="title function_">column</span>(<span class="string">"username"</span>, <span class="title class_">JDBCType</span>.<span class="property">VARCHAR</span>);</span><br><span class="line"></span><br><span class="line"> public final <span class="title class_">SqlColumn</span><<span class="title class_">String</span>> password = <span class="title function_">column</span>(<span class="string">"password"</span>, <span class="title class_">JDBCType</span>.<span class="property">VARCHAR</span>);</span><br><span class="line"></span><br><span class="line"> public final <span class="title class_">SqlColumn</span><<span class="title class_">String</span>> icon = <span class="title function_">column</span>(<span class="string">"icon"</span>, <span class="title class_">JDBCType</span>.<span class="property">VARCHAR</span>);</span><br><span class="line"></span><br><span class="line"> public final <span class="title class_">SqlColumn</span><<span class="title class_">String</span>> email = <span class="title function_">column</span>(<span class="string">"email"</span>, <span class="title class_">JDBCType</span>.<span class="property">VARCHAR</span>);</span><br><span class="line"></span><br><span class="line"> public final <span class="title class_">SqlColumn</span><<span class="title class_">String</span>> nickName = <span class="title function_">column</span>(<span class="string">"nick_name"</span>, <span class="title class_">JDBCType</span>.<span class="property">VARCHAR</span>);</span><br><span class="line"></span><br><span class="line"> public final <span class="title class_">SqlColumn</span><<span class="title class_">String</span>> note = <span class="title function_">column</span>(<span class="string">"note"</span>, <span class="title class_">JDBCType</span>.<span class="property">VARCHAR</span>);</span><br><span class="line"></span><br><span class="line"> public final <span class="title class_">SqlColumn</span><<span class="title class_">Date</span>> createTime = <span class="title function_">column</span>(<span class="string">"create_time"</span>, <span class="title class_">JDBCType</span>.<span class="property">TIMESTAMP</span>);</span><br><span class="line"></span><br><span class="line"> public final <span class="title class_">SqlColumn</span><<span class="title class_">Date</span>> loginTime = <span class="title function_">column</span>(<span class="string">"login_time"</span>, <span class="title class_">JDBCType</span>.<span class="property">TIMESTAMP</span>);</span><br><span class="line"></span><br><span class="line"> public final <span class="title class_">SqlColumn</span><<span class="title class_">Integer</span>> status = <span class="title function_">column</span>(<span class="string">"status"</span>, <span class="title class_">JDBCType</span>.<span class="property">INTEGER</span>);</span><br><span class="line"></span><br><span class="line"> public <span class="title class_">UmsAdmin</span>() {</span><br><span class="line"> <span class="variable language_">super</span>(<span class="string">"ums_admin"</span>);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><ul><li>利用好MBG生成的代码即可完成单表的CRUD操作了,比如下面最常见的操作。</li></ul><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 后台用户管理Service实现类</span></span><br><span class="line"><span class="comment"> * Created by macro on 2020/12/8.</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">@<span class="title class_">Service</span></span><br><span class="line">public <span class="keyword">class</span> <span class="title class_">UmsAdminServiceImpl</span> implements <span class="title class_">UmsAdminService</span> {</span><br><span class="line"> @<span class="title class_">Autowired</span></span><br><span class="line"> private <span class="title class_">UmsAdminMapper</span> adminMapper;</span><br><span class="line"></span><br><span class="line"> @<span class="title class_">Override</span></span><br><span class="line"> public <span class="keyword">void</span> <span class="title function_">create</span>(<span class="params">UmsAdmin entity</span>) {</span><br><span class="line"> adminMapper.<span class="title function_">insert</span>(entity);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> @<span class="title class_">Override</span></span><br><span class="line"> public <span class="keyword">void</span> <span class="title function_">update</span>(<span class="params">UmsAdmin entity</span>) {</span><br><span class="line"> adminMapper.<span class="title function_">updateByPrimaryKeySelective</span>(entity);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> @<span class="title class_">Override</span></span><br><span class="line"> public <span class="keyword">void</span> <span class="title function_">delete</span>(<span class="params">Long id</span>) {</span><br><span class="line"> adminMapper.<span class="title function_">deleteByPrimaryKey</span>(id);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> @<span class="title class_">Override</span></span><br><span class="line"> public <span class="title class_">UmsAdmin</span> <span class="title function_">select</span>(<span class="params">Long id</span>) {</span><br><span class="line"> <span class="title class_">Optional</span><<span class="title class_">UmsAdmin</span>> optionalEntity = adminMapper.<span class="title function_">selectByPrimaryKey</span>(id);</span><br><span class="line"> <span class="keyword">return</span> optionalEntity.<span class="title function_">orElse</span>(<span class="literal">null</span>);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> @<span class="title class_">Override</span></span><br><span class="line"> public <span class="title class_">List</span><<span class="title class_">UmsAdmin</span>> <span class="title function_">listAll</span>(<span class="params">Integer pageNum, Integer pageSize</span>) {</span><br><span class="line"> <span class="title class_">PageHelper</span>.<span class="title function_">startPage</span>(pageNum, pageSize);</span><br><span class="line"> <span class="keyword">return</span> adminMapper.<span class="title function_">select</span>(<span class="title class_">SelectDSLCompleter</span>.<span class="title function_">allRows</span>());</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="进阶使用"><a href="#进阶使用" class="headerlink" title="进阶使用"></a><strong>进阶使用</strong></h2><blockquote><p>想要用好Dynamic SQL,上面的基础操作是不够的,还需要一些进阶的使用技巧。</p></blockquote><h3 id="SqlBuilder"><a href="#SqlBuilder" class="headerlink" title="SqlBuilder"></a><strong>SqlBuilder</strong></h3><p>SqlBuilder是一个非常有用的类,使用它可以灵活地构建SQL语句的条件,一些常用的条件构建方法如下。</p><table><thead><tr><th align="left">条件</th><th align="left">例子</th><th align="left">对应SQL</th></tr></thead><tbody><tr><td align="left">Between</td><td align="left">where(foo, isBetween(x).and(y))</td><td align="left">where foo between ? and ?</td></tr><tr><td align="left">Equals</td><td align="left">where(foo, isEqualTo(x))</td><td align="left">where foo = ?</td></tr><tr><td align="left">Greater Than</td><td align="left">where(foo, isGreaterThan(x))</td><td align="left">where foo > ?</td></tr><tr><td align="left">In</td><td align="left">where(foo, isIn(x, y))</td><td align="left">where foo in (?,?)</td></tr><tr><td align="left">Like</td><td align="left">where(foo, isLike(x))</td><td align="left">where foo like ?</td></tr><tr><td align="left">Not Equals</td><td align="left">where(foo, isNotEqualTo(x))</td><td align="left">where foo <> ?</td></tr><tr><td align="left">Null</td><td align="left">where(foo, isNull())</td><td align="left">where foo is null</td></tr><tr><td align="left">Present Equals</td><td align="left">where(foo, isEqualToWhenPresent(x))</td><td align="left">where foo = ? (will render if x is non-null)</td></tr></tbody></table><h3 id="StatementProvider"><a href="#StatementProvider" class="headerlink" title="StatementProvider"></a><strong>StatementProvider</strong></h3><p>回想一下之前我们在mapper.xml中定义select标签的方式,各个select标签相当于Statement。而这里的StatementProvider好比是Statement中参数和SQL语句的封装,方便以Java的方式创建Statement。</p><h3 id="条件查询"><a href="#条件查询" class="headerlink" title="条件查询"></a><strong>条件查询</strong></h3><blockquote><p>使用SqlBuilder类构建StatementProvider,然后调用Mapper接口中的方法即可。</p></blockquote><ul><li>这里以按用户名和状态查询后台用户并按创建时间降序排列为例,SQL实现如下;</li></ul><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable constant_">SELECT</span></span><br><span class="line"> id,</span><br><span class="line"> username,</span><br><span class="line"> <span class="variable constant_">PASSWORD</span>,</span><br><span class="line"> icon,</span><br><span class="line"> email,</span><br><span class="line"> nick_name,</span><br><span class="line"> note,</span><br><span class="line"> create_time,</span><br><span class="line"> login_time,</span><br><span class="line"><span class="variable constant_">STATUS</span> </span><br><span class="line"><span class="variable constant_">FROM</span></span><br><span class="line"> ums_admin </span><br><span class="line"><span class="variable constant_">WHERE</span></span><br><span class="line"> ( username = <span class="string">'macro'</span> <span class="variable constant_">AND</span> <span class="variable constant_">STATUS</span> <span class="variable constant_">IN</span> ( <span class="number">0</span>, <span class="number">1</span> ) ) </span><br><span class="line"><span class="variable constant_">ORDER</span> <span class="variable constant_">BY</span></span><br><span class="line"> create_time <span class="variable constant_">DESC</span>;</span><br></pre></td></tr></table></figure><ul><li>使用Dynamic SQL对应的Java代码实现如下,使用SqlBuilder的select方法可以指定查询列,使用from方法可以指定查询表,使用where方法可以构建查询条件,使用orderBy方法可以指定排序。</li></ul><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 后台用户管理Service实现类</span></span><br><span class="line"><span class="comment"> * Created by macro on 2020/12/8.</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">@<span class="title class_">Service</span></span><br><span class="line">public <span class="keyword">class</span> <span class="title class_">UmsAdminServiceImpl</span> implements <span class="title class_">UmsAdminService</span> {</span><br><span class="line"> @<span class="title class_">Override</span></span><br><span class="line"> public <span class="title class_">List</span><<span class="title class_">UmsAdmin</span>> <span class="title function_">list</span>(<span class="params">Integer pageNum, Integer pageSize, <span class="built_in">String</span> username, List<Integer> statusList</span>) {</span><br><span class="line"> <span class="title class_">PageHelper</span>.<span class="title function_">startPage</span>(pageNum, pageSize);</span><br><span class="line"> <span class="title class_">SelectStatementProvider</span> selectStatement = <span class="title class_">SqlBuilder</span>.<span class="title function_">select</span>(<span class="title class_">UmsAdminMapper</span>.<span class="property">selectList</span>)</span><br><span class="line"> .<span class="title function_">from</span>(<span class="title class_">UmsAdminDynamicSqlSupport</span>.<span class="property">umsAdmin</span>)</span><br><span class="line"> .<span class="title function_">where</span>(<span class="title class_">UmsAdminDynamicSqlSupport</span>.<span class="property">username</span>, <span class="title function_">isEqualToWhenPresent</span>(username))</span><br><span class="line"> .<span class="title function_">and</span>(<span class="title class_">UmsAdminDynamicSqlSupport</span>.<span class="property">status</span>, <span class="title function_">isIn</span>(statusList))</span><br><span class="line"> .<span class="title function_">orderBy</span>(<span class="title class_">UmsAdminDynamicSqlSupport</span>.<span class="property">createTime</span>.<span class="title function_">descending</span>())</span><br><span class="line"> .<span class="title function_">build</span>()</span><br><span class="line"> .<span class="title function_">render</span>(<span class="title class_">RenderingStrategies</span>.<span class="property">MYBATIS3</span>);</span><br><span class="line"> <span class="keyword">return</span> adminMapper.<span class="title function_">selectMany</span>(selectStatement);</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h3 id="Lambda条件查询"><a href="#Lambda条件查询" class="headerlink" title="Lambda条件查询"></a><strong>Lambda条件查询</strong></h3><blockquote><p>使用Lambda表达式实现单表条件查询更加简单,实现上面的条件查询,对应Java代码实现如下。</p></blockquote><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 后台用户管理Service实现类</span></span><br><span class="line"><span class="comment"> * Created by macro on 2020/12/8.</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">@<span class="title class_">Service</span></span><br><span class="line">public <span class="keyword">class</span> <span class="title class_">UmsAdminServiceImpl</span> implements <span class="title class_">UmsAdminService</span> {</span><br><span class="line"> @<span class="title class_">Override</span></span><br><span class="line"> public <span class="title class_">List</span><<span class="title class_">UmsAdmin</span>> <span class="title function_">lambdaList</span>(<span class="params">Integer pageNum, Integer pageSize, <span class="built_in">String</span> username, List<Integer> statusList</span>) {</span><br><span class="line"> <span class="title class_">PageHelper</span>.<span class="title function_">startPage</span>(pageNum, pageSize);</span><br><span class="line"> <span class="title class_">List</span><<span class="title class_">UmsAdmin</span>> list = adminMapper.<span class="title function_">select</span>(c -> c.<span class="title function_">where</span>(<span class="title class_">UmsAdminDynamicSqlSupport</span>.<span class="property">username</span>, <span class="title function_">isEqualToWhenPresent</span>(username))</span><br><span class="line"> .<span class="title function_">and</span>(<span class="title class_">UmsAdminDynamicSqlSupport</span>.<span class="property">status</span>, <span class="title function_">isIn</span>(statusList))</span><br><span class="line"> .<span class="title function_">orderBy</span>(<span class="title class_">UmsAdminDynamicSqlSupport</span>.<span class="property">createTime</span>.<span class="title function_">descending</span>()));</span><br><span class="line"> <span class="keyword">return</span> list;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h3 id="子查询"><a href="#子查询" class="headerlink" title="子查询"></a><strong>子查询</strong></h3><blockquote><p>之前使用MBG需要在mapper.xml中手写SQL才能实现子查询,使用Dynamic SQL可以直接在Java代码中实现。</p></blockquote><ul><li>这里以按角色ID查询后台用户为例,SQL实现如下;</li></ul><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable constant_">SELECT</span></span><br><span class="line"> * </span><br><span class="line"><span class="variable constant_">FROM</span></span><br><span class="line"> ums_admin </span><br><span class="line"><span class="variable constant_">WHERE</span></span><br><span class="line"> id <span class="variable constant_">IN</span> ( <span class="variable constant_">SELECT</span> admin_id <span class="variable constant_">FROM</span> ums_admin_role_relation <span class="variable constant_">WHERE</span> role_id = <span class="number">1</span> )</span><br></pre></td></tr></table></figure><ul><li>使用Dynamic SQL对应的Java代码实现如下,可以发现SqlBuilder的条件构造方法isIn中还可以嵌套SqlBuilder的查询。</li></ul><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 后台用户管理Service实现类</span></span><br><span class="line"><span class="comment"> * Created by macro on 2020/12/8.</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">@<span class="title class_">Service</span></span><br><span class="line">public <span class="keyword">class</span> <span class="title class_">UmsAdminServiceImpl</span> implements <span class="title class_">UmsAdminService</span> {</span><br><span class="line"> @<span class="title class_">Override</span></span><br><span class="line"> public <span class="title class_">List</span><<span class="title class_">UmsAdmin</span>> <span class="title function_">subList</span>(<span class="params">Long roleId</span>) {</span><br><span class="line"> <span class="title class_">SelectStatementProvider</span> selectStatement = <span class="title class_">SqlBuilder</span>.<span class="title function_">select</span>(<span class="title class_">UmsAdminMapper</span>.<span class="property">selectList</span>)</span><br><span class="line"> .<span class="title function_">from</span>(<span class="title class_">UmsAdminDynamicSqlSupport</span>.<span class="property">umsAdmin</span>)</span><br><span class="line"> .<span class="title function_">where</span>(<span class="title class_">UmsAdminDynamicSqlSupport</span>.<span class="property">id</span>, <span class="title function_">isIn</span>(<span class="title class_">SqlBuilder</span>.<span class="title function_">select</span>(<span class="title class_">UmsAdminRoleRelationDynamicSqlSupport</span>.<span class="property">adminId</span>)</span><br><span class="line"> .<span class="title function_">from</span>(<span class="title class_">UmsAdminRoleRelationDynamicSqlSupport</span>.<span class="property">umsAdminRoleRelation</span>)</span><br><span class="line"> .<span class="title function_">where</span>(<span class="title class_">UmsAdminRoleRelationDynamicSqlSupport</span>.<span class="property">roleId</span>, <span class="title function_">isEqualTo</span>(roleId))))</span><br><span class="line"> .<span class="title function_">build</span>()</span><br><span class="line"> .<span class="title function_">render</span>(<span class="title class_">RenderingStrategies</span>.<span class="property">MYBATIS3</span>);</span><br><span class="line"> <span class="keyword">return</span> adminMapper.<span class="title function_">selectMany</span>(selectStatement);</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h3 id="Group和Join查询"><a href="#Group和Join查询" class="headerlink" title="Group和Join查询"></a><strong>Group和Join查询</strong></h3><blockquote><p>涉及到多表查询,之前使用MBG的时候基本只能在mapper.xml中手写SQL实现,使用Dynamic SQL可以支持多表查询。</p></blockquote><ul><li>这里以按角色统计后台用户数量为例,SQL实现如下;</li></ul><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable constant_">SELECT</span></span><br><span class="line"> ur.<span class="property">id</span> <span class="variable constant_">AS</span> roleId,</span><br><span class="line"> ur.<span class="property">NAME</span> <span class="variable constant_">AS</span> roleName,</span><br><span class="line"> <span class="title function_">count</span>( ua.<span class="property">id</span> ) <span class="variable constant_">AS</span> count </span><br><span class="line"><span class="variable constant_">FROM</span></span><br><span class="line"> ums_role ur</span><br><span class="line"> <span class="variable constant_">LEFT</span> <span class="variable constant_">JOIN</span> ums_admin_role_relation uarr <span class="variable constant_">ON</span> ur.<span class="property">id</span> = uarr.<span class="property">role_id</span></span><br><span class="line"> <span class="variable constant_">LEFT</span> <span class="variable constant_">JOIN</span> ums_admin ua <span class="variable constant_">ON</span> uarr.<span class="property">admin_id</span> = ua.<span class="property">id</span> </span><br><span class="line"><span class="variable constant_">GROUP</span> <span class="variable constant_">BY</span></span><br><span class="line"> ur.<span class="property">id</span>;</span><br></pre></td></tr></table></figure><ul><li>先在Dao中添加一个<code>groupList</code>方法,然后使用<code>@Results</code>注解定义好resultMap;</li></ul><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Created by macro on 2020/12/9.</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">public interface <span class="title class_">UmsAdminDao</span> {</span><br><span class="line"> @<span class="title class_">SelectProvider</span>(type = <span class="title class_">SqlProviderAdapter</span>.<span class="property">class</span>, method = <span class="string">"select"</span>)</span><br><span class="line"> @<span class="title class_">Results</span>(id = <span class="string">"RoleStatResult"</span>, value = {</span><br><span class="line"> @<span class="title class_">Result</span>(column = <span class="string">"roleId"</span>, property = <span class="string">"roleId"</span>, jdbcType = <span class="title class_">JdbcType</span>.<span class="property">BIGINT</span>, id = <span class="literal">true</span>),</span><br><span class="line"> @<span class="title class_">Result</span>(column = <span class="string">"roleName"</span>, property = <span class="string">"roleName"</span>, jdbcType = <span class="title class_">JdbcType</span>.<span class="property">VARCHAR</span>),</span><br><span class="line"> @<span class="title class_">Result</span>(column = <span class="string">"count"</span>, property = <span class="string">"count"</span>, jdbcType = <span class="title class_">JdbcType</span>.<span class="property">INTEGER</span>)</span><br><span class="line"> })</span><br><span class="line"> <span class="title class_">List</span><<span class="title class_">RoleStatDto</span>> <span class="title function_">groupList</span>(<span class="title class_">SelectStatementProvider</span> selectStatement);</span><br><span class="line">}</span><br></pre></td></tr></table></figure><ul><li>然后在Service中调用<code>groupList</code>方法传入StatementProvider即可,对应的Java代码实现如下。</li></ul><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 后台用户管理Service实现类</span></span><br><span class="line"><span class="comment"> * Created by macro on 2020/12/8.</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">@<span class="title class_">Service</span></span><br><span class="line">public <span class="keyword">class</span> <span class="title class_">UmsAdminServiceImpl</span> implements <span class="title class_">UmsAdminService</span> {</span><br><span class="line"> @<span class="title class_">Override</span></span><br><span class="line"> public <span class="title class_">List</span><<span class="title class_">RoleStatDto</span>> <span class="title function_">groupList</span>(<span class="params"></span>) {</span><br><span class="line"> <span class="title class_">SelectStatementProvider</span> selectStatement = <span class="title class_">SqlBuilder</span>.<span class="title function_">select</span>(<span class="title class_">UmsRoleDynamicSqlSupport</span>.<span class="property">id</span>.<span class="title function_">as</span>(<span class="string">"roleId"</span>), <span class="title class_">UmsRoleDynamicSqlSupport</span>.<span class="property">name</span>.<span class="title function_">as</span>(<span class="string">"roleName"</span>), <span class="title function_">count</span>(<span class="title class_">UmsAdminDynamicSqlSupport</span>.<span class="property">id</span>).<span class="title function_">as</span>(<span class="string">"count"</span>))</span><br><span class="line"> .<span class="title function_">from</span>(<span class="title class_">UmsRoleDynamicSqlSupport</span>.<span class="property">umsRole</span>)</span><br><span class="line"> .<span class="title function_">leftJoin</span>(<span class="title class_">UmsAdminRoleRelationDynamicSqlSupport</span>.<span class="property">umsAdminRoleRelation</span>)</span><br><span class="line"> .<span class="title function_">on</span>(<span class="title class_">UmsRoleDynamicSqlSupport</span>.<span class="property">id</span>, <span class="title function_">equalTo</span>(<span class="title class_">UmsAdminRoleRelationDynamicSqlSupport</span>.<span class="property">roleId</span>))</span><br><span class="line"> .<span class="title function_">leftJoin</span>(<span class="title class_">UmsAdminDynamicSqlSupport</span>.<span class="property">umsAdmin</span>)</span><br><span class="line"> .<span class="title function_">on</span>(<span class="title class_">UmsAdminRoleRelationDynamicSqlSupport</span>.<span class="property">adminId</span>, <span class="title function_">equalTo</span>(<span class="title class_">UmsAdminDynamicSqlSupport</span>.<span class="property">id</span>))</span><br><span class="line"> .<span class="title function_">groupBy</span>(<span class="title class_">UmsRoleDynamicSqlSupport</span>.<span class="property">id</span>)</span><br><span class="line"> .<span class="title function_">build</span>()</span><br><span class="line"> .<span class="title function_">render</span>(<span class="title class_">RenderingStrategies</span>.<span class="property">MYBATIS3</span>);</span><br><span class="line"> <span class="keyword">return</span> adminDao.<span class="title function_">groupList</span>(selectStatement);</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h3 id="条件删除"><a href="#条件删除" class="headerlink" title="条件删除"></a><strong>条件删除</strong></h3><blockquote><p>使用Dynamic SQL实现条件删除,直接调用Mapper接口中生成好的delete方法即可。</p></blockquote><ul><li>这里以按用户名删除后台用户为例,SQL实现如下;</li></ul><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable constant_">DELETE</span> </span><br><span class="line"><span class="variable constant_">FROM</span></span><br><span class="line"> ums_admin </span><br><span class="line"><span class="variable constant_">WHERE</span></span><br><span class="line"> username = <span class="string">'andy'</span>;</span><br></pre></td></tr></table></figure><ul><li>使用Dynamic SQL对应Java中的实现如下。</li></ul><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 后台用户管理Service实现类</span></span><br><span class="line"><span class="comment"> * Created by macro on 2020/12/8.</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">@<span class="title class_">Service</span></span><br><span class="line">public <span class="keyword">class</span> <span class="title class_">UmsAdminServiceImpl</span> implements <span class="title class_">UmsAdminService</span> {</span><br><span class="line"> @<span class="title class_">Override</span></span><br><span class="line"> public <span class="keyword">void</span> <span class="title function_">deleteByUsername</span>(<span class="params"><span class="built_in">String</span> username</span>) {</span><br><span class="line"> <span class="title class_">DeleteStatementProvider</span> deleteStatement = <span class="title class_">SqlBuilder</span>.<span class="title function_">deleteFrom</span>(<span class="title class_">UmsAdminDynamicSqlSupport</span>.<span class="property">umsAdmin</span>)</span><br><span class="line"> .<span class="title function_">where</span>(<span class="title class_">UmsAdminDynamicSqlSupport</span>.<span class="property">username</span>, <span class="title function_">isEqualTo</span>(username))</span><br><span class="line"> .<span class="title function_">build</span>()</span><br><span class="line"> .<span class="title function_">render</span>(<span class="title class_">RenderingStrategies</span>.<span class="property">MYBATIS3</span>);</span><br><span class="line"> adminMapper.<span class="title function_">delete</span>(deleteStatement);</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h3 id="条件修改"><a href="#条件修改" class="headerlink" title="条件修改"></a><strong>条件修改</strong></h3><blockquote><p>使用Dynamic SQL实现条件修改,直接调用Mapper接口中生成好的update方法即可。</p></blockquote><ul><li>这里以按指定ID修改后台用户的状态为例,SQL实现如下;</li></ul><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable constant_">UPDATE</span> ums_admin </span><br><span class="line"><span class="variable constant_">SET</span> <span class="variable constant_">STATUS</span> = <span class="number">1</span> </span><br><span class="line"><span class="variable constant_">WHERE</span></span><br><span class="line"> id <span class="variable constant_">IN</span> ( <span class="number">1</span>, <span class="number">2</span> );</span><br></pre></td></tr></table></figure><ul><li>使用Dynamic SQL对应Java中的实现如下。</li></ul><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 后台用户管理Service实现类</span></span><br><span class="line"><span class="comment"> * Created by macro on 2020/12/8.</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">@<span class="title class_">Service</span></span><br><span class="line">public <span class="keyword">class</span> <span class="title class_">UmsAdminServiceImpl</span> implements <span class="title class_">UmsAdminService</span> {</span><br><span class="line"> @<span class="title class_">Override</span></span><br><span class="line"> public <span class="keyword">void</span> <span class="title function_">updateByIds</span>(<span class="params">List<Long> ids, Integer status</span>) {</span><br><span class="line"> <span class="title class_">UpdateStatementProvider</span> updateStatement = <span class="title class_">SqlBuilder</span>.<span class="title function_">update</span>(<span class="title class_">UmsAdminDynamicSqlSupport</span>.<span class="property">umsAdmin</span>)</span><br><span class="line"> .<span class="title function_">set</span>(<span class="title class_">UmsAdminDynamicSqlSupport</span>.<span class="property">status</span>).<span class="title function_">equalTo</span>(status)</span><br><span class="line"> .<span class="title function_">where</span>(<span class="title class_">UmsAdminDynamicSqlSupport</span>.<span class="property">id</span>, <span class="title function_">isIn</span>(ids))</span><br><span class="line"> .<span class="title function_">build</span>()</span><br><span class="line"> .<span class="title function_">render</span>(<span class="title class_">RenderingStrategies</span>.<span class="property">MYBATIS3</span>);</span><br><span class="line"> adminMapper.<span class="title function_">update</span>(updateStatement);</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h3 id="一对多查询"><a href="#一对多查询" class="headerlink" title="一对多查询"></a><strong>一对多查询</strong></h3><blockquote><p>使用Dynamic SQL也可以实现一对多查询,只是由于Java注解无法实现循环引用,所以一对多的resultMap只能在mapper.xml来配置,这可能是唯一需要使用mapper.xml的地方。</p></blockquote><ul><li>这里以按ID查询后台用户信息(包含对应角色列表)为例,SQL实现如下;</li></ul><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable constant_">SELECT</span></span><br><span class="line"> ua.*,</span><br><span class="line"> ur.<span class="property">id</span> <span class="variable constant_">AS</span> role_id,</span><br><span class="line"> ur.<span class="property">NAME</span> <span class="variable constant_">AS</span> role_name,</span><br><span class="line"> ur.<span class="property">description</span> <span class="variable constant_">AS</span> role_description,</span><br><span class="line"> ur.<span class="property">create_time</span> <span class="variable constant_">AS</span> role_create_time,</span><br><span class="line"> ur.<span class="property">STATUS</span> <span class="variable constant_">AS</span> role_status,</span><br><span class="line"> ur.<span class="property">sort</span> <span class="variable constant_">AS</span> role_sort </span><br><span class="line"><span class="variable constant_">FROM</span></span><br><span class="line"> ums_admin ua</span><br><span class="line"> <span class="variable constant_">LEFT</span> <span class="variable constant_">JOIN</span> ums_admin_role_relation uarr <span class="variable constant_">ON</span> ua.<span class="property">id</span> = uarr.<span class="property">admin_id</span></span><br><span class="line"> <span class="variable constant_">LEFT</span> <span class="variable constant_">JOIN</span> ums_role ur <span class="variable constant_">ON</span> uarr.<span class="property">role_id</span> = ur.<span class="property">id</span> </span><br><span class="line"><span class="variable constant_">WHERE</span></span><br><span class="line"> ua.<span class="property">id</span> = <span class="number">1</span></span><br></pre></td></tr></table></figure><ul><li>然后在Dao接口中添加<code>selectWithRoleList</code>方法,这里使用<code>@ResultMap</code>注解引用mapper.xml中定义的resultMap;</li></ul><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Created by macro on 2020/12/9.</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">public interface <span class="title class_">UmsAdminDao</span> {</span><br><span class="line"></span><br><span class="line"> @<span class="title class_">SelectProvider</span>(type = <span class="title class_">SqlProviderAdapter</span>.<span class="property">class</span>, method = <span class="string">"select"</span>)</span><br><span class="line"> @<span class="title class_">ResultMap</span>(<span class="string">"AdminRoleResult"</span>)</span><br><span class="line"> <span class="title class_">AdminRoleDto</span> <span class="title function_">selectWithRoleList</span>(<span class="title class_">SelectStatementProvider</span> selectStatement);</span><br><span class="line">}</span><br></pre></td></tr></table></figure><ul><li>在mapper.xml中添加名称为<code>AdminRoleResult</code>的resultMap,这里有个小技巧,可以直接引用在Mapper接口中定义好的resultMap;</li></ul><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><resultMap id=<span class="string">"AdminRoleResult"</span> type=<span class="string">"com.macro.mall.tiny.domain.AdminRoleDto"</span></span><br><span class="line"> <span class="keyword">extends</span>=<span class="string">"com.macro.mall.tiny.mbg.mapper.UmsAdminMapper.UmsAdminResult"</span>></span><br><span class="line"> <span class="language-xml"><span class="tag"><<span class="name">collection</span> <span class="attr">property</span>=<span class="string">"roleList"</span> <span class="attr">resultMap</span>=<span class="string">"com.macro.mall.tiny.mbg.mapper.UmsRoleMapper.UmsRoleResult"</span> <span class="attr">columnPrefix</span>=<span class="string">"role_"</span>></span></span></span><br><span class="line"><span class="language-xml"> <span class="tag"></<span class="name">collection</span>></span></span></span><br><span class="line"></resultMap></span><br></pre></td></tr></table></figure><ul><li>然后在Service实现类中调用即可,为了方便结果集映射给查询列取了别名。</li></ul><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 后台用户管理Service实现类</span></span><br><span class="line"><span class="comment"> * Created by macro on 2020/12/8.</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">@<span class="title class_">Service</span></span><br><span class="line">public <span class="keyword">class</span> <span class="title class_">UmsAdminServiceImpl</span> implements <span class="title class_">UmsAdminService</span> { </span><br><span class="line"> @<span class="title class_">Override</span></span><br><span class="line"> public <span class="title class_">AdminRoleDto</span> <span class="title function_">selectWithRoleList</span>(<span class="params">Long id</span>) {</span><br><span class="line"> <span class="title class_">List</span><<span class="title class_">BasicColumn</span>> columnList = <span class="keyword">new</span> <span class="title class_">ArrayList</span><>(<span class="title class_">CollUtil</span>.<span class="title function_">toList</span>(<span class="title class_">UmsAdminMapper</span>.<span class="property">selectList</span>));</span><br><span class="line"> columnList.<span class="title function_">add</span>(<span class="title class_">UmsRoleDynamicSqlSupport</span>.<span class="property">id</span>.<span class="title function_">as</span>(<span class="string">"role_id"</span>));</span><br><span class="line"> columnList.<span class="title function_">add</span>(<span class="title class_">UmsRoleDynamicSqlSupport</span>.<span class="property">name</span>.<span class="title function_">as</span>(<span class="string">"role_name"</span>));</span><br><span class="line"> columnList.<span class="title function_">add</span>(<span class="title class_">UmsRoleDynamicSqlSupport</span>.<span class="property">description</span>.<span class="title function_">as</span>(<span class="string">"role_description"</span>));</span><br><span class="line"> columnList.<span class="title function_">add</span>(<span class="title class_">UmsRoleDynamicSqlSupport</span>.<span class="property">createTime</span>.<span class="title function_">as</span>(<span class="string">"role_create_time"</span>));</span><br><span class="line"> columnList.<span class="title function_">add</span>(<span class="title class_">UmsRoleDynamicSqlSupport</span>.<span class="property">status</span>.<span class="title function_">as</span>(<span class="string">"role_status"</span>));</span><br><span class="line"> columnList.<span class="title function_">add</span>(<span class="title class_">UmsRoleDynamicSqlSupport</span>.<span class="property">sort</span>.<span class="title function_">as</span>(<span class="string">"role_sort"</span>));</span><br><span class="line"> <span class="title class_">SelectStatementProvider</span> selectStatement = <span class="title class_">SqlBuilder</span>.<span class="title function_">select</span>(columnList)</span><br><span class="line"> .<span class="title function_">from</span>(<span class="title class_">UmsAdminDynamicSqlSupport</span>.<span class="property">umsAdmin</span>)</span><br><span class="line"> .<span class="title function_">leftJoin</span>(<span class="title class_">UmsAdminRoleRelationDynamicSqlSupport</span>.<span class="property">umsAdminRoleRelation</span>)</span><br><span class="line"> .<span class="title function_">on</span>(<span class="title class_">UmsAdminDynamicSqlSupport</span>.<span class="property">id</span>, <span class="title function_">equalTo</span>(<span class="title class_">UmsAdminRoleRelationDynamicSqlSupport</span>.<span class="property">adminId</span>))</span><br><span class="line"> .<span class="title function_">leftJoin</span>(<span class="title class_">UmsRoleDynamicSqlSupport</span>.<span class="property">umsRole</span>)</span><br><span class="line"> .<span class="title function_">on</span>(<span class="title class_">UmsAdminRoleRelationDynamicSqlSupport</span>.<span class="property">roleId</span>, <span class="title function_">equalTo</span>(<span class="title class_">UmsRoleDynamicSqlSupport</span>.<span class="property">id</span>))</span><br><span class="line"> .<span class="title function_">where</span>(<span class="title class_">UmsAdminDynamicSqlSupport</span>.<span class="property">id</span>, <span class="title function_">isEqualTo</span>(id))</span><br><span class="line"> .<span class="title function_">build</span>()</span><br><span class="line"> .<span class="title function_">render</span>(<span class="title class_">RenderingStrategies</span>.<span class="property">MYBATIS3</span>);</span><br><span class="line"> <span class="keyword">return</span> adminDao.<span class="title function_">selectWithRoleList</span>(selectStatement);</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a><strong>总结</strong></h2><p>当我们使用MyBatis官方代码生成器MBG时,配置的targetRuntime决定了使用它的使用方式。Dynamic SQL更倾向于使用Java API来实现SQL操作,传统的方式更倾向于在mapper.xml中手写SQL来实现SQL操作。虽然MyBatis官方推荐使用Dynamic SQL,但选择那种方式全看个人习惯了!</p><h2 id="参考资料"><a href="#参考资料" class="headerlink" title="参考资料"></a><strong>参考资料</strong></h2><p>官方文档:<a href="https://mybatis.org/mybatis-dynamic-sql/docs/introduction.html">https://mybatis.org/mybatis-dynamic-sql/docs/introduction.html</a></p><h2 id="项目源码地址"><a href="#项目源码地址" class="headerlink" title="项目源码地址"></a><strong>项目源码地址</strong></h2><p><a href="https://github.com/macrozheng/mall-learning/tree/master/mall-tiny-dynamic-sql">https://github.com/macrozheng/mall-learning/tree/master/mall-tiny-dynamic-sql</a></p>]]></content>
<summary type="html"><h1 id="干掉mapper-xml!MyBatis新特性动态SQL真香!"><a href="#干掉mapper-xml!MyBatis新特性动态SQL真香!" class="headerlink" title="干掉mapper.xml!MyBatis新特性动态SQL真香</summary>
<category term="Mybatis" scheme="http://example.com/tags/Mybatis/"/>
</entry>
<entry>
<title>Hello World</title>
<link href="http://example.com/2023/03/18/hello-world/"/>
<id>http://example.com/2023/03/18/hello-world/</id>
<published>2023-03-18T04:18:30.338Z</published>
<updated>2022-07-12T08:12:36.201Z</updated>
<content type="html"><![CDATA[<p>Welcome to <a href="https://hexo.io/">Hexo</a>! This is your very first post. Check <a href="https://hexo.io/docs/">documentation</a> for more info. If you get any problems when using Hexo, you can find the answer in <a href="https://hexo.io/docs/troubleshooting.html">troubleshooting</a> or you can ask me on <a href="https://github.com/hexojs/hexo/issues">GitHub</a>.</p><h2 id="Quick-Start"><a href="#Quick-Start" class="headerlink" title="Quick Start"></a>Quick Start</h2><h3 id="Create-a-new-post"><a href="#Create-a-new-post" class="headerlink" title="Create a new post"></a>Create a new post</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo new <span class="string">"My New Post"</span></span><br></pre></td></tr></table></figure><p>More info: <a href="https://hexo.io/docs/writing.html">Writing</a></p><h3 id="Run-server"><a href="#Run-server" class="headerlink" title="Run server"></a>Run server</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo server</span><br></pre></td></tr></table></figure><p>More info: <a href="https://hexo.io/docs/server.html">Server</a></p><h3 id="Generate-static-files"><a href="#Generate-static-files" class="headerlink" title="Generate static files"></a>Generate static files</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo generate</span><br></pre></td></tr></table></figure><p>More info: <a href="https://hexo.io/docs/generating.html">Generating</a></p><h3 id="Deploy-to-remote-sites"><a href="#Deploy-to-remote-sites" class="headerlink" title="Deploy to remote sites"></a>Deploy to remote sites</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo deploy</span><br></pre></td></tr></table></figure><p>More info: <a href="https://hexo.io/docs/one-command-deployment.html">Deployment</a></p>]]></content>
<summary type="html"><p>Welcome to <a href="https://hexo.io/">Hexo</a>! This is your very first post. Check <a href="https://hexo.io/docs/">documentation</a> for</summary>
</entry>
<entry>
<title>面试技巧</title>
<link href="http://example.com/2022/07/24/%E9%9D%A2%E8%AF%95%E6%8A%80%E5%B7%A7/"/>
<id>http://example.com/2022/07/24/%E9%9D%A2%E8%AF%95%E6%8A%80%E5%B7%A7/</id>
<published>2022-07-24T06:45:08.000Z</published>
<updated>2022-07-24T06:51:39.548Z</updated>
<content type="html"><![CDATA[<h1 id="自我介绍"><a href="#自我介绍" class="headerlink" title="自我介绍"></a>自我介绍</h1><p>虽然简历上已经包含了基本的个人信息,面试官也已经看过了你的简历,但是还是需要进行自我介绍,主要有以下作用:</p><ul><li>简历上的内容还是有点多,在自我介绍环节需要用几句话来总结自己的价值,让面试官对你产生兴趣。</li><li>给面试官一点时间重新浏览一下你的简历,然后想好要问的问题。</li><li>简历只是文字信息,自我介绍环节是面试官和你的首次面对面交流,在这个环节中可以让面试官对你有更感性的认识。人是很感性的动物,自我介绍环节如果让面试官对你有比较好的印象,那么后面的面试过程会比较轻松愉快。</li></ul><p>自我介绍可以看成是简历的精简版,不需要把简历都原原本本读一遍,可以挑一两个最能体现你价值的内容来介绍。比如你对 Java 最熟悉的话,那么可以说:“我的主编程语言是 Java,阅读过部分 JDK 源码,对 Java 虚拟机、并发编程、网络编程比较熟悉”。在你这么介绍之后,面试官接下来很可能就会开始问你 Java 相关的内容。如果你对项目比较有把握的话,就可以挑一个项目重点说一下。</p><p>学习能力</p><p>① 如何体现你的学习能力?</p><p>② 对于一个没有接触过的新领域,你是如何去学习的?</p><p>③ 有没了解最新的技术?</p><p>④ 平时逛技术社区吗?</p><p>⑤ 职业规划(或学习规划)</p><p>最好分成短期计划和长期计划,职业规划要充分体现出你打算为企业带来更高的价值,而不是只想着提高自我价值。</p><p>例如:如果能有幸加入XXXX,在入职之前我打算认真学习一下部门业务所涉及的一些技术,从而为入职做更好的准备。从长远来看,我打算利用工作之余深入学习工作中碰到的新知识,因为我觉得只有对这些新知识有很好的理解才能更好的完成工作任务。我还打算多去了解一下最新的一些技术,从而为将来的工作做准备。除此之外,我认为在团队中,交流表达能力和团队协作能力也是必不可少的,所以我打算找几本相关书籍进行学习。</p><h3 id="团队协作能力"><a href="#团队协作能力" class="headerlink" title="团队协作能力"></a>团队协作能力</h3><h3 id="交流表达能力"><a href="#交流表达能力" class="headerlink" title="交流表达能力"></a>交流表达能力</h3><h3 id="加入意愿"><a href="#加入意愿" class="headerlink" title="加入意愿"></a>加入意愿</h3><p>企业也很希望优秀的人加入,如果决定录用了某个候选人但是他最后没接受 Offer,那么会增加招聘成本。并且加入意愿不高的话,以后也很有可能会跳槽。</p><p>① 对我们公司熟悉吗(为什么想要加入我们公司)?</p><p>先说一下你对该公司的了解,并从企业氛围、技术能力、发展前景等方面优势来说明。如果能稍微 diss 一下其竞争对手的话也不错,但是不要说得太过,从客观事实去说明就好。</p><p>② 家庭情况(对城市有没有要求)?</p><p>有些公司希望你以后能接受调换工作城市。或者看你是否能接受工作城市离家很远。</p><h2 id="向面试官提问环节"><a href="#向面试官提问环节" class="headerlink" title="向面试官提问环节"></a>向面试官提问环节</h2><ol><li><p>部门的业务和技术栈</p></li><li><p>技术难点怎么解决</p></li><li><p>每天有多少时间花在写代码上</p></li><li><p>公司吸引您的地方有哪些</p><p>面试总结</p><p>面试是最好的查缺补漏机会,在参加完一场面试之后,可以对那些回答得不太好的问题重新思考一下,然后查资料并做好总结。</p><p>参加了几场面试之后,你会发现每场面试都会问很多一样的问题,比如项目中的难点等等,所以做面试总结的收益会非常大。</p><h2 id="6-内容"><a href="#6-内容" class="headerlink" title="6. 内容"></a>6. 内容</h2><h3 id="6-1-必填内容"><a href="#6-1-必填内容" class="headerlink" title="6.1 必填内容"></a>6.1 必填内容</h3><ul><li><strong>个人信息</strong>:姓名相当于你的 ID,应该把姓名放在最显眼的地方;为了方便 HR 联系你,也要把联系信息放在和姓名一样显眼的位置。</li><li><strong>教育经历</strong>:教育经历和工作经历按年份逆序来写。</li><li><strong>项目经历</strong>:项目经历可以写科研项目、个人项目、实习和工作项目,纸质简历上最好只放 1-2 个比较突出的项目。</li><li><strong>技能清单</strong>:技能清单最好可以让人一眼就看出你的熟练度,可以使用精通、熟悉、了解等词语,也可以使用 ★★☆ 这种图标。</li></ul><h3 id="6-2-非必填内容"><a href="#6-2-非必填内容" class="headerlink" title="6.2 非必填内容"></a>6.2 非必填内容</h3><ul><li><strong>个人博客</strong>:个人博客和 Github 等技术社区账号可以作为加分项,如果有不错的内容可以放在上面,否则不建议放。</li><li><strong>非必要个人信息</strong>:有些企业会根据性别、年龄等信息筛选人,最好写上,不写也没什么关系。</li><li><strong>兴趣爱好</strong>:只写可以给企业带来价值的兴趣爱好,其它的无关兴趣爱好可以不写。</li><li><strong>成绩</strong>:三好学生、奖学金、四六级成绩、绩点可以写,以证明你的学习能力,但是尽量不要占太多行,如果不是特别好的话也可以不写。</li><li><strong>获奖经历</strong>:和技术相关的大赛获奖经历可以写,但是其它一些比赛获奖就不需要写。</li><li><strong>个人评价</strong>:个人评价如果要写的话不要太空泛,什么热爱学习吃苦耐劳等就不要写了,要写得话可以写在最后当做是其它内容的总结。</li></ul><h2 id="7-项目介绍"><a href="#7-项目介绍" class="headerlink" title="7. 项目介绍"></a>7. 项目介绍</h2><p>项目经历一方面可以用于匹配技能,另一方面也可以通过你在项目中解决的问题来展示你的能力。可以按照下面的步骤介绍项目:</p><ul><li>为什么做这个项目;</li><li>项目有哪些功能;</li><li>如果进行技术选型;</li><li>你在项目中担任的角色;</li><li>碰到了哪些问题;</li><li>使用什么方式去解决问题的;</li><li>解决效果是怎样,和别人相比有什么优势。</li></ul><p>当然不能描述地过于详细,简历上的内容应当简练,突出重点,所以详细的内容可以放在面试的时候再提。</p><p>不要用太过主观的语言,而是用数据等客观事实。比如介绍碰到的问题时,不要用“非常难解决”等词语,而是用“并发用户数达到XXX导致响应时间增加到XXX”等数据。</p><p>也不要堆叠无意义的技术名词,比如“基于 Spring、SpringMVC、Hibernate 实现了 XXX 系统,包含增删查改等功能”。而是突出重点,例如使用 Redis+Local Cache 多级缓存解决高并发读带来的性能问题,缓存命中率达到 99%,将访问 DB 的 QPS 从 10000 降低到了 100,延迟从 100ms 降低到了 30ms。</p></li></ol>]]></content>
<summary type="html"><h1 id="自我介绍"><a href="#自我介绍" class="headerlink" title="自我介绍"></a>自我介绍</h1><p>虽然简历上已经包含了基本的个人信息,面试官也已经看过了你的简历,但是还是需要进行自我介绍,主要有以下作用:</p>
<ul></summary>
</entry>
<entry>
<title>nginx入门到实战</title>
<link href="http://example.com/2022/07/12/nginx%E5%85%A5%E9%97%A8%E5%88%B0%E5%AE%9E%E6%88%98/"/>
<id>http://example.com/2022/07/12/nginx%E5%85%A5%E9%97%A8%E5%88%B0%E5%AE%9E%E6%88%98/</id>
<published>2022-07-12T12:18:20.000Z</published>
<updated>2023-04-21T02:15:46.268Z</updated>
<content type="html"><![CDATA[<blockquote><p>提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档</p></blockquote><p>@<a href="%E6%96%87%E7%AB%A0%E7%9B%AE%E5%BD%95">TOC</a></p><hr><h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p><code>提示:以下是本篇文章正文内容,下面案例可供参考</code></p><h1 id="一-Nginx简介"><a href="#一-Nginx简介" class="headerlink" title="一.Nginx简介"></a>一.Nginx简介</h1><p>Nginx是俄罗斯人Igor Sysoev编写的轻量级Web服务器,它的发音为 [ˈendʒɪnks] ,它不仅是一个高性能的HTTP和反向代理服务器,同时也是一个IMAP/POP3/SMTP 代理服务器,差不多世界上每三个网站中就有一个网站使用nginx。</p><h1 id="二、Nginx入门"><a href="#二、Nginx入门" class="headerlink" title="二、Nginx入门"></a>二、Nginx入门</h1><h2 id="1-为什么要用Nginx"><a href="#1-为什么要用Nginx" class="headerlink" title="1.为什么要用Nginx"></a>1.为什么要用Nginx</h2><p>Nginx以事件驱动的方式编写,所以有非常好的性能,同时也是一个非常高效的反向代理、负载平衡服务器。在性能上,Nginx占用很少的系统资源,能支持更多的并发连接,达到更高的访问效率;在功能上,Nginx是优秀的代理服务器和负载均衡服务器;在安装配置上,Nginx安装简单、配置灵活。</p><p>Nginx支持热部署,启动速度特别快,还可以在不间断服务的情况下对软件版本或配置进行升级,即使运行数月也无需重新启动。</p><p>在微服务的体系之下,Nginx正在被越来越多的项目采用作为网关来使用,配合Lua做限流、熔断等控制。</p><h2 id="2-Nginx安装教程"><a href="#2-Nginx安装教程" class="headerlink" title="2.Nginx安装教程"></a>2.Nginx安装教程</h2><p><a href="https://www.nginx.cn/install">Nginx安装教程</a></p><h1 id="三、Nginx实战"><a href="#三、Nginx实战" class="headerlink" title="三、Nginx实战"></a>三、Nginx实战</h1><p>首先了解正向代理和反向代理<br>(一)正向代理<br> 比如说你要海外购买衣服,但是由于疫情不方便出国,然后需要通过电话(或者微信)海外购的朋友帮你来买这件衣服。并且你也知道衣服是从海外买来的,只是中间通过了你的朋友来购买,这就是正向代理。<br> 但是对于游览器来说正向代理需要配置代理服务器IP地址,不然找不到代理服务器的位置,这里的代理服务器就和海外购朋友的电话类似,正向代理没有直接响应请求的能力。<br> (二)反向代理<br> 比如说老师叫小明去买东西,但是小明就嫌麻烦然后叫了小张,让小张去帮老师买东西,然后小张把东西给小明后,小明给了老师,但是老师并不知道是小张买的以为是小明买的,这就是反向代理。<br> 游览器发送请求给代理服务器,代理服务器通过访问其他服务器,然后在将数据返回给游览器,游览器以为是代理服务器响应的请求。<br> <a href="https://juejin.cn/post/6958987684383555592">正向代理和反向代理详细介绍可以看此篇</a></p><h2 id="1-http反向代理"><a href="#1-http反向代理" class="headerlink" title="1.http反向代理"></a>1.http反向代理</h2><p>首先我们来实现一个简单的反向代理<br>nginx.conf 配置文件如下:</p><blockquote><p>注:conf/nginx.conf 是 nginx 的默认配置文件。你也可以使用 nginx -c 指定你的配置文件 注意空格</p></blockquote><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">在这里插入代码片</span><br><span class="line">server{</span><br><span class="line"> listen: 80;</span><br><span class="line"> server_name: www.123.com</span><br><span class="line"> location / {</span><br><span class="line"> proxy_pass http://127.0.0.0.1:8080</span><br><span class="line"> index index.html index.htm;</span><br><span class="line"> }</span><br><span class="line"> }</span><br></pre></td></tr></table></figure><p>1.重启nginx服务<br>2更改 host:在 C:\Windows\System32\drivers\etc 目录下的 host 文件中添加一条 DNS 记录</p><blockquote><p>127.0.0.1 <a href="http://www.123.com/">www.123.com</a><br>**</p></blockquote><p> <em><strong>这样就是一个简单的http反向代理,可以直接通过<a href="http://www.123.com可以直接访问到该地址的端口号8080内容/">www.123.com可以直接访问到该地址的端口号8080内容</a></strong></em></p><h2 id="2-https反向代理"><a href="#2-https反向代理" class="headerlink" title="2.https反向代理"></a>2.https反向代理</h2><p>https的反向代理有http反向代理类型<br>https是在http协议上附加ssl协议的一种协议</p><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">在这里插入代码片 server{</span><br><span class="line"> #监听443端口。443为知名端口号,主要用于HTTPS协议</span><br><span class="line"> listen 443 ssl;</span><br><span class="line"> server_name: www.123.com</span><br><span class="line"> location / {</span><br><span class="line"> proxy_pass http://127.0.0.0.1:8080</span><br><span class="line"> index index.html index.htm ;</span><br><span class="line"> }</span><br><span class="line"> } </span><br></pre></td></tr></table></figure><p>除了端口不一样以外,其他与http步骤一致。</p><h2 id="3-location指令说明"><a href="#3-location指令说明" class="headerlink" title="3.location指令说明"></a>3.location指令说明</h2><p>该指令用于匹配 URL。</p><p>语法如下:<br><img src="https://img-blog.csdnimg.cn/9b97a75c99754c41b6e76914e40a0b33.png" alt="在这里插入图片描述"><br>1、= :用于不含正则表达式的 uri 前,要求请求字符串与 uri 严格匹配,如果匹配成功,就停止继续向下搜索并立即处理该请求。</p><p>2、~:用于表示 uri 包含正则表达式,并且区分大小写。</p><p>3、~*:用于表示 uri 包含正则表达式,并且不区分大小写。</p><p>4、^~:用于不含正则表达式的 uri 前,要求 Nginx 服务器找到标识 uri 和请求字符串匹配度最高的 location 后,立即使用此 location 处理请求,而不再使用 location 块中的正则 uri 和请求字符串做匹配。</p><p>注意:如果 uri 包含正则表达式,则必须要有 ~ 或者 ~* 标识。</p><h2 id="4-负载均衡"><a href="#4-负载均衡" class="headerlink" title="4.负载均衡"></a>4.负载均衡</h2><p>随着互联网信息的爆炸性增长,负载均衡(load balance)已经不再是一个很陌生的话题,顾名思义,负载均衡即是将负载分摊到不同的服务单元,既保证服务的可用性,又保证响应足够快,给用户很好的体验。快速增长的访问量和数据流量催生了各式各样的负载均衡产品,很多专业的负载均衡硬件提供了很好的功能,但却价格不菲,这使得负载均衡软件大受欢迎, nginx 就是其中的一个,在 linux 下有 Nginx、LVS、Haproxy 等等服务可以提供负载均衡服务,而且 Nginx 提供了几种分配方式(策略).</p><p>nginx.conf 配置如下:</p><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line">http {</span><br><span class="line"> #设定mime类型,类型由mime.type文件定义</span><br><span class="line"> include /etc/nginx/mime.types;</span><br><span class="line"> default_type application/octet-stream;</span><br><span class="line"> #设定日志格式</span><br><span class="line"> access_log /var/log/nginx/access.log;</span><br><span class="line"></span><br><span class="line"> #设定负载均衡的服务器列表</span><br><span class="line"> upstream load_balance_server {</span><br><span class="line"> #weigth参数表示权值,权值越高被分配到的几率越大</span><br><span class="line"> server 192.168.1.11:80 weight=5;</span><br><span class="line"> server 192.168.1.12:80 weight=1;</span><br><span class="line"> server 192.168.1.13:80 weight=6;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> #HTTP服务器</span><br><span class="line"> server {</span><br><span class="line"> #侦听80端口</span><br><span class="line"> listen 80;</span><br><span class="line"></span><br><span class="line"> #定义使用www.xx.com访问</span><br><span class="line"> server_name www.helloworld.com;</span><br><span class="line"></span><br><span class="line"> #对所有请求进行负载均衡请求</span><br><span class="line"> location / {</span><br><span class="line"> root /root; #定义服务器的默认网站根目录位置</span><br><span class="line"> index index.html index.htm; #定义首页索引文件的名称</span><br><span class="line"> proxy_pass http://load_balance_server ;#请求转向load_balance_server 定义的服务器列表</span><br><span class="line"></span><br><span class="line"> #以下是一些反向代理的配置(可选择性配置)</span><br><span class="line"> #proxy_redirect off;</span><br><span class="line"> proxy_set_header Host $host;</span><br><span class="line"> proxy_set_header X-Real-IP $remote_addr;</span><br><span class="line"> #后端的Web服务器可以通过X-Forwarded-For获取用户真实IP</span><br><span class="line"> proxy_set_header X-Forwarded-For $remote_addr;</span><br><span class="line"> proxy_connect_timeout 90; #nginx跟后端服务器连接超时时间(代理连接超时)</span><br><span class="line"> proxy_send_timeout 90; #后端服务器数据回传时间(代理发送超时)</span><br><span class="line"> proxy_read_timeout 90; #连接成功后,后端服务器响应时间(代理接收超时)</span><br><span class="line"> proxy_buffer_size 4k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小</span><br><span class="line"> proxy_buffers 4 32k; #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置</span><br><span class="line"> proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2)</span><br><span class="line"> proxy_temp_file_write_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传</span><br><span class="line"></span><br><span class="line"> client_max_body_size 10m; #允许客户端请求的最大单文件字节数</span><br><span class="line"> client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><strong>负载均衡的策略</strong><br>轮询(默认)<br>每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器 down 掉,能自动剔除。</p><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">upstream bck_testing_01 {</span><br><span class="line"> #默认所有服务器权重为 1</span><br><span class="line"> server 192.168.250.220:8080</span><br><span class="line"> server 192.168.250.221:8080</span><br><span class="line"> server 192.168.250.222:8080</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>加权轮询<br>weight 代表权,重默认为 1,权重越高被分配的客户端越多</p><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">upstream bck_testing_01 {</span><br><span class="line"> server 192.168.250.220:8080 weight=3</span><br><span class="line"> server 192.168.250.221:8080 # default weight=1</span><br><span class="line"> server 192.168.250.222:8080 # default weight=1</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>最少连接</p><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">upstream bck_testing_01 {</span><br><span class="line"> least_conn;</span><br><span class="line"></span><br><span class="line"> # with default weight for all (weight=1)</span><br><span class="line"> server 192.168.250.220:8080</span><br><span class="line"> server 192.168.250.221:8080</span><br><span class="line"> server 192.168.250.222:8080</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>加权最少连接</p><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">upstream bck_testing_01 {</span><br><span class="line"> least_conn;</span><br><span class="line"></span><br><span class="line"> server 192.168.250.220:8080 weight=3</span><br><span class="line"> server 192.168.250.221:8080 # default weight=1</span><br><span class="line"> server 192.168.250.222:8080 # default weight=1</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>IP Hash</p><p>每个请求按访问 ip 的 hash 结果分配,这样每个访客固定访问一个后端服务器,可以解决 session 的问题。</p><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">upstream bck_testing_01 {</span><br><span class="line"></span><br><span class="line"> ip_hash;</span><br><span class="line"></span><br><span class="line"> # with default weight for all (weight=1)</span><br><span class="line"> server 192.168.250.220:8080</span><br><span class="line"> server 192.168.250.221:8080</span><br><span class="line"> server 192.168.250.222:8080</span><br><span class="line"></span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>普通 Hash</p><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">upstream bck_testing_01 {</span><br><span class="line"></span><br><span class="line"> hash $request_uri;</span><br><span class="line"></span><br><span class="line"> #with default weight for all (weight=1)</span><br><span class="line"> server 192.168.250.220:8080</span><br><span class="line"> server 192.168.250.221:8080</span><br><span class="line"> server 192.168.250.222:8080</span><br><span class="line"></span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="5-网站有多个-webapp-的配置"><a href="#5-网站有多个-webapp-的配置" class="headerlink" title="5.网站有多个 webapp 的配置"></a>5.网站有多个 webapp 的配置</h2><p>当一个网站功能越来越丰富时,往往需要将一些功能相对独立的模块剥离出来,独立维护。这样的话,通常,会有多个 webapp。</p><p>举个例子:假如 <a href="http://www.helloworld.com/">www.helloworld.com</a> 站点有好几个 webapp,finance(金融)、product(产品)、admin(用户中心)。访问这些应用的方式通过上下文(context)来进行区分:</p><p><a href="http://www.helloworld.com/finance/">www.helloworld.com/finance/</a></p><p><a href="http://www.helloworld.com/product/">www.helloworld.com/product/</a></p><p><a href="http://www.helloworld.com/admin/">www.helloworld.com/admin/</a></p><p>我们知道,http 的默认端口号是 80,如果在一台服务器上同时启动这 3 个 webapp 应用,都用 80 端口,肯定是不成的。所以,这三个应用需要分别绑定不同的端口号。</p><p>那么,问题来了,用户在实际访问 <a href="http://www.helloworld.com/">www.helloworld.com</a> 站点时,访问不同 webapp,总不会还带着对应的端口号去访问吧。所以,你再次需要用到反向代理来做处理。</p><p>配置也不难,来看看怎么做吧:</p><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line">http {</span><br><span class="line">#此处省略一些基本配置</span><br><span class="line"></span><br><span class="line">upstream product_server{</span><br><span class="line">server www.helloworld.com:8081;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">upstream admin_server{</span><br><span class="line">server www.helloworld.com:8082;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">upstream finance_server{</span><br><span class="line">server www.helloworld.com:8083;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">server {</span><br><span class="line">#此处省略一些基本配置</span><br><span class="line">#默认指向product的server</span><br><span class="line">location / {</span><br><span class="line">proxy_pass http://product_server;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">location /product/{</span><br><span class="line">proxy_pass http://product_server;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">location /admin/ {</span><br><span class="line">proxy_pass http://admin_server;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">location /finance/ {</span><br><span class="line">proxy_pass http://finance_server;</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="5-动静分离"><a href="#5-动静分离" class="headerlink" title="5.动静分离"></a>5.动静分离</h2><p>Nginx 动静分离简单来说就是把动态跟静态请求分开,不能理解成只是单纯的把动态页面和静态页面物理分离。严格意义上说应该是动态请求跟静态请求分开,可以理解成使用 Nginx 处理静态页面,Tomcat 处理动态页面。动静分离从目前实现角度来讲大致分为两种,</p><p>一种是纯粹把静态文件独立成单独的域名,放在独立的服务器上,也是目前主流推崇的方案;</p><p>另外一种方法就是动态跟静态文件混合在一起发布,通过 nginx 来分开。</p><p>通过 location 指定不同的后缀名实现不同的请求转发。通过 expires 参数设置,可以使浏览器缓存过期时间,减少与服务器之前的请求和流量。具体 Expires 定义:是给一个资源设定一个过期时间,也就是说无需去服务端验证,直接通过浏览器自身确认是否过期即可,所以不会产生额外的流量。此种方法非常适合不经常变动的资源。(如果经常更新的文件,不建议使用 Expires 来缓存),我这里设置 3d,表示在这 3 天之内访问这个 URL,发送一个请求,比对服务器该文件最后更新时间没有变化,则不会从服务器抓取,返回状态码304,如果有修改,则直接从服务器重新下载,返回状态码 200。</p><p>1.项目资源准备</p><p>2.进行 nginx 配置</p><p>3.找到 nginx 安装目录,打开/conf/nginx.conf 配置文件</p><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">server{</span><br><span class="line"> listen: 80;</span><br><span class="line"> server_name: www.123.com</span><br><span class="line"> location /www/ {</span><br><span class="line"> root /data/;</span><br><span class="line"> index index.html index.htm;</span><br><span class="line"> }</span><br><span class="line"> location /image/ {</span><br><span class="line"> root /data/;</span><br><span class="line"> autoindex on;</span><br><span class="line"> }</span><br><span class="line"> }</span><br></pre></td></tr></table></figure><p>添加监听端口,访问名字。</p><p>重点是添加 location,</p><p>最后检查 Nginx 配置是否正确即可,然后测试动静分离是否成功,之需要删除后端 tomcat 服务器上的某个静态文件,查看是否能访问,如果可以访问说明静态资源 nginx 直接返回了,不走后端 tomcat 服务器</p><h2 id="6-高可用"><a href="#6-高可用" class="headerlink" title="6.高可用"></a>6.高可用</h2><p><a href="https://zhuanlan.zhihu.com/p/391153042">Nginx高可用详细文章</a></p><h2 id="7-解决跨域问题"><a href="#7-解决跨域问题" class="headerlink" title="7.解决跨域问题"></a>7.解决跨域问题</h2><p>web 领域开发中,经常采用前后端分离模式。这种模式下,前端和后端分别是独立的 web 应用程序,例如:后端是 Java 程序,前端是 React 或 Vue 应用。</p><p>各自独立的 web app 在互相访问时,势必存在跨域问题。解决跨域问题一般有两种思路:</p><p>1.CORS<br>在后端服务器设置 HTTP 响应头,把你需要允许访问的域名加入 Access-Control-Allow-Origin 中。</p><p>2.jsonp<br>把后端根据请求,构造 json 数据,并返回,前端用 jsonp 跨域。</p><p>这两种思路,本文不展开讨论。</p><p>需要说明的是,nginx 根据第一种思路,也提供了一种解决跨域的解决方案。</p><p>举例:<a href="http://www.helloworld.com/">www.helloworld.com</a> 网站是由一个前端 app ,一个后端 app 组成的。前端端口号为 9000, 后端端口号为 8080。</p><p>前端和后端如果使用 http 进行交互时,请求会被拒绝,因为存在跨域问题。来看看,nginx 是怎么解决的吧:</p><p>首先,在 enable-cors.conf 文件中设置 cors :</p><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"># allow origin list</span><br><span class="line">set $ACAO '*';</span><br><span class="line"></span><br><span class="line"># set single origin</span><br><span class="line">if ($http_origin ~* (www.helloworld.com)$) {</span><br><span class="line"> set $ACAO $http_origin;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">if ($cors = "trueget") {</span><br><span class="line">add_header 'Access-Control-Allow-Origin' "$http_origin";</span><br><span class="line">add_header 'Access-Control-Allow-Credentials' 'true';</span><br><span class="line">add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';</span><br><span class="line">add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">if ($request_method = 'OPTIONS') {</span><br><span class="line"> set $cors "${cors}options";</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">if ($request_method = 'GET') {</span><br><span class="line"> set $cors "${cors}get";</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">if ($request_method = 'POST') {</span><br><span class="line"> set $cors "${cors}post";</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>接下来,在你的服务器中 include enable-cors.conf 来引入跨域配置:</p><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"># ----------------------------------------------------</span><br><span class="line"># 此文件为项目 nginx 配置片段</span><br><span class="line"># 可以直接在 nginx config 中 include(推荐)</span><br><span class="line"># 或者 copy 到现有 nginx 中,自行配置</span><br><span class="line"># www.helloworld.com 域名需配合 dns hosts 进行配置</span><br><span class="line"># 其中,api 开启了 cors,需配合本目录下另一份配置文件</span><br><span class="line"># ----------------------------------------------------</span><br><span class="line">upstream front_server{</span><br><span class="line"> server www.helloworld.com:9000;</span><br><span class="line">}</span><br><span class="line">upstream api_server{</span><br><span class="line"> server www.helloworld.com:8080;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">server {</span><br><span class="line"> listen 80;</span><br><span class="line"> server_name www.helloworld.com;</span><br><span class="line"></span><br><span class="line"> location ~ ^/api/ {</span><br><span class="line"> include enable-cors.conf;</span><br><span class="line"> proxy_pass http://api_server;</span><br><span class="line"> rewrite "^/api/(.*)$" /$1 break;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> location ~ ^/ {</span><br><span class="line"> proxy_pass http://front_server;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h1 id="四-nginx原理解析"><a href="#四-nginx原理解析" class="headerlink" title="四.nginx原理解析"></a>四.nginx原理解析</h1><p><a href="https://www.cnblogs.com/xiangsikai/p/8438772.html">nginx原理解析</a></p><hr><h1 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h1><p>花有重开之日,人无少年之时。</p>]]></content>
<summary type="html"><blockquote>
<p>提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档</p>
</blockquote>
<p>@<a href="%E6%96%87%E7%AB%A0%E7%9B%AE%E5%BD%95">TOC</a></p>
<hr>
<h1 i</summary>
<category term="nginx" scheme="http://example.com/tags/nginx/"/>
</entry>
</feed>