激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - SpringBoot使用Spring-Data-Jpa實(shí)現(xiàn)CRUD操作

SpringBoot使用Spring-Data-Jpa實(shí)現(xiàn)CRUD操作

2021-05-27 14:01心和夢(mèng)的方向 Java教程

這篇文章主要為大家詳細(xì)介紹了SpringBoot使用Spring-Data-Jpa實(shí)現(xiàn)CRUD操作,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文演示了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)入:

SpringBoot使用Spring-Data-Jpa實(shí)現(xiàn)CRUD操作

點(diǎn)擊 “添加圖書”:

進(jìn)入:

SpringBoot使用Spring-Data-Jpa實(shí)現(xiàn)CRUD操作

我們隨便輸入名稱,點(diǎn)擊“提交”,

SpringBoot使用Spring-Data-Jpa實(shí)現(xiàn)CRUD操作

選擇剛才添加的測(cè)試圖書,進(jìn)行修改

SpringBoot使用Spring-Data-Jpa實(shí)現(xiàn)CRUD操作

轉(zhuǎn)發(fā)執(zhí)行到列表頁面,然后點(diǎn)“修改”,

SpringBoot使用Spring-Data-Jpa實(shí)現(xiàn)CRUD操作

進(jìn)入修改頁面,修改下名稱,點(diǎn)擊“提交”,

選擇測(cè)試圖書,進(jìn)行刪除操作

SpringBoot使用Spring-Data-Jpa實(shí)現(xiàn)CRUD操作

再次轉(zhuǎn)發(fā)到列表頁面,我們點(diǎn)擊“刪除”,

SpringBoot使用Spring-Data-Jpa實(shí)現(xiàn)CRUD操作

刪掉數(shù)據(jù)后,再次轉(zhuǎn)發(fā)到列表頁面;

ok完成!

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://www.cnblogs.com/jedjia/p/CRUD.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产日韩在线 | 666sao| 一级做a爱片性色毛片 | 成年免费看| 色婷婷av一区二区三区久久 | 九一国产精品 | 色综合欧美 | xxxxhd18hd日本hd | 奇米影视8888狠狠狠狠 | 天天草天天干天天射 | 日本高清无遮挡 | 污版视频在线观看 | 黄色一级片在线观看 | 欧美一级久久久久久久大片 | 国产免费一区二区三区视频 | 久久黄色影院 | 国产精品久久久免费观看 | 国产一级淫片a级aaa | 福利在线影院 | 国产亚洲自拍一区 | 日韩视频观看 | h视频在线播放 | 亚洲成人福利电影 | 欧美一级黄 | 男女亲热网站 | 色诱亚洲精品久久久久久 | xnxx 美女19| 国产视频在线观看免费 | 久久日本| 久久伊人国产精品 | 久久免费视频一区二区三区 | 欧美片a| 亚洲最大中文字幕 | 国产精品久久久久久影视 | 一本色道久久99精品综合蜜臀 | 99成人在线 | 色女生影院 | 日韩精品中文字幕一区 | 日本中文字幕电影在线观看 | 日韩视频一区二区三区在线观看 | 7777欧美|