create: Creates tables every time (deletes old data).
update: Updates schema (recommended).
none: No automatic changes.
Step 3: Create Entity Class (User.java)
import jakarta.persistence.*;
@Entity
@Table(name = "users") // Table name in MySQL
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY) // Auto-increment ID
private Long id;
@Column(nullable = false)
private String name;
@Column(unique = true, nullable = false)
private String email;
// Constructors
public User() {}
public User(String name, String email) {
this.name = name;
this.email = email;
}
// Getters & Setters
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getEmail() { return email; }
public void setEmail(String email) { this.email = email; }
}
Step 4: Create Repository (UserRepository.java)
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UserRepository extends JpaRepository {
// Custom queries can be added here
}
✅ Spring Data JPA automatically provides CRUD operations!
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
// Get all users
public List getAllUsers() {
return userRepository.findAll();
}
// Get user by ID
public Optional getUserById(Long id) {
return userRepository.findById(id);
}
// Save user
public User saveUser(User user) {
return userRepository.save(user);
}
// Update user
public void updateUser(User user) {
userRepository.save(user);
}
// Delete user
public void deleteUser(Long id) {
userRepository.deleteById(id);
}
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Optional;
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
private UserService userService;
// ✅ Get all users
@GetMapping
public List getAllUsers() {
return userService.getAllUsers();
}
// ✅ Get user by ID
@GetMapping("/{id}")
public Optional getUserById(@PathVariable Long id) {
return userService.getUserById(id);
}
// ✅ Create user
@PostMapping
public User createUser(@RequestBody User user) {
return userService.saveUser(user);
}
// ✅ Update user
@PutMapping("/{id}")
public String updateUser(@RequestBody User user) {
userService.updateUser(user);
return "User updated with ID: " + user.getId();
}
// ✅ Delete user
@DeleteMapping("/{id}")
public String deleteUser(@PathVariable Long id) {
userService.deleteUser(id);
return "User deleted with ID: " + id;
}
}
Running the Spring Boot Application
Run the application using your IDE or with the following Maven command:
mvn spring-boot:run
Spring Boot starts an embedded Tomcat server (default: http://localhost:8080).
Testing the REST API with Postman:
Once the application is running, test the endpoints:
We use cookies and similar technologies to enhance your browsing experience, serve personalized content and advertisements, and analyze our traffic.
By clicking "Accept All", you consent to our use of cookies and the processing of your personal data for these purposes.
You can manage your preferences or learn more in our
Privacy Policy and
Cookie Policy.