springjava.com
  • Core Java
  • Spring Core
  • Spring Boot
  • Spring MVC
  • Angular Tutorial

Related Posts

  • Spring Boot Annotations How to Upload a File in Spring Boot JSP How to create a Spring Boot project in VS Code How to create a Spring Boot project in IntelliJ IDEA How to create a Spring Boot project in STS How to Create and Setup Spring Boot Project in Eclipse Spring Boot CRUD Operations with JSP Spring Boot Pagination & Sorting with REST API Spring Boot CRUD with Thymeleaf Sending Mail Using Gmail SMTP Server In Spring Boot
Spring Boot

How to Use Lombok in Spring Boot Project

Last Updated: 27-06-2022 Springjava

What is the Lombok?

Java Bean class or Pojo Class with some properties and their setter, getter methods, no parameterized constructor or parameterized constructor, toString method we can explicitly create. But Lombok provides a solution to this problem of the Pojo class or Java Bean. We don't need to create a setter, getter of properties, no parameterized constructor or parameterized constructor, or toString method of this class explicitly. Lombok is the most popular java library that minimizes or removes boilerplate code by using annotations.

Without Lombok Setter, Getter of properties:

public class Person { 
                                  
private Long id;
private String fName;
private String lName;
public Person(){
   }
}

With Lombok Setter, Getter of properties:

@NoArgsConstructor                                                
public class Person {
@Getter 
@Setter
private Long id;
@Getter 
@Setter
private String fName;
@Getter 
@Setter
private String lName;
}

How to Add Lombok Dependency:

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>

How to install the Lombok in our system?
The steps of the Installation of Lombok are:
1. Get more detail about Lombok from https://projectlombok.org/download 
2. Click on that downloaded Lombok.jar file
3. If installed IDEs paths are  not showing automatically then you have to click on Specify a location to provide a path of IDEs

install-lombok

4. Click on the Install/Update button.
5. Goto Eclipse then click on About Eclipse IDE.

intaller-confirmation-window 

How to use the Lombok in the Spring Boot Application?
Development Process:
1. Keep eclipse IDE ready(STS Integrated) 
2. Create a Spring Boot Starter Project 
3. Define Database Connection in the application.properties file
4. Create Entity class
5. Create a Repository
6. Create a Service
7. Create CommandLineRunner on SpringBootApplication class
8. Run the Project

1. Keep eclipse IDE ready(STS Integrated) 
Refer to this article How to Create Spring Project in IDE to create Spring Boot Project in Eclipse IDE.

2. Create a Spring Boot Starter Project 
Add the following dependencies: 
    • Spring Web
    • Spring Data JPA
    • Lombok
    • H2 Database

Maven Dependency
pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project 
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.6.3</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.poc</groupId>
	<artifactId>Spring_Boot_Lombok_Proj</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>Spring_Boot_Lombok_Proj</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>11</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>
		 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
		<dependency>
			<groupId>com.h2database</groupId>
			<artifactId>h2</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<excludes>
						<exclude>
							<groupId>org.projectlombok</groupId>
							<artifactId>lombok</artifactId>
						</exclude>
					</excludes>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

3. Define Database Connection in the application.properties file

spring.datasource.url=jdbc:h2:mem:test
 #spring.datasource.driverClassName=org.h2.Driver
   spring.datasource.username=sa
   spring.datasource.password=
   spring.jpa.show-sql=true
   spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
   spring.jpa.hibernate.ddl-auto= update
   spring.h2.console.enabled=true

4. Create Entity class
Person.java: 

package com.poc.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import lombok.Data;
@Entity
@Data
public class Person {
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;
	private String fName;
	private String lName;

}
  • @Getter and @Setter: Generates setter and getter methods.
  • @ToString: Generates toString method
  • @NoArgsConstructor and @AllArgsConstructor: Generates constructors that take no argument and one argument for every field.
  • @EqualsAndHashCode: Generates hashCode and equals implementations from the fields of your object.
  • @RequiredArgsConstructor: Generates one argument per final / non-null field.
  • @Data: A shortcut for @ToString, @EqualsAndHashCode, @Getter on all fields, and @Setter on all non-final fields and @RequiredArgsConstructor.

5. Create a Repository
PersonRepository.java:
 

 package com.poc.repo;
 import org.springframework.data.jpa.repository.JpaRepository;
 import com.poc.entity.Person;
 public interface PersonRepository extends JpaRepository<Person, Long> {
}

6. Create a Service
PersonService.java:

package com.poc.service;

import java.util.List;
import com.poc.entity.Person;

public interface PersonService {
	void save(Person person);
    List<Person> getPersonList();
}

PersonServiceImpl.java:

package com.poc.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.poc.entity.Person;
import com.poc.repo.PersonRepository;

@Service
public class PersonServiceImpl implements PersonService {
	@Autowired
	PersonRepository personRepo;

	@Override
	public void save(Person person) {
	personRepo.save(person);
}
	@Override
	public List<Person> getPersonList() {
	return personRepo.findAll();
	}
}

7. Create CommandLineRunner on SpringBootApplication class

package com.poc;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import com.poc.entity.Person;
import com.poc.service.PersonService;

@SpringBootApplication
public class SpringBootJpaEntityPocApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringBootJpaEntityPocApplication.class, args);
	}
	@Bean
	public CommandLineRunner demo(PersonService personService) {
		return (args) -> {
			// save few person
			Person person1 = new Person();
			person1.setfName("Peter");
			person1.setlName("Parker");

			Person person2 = new Person();
			person2.setfName("Robert");
			person2.setlName("Smith");

			personService.save(person1);
			personService.save(person2);

			// fetch all person
			System.out.println("-----List of Persons------");
			for (Person person : personService.getPersonList()) {
				System.out.println("Person Detail:" + person);
			}
		};
	}
}

8. Run the Project

  • Right-click on the java class(SpringBootLombokPocApplication.java)then click on Run as Java Application.
  • Type this URL http://localhost:8080/h2-console on the browser for the checking of the h2 database console.

Conclusion:
This topic is explained What is the Lombok? How to Add Lombok Dependency? How to use the Lombok in the Spring Boot Application? How to install the Lombok in our system?

What is Spring Boot How to Create a Spring Boot Project in IDE Spring MVC with Spring Boot Configure

Leave your thought here

Your email address will not be published. Required fields are marked *

springjava_com
  • springjavateam@gmail.com
  • springjava.com
Learn
    • Core Java
    • Spring Core
    • Spring Boot
    • Spring MVC
    • Angular Tutorial
Quick links
  • About Us
  • Contact Us
  • Privacy Policy
Subscribe to Blog via Email
Subscribe
© 2021 Spring Java. All Rights Reserved.
springjava.com
  • Core Java
  • Spring Core
  • Spring Boot
  • Spring MVC
  • Angular Tutorial
  • About Us
  • Contact Us