In Spring, a bean is simply an object that is managed by the Spring framework. It’s created and controlled by the Spring IoC (Inversion of Control) container.
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class ExampleApplication { public static void main(String[] args) { SpringApplication.run(ExampleApplication.class, args); } }
The @SpringBootApplication annotation is used to mark the main class of a Spring Boot application. It combines several annotations to simplify configuration.
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
The application context is where all the beans are stored and managed. It is created automatically when you start a Spring Boot application.
Spring Boot simplifies the setup and configuration of Spring applications. It provides default settings and reduces the need for manual configurations.
This file is used to configure various settings of your Spring Boot application, such as database connections and server ports.
JPA is a specification for managing data in Java applications. It provides an interface to interact with databases using Object-Relational Mapping (ORM). This means you can work with Java objects rather than writing SQL queries.
import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="Books") public class Book { @Id private Integer id; private String title; }
H2 is a lightweight, in-memory database that can also be used for disk-based storage. It’s commonly used for development and testing due to its simplicity.
A data model represents the structure of the data you need to store. It maps Java classes to database tables, allowing you to manage data in a structured way.
@Entity @Table(name="Library") public class Book { @Id private Integer id; @Column(name="Title") private String title; }
Entities are classes that represent data in the database. For example, in a music playlist application, entities might include artists, albums, and tracks.
@Entity @Table(name="Televisions") public class Television { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private Integer id; @Column(name="Brand") private String brand; }
The @Id annotation marks a field in an entity class as the primary key. This field uniquely identifies each record in the database table.
import org.springframework.data.repository.CrudRepository; import com.example.entities.Person; public interface PersonRepository extends CrudRepository{}
The @GeneratedValue annotation specifies how the primary key should be generated. It can use various strategies like auto-increment or UUID.
import org.springframework.web.bind.annotation.RestController; import com.example.repositories.PersonRepository; @RestController public class PersonController { private final PersonRepository personRepository; public PersonController(PersonRepository personRepository) { this.personRepository = personRepository; } }
Repositories are used to perform database operations. Spring Data JPA provides built-in methods for common tasks like saving, finding, and deleting entities.
@PostMapping("/people") public Person createPerson(@RequestBody Person person) { return this.personRepository.save(person); }
Spring automatically creates implementations of your repository interfaces and injects them where needed, so you don't have to write the implementation yourself.
@GetMapping("/people") public IterablegetAllPeople() { return this.personRepository.findAll(); }
The save method allows you to create or update an entity in the database. It returns the saved or updated entity.
@PostMapping("/people") public Person createPerson(@RequestBody Person person) { return this.personRepository.save(person); }
The findAll method retrieves all entities from the database. It returns an iterable collection of records.
@GetMapping("/people") public IterablegetAllPeople() { return this.personRepository.findAll(); }
The delete method removes an entity from the database. It requires the entity to be deleted or its ID.
@DeleteMapping("/people/{id}") public void deletePerson(@PathVariable Integer id) { this.personRepository.deleteById(id); }
The findById method retrieves an entity by its ID. It returns an Optional, which may contain the entity or be empty if not found.
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @RestController public class PersonController { @Autowired private PersonRepository personRepository; @GetMapping("/people/{id}") public ResponseEntity<Person> getPersonById(@PathVariable Integer id) { Optional<Person> person = personRepository.findById(id); return person.map(ResponseEntity::ok) .orElseGet(() -> ResponseEntity.notFound().build()); } }
Welcome to our comprehensive collection of programming language cheatsheets! Whether you're a seasoned developer or a beginner, these quick reference guides provide essential tips and key information for all major languages. They focus on core concepts, commands, and functions—designed to enhance your efficiency and productivity.
ManageEngine Site24x7, a leading IT monitoring and observability platform, is committed to equipping developers and IT professionals with the tools and insights needed to excel in their fields.
Monitor your IT infrastructure effortlessly with Site24x7 and get comprehensive insights and ensure smooth operations with 24/7 monitoring.
Sign up now!