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

时间:2015-07-17 14:19:56

标签: postgresql spring-boot spring-data spring-data-jpa

我无法在本地设置的Postgresql数据库(9.4)上进行读/写操作。我的应用总是使用嵌入式数据库。当我在我的数据库上执行一些选择查询时,没有插入数据。你能查一下我的文件吗?

我在Spring-Data-JPA中使用Spring-boot。

文件: 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";
    }
}

1 个答案:

答案 0 :(得分:0)

我在本周末找到了解决方案。

我有一些桌子&#34;客户&#34;和&#34;产品&#34;我的数据库中有大写字母和数据但是我的应用程序创建了表格&#34; customer&#34;和&#34;产品&#34;。所以,它没有提取我的数据

:/