Spring Data JPA綜合練習

選取京東圖書展示頁面

編碼

  • 新建一個Book實體類
package com.example.entity;

import lombok.Data;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

/**
 * Created by 史冬陽 on 2018/9/20.
 */
@Entity
@Data
public class Book {
    @Id
    @GeneratedValue
    private Integer id;
    private String avatar;
    private String name;
    private String author;
    private String price;
    private String introduction;
}
  • 新建一個DAO層
package com.example.dao;

import com.example.entity.Book;
import org.springframework.data.jpa.repository.JpaRepository;

/**
 * Created by 史冬陽 on 2018/9/20.
 */

/**
 * Integer 唯一標識符 數(shù)據(jù)庫的主鍵
 */
public interface BookRepository extends JpaRepository<Book,Integer> {
}
  • 新建一個BookService接口
package com.example.service;

import com.example.entity.Book;

import java.util.List;

/**
 * Created by 史冬陽 on 2018/9/20.
 */
public interface BookService {
    Book save(Book book);
    List<Book> getAll();
    Book get(int id);
    void delete(int id);
}
  • 新建一個service層的實現(xiàn)類
package com.example.service.impl;

import com.example.dao.BookRepository;
import com.example.entity.Book;
import com.example.service.BookService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by 史冬陽 on 2018/9/20.
 */
@Service
public class BookServiceImpl implements BookService {
    @Resource
    private BookRepository bookRepository;

    @Override
    @Transactional
    public Book save(Book book) {
        return bookRepository.save(book);
    }

    @Override
    public List<Book> getAll() {
        return bookRepository.findAll();
    }

    @Override
    @Transactional
    public Book get(int id) {
        return bookRepository.findById(id).get();
    }

    @Override
    @Transactional
    public void delete(int id) {
        bookRepository.deleteById(id);
    }
}
  • 新建一個test類
package com.example.service.impl;

import com.example.entity.Book;
import com.example.service.BookService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;

import static org.junit.Assert.*;

/**
 * Created by 史冬陽 on 2018/9/20.
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class BookServiceImplTest {
    @Resource
    private BookService bookService;

    @Test
    public void save() throws Exception {
        String[] names = {"獨家記憶","余生多關照","綠物語","時光行者的你","林深時見鹿","單向遷徙"};
        String[] authors = {"木浮生","原城","鐮足","桐華","宴生","張飲修"};
        String[] prices ={"23.8","24.3","26.2","28.5","20.6","33.6"};
        String[] introductions = {
                "世界上最美好的事情莫過于,我喜歡你的同時,剛好你也喜歡我。",
                "喜歡制造大悲或大喜的故事,從事自己熱愛的職業(yè),結識自己喜愛的人。",
                "不要被植物表面的柔軟和溫順欺騙,有時,一縷委婉涌動的潔白,數(shù)年后會引發(fā)無法挽救的巨大災難。",
                "他說:“后來,我遇見了一個將我的世界點亮的人。 他們都是時光里的傷心旅客,也是余生路上最好的旅伴。",
                "故事講述了少年顧延樹和少女鹿惜光幼年時曾相依相伴,卻無奈被命運分離,從此分隔兩地,各自在不同的環(huán)境中堅強而隱忍地長大,為了彼此成為更優(yōu)秀的人。 兩人從此經(jīng)歷了重重磨難和考驗,當年被迫分開的真相也漸漸浮出水面。",
                "突圍黑暗過往的自我救贖之作?;貞浗o自己,童話給讀者。也許某一天,你終會耗盡一切,但,愛我,本身就是一場單向遷徙。"};

        String[] avatars = {
                "http://peojfj6k8.bkt.clouddn.com/1.jpg",
                "http://peojfj6k8.bkt.clouddn.com/2.jpg",
                "http://peojfj6k8.bkt.clouddn.com/3.jpg",
                "http://peojfj6k8.bkt.clouddn.com/4.jpg",
                "http://peojfj6k8.bkt.clouddn.com/5.jpg",
                "http://peojfj6k8.bkt.clouddn.com/6.jpg"};

        for (int i=0; i<6; i++){
            Book book = new Book();
            book.setName(names[i]);
            book.setAuthor(authors[i]);
            book.setAvatar(avatars[i]);
            book.setPrice(prices[i]);
            book.setIntroduction(introductions[i]);
            System.out.println(bookService.save(book));


        }
    }

    @Test
    public void getAll() throws Exception {

    }

    @Test
    public void get() throws Exception {

    }

    @Test
    public void delete() throws Exception {

    }

}
  • 新建一個Controller層
package com.example.controller;

import com.example.service.BookService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;

/**
 * Created by 史冬陽 on 2018/9/20.
 */
@Controller
@RequestMapping(value = "/book")
public class BookController {
    private static final String BOOK_DETAIL_PATH_NAME = "bookDetail";
    private static final String BOOK_LIST_PATH_NAME = "bookList";

    @Resource
    BookService bookService;

    /**
     * 獲取 Book 列表
     * 處理 "/book" 的 GET 請求,用來獲取 Book 列表
     * 數(shù)據(jù)存入ModelMap,返回Thymeleaf頁面
     */
    @GetMapping()
    public String getBookList(ModelMap map) {
        map.addAttribute("bookList",bookService.getAll());
        return BOOK_LIST_PATH_NAME;
    }

    /**
     * 獲取 Book
     * 處理 "/book/{id}" 的 GET 請求
     */

