Seguindo o enunciado do desafio, eu tinha encontrado um tutorial de como conectar o Spring Boot no MySQL: https://spring.io/guides/gs/accessing-data-mysql.
Eu cheguei modificar os seguintes arquivos:
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-docker-compose</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-testcontainers</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mysql</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
...
spring:
datasource:
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/forum
username: 'myuser'
password: 'secret'
jpa:
database-platform: org.hibernate.dialect.MySQL8Dialect
defer-datasource-initialization: true
properties:
hibernate:
show_sql: true
format_sql: true
E cheguei a criar este arquivo:
services:
mysql:
image: 'mysql:latest'
environment:
- 'MYSQL_DATABASE=forum'
- 'MYSQL_PASSWORD=secret'
- 'MYSQL_ROOT_PASSWORD=verysecret'
- 'MYSQL_USER=myuser'
ports:
- '3306:3306'
O problema que estou tendo é que quando executo o postman com uma requisição GET em http://localhost:8080/topicos , recebo a seguinte resposta:
{
"timeStamp": "2024-09-20T09:11:26.4223736",
"status": 500,
"error": "INTERNAL_SERVER_ERROR",
"message": "JDBC exception executing SQL [select t1_0.id,t1_0.autor_id,t1_0.curso_id,t1_0.data_criacao,t1_0.mensagem,t1_0.status,t1_0.titulo from topico t1_0] [Table 'forum.topico' doesn't exist] [n/a]; SQL [n/a]",
"path": "/topicos"
}
E é mostrada a seguinte mensagem de erro no console do IntelliJ:
2024-09-20T09:11:26.103-03:00 INFO 1384 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2024-09-20T09:11:26.103-03:00 INFO 1384 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2024-09-20T09:11:26.105-03:00 INFO 1384 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 2 ms
Hibernate:
select
t1_0.id,
t1_0.autor_id,
t1_0.curso_id,
t1_0.data_criacao,
t1_0.mensagem,
t1_0.status,
t1_0.titulo
from
topico t1_0
2024-09-20T09:11:26.392-03:00 WARN 1384 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 1146, SQLState: 42S02
2024-09-20T09:11:26.392-03:00 ERROR 1384 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : Table 'forum.topico' doesn't exist
2024-09-20T09:11:26.465-03:00 WARN 1384 --- [nio-8080-exec-1] .m.m.a.ExceptionHandlerExceptionResolver : Resolved [org.springframework.dao.InvalidDataAccessResourceUsageException: JDBC exception executing SQL [select t1_0.id,t1_0.autor_id,t1_0.curso_id,t1_0.data_criacao,t1_0.mensagem,t1_0.status,t1_0.titulo from topico t1_0] [Table 'forum.topico' doesn't exist] [n/a]; SQL [n/a]]
Pelo que é mostrado nessas mensagens, a tabela 'forum.topico' não está sendo criada, quando o Spring Boot inicializa junto com o MySQL no Docker.
O que eu devo fazer para que o Spring Boot crie essa tabela?