Wednesday, 28 February 2018

Spring Boot JPA

Spring Boot JPA 

In 5 Simple Steps Integrate PostgreSQL Database | Tuorial - 4


Hello Friends, Welcome to Spring Boot JPA tutorial.

In this tutorial of Spring Boot JPA we are going to integrate  PostgreSQL database in 5 simple steps with Account Spring Boot Restful Web Service that we have created in our previous Spring Boot tutorial.

 

FIVE STEPS

STEP - 1

Adding Spring Boot JPA and PostgreSQL driver class dependency in build.gradle

STEP - 2 

Adding properties releated to Spring Boot JPA & PostgreSQL database in application.properties

STEP - 3

Converting Account domain to Account entity using Spring Boot JPA annotations

STEP - 4 

Implementing Spring Boot JPA Repository

STEP - 5

Adding support of Spring Boot JPA Repository in Service class

Spring Data JPA

  1. Spring data JPA does a integration between  Spring Application & JPA.
  2. JPA stands for JAVA Persistent API
  3. JPA is a collection of method & classes to persist data in database
  4. JPA forms a bridge between the Object Model (POJO) & Relational Model (Database Table)

Step-1: Adding Dependencies

buildscript {
ext {
springBootVersion = '1.5.10.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
group = 'com.sjtech.account.app'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile group: 'postgresql', name: 'postgresql', version: '9.0-801.jdbc4'
testCompile('org.springframework.boot:spring-boot-starter-test')
}
view raw build.gradle hosted with ❤ by GitHub
Step-2: Adding Properties

spring.datasource.url= jdbc:postgresql://127.0.0.1/sjtech
spring.datasource.username=postgres
spring.datasource.password=123456
spring.jpa.hibernate.ddl-auto=create
spring.datasource.driver-class-name=org.postgresql.Driver
Step-3:  Implementing JPA Entity

package com.sjtech.domain;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="account")
public class Account {
@Column(name="account_name")
String name;
@Id
@GeneratedValue
@Column(name="account_id")
Long id;
@Column(name="account_email")
String email;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
view raw Account.java hosted with ❤ by GitHub
Step-4: Implementing Spring Boot JPA Repository

package com.sjtech.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import com.sjtech.domain.Account;
public interface AccountRepository extends JpaRepository<Account, Long> {
}
Step-5: Adding support of JPA repository in Service

package com.sjtech.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sjtech.domain.Account;
import com.sjtech.repository.AccountRepository;
@Service
public class AccountService {
@Autowired
AccountRepository repository;
public Account findById(Long id) {
return repository.findOne(id);
}
public List<Account> getAll(){
return repository.findAll();
}
public void create(Account account) {
repository.save(account);
}
public void update (Account updatedAccount) {
Account accountToBeUpdated = repository.findOne(updatedAccount.getId());
accountToBeUpdated.setName(updatedAccount.getName());
accountToBeUpdated.setEmail(updatedAccount.getEmail());
repository.save(accountToBeUpdated);
}
public void delete (Long id) {
repository.delete(id);
}
}

For more details please check out following videos:
1. Restful Web Service Tutorial - https://goo.gl/7evDWv

2. Spring Boot Restful Web Service Tutorial - https://goo.gl/3F8W5g

No comments:

Post a Comment

Spring Boot JPA

Spring Boot JPA  In 5 Simple Steps Integrate PostgreSQL Database | Tuorial - 4 Hello Friends, Welcome to Spring Boot JPA tutorial...