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

  • 相关阅读:
    datanode报错Problem connecting to server
    使用命令查看hdfs的状态
    Access denied for user root. Superuser privilege is requ
    ElasticSearch默认的分页参数 size
    SparkStreaming Kafka 维护offset
    【容错篇】Spark Streaming的还原药水——Checkpoint
    251 Android 线性与相对布局简介
    250 Android Studio使用指南 总结
    249 如何解决项目导入产生的中文乱码问题
    248 gradle更新问题
  • 原文地址:https://www.cnblogs.com/JavaWeiBianCheng/p/12219281.html
Copyright © 2020-2023  润新知