zoukankan      html  css  js  c++  java
  • Spring Boot(3) 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分钟视频学习

  • 相关阅读:
    牛客练习赛53 A-E
    算导第二章笔记 (归并排序 之 插入排序优化)
    LightOJ 1372 (枚举 + 树状数组)
    LightOJ 1348 (树链剖分 + 线段树(树状数组))
    Light OJ 1343
    Light OJ 1266
    Light OJ 1085
    CodeForces 671C
    Codeforces Round #352 (Div. 2) (A-D)
    ZOJ1008
  • 原文地址:https://www.cnblogs.com/JavaWeiBianCheng/p/12219281.html
Copyright © 2011-2022 走看看