Spring|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 唯一标识符 数据库的主键
*/
public interface BookRepository extends JpaRepository {
}
- 新建一个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 getAll();
Book get(int id);
void delete(int id);
}
- 新建一个service层的实现类
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 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 = {
"世界上最美好的事情莫过于,我喜欢你的同时,刚好你也喜欢我。",
"喜欢制造大悲或大喜的故事,从事自己热爱的职业,结识自己喜爱的人。",
"不要被植物表面的柔软和温顺欺骗,有时,一缕委婉涌动的洁白,数年后会引发无法挽救的巨大灾难。",
"他说:“后来,我遇见了一个将我的世界点亮的人。 他们都是时光里的伤心旅客,也是余生路上最好的旅伴。",
"故事讲述了少年顾延树和少女鹿惜光幼年时曾相依相伴,却无奈被命运分离,从此分隔两地,各自在不同的环境中坚强而隐忍地长大,为了彼此成为更优秀的人。 两人从此经历了重重磨难和考验,当年被迫分开的真相也渐渐浮出水面。",
"突围黑暗过往的自我救赎之作。回忆给自己,童话给读者。也许某一天,你终会耗尽一切,但,爱我,本身就是一场单向迁徙。"};
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 = "https://www.it610.com/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 列表
* 数据存入ModelMap,返回Thymeleaf页面
*/
@GetMapping()
public String getBookList(ModelMap map) {
map.addAttribute("bookList",bookService.getAll());
return BOOK_LIST_PATH_NAME;
}/**
* 获取 Book
* 处理 "/book/{id}" 的 GET 请求
*/@GetMapping(value = "https://www.it610.com/{id}")
public String getBook(@PathVariable Integer id, ModelMap map) {
map.addAttribute("book", bookService.get(id));
return BOOK_DETAIL_PATH_NAME;
}}
- 图书列表页面
书籍列表 - 锐客网 .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;
}
文章图片
Spring Data JPA练习
文章图片
7.2万+ 条评论
- 图书详情页面
书籍详情 - 锐客网
文章图片
文章图片
京东价:
书籍介绍:
增值业务
礼品包装
重量
0.3kg
白条分期:
不分期
温馨提示:支持七天无理由退货
文章图片
展示效果图
- 图书列表页面
文章图片
- 【Spring|Spring Data JPA综合练习】图书详情页面
文章图片
推荐阅读
- Activiti(一)SpringBoot2集成Activiti6
- SpringBoot调用公共模块的自定义注解失效的解决
- 解决SpringBoot引用别的模块无法注入的问题
- 2018-07-09|2018-07-09 Spring 的DBCP,c3p0
- 数据库总结语句
- spring|spring boot项目启动websocket
- Spring|Spring Boot 整合 Activiti6.0.0
- vue组件中为何data必须是一个函数()
- Spring集成|Spring集成 Mina
- springboot使用redis缓存