Spring Boot(3) H2數據庫新增、修改、查詢、刪除

Java#Spring#SpringBoot#H2#數據庫#新增#修改#查詢#刪除

SpringBoot H2數據庫新增、修改、查詢、刪除java

視頻講解: https://www.bilibili.com/vide...

H2Application.javaweb

package com.example.h2;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class H2Application {
   public static void main(String[] args) {
      SpringApplication.run(H2Application.class, args);
   }
}

EmployeeController.javaspring

package com.example.h2;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class EmployeeController {
    @Autowired
    private EmployeeRep employeeRep;

    @GetMapping("/list")
    public List<Employee> findAll(){
        return employeeRep.findAll();
    }

    @PostMapping("/save")
    public Boolean save(@RequestBody Employee employee){
        employeeRep.save(employee);
        return Boolean.TRUE;
    }
    @PutMapping("/update")
    public Boolean update(@RequestBody Employee employee){
        employeeRep.save(employee);
        return Boolean.TRUE;
    }

    @DeleteMapping("/delete/{id}")
    public Boolean delete(@PathVariable Long id){
        employeeRep.deleteById(id);
        return Boolean.TRUE;
    }


}

Employee.java數據庫

package com.example.h2;

import lombok.Data;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
@Data
public class Employee {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String name;
}

EmployeeRep.javaapp

package com.example.h2;

import org.springframework.data.jpa.repository.JpaRepository;

public interface EmployeeRep extends JpaRepository<Employee,Long> {
}

application.propertieside

spring.datasource.url=jdbc:h2:D://data//test2;DB_CLOSE_DELAY=-1;
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=update

spring.h2.console.enabled=true
spring.h2.console.path=/h2
spring.h2.console.settings.trace=false
spring.h2.console.settings.web-allow-others=false

公衆號,堅持天天3分鐘視頻學習
學習

相關文章
相關標籤/搜索