Intro To SpringBoot and Rest and JPA
Intro To SpringBoot and Rest and JPA
i-design
---------------------------
POST and Get Using @RequestMapping
PatientController
-----------------------------------------------------------------------------------
-
package com.springboot.controller;
import java.util.List;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import com.springboot.dao.PatientDAO;
import com.springboot.domain.Patient;
import com.springboot.service.PatientService;
@Autowired
private PatientService patientService;
@RequestMapping(value="/patient/list",method=RequestMethod.GET)
public List<Patient> listPatients(){
//Fill the code
return patientService.getPatients();
}
--------------------------------------------------------------------
PatientDAOImpl
package com.springboot.dao;
import java.util.List;
import java.util.ArrayList;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import com.springboot.domain.Patient;
static {
patientList.add(new Patient(1L, "Swathy", "9876567234",
"[email protected]", "31-07-1989"));
patientList.add(new Patient(2L, "Vanmathi", "9873877234",
"[email protected]", "23-04-1992"));
patientList.add(new Patient(3L, "Kevin", "9823641234",
"[email protected]", "01-04-2000"));
}
}
---------------------------------------------------------------------
PatientServiceImpl
package com.springboot.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import com.springboot.dao.PatientDAO;
import com.springboot.dao.PatientDAOImpl;
import com.springboot.domain.Patient;
-----------------------------------------------------------------------------------
-------------------------------------------------------
Intro to SpringBoot
I-assess
-----------------------------------------------------------------------------------
-------------------------------------------------------
package com.springboot.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.springboot.dao.DoctorDAO;
import com.springboot.domain.Doctor;
import com.springboot.service.DoctorService;
import com.springboot.service.DoctorServiceImpl;
@Autowired
private DoctorService service;
@GetMapping("/doctor/list")
public List<Doctor> getDoctors(){
//Fill your code here
return service.list();
}
}
-----------------------------------------------------------------------------
package com.springboot.dao;
import java.util.*;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import com.springboot.domain.Doctor;
static{
doctors.add(new Doctor(1, "Elizabeth", "MBBS", 4.2, "Cardiologist",
750.));
doctors.add(new Doctor(2, "Michael", "MBBS", 2.0, "Dermatologist",
1500.));
doctors.add(new Doctor(3, "Charlotte", "MBBS", 3.1, "Pediatrics",
200.));
doctors.add(new Doctor(4, "Lucas", "BDS", 1.9, "Dentist", 250.));
}
-------------------------------------------------------
package com.springboot.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import com.springboot.dao.DoctorDAO;
import com.springboot.dao.DoctorDAOImpl;
import com.springboot.domain.Doctor;
===================================================================================
=======================================================
JPA and Rest
i-assess
package com.weather.jpa.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
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.RestController;
import com.weather.jpa.domain.WeatherReport;
import com.weather.jpa.service.WeatherService;
@GetMapping("/weatherReport")
public List<WeatherReport> getData() {
//Fill your code here
return service.getData();
}
}
----------------------------------------------------------------------------
package com.weather.jpa.domain;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "weather_report")
public class WeatherReport {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "city")
private String city;
@Column(name = "min_temperature")
private Double minTemperature;
@Column(name = "max_temperature")
private Double maxTemperature;
public WeatherReport() {
}
import org.springframework.stereotype.Repository;
import com.weather.jpa.domain.WeatherReport;
import org.springframework.data.repository.CrudRepository;
----------------------------------------------------------
package com.weather.jpa.service;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.weather.jpa.domain.WeatherReport;
import com.weather.jpa.repository.WeatherRepository;
}
public Boolean deleteCases(Long id) {
//Fill your code here
if(repo.findById(id).get()!=null) {
repo.deleteById(id);
return true;
}
return false;
}
-----------------------------------------------------------------------------------
-------------------------------------------------------
Rset and JPA
i-design
-----------------------------------------------------------------------------------
-------------------------------------------------------
package com.springboot.controller;
import com.springboot.dao.DoctorDAO;
import com.springboot.domain.Doctor;
import com.springboot.service.DoctorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@GetMapping("/list")
List<Doctor> getDoctors()
{
//Fill your code here
return service.list();
}
}
-------------
package com.springboot.dao;
import com.springboot.domain.Doctor;
import org.springframework.stereotype.Repository;
import java.util.ArrayList;
import java.util.List;
static
{
doctorList.add(new
Doctor(1,"Harinii","MBBS",4.2,"Orthologist",750.00));
doctorList.add(new
Doctor(2,"Nithin","MBBS",2.0,"Gynecologist",1500.00));
}
}
}
------------------------------
package com.springboot.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import com.springboot.dao.DoctorDAO;
import com.springboot.dao.DoctorDAOImpl;
import com.springboot.domain.Doctor;