What is JPA
JPA stands for Java Persistence API.JPA objects through JDK 5.0 annotations or XML description - mapping between relational tables, solid objects and run persistence to the database.
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.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.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
apllication.yml
server: port: 8080 spring: datasource: driver-class-name: com.mysql.jdbc.Driver username: root password: root url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&serverTimezone=GMT%2B8 jpa: hibernate: ddl-auto: update show-sql: true
Dao
Import com.vast.entity.Account; Import org.springframework.data.jpa.repository.JpaRepository; CRUD method / ** JPA provided, it can be invoked directly. Account database corresponding to the generic entity class simply more operations; Integer refers to the type of the primary key of the corresponding entity class * / public interface IAccountDao the extends JpaRepository <Account, Integer> { }
Controller (request method is Restfull style)
package com.vast.controller; import com.vast.dao.IAccountDao; import com.vast.entity.Account; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController @RequestMapping("/account") public class HomeController { @Autowired private IAccountDao accountDao; @Value("${my.name}") private String name; @Value("${my.age}") private int age; @GetMapping("/home") public String homePage(){ return name.concat("13__" + age); } @GetMapping("/{id}") public Account getAccountById(@PathVariable(value = "id") int id){ return accountDao.findOne(id); } @GetMapping("/list") public List<Account> getAccounts(){ return accountDao.findAll(); } @RequestMapping(value = "",method = RequestMethod.POST) public String postAccount(@RequestParam(value = "name") String name, @RequestParam(value = "money") double money) { Account account = new Account(); account.setName(name); account.setMoney(money); Account saveAccountResult = accountDao.save(account); return saveAccountResult.toString(); } @PutMapping("/{id}") public String updateAccountById(@PathVariable (value = "id") int id, @RequestParam(value = "name",required=false) String name, @RequestParam(value = "money",required=false) double money){ Account account = new Account(); account.setName(name); account.setMoney(money); account.setId(id); Account account1 = accountDao.saveAndFlush(account); return account1.toString(); } }