今天,我的新bing使用在等待多天的watinglist,申请终于生效了。作为一个Java老鸟,第一把肯定是要让它给我撸个入门级代码CRUD啦,不说啦,上图。
我问(以下简称 I ):Write code get crud example in java program
Okay, I will show you an example code for each step. Please note that this is just a sample code and you may need to modify it according to your requirements.
Step 1: Generate a starter project with Spring Web dependency.
You can use https://start.spring.io/ to generate a starter project with Spring Web dependency. You need to set the Java version to 11 and add Spring Web as a dependency. Then click on Generate button to download a zip file containing the project.
Step 2: Create an entity class with JPA annotations .
You need to create a class that represents your data model. For example, if you want to store employee details, you can create a class called Employee with fields like id, name, email, salary etc. You also need to add JPA annotations like @Entity, @Id, @GeneratedValue etc. to mark this class as an entity and map it to a database table.
Here is an example code for Employee class:
Okay, I will show you the next step.
Step 4: Create a controller class that handles HTTP requests and calls repository methods.
You need to create a class that is annotated with @RestController and @RequestMapping to define the base URL for your API. This class will have methods that are annotated with @GetMapping, @PostMapping, @PutMapping, @DeleteMapping etc. to handle different HTTP requests and perform CRUD operations using repository methods. You also need to use @RequestBody and @PathVariable annotations to get data from request body and URL path variables.
Here is an example code for EmployeeController class:
package com.example.demo.controller;
import com.example.demo.model.Employee;
import com.example.demo.repository.EmployeeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Optional;
@RestController
@RequestMapping("/employees")
public class EmployeeController {
@Autowired
private EmployeeRepository employeeRepository;
// Get all employees
@GetMapping
public List<Employee> getAllEmployees() {
return (List<Employee>) employeeRepository.findAll();
}
// Get employee by id
@GetMapping("/{id}")
public ResponseEntity<Employee> getEmployeeById(@PathVariable Long id) {
Optional<Employee> employee = employeeRepository.findById(id);
if (employee.isPresent()) {
return ResponseEntity.ok(employee.get());
} else {
return ResponseEntity.notFound().build();
}
}
// Create new employee
@PostMapping
public Employee createEmployee(@RequestBody Employee employee) {
return employeeRepository.save(employee);
}
// Update employee by id
@PutMapping("/{id}")
public ResponseEntity<Employee> updateEmployee(@PathVariable Long id, @RequestBody Employee updatedEmployee) {
Optional<Employee> employee = employeeRepository.findById(id);
if (employee.isPresent()) {
Employee currentEmployee = employee.get();
currentEmployee.setName(updatedEmployee.getName());
currentEmployee.setEmail(updatedEmployee.getEmail());
currentEmployee.setSalary(updatedEmployee.getSalary());
return ResponseEntity.ok(employeeRepository.save(currentEmployee));
} else {
return ResponseEntity.notFound().build();
}
}
// Delete employee by id
@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteEmployee(@PathVariable Long id) {
Optional<Employee> employee = employeeRepository.findById(id);
if (employee.isPresent()) {
employeeRepository.deleteById(id);
return ResponseEntity.ok().build();
} else {
return ResponseEntity.notFound().build();
}
}
}
```
Do you want me to show you the last step?
Source: Conversation with Bing, 2023/2/23(1) Spring Boot JPA CRUD Example (2023) - TechGeekNext. https://www.techgeeknext.com/spring-boot/spring-boot-jpa-crud-example Accessed 2023/2/23.
(2) Spring Boot - CrudRepository with Example - GeeksforGeeks. https://www.geeksforgeeks.org/spring-boot-crudrepository-with-example/ Accessed 2023/2/23.
(3) Spring Boot - CRUD Operations - GeeksforGeeks. https://www.geeksforgeeks.org/spring-boot-crud-operations/ Accessed 2023/2/23.
(4) Spring Boot CRUD Operations - javatpoint. https://www.javatpoint.com/spring-boot-crud-operations Accessed 2023/2/23.
(5) Spring Boot and Java Tutorial: Build a CRUD API - Auth0. https://auth0.com/blog/spring-boot-java-tutorial-build-a-crud-api/ Accessed 2023/2/23.