• First of all, you need to include some dependencies in order to implement JPA repositories, unit tests, and Spring Boot. In addition, there are several other dependencies that can help with launching a Spring Boot application.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.2.6.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.1.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.1.0.Final</version>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.32</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
Note: In order to ensure compatibility with the Spring Boot version you are using, it is necessary to specify the appropriate version of Hibernate. For example, in this application, we are using version `5.1.0.Final`.
• What does the folder structure of a Spring Boot application look like? Here is an example folder structure that can be referenced:
└── src
├── main
│ ├── java
│ │ └── com
│ │ └── flagtick
│ │ ├── JpaConfig.java
│ │ ├── SampleDataJpaApplication.java
│ │ ├── entity
│ │ │ └── User.java
│ │ ├── repository
│ │ │ └── UserRepository.java
│ │ └── controller
│ │ └── UserController.java
│ └── resources
│ ├── application.properties
│ └── log4j2.xml
└── test
└── java
└── com
└── flagtick
└── repository
└── UserRepositoryIntegrationTests.java
» JpaConfig.java
package com.flagtick;
import org.springframework.boot.orm.jpa.EntityScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@Configuration
@EnableJpaRepositories(basePackages = "com.flagtick.repository")
@EntityScan(basePackages = "com.flagtick.entity")
public class JpaConfig {
// TODO
}
» SampleDataJpaApplication.java
package com.flagtick;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@ComponentScan
@EnableAutoConfiguration
@Import(JpaConfiguration.class)
public class SampleDataJpaApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(SampleDataJpaApplication.class, args);
}
}
» UserRepository.java
package com.flagtick.repository;
import com.flagtick.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
interface UserRepository extends JpaRepository<User, Long> {
}
» User.java
package com.flagtick.entity;
import lombok.*;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;
import javax.persistence.*;
import java.sql.Date;
@Entity
@Table(name = "users")
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
@ToString
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@Column(nullable = false)
private String email;
private String password;
private String avatar;
@CreationTimestamp
private Date createdAt;
@UpdateTimestamp
private Date updatedAt;
}
» UserRepositoryIntegrationTests.java
package com.flagtick.repository;
import com.flagtick.SampleDataJpaApplication;
import com.flagtick.entity.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = SampleDataJpaApplication.class)
public class UserRepositoryIntegrationTests {
@Autowired
UserRepository userRepository;
@Test
public void saveUserTest() {
User user = new User();
user.setName("Flagtick Group");
user.setPassword("Abc@123456");
user.setEmail("[email protected]");
User savedUser = userRepository.save(user);
System.out.println(savedUser.toString());
}
}
» application.properties
server.port=8087
spring.datasource.url=jdbc:mysql://localhost:3306/springbootcrud?useSSL=false
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# Use for communicating with a MySQL database
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
# This is one of behavior of Hibernate and automatically update the database schema based on any
# changes to the entity classes
spring.jpa.hibernate.ddl-auto=create
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
Normally the console or terminal where the application is running and prints output to the standard output stream. Hence, we can see the result as below:
Hibernate:
insert
into
users
(avatar, createdAt, email, name, password, updatedAt)
values
(?, ?, ?, ?, ?, ?)
User(id=1, name=Flagtick Group, [email protected], password=Abc@123456, avatar=null, createdAt=2023-04-30, updatedAt=2023-04-30)