如何禁用嵌入式数据库 Spring-boot spring-data-jpa
Posted
技术标签:
【中文标题】如何禁用嵌入式数据库 Spring-boot spring-data-jpa【英文标题】:How to disable embedded database Spring-boot spring-data-jpa 【发布时间】:2015-10-07 07:18:50 【问题描述】:我无法在本地设置的 Postgresql 数据库 (9.4) 上读/写。我的应用程序总是使用嵌入式数据库。当我在我的数据库上执行一些选择查询时,没有插入数据。你能检查我的文件吗?
我正在使用 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";
【问题讨论】:
我在你的 pom.xml 中看不到对嵌入式数据库(H2、HSQLDB 等)的依赖。您如何确定您的应用正在使用一个? 【参考方案1】:这个周末我找到了解决方案。
我的数据库中有一些带有大写字母的“客户”和“产品”表,但我的应用程序为我创建了“客户”和“产品”表。所以,它没有提取我的数据
:/
【讨论】:
以上是关于如何禁用嵌入式数据库 Spring-boot spring-data-jpa的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Spring-Boot 2 中禁用安全性? [复制]
如何在 spring-boot 中禁用 spring-data-mongodb 自动配置
如何避免使用 Spring-Boot 下载嵌入式 MongoDb
如何通过依赖项禁用通过 spring.factories 注册的 spring 工厂并保留此 spring-boot 依赖项?