Spring Boot Tutorial
Spring Boot - Software Setup and Configuration (STS/Eclipse/IntelliJ)
Prerequisite (Spring Core Concepts)
Spring Boot Core
Spring Boot with REST API
Spring Boot with Database and Data JPA
Spring Boot with Kafka
Spring Boot with AOP
To integrate MySQL with a Spring Boot Maven project, you would primarily make use of the Spring Data JPA project. Here's how you can set up MySQL integration with Spring Boot step by step:
Start by adding the required dependencies to your pom.xml
:
<!-- Spring Boot Starter Data JPA --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- MySQL JDBC Driver --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>
Update your application.properties
or application.yml
with the necessary MySQL configurations:
# MySQL configurations spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase?useSSL=false&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=my-secret-pw spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.jpa.hibernate.ddl-auto=update spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
Replace mydatabase
, root
, and my-secret-pw
with your database name, username, and password respectively. Adjust the host and port if necessary.
Create your model (entity) classes and annotate them with JPA annotations:
import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; private String email; // Getters, setters, constructors... }
Spring Data JPA provides the CrudRepository
or JpaRepository
interface, which offers CRUD operations:
import org.springframework.data.jpa.repository.JpaRepository; public interface UserRepository extends JpaRepository<User, Long> { // Additional custom query methods (if needed) }
Inject and use the repositories in your service classes:
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.Optional; @Service public class UserService { @Autowired private UserRepository userRepository; public Iterable<User> getAllUsers() { return userRepository.findAll(); } public Optional<User> getUserById(Long id) { return userRepository.findById(id); } public User saveUser(User user) { return userRepository.save(user); } // Other CRUD operations... }
After you've set up the above configurations and code, you can run your Spring Boot application. Make sure that your MySQL server is running and you've created the database you've mentioned in the configuration (mydatabase
in this example).
HikariCP
connection pool which is highly optimized.spring.jpa.hibernate.ddl-auto
property as per your needs. In a production environment, it's often set to none
or validate
.Setting up a Maven project with Spring Boot and MySQL:
pom.xml
with dependencies:<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies>
Configuring MySQL connection in Spring Boot Maven project:
application.properties
or application.yml
file.spring.datasource.url=jdbc:mysql://localhost:3306/your_database_name spring.datasource.username=your_username spring.datasource.password=your_password spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
Spring Data JPA repository in a Maven project with MySQL:
JpaRepository
for JPA CRUD operations.public interface YourEntityRepository extends JpaRepository<YourEntity, Long> { // Custom queries or additional methods can be defined here }
CRUD operations with MySQL in Spring Boot Maven project:
@Service public class YourEntityService { @Autowired private YourEntityRepository entityRepository; public YourEntity saveEntity(YourEntity entity) { return entityRepository.save(entity); } public YourEntity getEntityById(Long id) { return entityRepository.findById(id).orElse(null); } public List<YourEntity> getAllEntities() { return entityRepository.findAll(); } public void deleteEntity(Long id) { entityRepository.deleteById(id); } }
Entity mapping for MySQL tables in Spring Boot:
@Entity @Table(name = "your_table_name") public class YourEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; // Other entity fields and annotations }
Using JpaRepository with MySQL in a Spring Boot Maven project:
public interface YourEntityRepository extends JpaRepository<YourEntity, Long> { List<YourEntity> findBySomeField(String someFieldValue); }
Transaction management with MySQL and Spring Boot:
@Service @Transactional public class YourEntityService { @Autowired private YourEntityRepository entityRepository; public YourEntity saveEntity(YourEntity entity) { return entityRepository.save(entity); } }
Handling relationships and associations in MySQL with Spring Boot:
@OneToOne
, @OneToMany
, etc.@Entity public class ParentEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @OneToMany(mappedBy = "parent") private List<ChildEntity> children; // Other fields and annotations } @Entity public class ChildEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @ManyToOne @JoinColumn(name = "parent_id") private ParentEntity parent; // Other fields and annotations }
Testing MySQL integration in a Spring Boot Maven project:
@DataJpaTest
.@RunWith(SpringRunner.class) @DataJpaTest public class YourEntityRepositoryTest { @Autowired private YourEntityRepository entityRepository; @Test public void testCRUDOperations() { // Your test logic here } }