
백엔드
Spring BootSpring Boot
Spring Boot는 Java 기반 엔터프라이즈 웹 프레임워크로, 기존 Spring Framework의 복잡한 설정을 자동화해 빠른 생산성을 제공한다. 내장 서버, 자동 설정, 프로덕션-레디 기능이 포함되어 있다.
기본 구조
java
@SpringBootApplication // @Configuration + @EnableAutoConfiguration + @ComponentScan
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
@RestController
@RequestMapping("/api/items")
public class ItemController {
private final ItemService itemService;
public ItemController(ItemService itemService) {
this.itemService = itemService;
}
@GetMapping("/{id}")
public ResponseEntity<ItemDto> getItem(@PathVariable Long id) {
return itemService.findById(id)
.map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build());
}
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public ItemDto createItem(@Valid @RequestBody ItemDto dto) {
return itemService.create(dto);
}
}Spring Data JPA
java
@Entity
@Table(name = "items")
public class Item {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false)
private String name;
private BigDecimal price;
}
public interface ItemRepository extends JpaRepository<Item, Long> {
List<Item> findByNameContaining(String keyword);
@Query("SELECT i FROM Item i WHERE i.price < :maxPrice")
List<Item> findCheapItems(@Param("maxPrice") BigDecimal maxPrice);
}설정 (application.yml)
yaml
spring:
datasource:
url: jdbc:postgresql://localhost:5432/mydb
username: user
password: pass
jpa:
hibernate:
ddl-auto: validate
show-sql: false
cache:
type: redis
server:
port: 8080