![](/img/trans.png)
[英]How can I create a table in my postgres db with Spring Boot?
[英]why Liquibase does not create my table in postgres when spring boot microservce is started?
为什么当我使用 Java 8、Spring 启动、Liquibase 和 Postgresql 运行我的项目时,我在我的 postgres 数据库中看不到任何新表? 我安装了 PostgreSQL 11.6,
这是更改日志-master.xml:
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">
<include file="/db/changelog/changes/create-table-changelog-1.xml"/>
</databaseChangeLog>
这是 create-table-changelog-1.xml
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">
<changeSet author="admin" id="1">
<createTable tableName="person11">
<column autoIncrement="true" name="id" type="INT">
<constraints primaryKey="true"/>
</column>
<column name="name" type="VARCHAR(255)">
<constraints nullable="false"/>
</column>
<column name="address" type="VARCHAR(255)"/>
</createTable>
<!-- <rollback>
<dropTable tableName="person11"/>
</rollback>-->
</changeSet>
</databaseChangeLog>
这是 application.properties:
spring.liquibase.changeLog = classpath:/db/changelog/changelog-master.xml
spring.datasource.url= jdbc:postgresql://localhost:5432/
spring.datasource.username=postgres
spring.datasource.password=postgres
这是 pom.xml 文件:
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
<version>3.8.9</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.14</version>
<scope>compile</scope>
</dependency>
我认为您还应该在数据源 url 的末尾指定数据库名称,例如jdbc:postgresql://localhost:5432/DB_NAME
您必须确保实现 JPA 以使 JPA 与数据库交互。 我一般使用hibernate自带的依赖spring-boot-starter-data-jpa
,在pom文件中添加依赖。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
您可能还需要在pom.xml
中添加以下依赖项:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
如果您使用的是 Spring Boot 2,那么您还需要在application.properties
文件中将属性名称更改为spring.liquibase.change-log
。
谢谢大家的回答,当我点击这个链接时,我的问题就解决了。 在此链接中有一个集成 Spring-Boot、JPA 和 Liquibase 的示例: https://auth0.com/postgre-li-quibase/data-jpa-
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.