java 为什么不创建数据库,表在数据库中的MySQL自动?

enxuqcxy  于 2023-03-28  发布在  Java
关注(0)|答案(1)|浏览(147)

目前,我正在开发一个Web应用程序使用Angular,Sping Boot ,MySQL。我需要在MySQL中自动创建和更新数据库和表。但我的计算机没有自动创建数据库,并给出了一个后端错误“未知数据库db”。然后我手动创建数据库。然后后端正确构建,但仍然没有创建表。
应用程序属性代码:

server.error.whitelabel.enabled=false
spring.datasource.url=jdbc:mysql://localhost:3306/db?useSSL=false
spring.datasource.username=root
spring.datasource.password=1234
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.database-platform = org.hibernate.dialect.MySQLDialect
spring.jpa.generate-ddl=true
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update

customer.java 代码:

package com.Manipulus.arctic.customer.model;

import jakarta.persistence.*;

import java.io.Serializable;

@Entity
@Table(name = "customers")
public class Customer implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(nullable = false,updatable = false)
    private long id;

    @Column(name = "nic_number" )
    private String nic_number;

    @Column(name = "customer_name")
    private String name;

    @Column(name = "contact_number")
    private int contactNumber;

    @Column(name = "address")
    private String address;

    @Column(name = "contact_person_name")
    private String contactPersonName;

    @Column(name = "designation")
    private String designation;

    @Column(name = "email")
    private String email;

    @Column(nullable = false,updatable = false)
    private String customerCode;

    public String getCustomerCode() {
        return customerCode;
    }

    public void setCustomerCode(String customerCode) {
        this.customerCode = customerCode;
    }

    public Customer(){

    }
    public Customer(String nic_number, String name, int contactNumber, String address, String contactPersonName, String designation, String email,String customerCode) {
        this.nic_number = nic_number;
        this.name = name;
        this.customerCode =customerCode;
        this.contactNumber = contactNumber;
        this.address = address;
        this.contactPersonName = contactPersonName;
        this.designation = designation;
        this.email = email;
    }

    public String getNic_number() {
        return nic_number;
    }

    public void setNic_number(String nic) {
        this.nic_number = nic;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getContactNumber() {
        return contactNumber;
    }

    public void setContactNumber(int contactNumber) {
        this.contactNumber = contactNumber;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getContactPersonName() {
        return contactPersonName;
    }

    public void setContactPersonName(String contactPersonName) {
        this.contactPersonName = contactPersonName;
    }

    public String getDesignation() {
        return designation;
    }

    public void setDesignation(String designation) {
        this.designation = designation;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public String toString(){
        return "Customer{" +
                "id=" + id +
                "nic_number=" + nic_number +
                "name=" +name +
                "contactNumber=" +contactNumber +
                "address="+ address +
                "contactPersonName="+contactPersonName+
                "designation="+ designation +
                "email="+email+
                '}';

    }

}

CustomerResource.java 代码:

package com.Manipulus.arctic.customer;

import com.Manipulus.arctic.customer.model.Customer;
import com.Manipulus.arctic.customer.service.CustomerService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/customer")
public class CustomerResource {
    private final CustomerService customerService;

    public CustomerResource(CustomerService customerService) {
        this.customerService = customerService;
    }

    @GetMapping("/all")
    public ResponseEntity<List<Customer>> getAllCustomer(){
        List<Customer> customers = customerService.findAllCustomers();
        return new ResponseEntity<>(customers, HttpStatus.OK);
    }

    @GetMapping("/find/{id}")
    public ResponseEntity<Customer> getCustomerById(@PathVariable("id") Long id){
       Customer customer = customerService.findCustomerById(id);
        return new ResponseEntity<>(customer, HttpStatus.OK);
    }

    @PostMapping("/add")
    public ResponseEntity<Customer> addCustomer(@RequestBody Customer customer){
        Customer newCustomer = customerService.addCustomer(customer);
        return new ResponseEntity<>(newCustomer, HttpStatus.CREATED);
    }

    @PutMapping("/update")
    public ResponseEntity<Customer> updateCustomer(@RequestBody Customer customer){
        Customer updateCustomer = customerService.updateCustomer(customer);
        return new ResponseEntity<>(updateCustomer, HttpStatus.CREATED);
    }

    @DeleteMapping ("/delete/{id}")
    public void deleteCustomer(@PathVariable("id") Long id){
        customerService.deleteCustomerById(id);
    }


//        return new ResponseEntity<>(HttpStatus.OK);

//    @DeleteMapping("/employees/{id}")
//    public ResponseEntity<Map<String,Boolean>> deleteEmployee(@PathVariable long id){
//        Employee emp = emp_repo.findById(id).orElseThrow(() -> new ResourceNotFoundException("Employee not exist with id: "+id));
//
//        emp_repo.delete(emp);
//        Map<String,Boolean> response = new HashMap<>();
//        response.put("Deleted",Boolean.TRUE);
//        return ResponseEntity.ok(response);
//    }

}

我使用的Java版本是18。我的前端或后端在编译或运行时没有显示任何类型的错误。但在Web应用程序的界面上,它显示“http://localhost:8080/customer/all的Http失败响应:0未知错误”我认为我的后端无法正确连接MySQL。而且我是一个初学者。所以我请求帮助我解决这个错误。

rhfm7lfc

rhfm7lfc1#

Spring Data JPA不会为您创建数据库。您必须在运行应用程序之前确保数据库存在,因此您必须手动创建它。这就是为什么您会看到“unknown database db”错误。
spring.jpa.hibernate.ddl-auto=update
上述配置将使hib检查您的实体和架构之间的差异已经存在的数据库。如果发现任何差异,将更新您的表。这也包括创建一个新的实体保存在您的数据库中。
关于你的第二个问题
http://localhost:8080/customer/all的Http失败响应:0未知错误
您是否为此端点创建了一个REST端点?请仔细检查服务器是否正在运行,端点URL是否正确。我建议您下载一个名为Postman的工具,它提供了一种更快的方法来测试您的API端点。

相关问题