UseCase Capgemini

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

Use Case:

Spring Boot Application:

package com.capgemini.demo;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

public class StudentAppDemoApplication {

public static void main(String[] args) {

SpringApplication.run(StudentAppDemoApplication.class, args);

Config:

package com.capgemini.demo.config;

import java.util.Properties;

import javax.persistence.EntityManagerFactory;

import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.core.env.Environment;

import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;

import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;

import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;

import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;

import org.springframework.transaction.PlatformTransactionManager;

import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration

@EnableTransactionManagement

public class PersistenceJPAConfig {

@Bean

public DataSource dataSource() {

DriverManagerDataSource dataSource = new DriverManagerDataSource();

dataSource.setDriverClassName("org.h2.Driver");

dataSource.setUrl("jdbc:h2:~/studentData;DB_CLOSE_DELAY=-1");

dataSource.setUsername("venu");

dataSource.setPassword("venu");

return dataSource;

@Bean

public LocalContainerEntityManagerFactoryBean entityManagerFactory(Environment env) {

LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new


LocalContainerEntityManagerFactoryBean();

entityManagerFactoryBean.setDataSource(dataSource());

entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());

entityManagerFactoryBean.setPackagesToScan("com.capgemini.demo.*");

entityManagerFactoryBean.setJpaProperties(additionalProperties());
return entityManagerFactoryBean;

private Properties additionalProperties() {

Properties properties = new Properties();

properties.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect");

properties.put("hibernate.show_sql", "true");

properties.put("hibernate.format_sql", "true");

properties.put("hibernate.hbm2ddl.auto", "update");

return properties;

@Bean

public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {

JpaTransactionManager transactionManager = new JpaTransactionManager();

transactionManager.setEntityManagerFactory(entityManagerFactory);

return transactionManager;

@Bean

public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {

return new PersistenceExceptionTranslationPostProcessor();

Application.properties:
server.port=8080

spring.application.name=student-demo

spring.h2.console.enabled=true

Entity:

package com.capgemini.demo.entity;

import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.GeneratedValue;

import javax.persistence.GenerationType;

import javax.persistence.Id;

@Entity(name = "STUDENT")

public class Student {

@Id

@GeneratedValue(strategy = GenerationType.AUTO)

@Column(name = "ID")

private Integer id;

@Column(name = "STUDENT_NAME")

private String studentName;

@Column(name = "AGE")

private String age;

@Column(name = "STUDENT_CLASS")

private String studentClass;

public Integer getId() {

return id;

}
public void setId(Integer id) {

this.id = id;

public String getStudentName() {

return studentName;

public void setStudentName(String studentName) {

this.studentName = studentName;

public String getAge() {

return age;

public void setAge(String age) {

this.age = age;

public String getStudentClass() {

return studentClass;

public void setStudentClass(String studentClass) {

this.studentClass = studentClass;

VO:

package com.capgemini.demo.vo;

import com.fasterxml.jackson.annotation.JsonInclude;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)

@JsonPropertyOrder({ "id", "studentName", "studentClass" })

public class StudentVO {

@JsonProperty("id")

private Integer id;

@JsonProperty("studentName")

private String studentName;

@JsonProperty("age")

private String age;

@JsonProperty("studentClass")

private String studentClass;

public Integer getId() {

return id;

public void setId(Integer id) {

this.id = id;

public String getStudentName() {

return studentName;

public void setStudentName(String studentName) {

this.studentName = studentName;

public String getAge() {

return age;

}
public void setAge(String age) {

this.age = age;

public String getStudentClass() {

return studentClass;

public void setStudentClass(String studentClass) {

this.studentClass = studentClass;

Controller:

package com.capgemini.demo.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.http.HttpStatus;

import org.springframework.http.ResponseEntity;

import org.springframework.web.bind.annotation.DeleteMapping;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.web.bind.annotation.PutMapping;

import org.springframework.web.bind.annotation.RequestBody;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.bind.annotation.RestController;
import com.capgemini.demo.service.StudentService;

import com.capgemini.demo.vo.StudentVO;

@RestController

@RequestMapping(value = "/student")

public class StudentController {

@Autowired

private StudentService studentService;

@PostMapping(value = "/addStudent")

public ResponseEntity<StudentVO> addStudent(@RequestBody StudentVO student) {

StudentVO studentResponse = null;

if (student != null) {

studentResponse = studentService.addStudent(student);

return new ResponseEntity<StudentVO>(studentResponse, HttpStatus.OK);

@PutMapping(value = "/updateStudent")

public ResponseEntity<StudentVO> updateStudent(@RequestBody StudentVO student) {

StudentVO studentResponse = null;

if (student != null && student.getId() != 0) {

studentResponse = studentService.updateStudent(student);

return new ResponseEntity<StudentVO>(studentResponse, HttpStatus.OK);

}
@DeleteMapping(value = "/deleteStudentById")

public ResponseEntity<StudentVO> deleteStudentById(@RequestParam("id") Integer id) throws


Exception {

StudentVO studentResponse = null;

if (id != 0) {

studentResponse = studentService.deleteStudentById(id);

return new ResponseEntity<StudentVO>(studentResponse, HttpStatus.OK);

@GetMapping(value = "/getAllStudents")

public ResponseEntity<List<StudentVO>> getAllStudent() {

List<StudentVO> students = studentService.getAllStudents();

return new ResponseEntity<List<StudentVO>>(students, HttpStatus.OK);

Service:

package com.capgemini.demo.service;

import java.util.List;

import com.capgemini.demo.vo.StudentVO;

public interface StudentService {

public StudentVO addStudent(StudentVO student);


public StudentVO updateStudent(StudentVO student);

public StudentVO deleteStudentById(Integer id) throws Exception;

public List<StudentVO> getAllStudents();

ServiceImpl

package com.capgemini.demo.service;

import java.util.ArrayList;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import com.capgemini.demo.entity.Student;

import com.capgemini.demo.mapper.StudentMapper;

import com.capgemini.demo.repository.StudentRepository;

import com.capgemini.demo.vo.StudentVO;

@Service

public class StudentServiceImpl implements StudentService {

@Autowired

private StudentMapper studentMapper;

@Autowired
private StudentRepository studentRepository;

@Override

public StudentVO addStudent(StudentVO student) {

Student studentData = null;

StudentVO studentVO = null;

try {

studentData = studentMapper.voToEntity(student);

studentData = studentRepository.save(studentData);

studentVO = studentMapper.entityToVo(studentData);

} catch (Exception e) {

e.printStackTrace();

return studentVO;

@Override

public StudentVO updateStudent(StudentVO student) {

Student studentData = null;

StudentVO studentVO = null;

try {

Integer studentId = student.getId();

studentData = studentRepository.getOne(studentId);

if (studentData != null) {

studentData = studentMapper.voToEntity(student);

studentData = studentRepository.saveAndFlush(studentData);

studentVO = studentMapper.entityToVo(studentData);

} catch (Exception e) {
e.printStackTrace();

return studentVO;

@Override

public StudentVO deleteStudentById(Integer id) throws Exception {

StudentVO studentVO = null;

try {

Student student = studentRepository.getOne(id);

if (student != null)

studentVO = studentMapper.entityToVo(student);

} catch (Exception e) {

throw new Exception("Exception Occured");

return studentVO;

@Override

public List<StudentVO> getAllStudents() {

List<StudentVO> studentsList = new ArrayList<StudentVO>();

try {

List<Student> listOfStudents = studentRepository.findAll();

if (listOfStudents.size() > 0)

studentsList = studentMapper.listOfEntityToVo(listOfStudents);

} catch (Exception e) {

e.printStackTrace();

return studentsList;
}

Mapper:

package com.capgemini.demo.mapper;

import java.util.List;

import org.mapstruct.Mapper;

import org.mapstruct.ReportingPolicy;

import org.mapstruct.factory.Mappers;

import com.capgemini.demo.entity.Student;

import com.capgemini.demo.vo.StudentVO;

@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE)

public interface StudentMapper {

public static StudentMapper STUDENT_MAPPER = Mappers.getMapper(StudentMapper.class);

Student voToEntity(StudentVO student);

StudentVO entityToVo(Student student);

List<StudentVO> listOfEntityToVo(List<Student> students);

}
Repository:

package com.capgemini.demo.repository;

import org.springframework.data.jpa.repository.JpaRepository;

import org.springframework.stereotype.Repository;

import com.capgemini.demo.entity.Student;

@Repository

public interface StudentRepository extends JpaRepository<Student, Integer>{

You might also like