數(shù)據(jù)持久化Spring Data JPA
項目集成了spring boot+spring data JPA+thymeleaf
前臺代碼地址:
https://blog.51cto.com/13501268/2319622
1.JPA理解:是用于管理Java EE和Java SE環(huán)境中的持久化,1.以及對象/關(guān)系映射的Java API
創(chuàng)新互聯(lián)公司于2013年創(chuàng)立,先為和龍等服務(wù)建站,和龍等地企業(yè),進行企業(yè)商務(wù)咨詢服務(wù)。為和龍企業(yè)網(wǎng)站制作PC+手機+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。
2.核心概念:
(1)實體:實體表示關(guān)系數(shù)據(jù)庫中的表
(2)每個實體實例對應于該表中的行
(3)類必須用javas.persistence.Entity注解
(4)類必須有一個public或protected的的無參構(gòu)造函數(shù)
(5)實體實例被當做值以分離對象方式進行傳遞(例如通過會話bean的遠程業(yè)務(wù)接口),則該類必須實現(xiàn)Serializable接口
(6)唯一的對象標識符:簡單主鍵(javax.persistence.Id),復合主鍵(javax.persistence.Embeddedld和javax.persistence.IdClass)
3.關(guān)系:
(1)一對一:@OneToOne
(2)一對多:@OneToMany
(3)多對一:@ManyToOne
(4)多對多:@ManyToMany
4.EntityManager接口:
(1)定義用于與持久化上下文進行交互的方法
(2)創(chuàng)建和刪除持久化實例,通過實體的主鍵查找實體
5.Spring Data JPA:
(1)是更大的Spring Data家族的一部分
(2)對基于JPA的數(shù)據(jù)訪問層的增強支持
(3)更容易構(gòu)建基于使用Spring數(shù)據(jù)訪問技術(shù)棧的應用
6.Spring Data JPA常用接口:
(1)CurdRepository:
(2)PagingAndSortingRepository:
7.Spring Data JPA自定義接口:
(1)根據(jù)方法名查詢,方法名命名一定要遵循規(guī)則
8.Spring Data JPA,Hibernate與Spring Boot的集成
(1)環(huán)境配置:MySQL數(shù)據(jù)庫,Hibernate框架,以及Mysql連接驅(qū)動
(2)修改pom.xml文件,添加Spring Data JPA以及數(shù)據(jù)庫連接驅(qū)動的依賴:
4.0.0
com.dhtt.spring.boot.blog
spring.data.action
0.0.1-SNAPSHOT
jar
spring.data.action
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.1.0.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-data-jpa
org.springframework.boot
spring-boot-starter-thymeleaf
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-devtools
org.springframework.boot
spring-boot-starter-test
test
mysql
mysql-connector-java
5.1.46
org.hibernate
hibernate-core
5.3.7.Final
org.springframework.boot
spring-boot-maven-plugin
(3)啟動項目進行測試,我們發(fā)現(xiàn)項目正常啟動,說明我們的環(huán)境已經(jīng)配置好了
9.集成后臺編碼:
(1)User實體:加@Entity注解,唯一標識@Id注解,制定主鍵生成策略,無參構(gòu)造改造為protected,防止直接使用,加toString方法,實現(xiàn)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{
}
(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);
}
/**
* 根據(jù)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);
}
/**
* 創(chuàng)建用戶
*
* @param id
* @param model
* @return
*/
@GetMapping("/form")
public ModelAndView createForm(Model model) {
model.addAttribute("user", new User(null, null, null));
model.addAttribute("title", "創(chuàng)建用戶");
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
接下里,啟動項目,項目運行正常說明集成成功,下一步我們就將數(shù)據(jù)進行持久化到數(shù)據(jù)庫中
10.將數(shù)據(jù)持久化到數(shù)據(jù)庫中
(1)進行數(shù)據(jù)庫配置,配置文件如下
#thymeleaf配置
spring.thymeleaf.encoding=UTF-8
#熱部署靜態(tài)文件,不需要緩存,實時觀察文件修改效果
spring.thymeleaf.cache=false
#使用html5標準
spring.thymeleaf.mode=HTML5
spring.thymeleaf.suffix=.html
spring.resources.chain.strategy.content.enabled=true
#數(shù)據(jù)庫連接配置
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)接下來啟動數(shù)據(jù)庫,建立數(shù)據(jù)庫
接下來我們啟動項目進行操作,我們會發(fā)現(xiàn)數(shù)據(jù)庫表成功創(chuàng)建,也可以操作表中數(shù)據(jù)
至此,Spring boot+Spring Data Jpa+mevan+Thymeleaf集成成功完成