为什么 Liquibase 在 spring 引导微服务启动时不在 postgres 中创建我的 table?
why Liquibase does not create my table in postgres when spring boot microservce is started?
为什么当我 运行 我的项目使用 Java 8,Spring Boot,Liquibase 和 Postgresql 时,我在我的 postgres 中看不到任何新的 table数据库?
我安装了 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>
这是创建-table-更新日志-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
。
谢谢大家的回答,
当我遵循这个 link 时,我的问题就解决了。在这个 link 中有一个集成 Spring-Boot、JPA 和 Liquibase 的示例:
https://auth0.com/blog/integrating-spring-data-jpa-postgresql-liquibase/
为什么当我 运行 我的项目使用 Java 8,Spring Boot,Liquibase 和 Postgresql 时,我在我的 postgres 中看不到任何新的 table数据库? 我安装了 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>
这是创建-table-更新日志-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
。
谢谢大家的回答, 当我遵循这个 link 时,我的问题就解决了。在这个 link 中有一个集成 Spring-Boot、JPA 和 Liquibase 的示例: https://auth0.com/blog/integrating-spring-data-jpa-postgresql-liquibase/