UseCase Capgemini
UseCase Capgemini
UseCase Capgemini
package com.capgemini.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
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
@Bean
dataSource.setDriverClassName("org.h2.Driver");
dataSource.setUrl("jdbc:h2:~/studentData;DB_CLOSE_DELAY=-1");
dataSource.setUsername("venu");
dataSource.setPassword("venu");
return dataSource;
@Bean
entityManagerFactoryBean.setDataSource(dataSource());
entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
entityManagerFactoryBean.setPackagesToScan("com.capgemini.demo.*");
entityManagerFactoryBean.setJpaProperties(additionalProperties());
return entityManagerFactoryBean;
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
transactionManager.setEntityManagerFactory(entityManagerFactory);
return transactionManager;
@Bean
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")
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID")
@Column(name = "STUDENT_NAME")
@Column(name = "AGE")
@Column(name = "STUDENT_CLASS")
return id;
}
public void setId(Integer id) {
this.id = id;
return studentName;
this.studentName = studentName;
return age;
this.age = age;
return 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)
@JsonProperty("id")
@JsonProperty("studentName")
@JsonProperty("age")
@JsonProperty("studentClass")
return id;
this.id = id;
return studentName;
this.studentName = studentName;
return age;
}
public void setAge(String age) {
this.age = age;
return 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")
@Autowired
@PostMapping(value = "/addStudent")
if (student != null) {
studentResponse = studentService.addStudent(student);
@PutMapping(value = "/updateStudent")
studentResponse = studentService.updateStudent(student);
}
@DeleteMapping(value = "/deleteStudentById")
if (id != 0) {
studentResponse = studentService.deleteStudentById(id);
@GetMapping(value = "/getAllStudents")
Service:
package com.capgemini.demo.service;
import java.util.List;
import com.capgemini.demo.vo.StudentVO;
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
@Autowired
@Autowired
private StudentRepository studentRepository;
@Override
try {
studentData = studentMapper.voToEntity(student);
studentData = studentRepository.save(studentData);
studentVO = studentMapper.entityToVo(studentData);
} catch (Exception e) {
e.printStackTrace();
return studentVO;
@Override
try {
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
try {
if (student != null)
studentVO = studentMapper.entityToVo(student);
} catch (Exception e) {
return studentVO;
@Override
try {
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;
}
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