本文演示了springboot下,實(shí)用spring-data-jpa來實(shí)現(xiàn)crud操作,視圖層采用freemarker
這里我們先把a(bǔ)pplication.properties修改成application.yml 主流格式
內(nèi)容也改成yml規(guī)范格式:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
server: port: 8888 context-path: / helloworld: spring boot\u4f60\u597d msyql: jdbcname: com.mysql.jdbc.driver dburl: jdbc:mysql: //localhost:3306/db_diary username: root password: 123456 spring: datasource: driver- class -name: com.mysql.jdbc.driver url: jdbc:mysql: //localhost:3306/db_book username: root password: passwd jpa: hibernate.ddl-auto: update show-sql: true |
yml格式有個(gè)注意點(diǎn) 冒號(hào)后面一定要加個(gè)空格
還有我們把context-path改成/方便開發(fā)應(yīng)用
先寫一個(gè)bookdao接口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
package com.hik.dao; import org.springframework.data.jpa.repository.jparepository; import com.hik.entity.book; /** * 圖書dao接口 * @author jed * */ public interface bookdao extends jparepository<book, integer>{ } |
要求實(shí)現(xiàn)jparepository,jparepository是繼承pagingandsortingrepository,pagingandsortingrepository是繼承crudrepository。crudrepository實(shí)現(xiàn)了實(shí)體增刪改查操作
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
|
/* * copyright 2008-2011 the original author or authors. * * licensed under the apache license, version 2.0 (the "license"); * you may not use this file except in compliance with the license. * you may obtain a copy of the license at * * http://www.apache.org/licenses/license-2.0 * * unless required by applicable law or agreed to in writing, software * distributed under the license is distributed on an "as is" basis, * without warranties or conditions of any kind, either express or implied. * see the license for the specific language governing permissions and * limitations under the license. */ package org.springframework.data.repository; import java.io.serializable; /** * interface for generic crud operations on a repository for a specific type. * * @author oliver gierke * @author eberhard wolff */ @norepositorybean public interface crudrepository<t, id extends serializable> extends repository<t, id> { /** * saves a given entity. use the returned instance for further operations as the save operation might have changed the * entity instance completely. * * @param entity * @return the saved entity */ <s extends t> s save(s entity); /** * saves all given entities. * * @param entities * @return the saved entities * @throws illegalargumentexception in case the given entity is {@literal null}. */ <s extends t> iterable<s> save(iterable<s> entities); /** * retrieves an entity by its id. * * @param id must not be {@literal null}. * @return the entity with the given id or {@literal null} if none found * @throws illegalargumentexception if {@code id} is {@literal null} */ t findone(id id); /** * returns whether an entity with the given id exists. * * @param id must not be {@literal null}. * @return true if an entity with the given id exists, {@literal false} otherwise * @throws illegalargumentexception if {@code id} is {@literal null} */ boolean exists(id id); /** * returns all instances of the type. * * @return all entities */ iterable<t> findall(); /** * returns all instances of the type with the given ids. * * @param ids * @return */ iterable<t> findall(iterable<id> ids); /** * returns the number of entities available. * * @return the number of entities */ long count(); /** * deletes the entity with the given id. * * @param id must not be {@literal null}. * @throws illegalargumentexception in case the given {@code id} is {@literal null} */ void delete(id id); /** * deletes a given entity. * * @param entity * @throws illegalargumentexception in case the given entity is {@literal null}. */ void delete(t entity); /** * deletes the given entities. * * @param entities * @throws illegalargumentexception in case the given {@link iterable} is {@literal null}. */ void delete(iterable<? extends t> entities); /** * deletes all entities managed by the repository. */ void deleteall(); } |
再寫一個(gè)bookcontroller類
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
|
package com.hik.controller; import javax.annotation.resource; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.getmapping; import org.springframework.web.bind.annotation.pathvariable; import org.springframework.web.bind.annotation.postmapping; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; import org.springframework.web.servlet.modelandview; import com.hik.dao.bookdao; import com.hik.entity.book; /** * book控制類 * @author jed * */ @controller @requestmapping ( "/book" ) public class bookcontroller { @resource private bookdao bookdao; /** * 查詢所有圖書 * @return */ @requestmapping (value= "/list" ) public modelandview list() { modelandview mav = new modelandview (); mav.addobject( "booklist" , bookdao.findall()); mav.setviewname( "booklist" ); return mav; } /** * 添加圖書 * @param book * @return */ @requestmapping (value= "/add" , method=requestmethod.post) public string add(book book) { bookdao.save(book); return "forward:/book/list" ; } @getmapping (value= "/preupdate/{id}" ) public modelandview preupdate( @pathvariable ( "id" ) integer id) { modelandview mav = new modelandview(); mav.addobject( "book" , bookdao.getone(id)); mav.setviewname( "bookupdate" ); return mav; } /** * 修改圖書 * @param book * @return */ @postmapping (value= "/update" ) public string update(book book) { bookdao.save(book); return "forward:/book/list" ; } /** * 刪除圖書 * @param id * @return */ @requestmapping (value= "/delete" ,method = requestmethod.get) public string delete(integer id) { bookdao.delete(id); return "forward:/book/list" ; } } |
實(shí)現(xiàn)了 crud
這里的@getmapping(value="xxx") 類似 @requestmapping(value="xxx",method=requestmethod.get)
以及@postmapping(value="xxx") 類似 @requestmapping(value="xxx",method=requestmethod.post)
booklist.ftl 展示數(shù)據(jù)
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
|
<!doctype html> <html> <head> <meta charset= "utf-8" > <title>圖書管理頁面</title> </head> <body> <a href= "/bookadd.html" rel= "external nofollow" >添加圖書</a> <table> <tr> <th>編號(hào)</th> <th>圖書名稱</th> <th>操作</th> </tr> <#list booklist as book> <tr> <td>${book.id}</td> <td>${book.bookname}</td> <td> <a href= "/book/preupdate/${book.id}" rel= "external nofollow" >修改</a> <a href= "/book/delete?id=${book.id}" rel= "external nofollow" >刪除</a> </td> </tr> </#list> </table> </body> </html> |
bookadd.html 圖書添加頁面
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<!doctype html> <html> <head> <meta charset= "utf-8" > <title>圖書添加頁面</title> </head> <body> <form action= "book/add" method= "post" > 圖書名稱:<input type= "text" name= "bookname" /><br/> <input type= "submit" value= "提交" /> </form> </body> </html> |
bookupdate.ftl圖書修改頁面
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<!doctype html> <html> <head> <meta charset= "utf-8" > <title>圖書修改頁面</title> </head> <body> <form action= "/book/update" method= "post" > <input type= "hidden" name= "id" value= "${book.id}" /> 圖書名稱:<input type= "text" name= "bookname" value= "${book.bookname}" /><br/> <input type= "submit" value= "提交" /> </form> </body> </html> |
瀏覽器請(qǐng)求:http://localhost:8888/book/list
進(jìn)入:
點(diǎn)擊 “添加圖書”:
進(jìn)入:
我們隨便輸入名稱,點(diǎn)擊“提交”,
選擇剛才添加的測(cè)試圖書,進(jìn)行修改
轉(zhuǎn)發(fā)執(zhí)行到列表頁面,然后點(diǎn)“修改”,
進(jìn)入修改頁面,修改下名稱,點(diǎn)擊“提交”,
選擇測(cè)試圖書,進(jìn)行刪除操作
再次轉(zhuǎn)發(fā)到列表頁面,我們點(diǎn)擊“刪除”,
刪掉數(shù)據(jù)后,再次轉(zhuǎn)發(fā)到列表頁面;
ok完成!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://www.cnblogs.com/jedjia/p/CRUD.html