In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
Data persistence Spring Data JPA
The project integrates spring boot+spring data JPA+thymeleaf
Foreground code address:
Https://blog.51cto.com/13501268/2319622
1.JPA understanding: is used to manage persistence in Java EE and Java SE environments, 1. And Java API for object / relational mapping
two。 Core concepts:
(1) entities: entities represent tables in a relational database
(2) each entity instance corresponds to the rows in the table
(3) classes must be annotated with javas.persistence.Entity
(4) the class must have a no-parameter constructor of public or protected
(5) if the entity instance is passed as a value by separating the object (for example, through the remote service interface of the session bean), the class must implement the Serializable interface.
(6) unique object identifiers: simple primary key (javax.persistence.Id), compound primary key (javax.persistence.Embeddedld and javax.persistence.IdClass)
3. Relationship:
(1) one to one: @ OneToOne
(2) one-to-many: @ OneToMany
(3) many to one: @ ManyToOne
(4) many-to-many: @ ManyToMany
4.EntityManager interface:
(1) define the methods used to interact with the persistence context
(2) create and delete persistent instances and find entities through the primary key of the entity
5.Spring Data JPA:
(1) is part of the larger Spring Data family
(2) enhanced support for JPA-based data access layer
(3) it is easier to build applications based on using Spring data access technology stack.
Common 6.Spring Data JPA APIs:
(1) CurdRepository:
(2) PagingAndSortingRepository:
7.Spring Data JPA custom API:
(1) query according to the method name, the naming of the method name must follow the rules
Integration of 8.Spring Data JPA,Hibernate and Spring Boot
(1) Environment configuration: MySql database, Hibernate framework, and Mysql connection driver
(2) modify the pom.xml file and add Spring Data JPA and database connection-driven dependencies:
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) start the project for testing, and we find that the project starts normally, indicating that our environment has been configured.
9. Integrated background coding:
(1) User entity: add @ Entity annotation, uniquely identify @ Id annotation, formulate primary key generation strategy, transform no-parameter structure into protected, prevent direct use, add toString method, and implement Serializable interface.
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 entity * * @ author QT * / @ Entitypublic class User implements Serializable {private static final long serialVersionUID = 516988700978313579L; @ Id / / primary key @ GeneratedValue (strategy = GenerationType.IDENTITY) / / self-increment policy private Long id / / unique identity of the user private String name; / / username private String email; / / user mailbox protected User () {/ / prevent direct use of 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) Resource library: write userRepository interface to inherit JpaRepository interface
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;/** * UserRepository interface * @ author QT * * / @ Repositorypublic interface UserRepository extends JpaRepository {}
(3) write userController classes to interact with the foreground
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 / * query all users * * @ param model * @ return * / @ GetMapping public ModelAndView getList (Model model) {model.addAttribute ("userList", userRepository.findAll ()); model.addAttribute ("title", "user Management"); return new ModelAndView ("users/list", "userModel", model) } / * query users according to 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", "user query") Return new ModelAndView ("users/view", "userModel", model);} / * create user * * @ param id * @ param model * @ return * / @ GetMapping ("/ form") public ModelAndView createForm (Model model) {model.addAttribute ("user", new User (null, null, null)) Model.addAttribute ("title", "create user"); return new ModelAndView ("users/form", "userModel", model);} / * add or modify users * * @ param user * @ return * / @ PostMapping public ModelAndView saveOrUpdateUser (User user) {user = userRepository.save (user) Return new ModelAndView ("redirect:/users", "userModel", user);} / * get delete user * * @ param id * @ return * / @ GetMapping ("/ delete/ {id}") public ModelAndView deleteUser (@ PathVariable ("id") Long id) {userRepository.deleteById (id);; return new ModelAndView ("redirect:/users") / / Redirect to list page} / * to modify the user interface * * @ 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", "modify user"); return new ModelAndView ("users/form", "userModel", model);}}
The front desk is the same as the previous project, just paste and copy it directly.
Foreground code project address:
Https://blog.51cto.com/13501268/2319622
Next, start the project, and the normal operation of the project indicates that the integration is successful. The next step is to persist the data to the database.
10. Persist the data to the database
(1) configure the database. The configuration file is as follows
# thymeleaf configure spring.thymeleaf.encoding=UTF-8# hot deployment static files without caching Real-time observe the effect of file modification spring.thymeleaf.cache=false# uses html5 standard spring.thymeleaf.mode=HTML5spring.thymeleaf.suffix=.htmlspring.resources.chain.strategy.content.enabled=true# database connection configuration spring.datasource.url=jdbc:mysql://localhost:3306/blog_test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8&useSSL=falsespring.datasource.username=rootspring.datasource.password=qitao1996spring.datasource.driver-class-name=com.mysql.jdbc.Driver#jpa configuration spring.jpa.show-sql=truespring.jpa.hibernate.ddl-auto=create-drop
(2) next, start the database and establish the database.
Next, when we start the project to operate, we will find that the database table has been created successfully, and we can also manipulate the data in the table.
At this point, the Spring boot+Spring Data Jpa+mevan+Thymeleaf integration has been completed successfully
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.