-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
589 lines (395 loc) · 44.3 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
<!DOCTYPE html>
<html>
<head><meta name="generator" content="Hexo 3.8.0">
<!--[if lt IE 9]>
<style>body {display: none; background: none !important} </style>
<meta http-equiv="Refresh" Content="0; url=//outdatedbrowser.com/" />
<![endif]-->
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<meta name="format-detection" content="telephone=no">
<meta name="author" content="freekeeper">
<meta property="og:type" content="website">
<meta property="og:title" content="Hexo">
<meta property="og:url" content="http://yoursite.com/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:locale" content="default">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Hexo">
<link rel="apple-touch-icon" href="/apple-touch-icon.png">
<link rel="alternate" href="/atom.xml" title="Hexo" type="application/atom+xml">
<link rel="shortcut icon" href="/favicon.png">
<link href="//cdn.bootcss.com/animate.css/3.5.1/animate.min.css" rel="stylesheet">
<link href="//cdn.bootcss.com/fancybox/2.1.5/jquery.fancybox.min.css" rel="stylesheet">
<script src="//cdn.bootcss.com/pace/1.0.2/pace.min.js"></script>
<link href="//cdn.bootcss.com/pace/1.0.2/themes/blue/pace-theme-minimal.css" rel="stylesheet">
<link rel="stylesheet" href="/css/style.css">
<style> .article { opacity: 0;} </style>
<link href="//cdn.bootcss.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet">
<title>Hexo</title>
<script src="//cdn.bootcss.com/jquery/2.2.4/jquery.min.js"></script>
<script src="//cdn.bootcss.com/clipboard.js/1.5.10/clipboard.min.js"></script>
<script>
var yiliaConfig = {
fancybox: true,
animate: true,
isHome: true,
isPost: false,
isArchive: false,
isTag: false,
isCategory: false,
fancybox_js: "//cdn.bootcss.com/fancybox/2.1.5/jquery.fancybox.min.js",
scrollreveal: "//cdn.bootcss.com/scrollReveal.js/3.1.4/scrollreveal.min.js",
search:
}
</script>
<script> yiliaConfig.jquery_ui = [false]; </script>
<script> yiliaConfig.rootUrl = "\/";</script>
</head></html>
<body>
<div id="container">
<div class="left-col">
<div class="overlay"></div>
<div class="intrude-less">
<header id="header" class="inner">
<a href="/" class="profilepic">
<img src="/img/touxiang.png" class="animated zoomIn">
</a>
<hgroup>
<h1 class="header-author"><a href="/">freekeeper</a></h1>
</hgroup>
<div id="switch-btn" class="switch-btn">
<div class="icon">
<div class="icon-ctn">
<div class="icon-wrap icon-house" data-idx="0">
<div class="birdhouse"></div>
<div class="birdhouse_holes"></div>
</div>
<div class="icon-wrap icon-ribbon hide" data-idx="1">
<div class="ribbon"></div>
</div>
<div class="icon-wrap icon-link hide" data-idx="2">
<div class="loopback_l"></div>
<div class="loopback_r"></div>
</div>
<div class="icon-wrap icon-me hide" data-idx="3">
<div class="user"></div>
<div class="shoulder"></div>
</div>
</div>
</div>
<div class="tips-box hide">
<div class="tips-arrow"></div>
<ul class="tips-inner">
<li>菜单</li>
<li>标签</li>
<li>友情链接</li>
<li>关于我</li>
</ul>
</div>
</div>
<div id="switch-area" class="switch-area">
<div class="switch-wrap">
<section class="switch-part switch-part1">
<nav class="header-menu">
<ul>
<li><a href="/">主页</a></li>
<li><a href="/archives/">所有文章</a></li>
<li><a href="/tags/">标签云</a></li>
<li><a href="/about/">关于我</a></li>
</ul>
</nav>
<nav class="header-nav">
<ul class="social">
<a class="fa Email" href="mailto:[email protected]" title="Email"></a>
<a class="fa GitHub" href="#" title="GitHub"></a>
<a class="fa RSS" href="/atom.xml" title="RSS"></a>
</ul>
</nav>
</section>
<section class="switch-part switch-part2">
<div class="widget tagcloud" id="js-tagcloud">
</div>
</section>
<section class="switch-part switch-part3">
<div id="js-friends">
<a class="main-nav-link switch-friends-link" href="https://hexo.io">Hexo</a>
<a class="main-nav-link switch-friends-link" href="https://pages.github.com/">GitHub</a>
<a class="main-nav-link switch-friends-link" href="http://moxfive.xyz/">MOxFIVE</a>
</div>
</section>
<section class="switch-part switch-part4">
<div id="js-aboutme">专注于前端</div>
</section>
</div>
</div>
</header>
</div>
</div>
<div class="mid-col">
<nav id="mobile-nav">
<div class="overlay">
<div class="slider-trigger"></div>
<h1 class="header-author js-mobile-header hide"><a href="/" title="回到主页">freekeeper</a></h1>
</div>
<div class="intrude-less">
<header id="header" class="inner">
<a href="/" class="profilepic">
<img src="/img/touxiang.png" class="animated zoomIn">
</a>
<hgroup>
<h1 class="header-author"><a href="/" title="回到主页">freekeeper</a></h1>
</hgroup>
<nav class="header-menu">
<ul>
<li><a href="/">主页</a></li>
<li><a href="/archives/">所有文章</a></li>
<li><a href="/tags/">标签云</a></li>
<li><a href="/about/">关于我</a></li>
<div class="clearfix"></div>
</ul>
</nav>
<nav class="header-nav">
<ul class="social">
<a class="fa Email" target="_blank" href="mailto:[email protected]" title="Email"></a>
<a class="fa GitHub" target="_blank" href="#" title="GitHub"></a>
<a class="fa RSS" target="_blank" href="/atom.xml" title="RSS"></a>
</ul>
</nav>
</header>
</div>
<link class="menu-list" tags="标签" friends="友情链接" about="关于我">
</nav>
<div class="body-wrap">
<article id="post-Spring-Boot-JPA" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/2019/03/31/Spring-Boot-JPA/" class="article-date">
<time datetime="2019-03-31T08:54:46.000Z" itemprop="datePublished">2019-03-31</time>
</a>
</div>
<div class="article-inner">
<input type="hidden" class="isFancy">
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2019/03/31/Spring-Boot-JPA/">Spring Boot JPA</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<h3 id="一-构建SrpingBoot项目"><a href="#一-构建SrpingBoot项目" class="headerlink" title="一. 构建SrpingBoot项目"></a>一. 构建SrpingBoot项目</h3><h4 id="1-通过IDEA创建Boot项目-File-gt-New-gt-Project"><a href="#1-通过IDEA创建Boot项目-File-gt-New-gt-Project" class="headerlink" title="1. 通过IDEA创建Boot项目(File->New->Project)"></a>1. 通过IDEA创建Boot项目(File->New->Project)</h4><p><img src="/2019/03/31/Spring-Boot-JPA/01.gif" alt="01"></p>
<h4 id="2-选择需要的依赖:web、jpa、mysql"><a href="#2-选择需要的依赖:web、jpa、mysql" class="headerlink" title="2. 选择需要的依赖:web、jpa、mysql"></a>2. 选择需要的依赖:web、jpa、mysql</h4><p><img src="/2019/03/31/Spring-Boot-JPA/02.gif" alt="02"></p>
<blockquote>
<p>默认官网的源速度感人,可替换为阿里的源,使用附件中的setting文件。</p>
<p><img src="/2019/03/31/Spring-Boot-JPA/1527291694852.png" alt="1527291694852"></p>
</blockquote>
<h4 id="3-未使用IDEA或MyEclipse-等IDE的同学,可以通过官方网站创建项目"><a href="#3-未使用IDEA或MyEclipse-等IDE的同学,可以通过官方网站创建项目" class="headerlink" title="3. 未使用IDEA或MyEclipse 等IDE的同学,可以通过官方网站创建项目"></a>3. 未使用IDEA或MyEclipse 等IDE的同学,可以通过官方网站创建项目</h4><ul>
<li><p>打开URL: start.spring.io,同样输入项目相关信息及所需的依赖</p>
<p><img src="/2019/03/31/Spring-Boot-JPA/03.gif" alt="03"></p>
</li>
<li><p>下载解压后通过Maven方式导入(由于项目为Maven项目,所以必须已经安装Maven环境)</p>
<p><img src="/2019/03/31/Spring-Boot-JPA/04.gif" alt="04"></p>
</li>
</ul>
<h4 id="4-进行项目相关配置"><a href="#4-进行项目相关配置" class="headerlink" title="4. 进行项目相关配置"></a>4. 进行项目相关配置</h4><ul>
<li><p>修改配置文件类型为yml(可选)</p>
<p><img src="/2019/03/31/Spring-Boot-JPA/05.gif" alt="05"></p>
</li>
<li><p>配置项目相关信息:数据库连接信息、JPA SQL语句输出、日志打印级别;也可以在这设置项目端口、路径等</p>
<p><img src="/2019/03/31/Spring-Boot-JPA/06.gif" alt="06"></p>
</li>
</ul>
<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><span class="line">10</span><br><span class="line">11</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"> driver-class-name:</span> <span class="string">com.mysql.jdbc.Driver</span></span><br><span class="line"><span class="attr"> username:</span> <span class="string">root</span></span><br><span class="line"><span class="attr"> password:</span> <span class="string">root</span></span><br><span class="line"><span class="attr"> url:</span> <span class="attr">jdbc:mysql://localhost:3306/book?useSSL=false</span></span><br><span class="line"><span class="attr"> jpa:</span></span><br><span class="line"><span class="attr"> show-sql:</span> <span class="literal">true</span></span><br><span class="line"><span class="attr">logging:</span></span><br><span class="line"><span class="attr"> level:</span></span><br><span class="line"> <span class="string">cn.edu.ncu.bootjpademo:</span> <span class="string">debug</span></span><br></pre></td></tr></table></figure>
<p>jpa.show-sql: true 运行时输出SQL,方便进行调试。</p>
<p>logging.level 可以指定不同包的日志输出级别。</p>
<h3 id="二-编码实现"><a href="#二-编码实现" class="headerlink" title="二. 编码实现"></a>二. 编码实现</h3><h4 id="1-创建相应的package:entity、dao、service、controller"><a href="#1-创建相应的package:entity、dao、service、controller" class="headerlink" title="1. 创建相应的package:entity、dao、service、controller"></a>1. 创建相应的package:entity、dao、service、controller</h4><p><img src="/2019/03/31/Spring-Boot-JPA/1527289497629.png" alt="1527289497629"></p>
<h4 id="2-书籍分类查询功能"><a href="#2-书籍分类查询功能" class="headerlink" title="2. 书籍分类查询功能"></a>2. 书籍分类查询功能</h4><ul>
<li><p>按照表结构创建分类entity,并通过idea自动生成getter、setter及toString方法</p>
<ul>
<li>类标记注解为@Entity,并在主键(@Id)和自增字段(@GeneratedValue)上添加相应的注解</li>
</ul>
<p><img src="/2019/03/31/Spring-Boot-JPA/1527298927655.png" alt="1527298927655"></p>
</li>
<li><p>创建dao接口,接口类 extends JpaRepository<T,ID>,T和ID分别为相应的实体和主键类型</p>
<p><img src="/2019/03/31/Spring-Boot-JPA/1527292519397.png" alt="1527292519397"></p>
</li>
<li><p>创建相应的service</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></pre></td><td class="code"><pre><span class="line"><span class="meta">@Service</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">BookCategoryService</span> </span>{</span><br><span class="line"> <span class="meta">@Autowired</span></span><br><span class="line"> <span class="keyword">private</span> BookCategoryDao bookCategoryDao;</span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> <span class="doctag">@description</span> 查询所有分类信息</span></span><br><span class="line"><span class="comment"> <span class="doctag">@param</span></span></span><br><span class="line"><span class="comment"> <span class="doctag">@return</span> List<BookCategory></span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> List<BookCategory> <span class="title">findAll</span><span class="params">()</span></span>{</span><br><span class="line"> <span class="keyword">return</span> bookCategoryDao.findAll();</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
</li>
</ul>
<ul>
<li>创建service的单元测试方法<br><img src="/2019/03/31/Spring-Boot-JPA/test.gif" alt="test"></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RunWith</span>(SpringRunner.class)</span><br><span class="line"><span class="meta">@SpringBootTest</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">BookCategoryServiceTest</span> </span>{</span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">final</span> Logger logger =</span><br><span class="line"> LoggerFactory.getLogger(<span class="keyword">this</span>.getClass());</span><br><span class="line"> <span class="meta">@Autowired</span></span><br><span class="line"> <span class="keyword">private</span> BookCategoryService service;</span><br><span class="line"> <span class="meta">@Test</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">findAll</span><span class="params">()</span> </span>{</span><br><span class="line"> logger.debug(service.findAll().toString());</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<ul>
<li><p>运行测试方法,观察日志输入是否正常(可先在数据库表中添加测试数据)</p>
</li>
<li><p>创建controller类(<strong>BookCategoryController</strong>)及方法(<strong>findAl</strong>l)</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></pre></td><td class="code"><pre><span class="line"><span class="meta">@RestController</span></span><br><span class="line"><span class="meta">@RequestMapping</span>(value = <span class="string">"category"</span>)</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">BookCategoryController</span> </span>{</span><br><span class="line"> <span class="meta">@Autowired</span></span><br><span class="line"> <span class="keyword">private</span> BookCategoryService service;</span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> <span class="doctag">@Description</span> 获取全部分类信息</span></span><br><span class="line"><span class="comment"> <span class="doctag">@Param</span></span></span><br><span class="line"><span class="comment"> <span class="doctag">@Return</span> List<BookCategory></span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="meta">@RequestMapping</span>(value = <span class="string">""</span>,method = RequestMethod.GET)</span><br><span class="line"> <span class="function"><span class="keyword">public</span> List<BookCategory> <span class="title">findAll</span><span class="params">()</span></span>{</span><br><span class="line"> <span class="keyword">return</span> service.findAll();</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
</li>
</ul>
<ul>
<li><p>运行整个项目,并使用postman访问(get方法也可以通过浏览器访问)</p>
<p><img src="/2019/03/31/Spring-Boot-JPA/run.gif" alt="run"></p>
<p>Postman获取方式:(<a href="https://www.getpostman.com/" target="_blank" rel="noopener">https://www.getpostman.com/</a> 或者 添加chrome插件方式获取)</p>
<p><img src="/2019/03/31/Spring-Boot-JPA/1527295750988.png" alt="1527295750988"></p>
</li>
</ul>
<h4 id="3-insert功能"><a href="#3-insert功能" class="headerlink" title="3.insert功能"></a>3.insert功能</h4><ul>
<li>简单的CRUD功能JPA已经提供,不需要手工完成dao的实现类</li>
</ul>
<ul>
<li><p>dao不用修改,直接在service(<strong>BookServiceService</strong>)中添加插入功能(<strong>saveBookCategory</strong>)</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></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"><span class="doctag">@description</span> 添加书籍分类信息</span></span><br><span class="line"><span class="comment"><span class="doctag">@param</span> bookCategory</span></span><br><span class="line"><span class="comment"><span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">saveBookCategory</span><span class="params">(BookCategory bookCategory)</span> </span>{</span><br><span class="line"> bookCategoryDao.save(bookCategory);</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
</li>
<li><p>创建测试方法</p>
<ul>
<li>因为JPA是懒加载,所以需要在测试类上加上注解@Transactional</li>
</ul>
<p><img src="/2019/03/31/Spring-Boot-JPA/1527299391349.png" alt="1527299391349"></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></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">save</span><span class="params">()</span> </span>{</span><br><span class="line"> BookCategory bookCategory = <span class="keyword">new</span> BookCategory();</span><br><span class="line"> Timestamp t = <span class="keyword">new</span> Timestamp(System.currentTimeMillis());</span><br><span class="line"> bookCategory.setCategoryName(<span class="string">"科技"</span>);</span><br><span class="line"> bookCategory.setCreateTime(t);</span><br><span class="line"> bookCategory.setUpdateTime(t);</span><br><span class="line"> service.saveBookCategory(bookCategory);</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
</li>
</ul>
<ul>
<li><p>执行测试,查看结果(由于添加@Transactional 之后在测试类中并不会commit,所以数据库中不会有新增记录)</p>
</li>
<li><p>添加contorller方法</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></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"><span class="doctag">@description</span> 通过POST获取数据,并添加分类信息</span></span><br><span class="line"><span class="comment"><span class="doctag">@param</span> bookCategory</span></span><br><span class="line"><span class="comment"><span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="meta">@PostMapping</span>(<span class="string">""</span>)</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">saveBookCategory</span><span class="params">(BookCategory bookCategory)</span></span>{</span><br><span class="line"> service.saveBookCategory(bookCategory);</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>使用@PostMapping 等于 @RequestMapping(value,method = RequestMethod.POST)</p>
</li>
<li><p>启动项目,并使用Postman进行测试</p>
<p><img src="/2019/03/31/Spring-Boot-JPA/1527306639192.png" alt="1527306639192"></p>
<blockquote>
<p>时间格式为:yyyy-mm-dd hh:mm:ss</p>
</blockquote>
<p>查看数据库中数据,并观察控制台的输出。</p>
<p><strong>思考题:JPA如何将post中的数据存入数据库的?</strong></p>
<p><strong>自己动手完成删除、更新功能</strong></p>
</li>
</ul>
<h3 id="三-手动映射及SQL"><a href="#三-手动映射及SQL" class="headerlink" title="三. 手动映射及SQL"></a>三. 手动映射及SQL</h3><h4 id="1-表和字段"><a href="#1-表和字段" class="headerlink" title="1. 表和字段"></a>1. 表和字段</h4><ul>
<li><p>建立一个非驼峰风格表</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></pre></td><td class="code"><pre><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> <span class="string">`book`</span> (</span><br><span class="line"> <span class="string">`id`</span> <span class="built_in">varchar</span>(<span class="number">40</span>) <span class="keyword">NOT</span> <span class="literal">NULL</span>,</span><br><span class="line"> <span class="string">`name`</span> <span class="built_in">varchar</span>(<span class="number">100</span>) <span class="keyword">NOT</span> <span class="literal">NULL</span>,</span><br><span class="line"> <span class="string">`author`</span> <span class="built_in">varchar</span>(<span class="number">100</span>) <span class="keyword">NOT</span> <span class="literal">NULL</span>,</span><br><span class="line"> PRIMARY <span class="keyword">KEY</span> (<span class="string">`id`</span>))</span><br></pre></td></tr></table></figure>
</li>
<li><p>建立entity(记得生成getter、setter方法)</p>
</li>
</ul>
<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></pre></td><td class="code"><pre><span class="line"><span class="meta">@Entity</span></span><br><span class="line"><span class="meta">@Table</span>(name = <span class="string">"book"</span>)</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Book</span> </span>{</span><br><span class="line"> <span class="meta">@Id</span></span><br><span class="line"> <span class="meta">@Column</span>(name = <span class="string">"id"</span>)</span><br><span class="line"> <span class="keyword">private</span> String id;</span><br><span class="line"></span><br><span class="line"> <span class="meta">@Column</span>(name = <span class="string">"name"</span>)</span><br><span class="line"> <span class="keyword">private</span> String name;</span><br><span class="line"></span><br><span class="line"> <span class="meta">@Column</span>(name = <span class="string">"author"</span>)</span><br><span class="line"> <span class="keyword">private</span> String author;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<ul>
<li>创建dao</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Repository</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">BookDao</span> <span class="keyword">extends</span> <span class="title">JpaRepository</span><<span class="title">Book</span>,<span class="title">String</span>></span>{</span><br><span class="line"> <span class="meta">@Query</span>(value = <span class="string">"select * from book where id < ?"</span>,nativeQuery = <span class="keyword">true</span>)</span><br><span class="line"> <span class="function">List<Book> <span class="title">queryBook</span><span class="params">(String id)</span></span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<ul>
<li>创建service</li>
</ul>
<figure class="highlight java"><table><tr><td class="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="meta">@Service</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">BookService</span> </span>{</span><br><span class="line"> <span class="meta">@Autowired</span></span><br><span class="line"> <span class="keyword">private</span> BookDao dao;</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> List<Book> <span class="title">queryBook</span><span class="params">(String id)</span></span>{</span><br><span class="line"> <span class="keyword">return</span> dao.queryBook(id);</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<ul>
<li>进行单元测试,观察输出结果是否正确。</li>
</ul>
<p><strong>自己创建controller类及相应方法,并使用Postman或浏览器访问路径,并打印结果</strong></p>
<h3 id="四-文件上传"><a href="#四-文件上传" class="headerlink" title="四. 文件上传"></a>四. 文件上传</h3><h4 id="1-建立上传文件夹"><a href="#1-建立上传文件夹" class="headerlink" title="1. 建立上传文件夹"></a>1. 建立上传文件夹</h4><p><img src="/2019/03/31/Spring-Boot-JPA/1527312215520.png" alt="1527312215520"></p>
<p>建立 upload/images 文件夹</p>
<h4 id="2-在controller-package下创建-UploadFileController类"><a href="#2-在controller-package下创建-UploadFileController类" class="headerlink" title="2. 在controller package下创建 UploadFileController类"></a>2. 在controller package下创建 UploadFileController类</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RestController</span></span><br><span class="line"><span class="meta">@RequestMapping</span>(<span class="string">"/file"</span>)</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UploadFileController</span> </span>{</span><br><span class="line"> </span><br><span class="line"> <span class="meta">@PostMapping</span>(<span class="string">"/images"</span>)</span><br><span class="line"> <span class="function"><span class="keyword">public</span> String <span class="title">singleFileUpload</span><span class="params">(@RequestParam(<span class="string">"file"</span>)</span> MultipartFile file) </span>{</span><br><span class="line"> <span class="keyword">if</span> (file.isEmpty()) {</span><br><span class="line"> <span class="keyword">return</span> <span class="string">"请选择上传文件"</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">try</span> { </span><br><span class="line"> <span class="keyword">byte</span>[] bytes = file.getBytes();</span><br><span class="line"> Path path = Paths.get(<span class="string">"upload/images/"</span>+file.getOriginalFilename());</span><br><span class="line"> Files.write(path, bytes);</span><br><span class="line"> } <span class="keyword">catch</span> (IOException e) {</span><br><span class="line"> e.printStackTrace();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="string">"上传成功"</span>;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h4 id="3-启动项目,使用Postman进行测试"><a href="#3-启动项目,使用Postman进行测试" class="headerlink" title="3. 启动项目,使用Postman进行测试"></a>3. 启动项目,使用Postman进行测试</h4><p><img src="/2019/03/31/Spring-Boot-JPA/1527312710438.png" alt="1527312710438"></p>
<p>URL:<a href="http://localhost:8080/file/images" target="_blank" rel="noopener">http://localhost:8080/file/images</a> </p>
<h4 id="4-也可以启动Nginx或Apache等Web服务器,编写html页面进行测试(也可以直接打开)"><a href="#4-也可以启动Nginx或Apache等Web服务器,编写html页面进行测试(也可以直接打开)" class="headerlink" title="4. 也可以启动Nginx或Apache等Web服务器,编写html页面进行测试(也可以直接打开)"></a>4. 也可以启动Nginx或Apache等Web服务器,编写html页面进行测试(也可以直接打开)</h4><figure class="highlight html"><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 class="meta"><!DOCTYPE html></span></span><br><span class="line"><span class="tag"><<span class="name">html</span>></span></span><br><span class="line"><span class="tag"><<span class="name">head</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"UTF-8"</span>></span></span><br><span class="line"><span class="tag"></<span class="name">head</span>></span></span><br><span class="line"><span class="tag"><<span class="name">body</span>></span></span><br><span class="line"><span class="tag"><<span class="name">h1</span>></span>Spring Boot file upload example<span class="tag"></<span class="name">h1</span>></span></span><br><span class="line"><span class="tag"><<span class="name">form</span> <span class="attr">method</span>=<span class="string">"POST"</span> <span class="attr">action</span>=<span class="string">"http://localhost:8080/file/images"</span> <span class="attr">enctype</span>=<span class="string">"multipart/form-data"</span> <span class="attr">accept-charset</span>=<span class="string">"UTF-8"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">input</span> <span class="attr">type</span>=<span class="string">"file"</span> <span class="attr">name</span>=<span class="string">"file"</span> /></span><span class="tag"><<span class="name">br</span>/></span><span class="tag"><<span class="name">br</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">input</span> <span class="attr">type</span>=<span class="string">"submit"</span> <span class="attr">value</span>=<span class="string">"Submit"</span> /></span></span><br><span class="line"><span class="tag"></<span class="name">form</span>></span></span><br><span class="line"><span class="tag"></<span class="name">body</span>></span></span><br><span class="line"><span class="tag"></<span class="name">html</span>></span></span><br></pre></td></tr></table></figure>
<p><img src="/2019/03/31/Spring-Boot-JPA/upload.gif" alt="upload"></p>
<p><img src="/2019/03/31/Spring-Boot-JPA/1527313136713.png" alt="1527313136713"></p>
</div>
<div class="article-info article-info-index">
<div class="clearfix"></div>
</div>
</div>
</article>
<article id="post-hello-world" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/2019/03/31/hello-world/" class="article-date">
<time datetime="2019-03-31T05:00:18.997Z" itemprop="datePublished">2019-03-31</time>
</a>
</div>
<div class="article-inner">
<input type="hidden" class="isFancy">
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2019/03/31/hello-world/">Hello World</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<p>Welcome to <a href="https://hexo.io/" target="_blank" rel="noopener">Hexo</a>! This is your very first post. Check <a href="https://hexo.io/docs/" target="_blank" rel="noopener">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" target="_blank" rel="noopener">troubleshooting</a> or you can ask me on <a href="https://github.com/hexojs/hexo/issues" target="_blank" rel="noopener">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" target="_blank" rel="noopener">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" target="_blank" rel="noopener">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" target="_blank" rel="noopener">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/deployment.html" target="_blank" rel="noopener">Deployment</a></p>
</div>
<div class="article-info article-info-index">
<div class="clearfix"></div>
</div>
</div>
</article>
</div>
<footer id="footer">
<div class="outer">
<div id="footer-info">
<div class="footer-left">
<i class="fa fa-copyright"></i>
2016-2019 freekeeper
</div>
<div class="footer-right">
<a href="http://hexo.io/" target="_blank" title="快速、简洁且高效的博客框架">Hexo</a> Theme <a href="https://github.com/MOxFIVE/hexo-theme-yelee" target="_blank" title="简而不减 Hexo 双栏博客主题 v3.5">Yelee</a> by MOxFIVE <i class="fa fa-heart animated infinite pulse"></i>
</div>
</div>
<div class="visit">
<span id="busuanzi_container_site_pv" style="display:none">
<span id="site-visit" title="本站到访数"><i class="fa fa-user" aria-hidden="true"></i><span id="busuanzi_value_site_uv"></span>
</span>
</span>
<span>| </span>
<span id="busuanzi_container_page_pv" style="display:none">
<span id="page-visit" title="本页阅读量"><i class="fa fa-eye animated infinite pulse" aria-hidden="true"></i><span id="busuanzi_value_page_pv"></span>
</span>
</span>
</div>
</div>
</footer>
</div>
<script data-main="/js/main.js" src="//cdn.bootcss.com/require.js/2.2.0/require.min.js"></script>
<script>
$(document).ready(function() {
var iPad = window.navigator.userAgent.indexOf('iPad');
if (iPad > -1 || $(".left-col").css("display") === "none") {
var bgColorList = ["#9db3f4", "#414141", "#e5a859", "#f5dfc6", "#c084a0", "#847e72", "#cd8390", "#996731"];
var bgColor = Math.ceil(Math.random() * (bgColorList.length - 1));
$("body").css({"background-color": bgColorList[bgColor], "background-size": "cover"});
}
else {
var backgroundnum = 5;
var backgroundimg = "url(/background/bg-x.jpg)".replace(/x/gi, Math.ceil(Math.random() * backgroundnum));
$("body").css({"background": backgroundimg, "background-attachment": "fixed", "background-size": "cover"});
}
})
</script>
<div class="scroll" id="scroll">
<a href="#" title="返回顶部"><i class="fa fa-arrow-up"></i></a>
<a href="#comments" onclick="load$hide();" title="查看评论"><i class="fa fa-comments-o"></i></a>
<a href="#footer" title="转到底部"><i class="fa fa-arrow-down"></i></a>
</div>
<script>
// Open in New Window
var oOpenInNew = {
archives: ".archive-article-title",
miniArchives: "a.post-list-link",
friends: "#js-friends a",
socail: ".social a"
}
for (var x in oOpenInNew) {
$(oOpenInNew[x]).attr("target", "_blank");
}
</script>
<script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js">
</script>
</div>
</body>
</html>