### Instruction:
Write a Java program that implements a simple RESTful API using the Spring Boot framework. The API should have endpoints for creating, reading, updating, and deleting (CRUD) records of an "Employee" entity with fields: id, name, email, and department. Use an in-memory H2 database to store the data.


### Response:

To create a simple RESTful API using Spring Boot, follow these steps:

1. Install the required tools: Java Development Kit (JDK), Integrated Development Environment (IDE) like IntelliJ IDEA or Eclipse, and Apache Maven.

2. Create a new Spring Boot project using Spring Initializr (https://start.spring.io/). Select "Web", "H2 Database", "JPA" as dependencies, and generate the project.

3. Extract the generated project and open it in your IDE.

4. Create an Employee entity class with fields id, name, email, and department. Add appropriate annotations for JPA to map this class to a database table.

```java
// src/main/java/com/example/demo/entity/Employee.java

package com.example.demo.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Employee {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String name;
    private String email;
    private String department;

    // Getters and setters
}
```

5. Create a repository interface that extends JpaRepository for performing CRUD operations on the Employee entity.

```java
// src/main/java/com/example/demo/repository/EmployeeRepository.java

package com.example.demo.repository;

import com.example.demo.entity.Employee;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
}
```

6. Create a service class for handling business logic related to Employee operations.

```java
// src/main/java/com/example/demo/service/EmployeeService.java

package com.example.demo.service;

import com.example.demo.entity.Employee;
import com.example.demo.repository.EmployeeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmployeeService {

    @Autowired
    private EmployeeRepository employeeRepository;

    public Employee createEmployee(Employee employee) {
        return employeeRepository.save(employee);
    }

    public List<Employee> getAllEmployees() {
        return employeeRepository.findAll();
    }

    public Employee getEmployeeById(Long id) {
        return employeeRepository.findById(id).orElse(null);
    }

    public Employee updateEmployee(Long id, Employee updatedEmployee) {
        Employee existingEmployee = employeeRepository.findById(id).orElse(null);
        if (existingEmployee != null) {
            existingEmployee.setName(updatedEmployee.getName());
            existingEmployee.setEmail(updatedEmployee.getEmail());
            existingEmployee.setDepartment(updatedEmployee.getDepartment());
            return employeeRepository.save(existingEmployee);
        }
        return null;
    }

    public void deleteEmployee(Long id) {
        employeeRepository.deleteById(id);
    }
}
```

7. Create a controller class for handling HTTP requests related to Employee operations.

```java
// src/main/java/com/example/demo/controller/EmployeeController.java

package com.example.demo.controller;

import com.example.demo.entity.Employee;
import com.example.demo.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/employees")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @PostMapping
    public Employee createEmployee(@RequestBody Employee employee) {
        return employeeService.createEmployee(employee);
    }

    @GetMapping
    public List<Employee> getAllEmployees() {
        return employeeService.getAllEmployees();
    }

    @GetMapping("/{id}")
    public Employee getEmployeeById(@PathVariable Long id) {
        return employeeService.getEmployeeById(id);
    }

    @PutMapping("/{id}")
    public Employee updateEmployee(@PathVariable Long id, @RequestBody Employee updatedEmployee) {
        return employeeService.updateEmployee(id, updatedEmployee);
    }

    @DeleteMapping("/{id}")
    public void deleteEmployee(@PathVariable Long id) {
        employeeService.deleteEmployee(id);
    }
}
```

8. Configure the H2 database in `application.properties` file.

```properties
# src/main/resources/application.properties

spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=

spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=update

spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
```

9. Run the application by executing the main class (DemoApplication.java) or using the command `mvn spring-boot:run`.

10. Test the API endpoints using a tool like Postman or curl.

You have now created a simple RESTful API with CRUD operations for an Employee entity using Spring Boot and an in-memory H2 database.