您好,登錄后才能下訂單哦!
數據持久化Spring Data JPA
項目集成了spring boot+spring data JPA+thymeleaf
前臺代碼地址:
https://blog.51cto.com/13501268/2319622
1.JPA理解:是用于管理Java EE和Java SE環境中的持久化,1.以及對象/關系映射的Java API
2.核心概念:
(1)實體:實體表示關系數據庫中的表
(2)每個實體實例對應于該表中的行
(3)類必須用javas.persistence.Entity注解
(4)類必須有一個public或protected的的無參構造函數
(5)實體實例被當做值以分離對象方式進行傳遞(例如通過會話bean的遠程業務接口),則該類必須實現Serializable接口
(6)唯一的對象標識符:簡單主鍵(javax.persistence.Id),復合主鍵(javax.persistence.Embeddedld和javax.persistence.IdClass)
3.關系:
(1)一對一:@OneToOne
(2)一對多:@OneToMany
(3)多對一:@ManyToOne
(4)多對多:@ManyToMany
4.EntityManager接口:
(1)定義用于與持久化上下文進行交互的方法
(2)創建和刪除持久化實例,通過實體的主鍵查找實體
5.Spring Data JPA:
(1)是更大的Spring Data家族的一部分
(2)對基于JPA的數據訪問層的增強支持
(3)更容易構建基于使用Spring數據訪問技術棧的應用
6.Spring Data JPA常用接口:
(1)CurdRepository:
(2)PagingAndSortingRepository:
7.Spring Data JPA自定義接口:
(1)根據方法名查詢,方法名命名一定要遵循規則
8.Spring Data JPA,Hibernate與Spring Boot的集成
(1)環境配置:MySql數據庫,Hibernate框架,以及Mysql連接驅動
(2)修改pom.xml文件,添加Spring Data JPA以及數據庫連接驅動的依賴:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://×××w.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dhtt.spring.boot.blog</groupId>
<artifactId>spring.data.action</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring.data.action</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 內存數據庫h3 -->
<!--
<dependency>
<groupId>com.h3database</groupId>
<artifactId>h3</artifactId>
</dependency>
-->
<!-- Mysql數據庫連接驅動 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<!-- hibernate持久層框架引入 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.3.7.Final</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
(3)啟動項目進行測試,我們發現項目正常啟動,說明我們的環境已經配置好了
9.集成后臺編碼:
(1)User實體:加@Entity注解,唯一標識@Id注解,制定主鍵生成策略,無參構造改造為protected,防止直接使用,加toString方法,實現Serializable接口;
package com.dhtt.spring.boot.blog.spring.data.action.entity;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
/**
* user實體
*
* @author QT
*/
@Entity
public class User implements Serializable {
private static final long serialVersionUID = 516988700978313579L;
@Id // 主鍵
@GeneratedValue(strategy = GenerationType.IDENTITY) // 自增策略
private Long id; // 用戶的唯一標識
private String name; // 用戶名
private String email; // 用戶郵箱
protected User() { // 防止直接使用
super();
}
public User(Long id, String name, String email) {
super();
this.id = id;
this.name = name;
this.email = email;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", email=" + email + "]";
}
}
(2)資源庫:寫userRepository接口繼承JpaRepository接口
package com.dhtt.spring.boot.blog.spring.data.action.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.dhtt.spring.boot.blog.spring.data.action.entity.User;
/**
* User Repository接口
* @author QT
*
*/
@Repository
public interface UserRepository extends JpaRepository<User, Long>{
}
(3)編寫userController類,用于與前臺進行交互
package com.dhtt.spring.boot.blog.spring.data.action.web.user;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.dhtt.spring.boot.blog.spring.data.action.entity.User;
import com.dhtt.spring.boot.blog.spring.data.action.repository.UserRepository;
@Controller
@RequestMapping("/users")
public class userController {
@Autowired
private UserRepository userRepository;
/**
* 查詢所有用戶
*
* @param model
* @return
*/
@GetMapping
public ModelAndView getList(Model model) {
model.addAttribute("userList", userRepository.findAll());
model.addAttribute("title", "用戶管理");
return new ModelAndView("users/list", "userModel", model);
}
/**
* 根據Id查詢用戶
*
* @param id
* @param model
* @return
*/
@GetMapping("{id}")
public ModelAndView view(@PathVariable("id") Long id, Model model) {
User user = userRepository.getOne(id);
model.addAttribute("user", user);
model.addAttribute("title", "用戶查詢");
return new ModelAndView("users/view", "userModel", model);
}
/**
* 創建用戶
*
* @param id
* @param model
* @return
*/
@GetMapping("/form")
public ModelAndView createForm(Model model) {
model.addAttribute("user", new User(null, null, null));
model.addAttribute("title", "創建用戶");
return new ModelAndView("users/form", "userModel", model);
}
/**
* 新增或修改用戶
*
* @param user
* @return
*/
@PostMapping
public ModelAndView saveOrUpdateUser(User user) {
user = userRepository.save(user);
return new ModelAndView("redirect:/users", "userModel", user);
}
/**
* 獲取刪除用戶
*
* @param id
* @return
*/
@GetMapping("/delete/{id}")
public ModelAndView deleteUser(@PathVariable("id") Long id) {
userRepository.deleteById(id);;
return new ModelAndView("redirect:/users"); // 重定向到list頁面
}
/**
* 獲取修改用戶界面
*
* @param id
* @param model
* @return
*/
@GetMapping("/modify/{id}")
public ModelAndView modify(@PathVariable("id") Long id, Model model) {
User user = userRepository.getOne(id);
model.addAttribute("user", user);
model.addAttribute("title", "修改用戶");
return new ModelAndView("users/form", "userModel", model);
}
}
前臺與上一個項目一樣,直接粘貼復制就好
前臺代碼項目地址:
https://blog.51cto.com/13501268/2319622
接下里,啟動項目,項目運行正常說明集成成功,下一步我們就將數據進行持久化到數據庫中
10.將數據持久化到數據庫中
(1)進行數據庫配置,配置文件如下
#thymeleaf配置
spring.thymeleaf.encoding=UTF-8
#熱部署靜態文件,不需要緩存,實時觀察文件修改效果
spring.thymeleaf.cache=false
#使用html5標準
spring.thymeleaf.mode=HTML5
spring.thymeleaf.suffix=.html
spring.resources.chain.strategy.content.enabled=true
#數據庫連接配置
spring.datasource.url=jdbc:mysql://localhost:3306/blog_test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8&useSSL=false
spring.datasource.username=root
spring.datasource.password=qitao1996
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#jpa配置
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create-drop
(2)接下來啟動數據庫,建立數據庫
接下來我們啟動項目進行操作,我們會發現數據庫表成功創建,也可以操作表中數據
至此,Spring boot+Spring Data Jpa+mevan+Thymeleaf集成成功完成
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。