Spring Boot Part-2

 

Note:
Understand the current blog(Spring Boot-2). Please walk through the previous blog-Spring Boot-1


Implement the update the delete operations in Spring Boot

Add a update method in service

 

Write the delete the service
a)   Write a delete method in the controller



Write delete method in the service class

 
 

Spring Data JPA: The Data Tire

Java Persistence API - specification of ORM. Object - Releational Mapping - SQL db (Relational DB) Entity class map to table. You don't need to query in the db.  Spring Data JPA - working with ORM tool. To connect to db. 

 
Create a new Project for SPRING JPA
New Spring Starter Project in STS
Name: course-api-data
Type: Maven Project
Java version: 1.8
Package: Jar
Language:Java
Group: io.javabrains
artifact:course-api-data
Description: Coursse API WITH data for Spring Boot Quick start
Package:io.javabrains
Click NextSelect the Dependencies
Web -> Web
SQL -> JPA (SPRING JPA),  Apache Derby (embedded db)
Click Next – Finish
 

Add a Boot class

 
 
 
Creating a Spring Data JPA Repository
 
a) Model needs to implement the JPA
@Entity - Define in Class level. This will create a table.
@Entity
public class Topic 
b) Define the primary key
@Id
private String id;
Service talks to Data Repository
Create TopicRepository interface. Framework will take care the CRUD operations by CrudRepository interface.
//getAllTopics()
//getTopic(String id)
//addTopic(Topic t)
//updateTopic(Topic t, String id)
//deleteTopic(String id)
 
public interface TopicRepository extends CrudRepository <Topic, String>{ 
}

 




 
 
 
 

 
 

Making CRUD Operations.

Change in the Service class
a) Declare the TopicRepoistory and add AutoWired
b) service method call the repoistory method
 
public List<Topic> getAllTopics(){
 List<Topic> topic = new ArrayList<>();
 topicRepoistory.findAll().forEach(topic::add);
 return topics;
}
public void addTopic(Topic topic){
 topicRepository.save(topic);
}
public Topic getTopic(String id){
 topicRepository.findOne(id);
}
public void updateTopic(String id, Topic topic){
 topicRepository.save(topic);  //used for update or insert
}
public Topic deleteTopic(String id){
 topicRepository.delete(id);
}
 
 
 

Created the Repository
 

Add the jpa and db dependencies in the pom.xml file
Marked the entity class as @Entity and primary column as @Id in the member variable.
Implemented the Repository interface which extends CrudRepoistory<Topic, String>
Add the service which talk to Data Repository
 
Adding Course APICopy the Topic package to new package called "course"
Rename Class Topic to course for Model, controller, repoistory, service
Link course and Topic by adding member topic in Course class


 
 
 
 

Adding Entity Relationship and extending repositoryCustom Method in Crud Repository
CrudRepository custom methods.
findBy<element>
findByName(String name)
findByDescription(String description)
findBy<Object><Member>(parameter id)
findByTopicId(String topic id);

 
 
Packaging and running a Spring Boot app
Built jar file and run
compile and generate the jar file
 mvn clean install
clean project , download necessary dependencies , compile and do the unit test and create a jar file and make that available in the project folder.
Project Name\target\project.xxxx.jar
java -jar target/project.jar
Spring Boot Actuator
Production ready feature to help you monitor and manage you application.
spring-boot-starter-actuator add dependency in the pom.xml
http://localhost:8080/health
 
 

Source from https://www.javabrains.io





 

Comments

Popular posts from this blog

Robotic Process Automation

WorkFusion

7 Rules of Success A.P.J Abdul Kalam