8/8/2019 Spring Boot Crud Example With Mysql
Note that on the fourth line, springbootcrudapi is the name of the database that we’ve created earlier. Do not forget to change the values of spring.datasource.username and spring.datasource.password on lines 5 and 6 to match your own database properties, otherwise the connection could fail. The server.port property configures the app to be served on port 4500, if you are already using.
This guide will help you to build Spring Boot RESTful CRUD Example with MySQL Database. As we know, making REST service in Spring Boot application is super easy and fast. Spring Boot data provides
CrudRepository interface for generic CRUD operation in org.springframework.data.repository package.
To create a connection with the MySQL database, we need to configure data source properties in the application.properties file starting with
spring.datasource.* . Spring boot uses spring-boot-starter-data-jpa to configure spring Java Persistence API (JPA).
What we’ll build
In this example, we will create a Spring Boot application that connects with our external MySQL database, consume and produce the JSON data and performs the following operations:
1. CrudRepository InterfaceCrudRepository is an interface which provided by Spring Framework itself. CrudRepository extends Spring Data Repository which is a central repository marker interface. CrudRepository provides the generic method for create, read, update, and delete (CRUD) operation.
CrudRepository contains total 11 methods for CRUD operation, some of them are listed below that we will use in this application:
<S extends T> S save(S entity) : Save and update a given entity. Entity can not be null and the saved entity will never be null.
Iterable<T> findAll() : Returns all entities.
Optional<T> findById(ID id) : Retrieves an entity by its ID. ID cannot be null.
void deleteById(ID id) : Deletes the entity with the given ID. ID cannot be null.
2. Technologies Used
Find the list of all technologies used in this application.
3. Database Schema
Find the table structure of the MySQL database used in this example.
4. Dependencies Required
Building a Spring Boot RESTful CRUD application, you need the following dependencies.
5. Project Structure
Final project structure of our application in STS ide will look like as follows.
6. application.properties
Configure datasource, JPA properties, etc in application.properties file. These properties automatically read by Spring Boot.
7. Create the Entity
Create a
Country model class, define its attributes and annotate with @Enitity and @Table annotation at the top of the class. @Table annotation is used to map your existing database table with this class and @Column annotation map table columns.
Note: If the table is not available in your database,
@Entity annotation tells Hibernate to make a table out of this class.
Country.java
8. Create the Repository
Create a
CountryRepository interface which extends CrudRepository . This will be AUTO IMPLEMENTED by Spring into a Bean called countryRepository.
9. Create the Controller
Create
CountryController class which handles the user request to perform create, read, update, and delete operation. Learn more about @RestController and @Autowired annotation.
CountryController.java
10. Make the application executable
Create an
App class and run it.
11. Test the application
Now everything is done. Let’s test the application. To test the application open the Postman and follow the below steps:
1. To add a new country use the http://localhost:8080/country/add URL, select the POST method, set
Content-Type='application/json' in the Headers tab, select raw in Body tab and paste the following code.
2. To fetch all country list use the http://localhost:8080/country/all URL with GET request.
3. Similarly, you can perform the update and delete operation. For update, use PUT and delete use DELETE request.
Download Source Code: spring-boot-restful-crud-example-with-mysql-database.zip
ReferencesSimilar PostsAbout the AuthorWebsparrowI like sharing my experiments and ideas with everyone by writing articles on the latest technological trends.
Please enable JavaScript to view the comments powered by Disqus.
Spring boot JPA With Mysql
This tutorial shows you how to configure spring boot JPA MySQL with an example. Here we provide quick programming examples which can be used to quickly create a MySQL-based Spring Boot app.
In here spring data jpa maven using Java Persistence API is a standard technology that lets you “map” objects to relational databases. The spring-boot-starter-data-JPA POM provides a quick way to get started.
Spring Data JPA provides the following key dependencies:
Environment Used
Spring Boot data JPA Repository example
Central repository marker interface captures the domain type to manage as well as the domain type’s id type. In general, the purpose is to hold type information as well as being able to discover interfaces that extend this one during classpath scanning for easy Spring bean creation.
Spring crud repository![]()
Interface for generic CRUD operations on a repository for a specific type.
Spring boot JPA hibernate configuration in application.properties
Service InterfaceService ImplementationControllerBean ClassApplication.javaSpring boot data JPA Maven configuration (pom.xml)
By adding
spring-boot-starter-data-jpa in you pom.xml your dependency in ready to use.
How to runDownload
Spring boot data JPA mysql maven – Download
References
JPA repository – Official site
JPA CrudRepository – Official site
Related posts:Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |