Difference between revisions of "SpringBoot"
(→JPA - Java Persistance API) |
|||
Line 226: | Line 226: | ||
== [[JPA - Java Persistance API]] == | == [[JPA - Java Persistance API]] == | ||
+ | |||
+ | |||
+ | ===[[ORM - Object Relational Mapping]]=== | ||
+ | ===[[Spring Data JPA]]=== | ||
+ | |||
+ | ===Making CRUD Operation with Repository=== | ||
+ | |||
+ | '''BrandRepository interface''' | ||
+ | |||
+ | <syntaxhighlight lang="java"> | ||
+ | package com.oasissofttech.springboot.brand; | ||
+ | |||
+ | import org.springframework.data.repository.CrudRepository; | ||
+ | |||
+ | public interface BrandRepository extends CrudRepository<Brand, Integer> { | ||
+ | |||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | |||
+ | |||
+ | '''BrandService Class''' | ||
+ | <syntaxhighlight lang="java"> | ||
+ | package com.oasissofttech.springboot.brand; | ||
+ | |||
+ | import java.util.ArrayList; | ||
+ | import java.util.Arrays; | ||
+ | import java.util.List; | ||
+ | import java.util.Optional; | ||
+ | |||
+ | import org.springframework.beans.factory.annotation.Autowired; | ||
+ | import org.springframework.stereotype.Service; | ||
+ | |||
+ | @Service | ||
+ | public class BrandService { | ||
+ | @Autowired | ||
+ | private BrandRepository brandRepository; | ||
+ | |||
+ | public List<Brand> getAllBrands(){ | ||
+ | List<Brand> brands = new ArrayList<>(); | ||
+ | brandRepository.findAll() | ||
+ | .forEach(brands::add); | ||
+ | |||
+ | return brands; | ||
+ | } | ||
+ | |||
+ | |||
+ | public Optional<Brand> getBrand(int id) { | ||
+ | return brandRepository.findById(id); | ||
+ | } | ||
+ | |||
+ | |||
+ | public void addBrand(Brand brand) { | ||
+ | brandRepository.save(brand); | ||
+ | } | ||
+ | |||
+ | |||
+ | public void updateBrand(Brand brand, int id) { | ||
+ | brandRepository.save(brand); | ||
+ | } | ||
+ | |||
+ | |||
+ | public void deleteBrand(int id) { | ||
+ | brandRepository.deleteById(id); | ||
+ | } | ||
+ | |||
+ | } | ||
+ | </syntaxhighlight> |
Revision as of 11:58, 3 June 2018
Contents
SpringBoot - Maven - Pom.xml
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.oasissoftech.springboot</groupId>
<artifactId>SpringBoot-Tutorial-1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringBoot-Tutorial-1</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<properties>
<java.version>1.8</java.version>
</properties>
</project>
to get latest Spring Boot Starter Parent pom.xml configurations visit : https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-parent
to get latest Spring Boot Web Starter pom.xml configurations visit : https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web
SpringBoot - Application Boot
Spring Boot application startup class :
package com.oasissofttech.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootTutorial1App {
public static void main(String[] args) {
SpringApplication.run(SpringBootTutorial1App.class,args);
}
}
SpringBoot - Controller Class - GET, POST, PUT, DELETE
package com.oasissofttech.springboot.brand;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class BrandController {
@Autowired
private BrandService brandService;
@RequestMapping("/brands")
public List<Brand> getAllBrands() {
return brandService.getAllBrands();
}
@RequestMapping("/brand/{brandId}")
public Brand getBrand(@PathVariable("brandId") int id) {
return brandService.getBrand(id);
}
@RequestMapping(method=RequestMethod.POST,value="/brands")
public void addBrand(@RequestBody Brand brand) {
brandService.addBrand(brand);
}
@RequestMapping(method=RequestMethod.PUT,value="/brands/{id}")
public void addBrand(@RequestBody Brand brand,@PathVariable int id) {
brandService.updateBrand(brand,id);
}
@RequestMapping(method=RequestMethod.DELETE,value="/brands/{id}")
public void deleteBrand(@PathVariable int id) {
brandService.deleteBrand(id);
}
}
SpringBoot - Service Class
package com.oasissofttech.springboot.brand;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.springframework.stereotype.Service;
@Service
public class BrandService {
private List<Brand> brands = new ArrayList<>(Arrays.asList(
new Brand(1,"Aston Martin","(1913–present)"),
new Brand(2,"Bentley","(1919–present)"),
new Brand(3,"Jaguar Land Rover","(2013-present)"),
new Brand(4,"Lotus","(1952–present)"),
new Brand(5,"Rolls Royce","(1904–present)")
));
public List<Brand> getAllBrands(){
return brands;
}
public Brand getBrand(int id) {
return brands.stream().filter(b->b.getId()==id).findFirst().get();
}
public void addBrand(Brand brand) {
brands.add(brand);
}
public void updateBrand(Brand brand, int id) {
int c = brands.size();
for(int i=0;i<c;i++) {
if(brands.get(i).getId()==id) {
brands.set(i, brand);
return;
}
}
}
public void deleteBrand(int id) {
brands.removeIf(t->t.getId()==id);
}
}
Others
package com.oasissofttech.springboot.brand;
public class Brand {
private int id;
private String brandName;
private String description;
public Brand() {
}
public Brand(int id, String brandName, String description) {
super();
this.id = id;
this.brandName = brandName;
this.description = description;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getBrandName() {
return brandName;
}
public void setBrandName(String brandName) {
this.brandName = brandName;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
Common application properties
application.properties file
#===========================================
#=== application.properties file content ===
server.port=3000 #server HTTP port.
#===================end=====================
springboot properties file manual : https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html
JPA - Java Persistance API
ORM - Object Relational Mapping
Spring Data JPA
Making CRUD Operation with Repository
BrandRepository interface
package com.oasissofttech.springboot.brand;
import org.springframework.data.repository.CrudRepository;
public interface BrandRepository extends CrudRepository<Brand, Integer> {
}
BrandService Class
package com.oasissofttech.springboot.brand;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class BrandService {
@Autowired
private BrandRepository brandRepository;
public List<Brand> getAllBrands(){
List<Brand> brands = new ArrayList<>();
brandRepository.findAll()
.forEach(brands::add);
return brands;
}
public Optional<Brand> getBrand(int id) {
return brandRepository.findById(id);
}
public void addBrand(Brand brand) {
brandRepository.save(brand);
}
public void updateBrand(Brand brand, int id) {
brandRepository.save(brand);
}
public void deleteBrand(int id) {
brandRepository.deleteById(id);
}
}