We talked about the spring boot integration jdbctemplate for data persistence,
In this article, we will be using spring boot to integrate JPA to achieve the persistence of data.
First, the code implementation
- Modify Pom to introduce dependency
<!-- Introducing JPA dependency --> < dependency Span style= "COLOR: #0000ff" >> < gr Oupid > org.springframework.boot</ groupid > < artifactid > Spring-boot-starter-data-jpa</ artifactid > </ dependency >
- Modify Application.properties, configure related information
#修改tomcat默认端口号server. port=8090# Modifying the context pathserver.context-path=/test# Configuring data source Information Spring.datasource.driver-class-name=com.mysql.jdbc.driverspring.datasource.url=jdbc:mysql://localhost : 3306/testspring.datasource.username=rootspring.datasource.password=root# Configuration jpaspring.jpa.hibernate.ddl-auto= Updatespring.jpa.show-sql=truespring.jackson.serialization.indent_output=true
- Create an entity class
Packagecom.study.entity;Importjavax.persistence.Entity;ImportJavax.persistence.GeneratedValue;ImportJavax.persistence.GenerationType;Importjavax.persistence.Id;Importjavax.persistence.Table; @Entity @table (name= "T_user") Public classUser {@Id @GeneratedValue (strategy=Generationtype.auto)PrivateInteger ID; PrivateString UserName; PrivateString password; PublicInteger getId () {returnID; } Public voidsetId (Integer id) { This. ID =ID; } PublicString GetUserName () {returnUserName; } Public voidsetusername (String userName) { This. UserName =UserName; } PublicString GetPassword () {returnpassword; } Public voidSetPassword (String password) { This. Password =password; }}
- Create the Repository interface and inherit the Crudrepository
Packagecom.study.repository;ImportOrg.springframework.data.jpa.repository.Query;Importorg.springframework.data.repository.CrudRepository;ImportOrg.springframework.data.repository.query.Param;ImportCom.study.entity.User;/*** Note: * 1. Here is interface, not class * * 2.CrudRepository inside the generics, the first is the entity class, the second is the type of the primary key * 3. Since there are already some interfaces in crudrepository, such as Del Eteall,findone and so on, we call directly can * 4. Of course, we can also implement their own interface according to their own situation, such as the following GetUser () method, JPQL statement and HQL statement almost * **/ Public InterfaceUserrepositoryextendsCrudrepository<user, integer> { /*** We only need to write the interface here, do not need to write the implementation, spring boot will help automatically implement * **/@Query ("From User where ID =:id") PublicUser GetUser (@Param ("id") (Integer ID);}
- Create service
- Interface
Package Com.study.service; Import Com.study.entity.User; Public Interface UserService { public User getUser (Integer ID);}
- Realize
PackageCom.study.service.impl;Importorg.springframework.beans.factory.annotation.Autowired;ImportOrg.springframework.stereotype.Service;ImportCom.study.entity.User;Importcom.study.repository.UserRepository;ImportCom.study.service.UserService; @Service Public classUserserviceimplImplementsuserservice {@Autowired userrepository repository; @Override PublicUser GetUser (Integer id) {//There are two ways of doing this://1. Calling the Crudrepository interface//return Repository.findone (ID); //2. Call our own Write interface returnRepository.getuser (ID); } }
- Create a Controller
PackageCom.study.controller;Importorg.springframework.beans.factory.annotation.Autowired;Importorg.springframework.web.bind.annotation.PathVariable;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RestController;ImportCom.study.entity.User;ImportCom.study.service.UserService; @RestController Public classUsercontroller {@Autowired userservice service; @RequestMapping ("/getuser/{id}") PublicUser GetUser (@PathVariable ("id") (Integer ID) {returnService.getuser (ID); }}
- Test, page displays database values in JSON format
Ii. extension of knowledge points
About the repository knowledge point, you can go to the following article
1190000012346333
Spring Boot series Four: Spring Boot integrated JPA