Spring Boot + Spring data JPA + MySQL
By:Roy.LiuLast updated:2019-08-11
The previous Spring Boot + Spring data JPA will be reused, modify to support MySQL database.
Technologies used :
- Spring Boot 2.1.2.RELEASE
- Spring 5.1.4.RELEASE
- Hibernate 5.3.7
- HikariCP 3.2.0
- mysql-connector-java:jar 8.0.13
- Maven 3
- Java 8
1. Maven
To connect a MySQL, declares mysql-connector-java
pom.xml
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <artifactId>spring-boot-data-jpa</artifactId> <packaging>jar</packaging> <name>Spring Boot Spring Data JPA</name> <version>1.0</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.2.RELEASE</version> </parent> <properties> <java.version>1.8</java.version> <downloadSources>true</downloadSources> <downloadJavadocs>true</downloadJavadocs> </properties> <dependencies> <!-- jpa, crud repository --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- MySQL --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.0</version> </plugin> </plugins> </build> </project>
2. Configure MySQL
2.1 Update the MySQL setting in spring.datasource.*
application.properties
## MySQL spring.datasource.url=jdbc:mysql://localhost:3306/wordpress spring.datasource.username=mkyong spring.datasource.password=password #`hibernate_sequence' doesn't exist spring.jpa.hibernate.use-new-id-generator-mappings=false # drop n create table, good for testing, comment this in production spring.jpa.hibernate.ddl-auto=create
Done.
From:一号门
COMMENTS