選取京東圖書展示頁面
編碼
- 新建一個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 = {"獨家記憶","余生多關(guān)照","綠物語","時光行者的你","林深時見鹿","單向遷徙"};
String[] authors = {"木浮生","原城","鐮足","桐華","宴生","張飲修"};
String[] prices ={"23.8","24.3","26.2","28.5","20.6","33.6"};
String[] introductions = {
"世界上最美好的事情莫過于萤捆,我喜歡你的同時庸队,剛好你也喜歡我论矾。",
"喜歡制造大悲或大喜的故事固以,從事自己熱愛的職業(yè)到千,結(jié)識自己喜愛的人包晰。",
"不要被植物表面的柔軟和溫順欺騙,有時演侯,一縷委婉涌動的潔白姿染,數(shù)年后會引發(fā)無法挽救的巨大災(zāi)難。",
"他說:“后來秒际,我遇見了一個將我的世界點亮的人悬赏。 他們都是時光里的傷心旅客,也是余生路上最好的旅伴娄徊。",
"故事講述了少年顧延樹和少女鹿惜光幼年時曾相依相伴闽颇,卻無奈被命運分離,從此分隔兩地嵌莉,各自在不同的環(huán)境中堅強而隱忍地長大进萄,為了彼此成為更優(yōu)秀的人。 兩人從此經(jīng)歷了重重磨難和考驗锐峭,當年被迫分開的真相也漸漸浮出水面中鼠。",
"突圍黑暗過往的自我救贖之作⊙伛回憶給自己援雇,童話給讀者。也許某一天椎扬,你終會耗盡一切惫搏,但,愛我蚕涤,本身就是一場單向遷徙筐赔。"};
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è)務(wù)</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>
展示效果圖
-
圖書列表頁面
-
圖書詳情頁面