Spring Data JPA練習(xí)

1、Entity

@Entity

@Data

public class Bookimplements Serializable{

@Id

@GeneratedValue

? ? private long id;

? ? private StringbookName;

? ? private StringbookAuther;

? ? private StringbookIntroduce;

? ? private StringbookCover;

}

2、DAO

public interface BookRepositoryextends JpaRepository {

}

3、service

public interface BookService {

/**

* 獲取所有 Book

*/

? ? ListfindAll();

? ? /**

* 獲取 Book

*

? ? * @param id 編號(hào)

*/

? ? BookfindById(Long id);

}


@Service

public class BookServiceImplimplements BookService {

@Autowired

? ? BookRepositorybookRepository;

? ? @Override

? ? public ListfindAll() {

return bookRepository.findAll();

? ? }

@Override

? ? public BookfindById(Long id) {

return bookRepository.findById(id).get();

? ? }

}

4、Controller

@Service

public class BookServiceImplimplements BookService {

@Autowired

? ? BookRepositorybookRepository;

? ? @Override

? ? public ListfindAll() {

return bookRepository.findAll();

? ? }

@Override

? ? public BookfindById(Long id) {

return bookRepository.findById(id).get();

? ? }

}

5、index

<html xmlns:th="http://www.thymeleaf.org">

<html lang="zh-CN">

? ? <script type="text/javascript" th:src="@{https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js}">

? ? <link th:href="@{https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css}" rel="stylesheet"/>

? ? <meta charset="UTF-8"/>

? ? <title>書籍列表

? ? ? ? p{

display:inline;

? ? ? ? }

down{

margin-top:20px;

? ? ? ? }

title{

font-size:25px;;

? ? ? ? }

content{

padding-top:17px;

? ? ? ? }

container{

width:800px;

? ? ? ? }

bookCover{

height:150px;;

? ? ? ? ? ? width:270px;;

? ? ? ? }

<div class="media">

? ? <div class="media-body">

? ? ? ? <div class="container">

? ? ? ? ? ? <div style="margin-left: -70px">

? ? ? ? ? ? ? ? <img src="/img/000.png">

? ? ? ? ? ? <div class="alert alert-success" th:each="Book:${bookList}">

? ? ? ? ? ? ? ? <div class="top">

? ? ? ? ? ? ? ? ? ? <div class="media">

? ? ? ? ? ? ? ? <div class="media">

? ? ? ? ? ? ? ? ? ? <div class="media-left">

? ? ? ? ? ? ? ? ? ? ? ? ? ? <div class="pic">

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <img th:src="@{${Book.bookCover}}">

? ? ? ? ? ? ? ? ? ? <div class="media-right">

? ? ? ? ? ? ? ? ? ? ? ? <div class="title">

? ? ? ? ? ? ? ? ? ? ? ? ? ? <a th:href="@{/book/{bookId}(bookId=${Book.id})}" th:text="${Book.bookName}">

? ? ? ? ? ? ? ? ? ? ? ? <div class="content">

? ? ? ? ? ? ? ? ? ? ? ? ? ? <p th:text="${Book.bookIntroduce}">

</html>


<html xmlns:th="http://www.thymeleaf.org">

<html lang="zh-CN">

? ? <script type="text/javascript" th:src="@{https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js}">

? ? <link th:href="@{https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css}" rel="stylesheet"/>

? ? <meta charset="UTF-8"/>

? ? <title>書籍詳情

<div class="media">

? ? <div class="media-body">

? ? ? ? <div class="container">

? ? ? ? ? ? <div style="margin-left: -70px">

? ? ? ? ? ? ? ? <img src="/img/001.png">

? ? ? ? ? ? <img th:src="@{${book.bookCover}}">


? ? ? ? ? ? <p th:text="${book.bookName}">

? ? ? ? ? ? <p th:text="${book.bookAuther}">

? ? ? ? ? ? <p th:text="${book.bookIntroduce}">

</html>


6、運(yùn)行截圖


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容