Alphalearningschool
        

<<< Prev

Next >>>

Up
CRUD Operation Example

Let us create a basic CRUD operation example using Spring Boot for better understanding.

Project Layout:

          There is no specific rule to prepare your project layout. But there are some best practices that will help us for better understanding.

 

ExampleApplication.java:

                This Class is treated as entry point of Spring Boot Application because this class must have a main method declared with @SpringBootApplication annotation.

package com.alphalearning.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ExampleApplication {

	public static void main(String[] args) {
		SpringApplication.run(ExampleApplication.class, args);
	}

}

 

Here @SpringBootApplication is used for three annotations those are:

  1. @ComponentScan: enable scan on the package where the application is located
  2. @EnableAutoConfiguration: enable Spring Boot’s auto-configuration mechanism
  3. @Configuration: allow to register extra beans in the context or import additional configuration classes.

application.properties:

           We all know that Spring Boot helps in maintaining lesser configuration codes than other spring frameworks so, Spring Boot introduced application.properties file where we can write all the configurations in a single file. And the main advantage of using this application.properties file is it is Spring Boot’s default configuration file which is auto-detected without writing any spring based configurations. It should always be present inside “src/main/resources” directory.

                We can mention various properties like Logging, AOP, Identity, Hibernate, JPA, JMX, Email, etc. In our application, we will only be mentioning hibernate properties and database connection properties.

For better understanding here is the example of application.properties file we used to create our application:

(note: Before adding hibernate configuration and database proiperties make sure you add respective database dependency to your pom.xml file)

server.port=8060

# Hibernate Properties
 spring.datasource.url=jdbc:mysql://localhost:3306/crud
 spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
 spring.jpa.hibernate.ddl-auto=update
 spring.jpa.show-sql=true
 
 # database properties
 spring.datasource.username=root
 spring.datasource.password=12345