要會(huì)接口測(cè)試,首先得會(huì)寫(xiě)一個(gè)接口,發(fā)現(xiàn)用jpa寫(xiě)一個(gè)接口真的超級(jí)方便方便
首先導(dǎo)包

image
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.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">
<parent>
<artifactId>usermanager</artifactId>
<groupId>com.fengys.test</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>managerJpa</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- jpa依賴(lài) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- mysql依賴(lài) -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.14</version>
</dependency>
<!-- 熱部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<!-- optional=true, 依賴(lài)不會(huì)傳遞, 該項(xiàng)目依賴(lài)devtools; 之后依賴(lài)boot項(xiàng)目的項(xiàng)目如果想要使用devtools, 需要重新引入 -->
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
實(shí)體類(lèi)User
package com.fengys.entity;
import lombok.Data;
import javax.persistence.*;
@Entity
@Data
@Table(name="t_user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private StringuserName;
private Stringpassword;
private Stringage;
private Stringsex;
private StringisDelete;
private Stringpermission;
}
這里用了lombok ,get set方法都不用寫(xiě)了
SpringbootJpaApplication啟動(dòng)類(lèi):
package com.fengys;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringbootJpaApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootJpaApplication.class, args);
}
}
IUserDto接口:
package com.fengys.dto;
import com.fengys.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
public interface IUserDtoextends extends JpaRepository<User,Integer> {
}
繼承了JpaRepository,里面自帶了很多方法,可以進(jìn)去看下源碼
UserController:
package com.fengys.controller;
import com.fengys.dto.IUserDto;
import com.fengys.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class UserController {
@Autowired
IUserDtodto;
@GetMapping("/insert")
public Userinsertuser(User user){
User save =dto.save(user);
return save;
}
@GetMapping("/all")
public ListfindAll(){
return dto.findAll();
}
@GetMapping(value="/user/{id}")
public Usergetuser(@PathVariable("id") Integer id){
User user =dto.findOne(id);
return user;
}
}
application.yml
spring:
profiles:
active: test
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3307/fengys?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
username: root
password:
jpa:
database: MYSQL
hibernate:
ddl-auto: update
show-sql: true
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: UTC
server:
port: 8889
application-test.yml
spring:
devtools:
restart:
enabled: true
additional-paths: src/main/java
啟動(dòng)BOOT
用postman調(diào)用接口

image