Spring Boot H2数据库新增修改查询删除

Posted JAVA微编程

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Boot H2数据库新增修改查询删除相关的知识,希望对你有一定的参考价值。

#Java#Spring#SpringBoot#H2#数据库#新增#修改#查询#删除#

SpringBoot H2数据库新增、修改、查询、删除

视频讲解: https://www.bilibili.com/video/av83944935/

H2Application.java
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.java
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.java
package com.example.h2;

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

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

application.properties


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分钟视频学习

以上是关于Spring Boot H2数据库新增修改查询删除的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot 上 H2 数据库的 HTTP 删除

Spring Boot制作个人博客-后端管理(博客管理)

Spring Boot制作个人博客-后端管理(博客管理)

Spring boot H2 数据库应用

Spring Boot整合区块链—区块链服务开发实例

通过 Spring Boot JPA + Postgresql + H2 执行间隔