#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;@SpringBootApplicationpublic 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; @RestControllerpublic 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@Datapublic 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.Driverspring.datasource.username=saspring.datasource.password=spring.jpa.database-platform=org.hibernate.dialect.H2Dialectspring.jpa.hibernate.ddl-auto=update spring.h2.console.enabled=truespring.h2.console.path=/h2spring.h2.console.settings.trace=falsespring.h2.console.settings.web-allow-others=false
公众号,坚持每天3分钟视频学习
原文地址:https://www.cnblogs.com/JavaWeiBianCheng/p/12219281.html
时间: 2024-10-11 13:11:21