diff --git a/Readme.md b/Readme.md index 5c1f813..ae2690a 100644 --- a/Readme.md +++ b/Readme.md @@ -1,22 +1,22 @@ -## Spring Boot, PostgreSQL, JPA, Hibernate REST API Demo +## Spring Boot, MySQL, JPA, Hibernate REST API Demo ## Tutorial Check out the complete tutorial on the CalliCoder blog - -[Spring Boot, PostgreSQL, JPA, Hibernate RESTful CRUD API Example](https://www.callicoder.com/spring-boot-jpa-hibernate-postgresql-restful-crud-api-example/) +[Spring Boot, MySQL, JPA, Hibernate RESTful CRUD API Example](https://www.callicoder.com/spring-boot-jpa-hibernate-postgresql-restful-crud-api-example/) ## Steps to Setup **1. Clone the repository** ```bash -git clone https://github.com/callicoder/spring-boot-postgresql-jpa-hibernate-rest-api-demo.git +https://github.com/DouglasEleuterio/spring-boot-postgresql-jpa-hibernate-rest-api-demo.git ``` -**2. Configure PostgreSQL** +**2. Configure MySQL** -First, create a database named `postgres_demo`. Then, open `src/main/resources/application.properties` file and change the spring datasource username and password as per your PostgreSQL installation. +First, create a database named `demo`. Then, open `src/main/resources/application.properties` file and change the spring datasource username and password as per your MySQL installation. **3. Run the app** diff --git a/pom.xml b/pom.xml index 2b558f8..3695cc9 100644 --- a/pom.xml +++ b/pom.xml @@ -1,43 +1,42 @@ - 4.0.0 - - com.example - postgres-demo - 0.0.1-SNAPSHOT - jar - - postgres-demo - Demo project for Spring Boot - - - org.springframework.boot - spring-boot-starter-parent - 2.2.1.RELEASE - - - - - UTF-8 - UTF-8 - 1.8 - - - - - org.springframework.boot - spring-boot-starter-data-jpa - - - org.springframework.boot - spring-boot-starter-web - + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + + com.example + postgres-demo + 0.0.1-SNAPSHOT + jar + + postgres-demo + Demo project for Spring Boot + + + org.springframework.boot + spring-boot-starter-parent + 2.2.1.RELEASE + + + + + UTF-8 + UTF-8 + 1.8 + + + + + org.springframework.boot + spring-boot-starter-data-jpa + + + org.springframework.boot + spring-boot-starter-web + - org.postgresql - postgresql - runtime + mysql + mysql-connector-java org.springframework.boot @@ -46,14 +45,14 @@ - - - - org.springframework.boot - spring-boot-maven-plugin - - - + + + + org.springframework.boot + spring-boot-maven-plugin + + + diff --git a/src/main/java/com/example/postgresdemo/model/Answer.java b/src/main/java/com/example/postgresdemo/model/Answer.java index b5e48d0..95b9ee2 100644 --- a/src/main/java/com/example/postgresdemo/model/Answer.java +++ b/src/main/java/com/example/postgresdemo/model/Answer.java @@ -5,17 +5,17 @@ import org.hibernate.annotations.OnDeleteAction; import javax.persistence.*; +import java.io.Serializable; @Entity @Table(name = "answers") -public class Answer extends AuditModel { +public class Answer extends AuditModel implements Serializable { + + private static final long servialVersionUID = 1L; + @Id - @GeneratedValue(generator = "answer_generator") - @SequenceGenerator( - name = "answer_generator", - sequenceName = "answer_sequence", - initialValue = 1000 - ) + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; @Column(columnDefinition = "text") diff --git a/src/main/java/com/example/postgresdemo/model/Question.java b/src/main/java/com/example/postgresdemo/model/Question.java index d16a459..51213a1 100644 --- a/src/main/java/com/example/postgresdemo/model/Question.java +++ b/src/main/java/com/example/postgresdemo/model/Question.java @@ -3,17 +3,15 @@ import javax.persistence.*; import javax.validation.constraints.NotBlank; import javax.validation.constraints.Size; +import java.io.Serializable; @Entity @Table(name = "questions") -public class Question extends AuditModel { +public class Question extends AuditModel implements Serializable { + private static final long servialVersionUID = 1L; + @Id - @GeneratedValue(generator = "question_generator") - @SequenceGenerator( - name = "question_generator", - sequenceName = "question_sequence", - initialValue = 1000 - ) + @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @NotBlank diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 35b376a..40c5e00 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,10 +1,16 @@ -## Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties) -spring.datasource.url=jdbc:postgresql://localhost:5432/postgres_demo -spring.datasource.username= postgres -spring.datasource.password= +#Trabalhando com MySQL +spring.datasource.url=jdbc:mysql://localhost:3306/demo?useTimezone=true&serverTimezone=UTC&useSSL=false +spring.datasource.username=root +spring.datasource.password=root # The SQL dialect makes Hibernate generate better SQL for the chosen database -spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect +spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL8Dialect # Hibernate ddl auto (create, create-drop, validate, update) -spring.jpa.hibernate.ddl-auto = update +spring.jpa.hibernate.ddl-auto=update +spring.jpa.show-sql=true +spring.jpa.properties.hibernate.format_sql=true + +spring.datasource.hikari.maximum-pool-size=25 + +