本帖來演示下SpringBoot下,實用Spring-Data-Jpa來實現CRUD操做,視圖層採用Freemarkerhtml
這裏咱們先把application.properties修改爲application.yml 主流格式java
內容也改爲yml規範格式mysql
server:
web
port: 8888
context-path: /
helloWorld: spring Boot\u5927\u7237\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: 123456
jpa:
hibernate.ddl-auto: update
show-sql: true
yml格式有個注意點 冒號後面必定要加個空格spring
還有咱們把context-path改爲/方便開發應用sql
先寫一個BookDao接口瀏覽器
import
org.springframework.data.jpa.repository.JpaRepository;
app
import
com.java1234.entity.Book;
/**
* 圖書Dao接口
* @author user
*
*/
public
interface
BookDao
extends
JpaRepository<Book, Integer>{
}
要求實現JpaRepositorypost
再寫一個BookController類url
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.java1234.dao.BookDao;
import
com.java1234.entity.Book;
/**
* Book控制類
* @author user
*
*/
@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"
;
}
}
實現了 CRUD
這裏的@GetMapping(value="xxx") 相似 @RequestMapping(value="xxx",method=RequestMethod.GET)
以及@PostMapping(value="xxx") 相似 @RequestMapping(value="xxx",method=RequestMethod.POST)
bookList.ftl 展現數據
<!DOCTYPE html>
<
html
>
<
head
>
<
meta
charset
=
"UTF-8"
>
<
title
>圖書管理頁面</
title
>
</
head
>
<
body
>
<
a
href
=
"/bookAdd.html"
>添加圖書</
a
>
<
table
>
<
tr
>
<
th
>編號</
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}"
>修改</
a
>
<
a
href
=
"/book/delete?id=${book.id}"
>刪除</
a
>
</
td
>
</
tr
>
</#list>
</
table
>
</
body
>
</
html
>
<!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
>
<!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
>
瀏覽器請求:http://localhost:8888/book/list
進入:
點擊 「添加圖書」:
進入:
咱們隨便輸入名稱,點擊「提交」,
轉發執行到列表頁面,而後點「修改」,
進入修改頁面,修改下名稱,點擊「提交」,
再次轉發到列表頁面,咱們點擊「刪除」,
刪掉數據後,再次轉發到列表頁面;