Spring Boot + Hibernate CRUD Example

Spring Boot + Hibernate CRUD Example

Let us see an example of spring boot + hibernate integration example with Crud (Create, Read, Update and Delete) operations.

In the below example we are going to use Spring jpa and CrudRepository.

Annotations used in the below example:

@EnableJpaRepositories: Used to enable JPA repositories.
@EnableTransactionManagement:  Used for enabling transaction management, similar to <tx>.

Crud Repository:
In the below example, we have used crud repository, let us understand about this repository. The CrudRepository provides CRUD functionality for the Entity class. It has in-build functions for performing CRUD operations which allow us to write less code and use the functions directly from this repository.

Now let us see an example of CRUD operations with Spring Boot and Hibernate. (You can Download the Code Here)

Structure:

pom.xml

application.properties:

com.javainfinite.model

com.javainfinite.EmployeeDAO

com.javainfinite.application.controller

com.javainfinite.application

 Saving Employee – First Record

Saving Employee – Second Record

Get all Employees – To List Entered Both Records

Get By Id:

Updating Record

After Update Getting By Id:

Deleting a Record:

 

After Deleting a Record

You can Download the Code Here – Please modify the database properties.

 

Read More

Simple SpringBoot RestAPI Example

Simple SpringBoot RestAPI Example:
Spring Boot is a way to create stand-alone applications with less or no configurations and has defaults for annotation configuration which we will use in our application.

In this example, Let us a see a very simple SpringBoot with one RESTAPI url. This example does not have any database connection or pojo classes.

Structure:

 

POM.xml

SpringController.java

SpringRest.java

Let us start the spring boot application,

 

Hitting the URL with PostMan,

 

Read More

SpringBoot Annotations

SpringBoot Annotations

Before proceeding further let us first understand some important annotations of Spring Boot.

@SpringBootApplication: annotation is equivalent to using @Configuration, @EnableAutoConfiguration and @ComponentScan with their default attributes.  One thing to note here regarding ComponentScan is, @SpringBootApplication scans only the subpackages. i.e. for example if the main class is in com.javainfinite, then it will scan all classes from com.javainfinite.* packages.

@EnableAutoConfiguration: attempting to guess and configure beans. If we use SpringBootApplication this annotation is auto enabled and has no effect adding it again

@Controller: marks as web application controller

@RestController: works same as @Controller annotation except @ResponseBody is active by default. In case of @Controller we need to declare @ResponseBody.

@ResponseBody: returns value to the response as HTTP Response body

@RequestMapping: to map the URL for the particular method of entire class

@RequestParam: accessing query parameter values from the request

@PathVariable: pattern that is used in the URI for the incoming request

There are various other annotations too, above explained annotations are the ones most commonly used in springboot.

Read More

Basic Spring Boot Application Example

Basic Spring Boot Application Example:

We have already went through various examples using spring, To understand spring please refer here.

What is Spring Boot?
Spring Boot is a way to create stand-alone applications with less or no configurations and has defaults for annotation configuration which we will use in our application

Advantages of Spring Boot:

  • Reduces development time
  • Embedded Servers
  • No XML configuration
  • Integration with Spring JDBC, Spring ORM etc.

Now let us see an basic example to create spring boot application in Netbeans.

Structure:

pom.xml

SpringBootApp.java

Output:

We have created basic spring boot application!

 

 

Read More