tags | projects |
---|---|
This guide walks you through the process of creating a Spring application connected with a MySQL Database.
You’ll create a MySQL database, build a Spring application and connect it with the newly created database.
Note
|
MySQL is `GPL’d, so ANY PROGRAM USING IT MUST BE GPL’d too |
MySQL version 5.6
Go to the terminal (Command Prompt (cmd) in Microsoft Windows). Open MySQL Client with a user that can create new users.
For example: On a Linux, use the command
---
$ sudo mysql --password
---
Note
|
This connects to MySQL as a root, this is NOT THE RECOMMENDED WAY. |
Create a new database
---
mysql> create database db_example; -- Create the new database
mysql> create user 'springuser'@'localhost' identified by 'ThePassword'; -- Creates the user
mysql> grant all on db_example.* to 'springuser'@'localhost'; -- Gives all the privileges to the new user on the newly created database
---
Add pom.xml
dependencies
---
<!-- JPA Data (We are going to use Repositories, Entities, Hibernate, etc...) -->
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> <version>1.4.3.RELEASE</version> </dependency>
<!-- Use MySQL Connector-J -→
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.40</version> </dependency> ---
or in Gradle build.gradle
:
---
// JPA Data (We are going to use Repositories, Entities, Hibernate, etc...)
compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: '1.4.3.RELEASE'
compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.40' ---
Spring Boot gives you defaults on all things, the default in database is H2SQLDB
, so when you want to change this and use any other database you must define the connection attributes in the application.properties
file
In the sources folder, you create a resource file src/main/resources/application.properties
src/main/resources/application.properties
---
link:complete/src/main/resources/application.properties[role=include]
---
Here, spring.jpa.hibernate.ddl-auto
can be update
, create
, create-drop
, refer to the Hibernate documentation for details.
src/main/java/mysqlapp/models/User.java
link:complete/src/main/java/mysqlapp/models/User.java[role=include]
This is the entity class which Hibernate will automatically translate into a table
src/main/java/mysqlapp/repositories/UsersRepository.java
link:complete/src/main/java/mysqlapp/repositories/UsersRepository.java[role=include]
This is the repository interface, this will be automatically implemented by Spring in a bean with the same name with changing case
The bean name will be usersRepository
Note
|
If you want to make the interface an inner one, use the @EnableJpaRepository annotation on the controller with the parameter considerNestedRepositories = true
|
src/main/java/mysqlapp/controllers/MainController.java
link:complete/src/main/java/mysqlapp/controllers/MainController.java[role=include]
Note
|
The above example does not specify GET vs. PUT , POST , and so forth, because @RequestMapping maps all HTTP operations by default. Use @RequestMapping(method=GET) to narrow this mapping.
|
Although it is possible to package this service as a traditional WAR file for deployment to an external application server, the simpler approach demonstrated below creates a standalone application. You package everything in a single, executable JAR file, driven by a good old Java main()
method. Along the way, you use Spring’s support for embedding the Tomcat servlet container as the HTTP runtime, instead of deploying to an external instance.
src/main/java/mysqlapp/Application.java
link:complete/src/main/java/mysqlapp/Application.java[role=include]
Logging output is displayed. The service should be up and running within a few seconds.
Now that the application is running, you can test it.
Use curl
for example.
Now you have 2 REST Web Services you can test
localhost:8080/demo/getAll
This gets all data
localhost:8080/demo/add
This adds one user to the data
---
$ curl 'localhost:8080/demo/add?name=First&[email protected]'
---
The reply should be
---
Saved
---
---
$ curl 'localhost:8080/demo/getAll'
---
The reply should be
---
[{"id":1,"name":"First","email":"[email protected]"}]
---
Congratulations! You’ve just developed a Spring application!