要会接口测试,首先得会写一个接口,发现用jpa写一个接口真的超级方便方便
首先导包
<?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依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- mysql依赖 -->
<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, 依赖不会传递, 该项目依赖devtools; 之后依赖boot项目的项目如果想要使用devtools, 需要重新引入 -->
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
实体类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方法都不用写了
SpringbootJpaApplication启动类:
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,里面自带了很多方法,可以进去看下源码
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
启动BOOT
用postman调用接口