用springboot-jpa写一个用户新增查询接口

要会接口测试,首先得会写一个接口,发现用jpa写一个接口真的超级方便方便

首先导包


用springboot-jpa写一个用户新增查询接口_第1张图片
image



    

        usermanager

        com.fengys.test

        1.0-SNAPSHOT

    

    4.0.0

    managerJpa



        

            org.springframework.boot

            spring-boot-starter

        

        

            org.springframework.boot

            spring-boot-starter-test

            test

        

        

        

            org.springframework.boot

            spring-boot-starter-data-jpa

        

        

        

            mysql

            mysql-connector-java

            runtime

        



org.projectlombok

lombok

1.16.14



        

        

            org.springframework.boot

            spring-boot-devtools

            

            true

        

    

    

        

            

                org.springframework.boot

                spring-boot-maven-plugin

            

        

    


实体类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  {

}

继承了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调用接口


用springboot-jpa写一个用户新增查询接口_第2张图片
image

你可能感兴趣的:(用springboot-jpa写一个用户新增查询接口)