Spring Data JPA练习

2018-09-25  本文已影响0人  风丶无痕

1.pom.xml

<!-- Web 依赖 --> 
<dependency> 
<groupId>org.springframework.boot</groupId> 
<artifactId>spring-boot-starter-web</artifactId> 
</dependency> 

<!-- 模板引擎 Thymeleaf 依赖 --> 
<dependency> 
<groupId>org.springframework.boot</groupId> 
<artifactId>spring-boot-starter-thymeleaf</artifactId> 
</dependency> 

<!-- mysql驱动 依赖 --> 
<dependency> 
<groupId>mysql</groupId> 
<artifactId>mysql-connector-java</artifactId> 
</dependency> 

<!-- spring-data-jpa 依赖 --> 
<dependency> 
<groupId>org.springframework.boot</groupId> 
<artifactId>spring-boot-starter-data-jpa</artifactId> 
</dependency> 

<!-- lombok 依赖 --> 
<dependency> 
<groupId>org.projectlombok</groupId> 
<artifactId>lombok</artifactId> 
</dependency> 

2.application.properties

############################ 
###数据库配置信息 
############################ 
##基础信息 
spring.datasource.url=jdbc:mysql://localhost:3306/springboot_test?
useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root 
spring.datasource.password=root 
spring.datasource.driver-class-name=com.mysql.jdbc.Driver 
##连接池中最大的活跃连接数 
spring.datasource.tomcat.max-active=20 
##连接池中最大、最小的空闲连接数 
spring.datasoure.max-idle=8 
spring.datasoure.min-idle=8 
##初始化连接数 
spring.datasoure.initial=10 


############################ 
###Spring Data JPA配置信息 
############################ 
spring.jpa.database=mysql 
# 显示SQL语句 
spring.jpa.show-sql=true 
##指定DDL mode (none, validate, update, create, create-drop) 
spring.jpa.properties.hibernate.hbm2ddl.auto=update 

spring.thymeleaf.cache=false 

3.entity

package com.example.springdatajpa.entity; 

import lombok.Data; 

import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.Id; 
import java.io.Serializable; 

/** 
* 创建Student持久化类 
* 1.使用@Entity注解实现实体类的持久化,JPA检测到之后,可以在数据库中生成对应的表 
* 2.使用@Id指定主键 
* 3.使用 @GeneratedValue指定主键策略,mysql默认自增 
* 4.使用@Data简化get/set 
*/ 
@Entity 
@Data 
public class Book implements Serializable { 
@Id 
@GeneratedValue 
private Long id; //编号 

private String name; //书名 
private String writer; //作者 
private String introduction; //简介 

} 

4.dao

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

/** 
* Book 数据持久层操作接口 
*/ 
public interface BookRepository extends JpaRepository<Book, Long> { 
} 

5.service及实现

package com.example.springdatajpa.service; 

import com.example.springdatajpa.entity.Book; 
import java.util.List; 

/** 
* Book 业务接口层 
*/ 
public interface BookService { 
/** 
* 获取所有 Book 
*/ 
List<Book> findAll(); 

/** 
* 获取 Book 
* 
* @param id 编号 
*/ 
Book findById(Long id); 
} 

package com.example.springdatajpa.service.impl; 

import com.example.springdatajpa.dao.BookRepository; 
import com.example.springdatajpa.entity.Book; 
import com.example.springdatajpa.service.BookService; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Service; 
import java.util.List; 
/** 
* Book 业务层实现 
*/ 
@Service 
public class BookServiceImpl implements BookService { 

@Autowired 
BookRepository bookRepository; 

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

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

6.controller

ackage com.example.springdatajpa.controller; 
import com.example.springdatajpa.entity.Book; 
import com.example.springdatajpa.service.BookService; 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.ModelMap; 
import org.springframework.web.bind.annotation.*; 
import javax.annotation.Resource; 
/** 
* Book 控制层 
*/ 
@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/all" 的 GET 请求,用来获取 Book 列表 
* 数据存入ModelMap,返回Thymeleaf页面 
*/ 
@GetMapping("/all") 
public String getBookList(ModelMap map) { 
map.addAttribute("bookList", bookService.findAll()); 
return BOOK_LIST_PATH_NAME; 
} 

/** 
* 获取 Book 
* 处理 "/book/{id}" 的 GET 请求 
* 返回单个数据的模板页面  
*/ 
@GetMapping(value = "/{id}") 
public String getBook(@PathVariable Long id, ModelMap map) { 
map.addAttribute("book", bookService.findById(id)); 
return BOOK_DETAIL_PATH_NAME; 
} 
}

7.bookList.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"> 
<h3>Spring Data JPA练习 </h3> 
<table class="table table-hover table-condensed"> 
<legend> 
<strong>书籍列表</strong> 
</legend> 
<thead> 
<tr> 
<th>书籍编号</th> 
<th>书名</th> 
<th>作者</th> 
<th>简介</th> 
</tr> 
</thead> 
<tbody> 
<tr th:each="book : ${bookList}"> 
<th scope="row" th:text="${book.id}"></th> 
<td><a th:href="@{/book/{bookId}(bookId=${book.id})}"
th:text="${book.name}"></a></td>

<td th:text="${book.writer}"></td> 
<td th:text="${book.introduction}"></td> 
</tr> 
</tbody> 
</table> 
</div> 
</body> 
</html>

8.bookDetail.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"> 
<h3>Spring Data JPA练习 </h3> 
<legend> 
<strong>书籍详情</strong> 
</legend> 
<div class="col-md-4"> 
<p th:text="${book.name}"></p> 
<p th:text="${book.writer}"></p> 
<p th:text="${book.introduction}"></p> 
</div> 
</div> 
</body> 
</html> 

9.运⾏效果

效果图.png
上一篇 下一篇

猜你喜欢

热点阅读