如何禁用嵌入式数据库 Spring-boot spring-data-jpa

How to disable embedded database Spring-boot spring-data-jpa

我无法在本地设置我的 Postgresql 数据库 (9.4) Read/Write。我的应用程序始终使用嵌入式数据库。当我在我的数据库上执行一些 select 查询时,没有插入任何数据。你能检查一下我的文件吗?

我正在使用 Spring-boot 和 Spring-Data-JPA。

文件:application.properties

##DATABASE PROPERTIES
spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/myDataBase
spring.datasource.username=postgres
spring.datasource.password=admin
spring.data.jpa.repositories.enabled=true
spring.jpa.show-sql=true
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.database=postgresql
spring.jpa.hibernate.ddl-auto=create-drop

文件:pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>fr.mydomain</groupId>
    <artifactId>MyApp</artifactId>
    <version>0.1.0</version>
    <packaging>war</packaging>

    <name>MyApp</name>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.5.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.4-1201-jdbc41</version>
        </dependency>
    </dependencies>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

文件:Application.java

package fr.mydomain.myApp.config;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.boot.orm.jpa.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@SpringBootApplication
@ComponentScan({ "fr.mydomain.myApp.*" })
@EnableJpaRepositories(basePackages = "fr.mydomain.myApp.dao")
@EntityScan(basePackages = "fr.mydomain.myApp.model")
public class Application extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Override
    protected final SpringApplicationBuilder configure(final SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

}

文件:Customer.java

package fr.mydomain.myApp.model;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Customer implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    /** ID of the customer */
    private long customerID;

    @Column
    /** Lastname of the customer */
    private String lastname;

    @Column
    /** Firstname of the customer */
    private String firstname;

    protected Customer() {

    }

    public Customer(String lastname, String firstname) {
        this.lastname = lastname;
        this.firstname = firstname;
    }

    public long getCustomerID() {
        return customerID;
    }

    public void setCustomerID(long customerID) {
        this.customerID = customerID;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    @Override
    public String toString() {
        return String.format("Customer[CustomerID= %d, Lastname= '%s', Firstname= '%s'", customerID, lastname,
                firstname);
    }
}

文件:CustomerRepository.java

package fr.mydomain.myApp.dao;

import java.util.List;

import org.springframework.data.repository.CrudRepository;

import fr.ineatconseil.picomBO.model.Customer;

public interface CustomerRepository extends CrudRepository<Customer, Long> {

    List<Customer> findCustomerDistinctByLastnameOrFirstname(String lastname, String firstname);

}

文件:BeaconController.java

package fr.mydomain.myApp.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import fr.mydomain.myApp.dao.CustomerRepository;
import fr.mydomain.myApp.model.Customer;

@Controller
public class BeaconController {

    private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());

    @Autowired
    CustomerRepository customerRepository;

    @RequestMapping(value = "/Beacon", method = RequestMethod.GET)
    public String index() {

        customerRepository.save(new Customer("Jack", "Bauer"));
        customerRepository.save(new Customer("Johnny", "Joe"));
        customerRepository.save(new Customer("Bobby", "Bob"));
        customerRepository.save(new Customer("Jean", "Bonbeurre"));
        customerRepository.save(new Customer("Anna-Lyse", "Durine"));

        // fetch all customers
        System.out.println("Customers found with findAll():");
        System.out.println("-------------------------------");
        for (Customer customer : customerRepository.findAll()) {
            System.out.println(customer);
        }
        System.out.println();

        Customer customer = customerRepository.findOne(2L);
        System.out.println("Customer found with findOne(2L):");
        System.out.println("--------------------------------");
        System.out.println(customer);
        System.out.println();

        // fetch all customers by lastname or firstname
        System.out.println("Customers found with findCustomerDistinctByLastnameOrFirstname():");
        System.out.println("-------------------------------");
        for (Customer customer2 : customerRepository.findCustomerDistinctByLastnameOrFirstname("Jack", "Bob")) {
            System.out.println(customer2);
        }
        System.out.println();

        return "beacon";
    }
}

我这个周末找到了解决方案。

我的数据库中有一些带有大写字母的表 "Customer" 和 "Product",但我的应用程序为我创建了表 "customer" 和 "product"。所以,它没有提取我的数据

:/