    @GetMapping(value = "/{id}")
    public String getBook(@PathVariable Integer id, ModelMap map) {
        map.addAttribute("book", bookService.get(id));
        return BOOK_DETAIL_PATH_NAME;
    }

    }
  • 圖書列表頁面
<html xmlns:th="http://www.thymeleaf.org">
<html lang="zh-CN">
<head>
    <script type="text/javascript" th:src="@{https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js}"></script>
    <link th:href="@{https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css}" rel="stylesheet"/>
    <meta charset="UTF-8"/>
    <title>書籍列表</title>
</head>
<style>
    .top-set{
        width: 1400px;
        margin-left: 56px;
    }
    .price-set{
        color: #E3393C;
        font-size: 16px;
    }
    .name-set{
        font-size: 14px;
        color: black;
    }
    .amount-set{
        color: #649cd9;
        font-size: 14px;
    }
    .location-set{
     font-size: 12px;

    }
</style>

<body>
<div >
    <img src="http://peojfj6k8.bkt.clouddn.com/topPic.png" class="top-set">
</div>


<div class="container">

    <h3>Spring Data JPA練習 </h3>


    <div class="row" >
        <div class="col-xs-6 col-sm-3"  th:each="book : ${bookList}">
            <div class="thumbnail">

                <img th:src="@{${book.avatar}}">
                <div class="caption location-set">
                    <p th:text="¥+' '+${book.price}" class="price-set"></p>
                    <p class="name-set "><a th:href="@{/book/{bookId}(bookId=${book.id})}" th:text="${book.name}"></a></p>
                    <p><text class="amount-set">7.2萬+</text><text>條評論</text></p>

                    <!--<h4 th:text="${book.author}"></h4>-->

                </div>
            </div>
        </div>

    </div>


</div>

</body>
</html>
  • 圖書詳情頁面
<html xmlns:th="http://www.thymeleaf.org">
<html lang="zh-CN">
<head>
    <script type="text/javascript" th:src="@{https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js}"></script>
    <link th:href="@{https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css}" rel="stylesheet"/>
    <meta charset="UTF-8"/>
    <title>書籍詳情</title>
</head>
<body>
<div class="container">
    <div>
        <img src="http://peojfj6k8.bkt.clouddn.com/detail-top.jpg" style="margin-left: -200px;height: 195px; width: 1546px">
    </div>
    <div class="col-md-4">
        <div style="border: 1px solid gainsboro; margin-top: 30px">
            <img th:src="@{${book.avatar }}" style="width: 300px;height: 350px" >
        </div>
    </div>
    <div class="col-md-5" style="margin-top: 30px">
        <p th:text="${book.name}" style="font-weight: bolder; font-size: 22px"></p>
        <p th:text="${book.author}" style="font-size: 12px"></p>
        <p>京東價:</p>
        <p th:text="${book.price}" style="color: red;font-size: 18px;margin-top: -35px;margin-left: 50px"></p>
        <p>書籍介紹:</p>
        <p th:text="${book.introduction}" style="margin-top: -29px;margin-left: 65px;color: grey"></p>
        <p style="color: grey">增值業(yè)務</p>
        <p style="color: red; margin-top: -30px;margin-left: 65px">禮品包裝</p>
        <p style="color: grey">重量</p>
        <p style="color: grey;margin-top: -30px; margin-left: 65px">0.3kg</p>
        <p style="color: grey">白條分期:</p>
        <a class="btn btn-default" href="#" role="button" style="color: grey">不分期</a>
        <button class="btn btn-default" type="submit" style="color: grey">¥7.06起x3期</button>
        <input class="btn btn-default" type="button" value="¥3.6起x6期" style="color: grey">
        <input class="btn btn-default" type="submit" value="¥1.86起x12期" style="color:grey;">
        <button type="button" class="btn btn-danger" style="margin-top: 30px">加入購物車</button>
        <button type="button" class="btn btn-default" style="color: red; border: 1px solid red; margin-top: 30px; margin-left: 30px">購買電子書免費</button>
        <p style="color: grey; font-size: 10px; margin-top: 20px">溫馨提示:支持七天無理由退貨</p>
    </div>

    <div class="col-md-3">
        <img src="http://peojfj6k8.bkt.clouddn.com/right.png" style="width: 250px;height: 250px">
    </div>
</div>
</body>
</html>

展示效果圖

  • 圖書列表頁面


  • 圖書詳情頁面


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

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

  • (ps:將數(shù)據(jù)庫內(nèi)容返回到web頁面) 1.pom.xml <!-- Web 依賴 --> org.springf...
    逍遙_6b76閱讀 661評論 0 6
  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,544評論 19 139
  • 這是我喜歡你的不知道多少個白天黑夜了從九月份開學我遇見的就是一個高高瘦瘦的男生從我的身邊路過 剛開始我覺得只是個毫...
    知覺先生閱讀 230評論 0 0
  • 如果你只做一個乖女孩, 除了一個乖字, 其他什么都得不到。 ——小解 在和喜歡的人確定關系之前, 曖昧可以說是一條...
    一只小解閱讀 1,020評論 0 0
  • 我是一個非常孤獨死板de人。 心地很善良,但能力不強。 曾經(jīng)也想過自殺, 所以我一直認真自殺的人都特別善良, 總把...
    洪吉娃兒閱讀 261評論 2 1

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