How to build a CRUD app with MySQL and Spring-boot

PART ONE: MySQL

//create database
CREATE DATABASE book_shelf;

PART TWO: Spring-boot

  • Spring Web
  • Spring Data JPA
  • MySQL Driver
  • Spring Boot DevTools
Spring Initializer
File Structure
spring.datasource.url = jdbc:mysql://localhost:3306/book_shelf
spring.datasource.username = root
spring.datasource.password = Your mysql root password

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect

spring.jpa.hibernate.ddl-auto = update
  • Book.java (com.example.bookshelf.model.Book) — To work as the blueprint of the books we are saving in the database.
  • BookController.java (com.example.bookshelf.controller.BookController) — To work as the API/Rest controller of our application. This is where we define all the endpoints.
  • BookNotFoundException.java (com.example.bookshelf.exception.BookNotFoundException) — Well, the file name speaks for it. This is the error handler of our application.
  • BookRepository.java (com.example.bookshelf.repository.) — This is an interface we use to do all the query and inserts we need to do. Unless we need to customize our queries we can use built-in methods in JPA repository since we are going extend this one from that.
Book.java
BookRepository.java
BookNotFoundException.java
  • We should be able to list all the books on the bookshelf. Thus we need to create an endpoint to GET the books out of the table. GET — book/
  • We should be able to add books to the shelf. Thus we need to create an endpoint to POST a new book to the table. POST — book/
  • We need another endpoint to remove books from the shelf. DELETE — book/{id}
BookController.java

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store