- 👽
Criação do Projeto Spring 🔗
com as dependencias Lombok, Spring WEB, MySQL Driver, Liquibase Migration e Spring Data JPA
2. ⚙ Configurações do Bando de Dados, JPA e Liquibase ▾
src/main/resources/
application.properties
# BANCO DE DADOS CONFIG #
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/filmes
spring.datasource.username=root
spring.datasource.password=
# JPA CONFIG #
spring.jpa.show-sql=true
spring-jpa.properties.hibernate.format_sql=true
spring.jpa.hibernate.ddl-auto=none
# LIQUIBASE CONFIG #
spring.liquibase.enabled=true
spring.liquibase.drop-first=false
spring.liquibase.change-log=classpath:db/master.xml
3. ⚙ Configuração Liquibase Changelog (master.xml) ▾
src/main/resources/db/
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"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.3.xsd">
<property name="bigint" value="java.sql.Types.BIGINT" dbms="mariadb, mysql, postgresql"/>
<property name="double" value="java.sql.Types.DOUBLE" dbms="mariadb, mysql, postgresql"/>
<property name="string" value="java.sql.Types.VARCHAR(255)" dbms="mariadb, mysql, postgresql"/>
<property name="text" value="java.sql.Types.LONGVARCHAR" dbms="mariadb, mysql, postgresql"/>
<property name="date" value="java.sql.Types.DATE" dbms="mariadb, mysql, postgresql"/>
<includeAll path="changelog" relativeToChangelogFile="true"></includeAll>
</databaseChangeLog>
4. ⛏️ Liquibase Changelog (INIT.xml) ▾
src/main/resources/db/changelog/
INIT.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"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.3.xsd">
<changeSet id="0" author="hugo_"> <!-- Diretor -->
<preConditions onFail="MARK_RAN" onFailMessage="tabela diretor já existe!">
<not>
<tableExists tableName="diretor"/>
</not>
</preConditions>
<createTable tableName="diretor">
<column name="id" type="${bigint}" autoIncrement="true">
<constraints primaryKey="true" primaryKeyName="pk_diretor" nullable="false"></constraints>
</column>
<column name="nome" type="${string}">
<constraints nullable="false"></constraints>
</column>
</createTable>
<addAutoIncrement tableName="diretor" columnName="id" columnDataType="${bigint}" incrementBy="1" startWith="1" />
</changeSet>
<changeSet id="1" author="hugo_"> <!-- Filme -->
<preConditions onFail="MARK_RAN" onFailMessage="tabela filme já existe!" >
<not>
<tableExists tableName="filme"/>
</not>
</preConditions>
<createTable tableName="filme">
<column name="id" type="${bigint}" autoIncrement="true">
<constraints primaryKey="true" primaryKeyName="pk_filme" nullable="false"></constraints>
</column>
<column name="title" type="${string}">
<constraints nullable="false"></constraints>
</column>
<column name="data" type="${date}"/>
<column name="poster" type="${string}"/>
<column name="orcamento" type="${double}"/>
<column name="receita" type="${double}"/>
<column name="diretor_id" type="${bigint}"/>
</createTable>
<addAutoIncrement tableName="filme" columnName="id" columnDataType="${bigint}" incrementBy="1" startWith="1" />
</changeSet>
<changeSet id="2" author="hugo_"> <!-- Gênero -->
<preConditions onFail="MARK_RAN" onFailMessage="tabela genero já existe!">
<not>
<tableExists tableName="genero"/>
</not>
</preConditions>
<createTable tableName="genero">
<column name="id" type="${bigint}" autoIncrement="true">
<constraints primaryKey="true" primaryKeyName="pk_genero" nullable="false"></constraints>
</column>
<column name="nome" type="${string}">
<constraints nullable="false"/>
</column>
</createTable>
<addAutoIncrement tableName="genero" columnName="id" columnDataType="${bigint}" incrementBy="1" startWith="1" />
</changeSet>
<changeSet id="3" author="hugo_"> <!-- Generos_do_filme (associação) -->
<preConditions onFail="MARK_RAN" onFailMessage="generos_do_filme ja existe">
<not>
<tableExists tableName="generos_do_filme"/>
</not>
</preConditions>
<createTable tableName="generos_do_filme" >
<column name="id_filme" type="${bigint}">
<constraints nullable="false" />
</column>
<column name="id_genero" type="${bigint}">
<constraints nullable="false"/>
</column>
</createTable>
<addPrimaryKey tableName="generos_do_filme" columnNames="id_filme, id_genero" constraintName="pk_genero_filme"/>
</changeSet>
<changeSet id="4" author="hugo_"> <!-- ForeignKeys -->
<addForeignKeyConstraint constraintName="fk_diretor"
baseTableName="filme"
baseColumnNames="diretor_id"
referencedTableName="diretor"
referencedColumnNames="id"
onDelete="SET NULL"/>
<addForeignKeyConstraint constraintName="fk_filme"
baseTableName="generos_do_filme"
baseColumnNames="id_filme"
referencedTableName="filme"
referencedColumnNames="id"
onDelete="CASCADE"/>
<addForeignKeyConstraint constraintName="fk_genero"
baseTableName="generos_do_filme"
baseColumnNames="id_genero"
referencedTableName="genero"
referencedColumnNames="id"
onDelete="CASCADE"/>
<sqlFile path="import.sql" stripComments="true" />
</changeSet>
</databaseChangeLog>