Saturday, July 8, 2017

Database access with Spring boot

The simplest way of creating Spring boot with MySQL application is via Spring starter project using STS (Spring Tool Suite) IDE.

  • Create a new project by selecting spring starter project wizard
  • Select project type as Maven, provide project name, packaging, and Java version etc.

  • Select spring boot version and project dependencies

  • After finishing, It will create the project structure with all required dependency.
  • Configure MySQL configuration in file
  • Now, Create the @Entity model (User) which should be persisted in the database. Hibernate will automatically translate entity class into a table.

  • Create the repository ( The Repository interface will be automatically implemented by Spring in a bean with the same name with changing case. For UserRepository, the bean name will be userRepository.

  • To handle HTTP request, Create a Controller class ( having method POST and GET operations.

  • Now run the as a Java application. It will start the tomcat server on default port i.e. 8080
  • Open browser and hit url http://localhost:8080/users

  • As there is no user present into the database that is why we see empty list. Let's add few users using POSTMAN.
  • To add a user, Open Postman and run below http request

  • Congratulations! We've just developed a Spring application which is bound to a MySQL database, Ready for production! Source code can be downloaded from GITHUB.
Happy Coding...!!!

No comments:

Post a Comment

How TOPT Works: Generating OTPs Without Internet Connection

Introduction Have you ever wondered how authentication apps like RSA Authenticator generate One-Time Passwords (OTPs) without requiring an i...