API Requirement
* Create a REST API for the Employee Directory
* REST client Should be able to
1. Get a list of employees
2. Get a single employee by id
3. Add a new employee
4. Update a new employee
5. Delete and employee
HTTP Method Crud Action
1. POST :- /api/employee - create a new employee
2. GET :- /api/employee - Read a list of employee
3. GET :- /api/employee /{employee} - Read a single employee
4. PUT :- /api/employee - Update an existing employee
5. DELETE :- /api/employee/ {emloyeeId} - delete an existing employ
* Development Process
1. Set up Database Dev Enivironment
2. Create Spring boot Project using Spring initializr
3. Get list of employee
4 Get list of employee by ID
5.Add a new employee
6 Update an existing employee
* Application Architecture
Employee Employee Employee
REST <-----> Service <-----> DAO <-----> DATABASE
Controller
............................................................
* Create Employee Table on database
1. Employee.sql
Download file in http://www.luv2code.com/spring-boot-employee-sql-script
2. create a project in crud demo
* add some dependency
1 Spring web
2. spring jpa
3. spring devtool
4. MYSQL driver
* Hibernate Session Factory
1. in the Past ,our DAO used a Hibernate Session Factory
2. Hibernate Session Factory nedds a data Source
3. The Data Source Defines database Connection info
* DAO class Architecture
Employee Session Data
DAO <---------> Factory <---------> Source <---------> database
* Spring Boot to the Rescue
1. Spring boot will automatically configure your data source for you
2. based on entire from Maven Pom file
1.1 JDBC Driver: mysql-connector -java
1.2 spring Data(ORM) : spring-boot-starter-data-jpa
1.3 DB connection info from application.properties
-** application.properties
spring.datasource.url = jdbc:mysql://localhost:3306/emloyee_directory?useSSL=
false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=Sanjay1996@
..................//..................
//what is JPA
JAVA Persistence API
1. Standard API for Object-to-Relational-Mapping(ORM)
2. only a Specification
3.defines a set of interface
4. Required an implementationto be usable
..................//..............
* Auto Data Source Configuration
1 in Spring boot ,Hibernate is default implementation of JPA .
2 EntityManager is similar to Hibernate Sessionfactory .
3. entityManager can Serve as a Wrapper for a Hibernate Session object
4. we can inject the EntityManager into our DAO
................//..............
* Version DAO Techniques
1. Version 1:-use EntityManager but Leveragenative Hiberante API
2. version 2 : use EntityManager and standard JPA API
3. Version 3 :- Spring Data JPA
................//............
* DAO Interface
Public interface EmployeeDAO{
public List<Employee>findAll();
}
..............//.............
@Repository
public class EmployeeDAOHibernateImpl implements EmployeeDAO{
private EntityManager entityManager;
@Autowired
public EmployeeDAOHibernateImpl (EntityManager theEntityManager){
entityManager - theEntityManager;
}
}
.....................//..................
* Developemet Process
1. Update db Configin applicaton.properties
2. Create Employee entity
3. CReate DAO interface
4. Create DAO Implementation
5. CREATE REST controller to use DAO
...............................//............................
* Read a single Employee
GET /api/employees/{employeeId}
REST -------------------------------------> Employee
client<-----------------------------------REST Controller
................//...............
* Create a New Empoyee
POST /api/employees/
fistname,lastname,email
REST -------------------------------------> Employee
client<-----------------------------------REST Controller
...................//...................
* update a Employee detail
PUT /api/employees/
REST -------------------------------------> Employee
client<-----------------------------------REST Controller
..........................//....................
* delete a employe detail
DELETE /api/employees/{employeeId}
REST -------------------------------------> Employee
client<-----------------------------------REST Controller
...........................//..................