μ΄λ²λ
μ λ΄ λ―Έλλ₯Ό μν΄ λλΌλ ν΄μΌνλ€λ μκ°μΌλ‘
μλ
μ κΈ°μμ¬ λ£Έλ©μ΄νΈλ‘ λ§λ μΉκ΅¬ λλΆμ λ©μ¬λΌλ λμ리λ₯Ό μκ²λκ³ ,
λ§μ΄ λΆμ‘±ν μ€λ ₯μ΄μ§λ§ λ€μ΄κ°κ² λμλ€.
μ무κ²λ λͺ¨λ₯΄κ³ λ°©ν©νλ λμκ² κ·Έ μΉκ΅¬κ° μμμ μ΄ λ λλμ΄μ¬μ κ³ λ§μ λ€.
μ¬νΌ κ·Έλμ 3μλ¬λΆν° μμλ λ°±μλ μ€ν°λμ λν΄μ κΈ°λ‘, 볡μ΅μ μν΄ velogμ κΈ°λ‘νλ €κ³ νλ€.
λ΄ λ―Έλ, κΏ, μ§μ
κ°μ κ²λ€μ velogμ κΈ°λ‘νκ³ , μ·¨λ―ΈμΈ ν΄λΌμ΄λ°λ±λ±μ instagram, μΌμμ blogμ μ μ₯νμλ λͺ©νκ° μλ€. μΆμ΅νκ³ κ²½νμ κΈ°λ‘μΌλ‘ λ¨κΈ°λκ² μ’μ κ² κ°λ€κ³ μκ°μ νλ€.
API (Application Programing Interface)λ λ€λ₯Έ μννΈμ¨μ΄ μμ€ν κ³Ό ν΅μ νκΈ° μν΄ λ°λΌμΌ νλ κ·μΉμ μ μνλ κ²μ λ§νλ€.
νλ§λλ‘ μ΄λ ν μμ©νλ‘κ·Έλ¨μμ λ°μ΄ν°λ₯Ό μ£Όκ³ λ°κΈ° μν λ°©λ²μ μλ―Ένλ κ² κ°λ€.
μΈν°νμ΄μ€λ μ΄λ€ κΈ°κ³κ°μ μ₯μΉλΌλ¦¬ μ 보λ₯Ό κ΅ννκΈ° μν μλ¨μ΄λ λ°©λ²μ μλ―Ένλ€.
μΌμ’
μ 맀κ°μ²΄λ‘ μ€λ§νΈν°κ³Ό μ¬λκ°μ μ΄λ ν μ νΈλ₯Ό μ£Όκ³ λ°λ 맀κ°μ²΄κ° λλ μννΈμ¨μ΄μΈκ²κ°λ€.
UI(USER INTERFACE) μ°λ¦¬κ° λμΌλ‘ 보λ νλ©΄μ UIλΌ νλ€.
JPA (Java Persistence API)λ μλ° μ ν리μΌμ΄μ
μμ κ΄κ³ν λ°μ΄ν°λ² μ΄μ€λ₯Ό μ¬μ©νλ νμ€λ°©λ²μ μ 곡νλ APIμ΄λ€.
JPAλ κ°μ²΄-κ΄κ³ 맀ν(ORM) κΈ°μ μ μ¬μ©ν΄μ μλ° κ°μ²΄λ₯Ό λ°μ΄ν°λ² μ΄μ€ ν
μ΄λΈμ 맀ννλ€.
JDBC (Java Database Connectivity)λ μλ° μ ν리μΌμ΄μ
μ΄ λ°μ΄ν°λ² μ΄μ€μ μ°κ²°νκ³ SQL 쿼리λ₯Ό μ€ννμ¬ λ°μ΄ν°λ₯Ό κ²μνκ³ μ‘°μν μ μκ² νλ μ μμ€ API λΌκ³ νλ€.
JDBCλ λ°μ΄ν°λ² μ΄μ€μ μ μ₯λ λ°μ΄ν°λ₯Ό μλ° μ ν리μΌμ΄μ
μμ μ¬μ©ν μ μκ² νλ€.
ORM (Objcect-Relational Mapping)λ κ°μ²΄ μ§ν₯ νλ‘κ·Έλλ° μΈμ΄μ κ°μ²΄λ₯Ό κ΄κ³ν λ°μ΄ν°λ² μ΄μ€μ ν
μ΄λΈμ 맀ννλ κΈ°μ μ΄λ€.
λ°μ΄ν°λ² μ΄μ€μ λ°μ΄ν°λ₯Ό μλ° κ°μ²΄λ‘ 맀ννκ³ , μλ° κ°μ²΄λ₯Ό λ°μ΄ν°λ² μ΄μ€μ λ°μ΄ν°λ‘ 맀ννμ¬ νλ‘κ·Έλλ° ν¨λ¬λ€μμ λΆμΌμΉλ₯Ό ν΄κ²°νλ€.
ν¨λ¬λ€μμ λΆμΌμΉ ν΄κ²°
κ°μ²΄ μ€μ¬μ κ°λ°
μμ°μ± ν₯μ
μ±λ₯ κ°μ
μ μ§λ³΄μ μ©μ΄
build.gradle:
plugins {
id 'org.springframework.boot' version '2.6.6'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
runtimeOnly 'org.mariadb.jdbc:mariadb-java-client'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
test {
useJUnitPlatform()
}
src/main/resources/application.properties νμΌμ μμ±νκ³ , MariaDB λ°μ΄ν°λ² μ΄μ€ μ°κ²° μ 보λ₯Ό μ€μ νλ€.
application.properties:
spring.datasource.url=jdbc:mariadb://localhost:3306/your_database
spring.datasource.username=your_username
spring.datasource.password=your_password
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MariaDBDialect
λ°μ΄ν°λ² μ΄μ€ ν μ΄λΈμ 맀νλ μλ° ν΄λμ€λ₯Ό μ μνλ€.
User.java:
package com.example.demo;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String email;
// Getters and 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;
}
}
JPAλ₯Ό μ¬μ©νμ¬ CRUD μμ μ μνν 리ν¬μ§ν 리λ₯Ό μ μνλ€.
UserRepository.java:
package com.example.demo;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository<User, Long> {
}
λΉμ¦λμ€ λ‘μ§μ μ²λ¦¬νλ μλΉμ€ ν΄λμ€λ₯Ό ꡬννλ€.
UserService.java:
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
public User saveUser(User user) {
return userRepository.save(user);
}
public List<User> getAllUsers() {
return userRepository.findAll();
}
public User getUserById(Long id) {
return userRepository.findById(id).orElse(null);
}
public void deleteUser(Long id) {
userRepository.deleteById(id);
}
}
HTTP μμ²μ μ²λ¦¬νλ 컨νΈλ‘€λ¬ ν΄λμ€λ₯Ό ꡬννλ€.
UserController.java:
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
private UserService userService;
@PostMapping
public ResponseEntity<User> createUser(@RequestBody User user) {
User createdUser = userService.saveUser(user);
return new ResponseEntity<>(createdUser, HttpStatus.CREATED);
}
@GetMapping
public ResponseEntity<List<User>> getAllUsers() {
List<User> users = userService.getAllUsers();
return new ResponseEntity<>(users, HttpStatus.OK);
}
@GetMapping("/{id}")
public ResponseEntity<User> getUserById(@PathVariable Long id) {
User user = userService.getUserById(id);
return user != null ? new ResponseEntity<>(user, HttpStatus.OK) : new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteUser(@PathVariable Long id) {
userService.deleteUser(id);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
}
DemoApplication.java:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
μ΄μ μ ν리μΌμ΄μ μ μ€ννλ©΄, Spring Boot μ ν리μΌμ΄μ μ΄ μμλκ³ MariaDB λ°μ΄ν°λ² μ΄μ€μ μ°κ²°λ©λλ€. Postman λλ λ€λ₯Έ HTTP ν΄λΌμ΄μΈνΈλ₯Ό μ¬μ©νμ¬ CRUD μμ μ μνν μ μλ€.
Create User (POST μμ²)
URL: http://localhost:8080/users
Method: POST
Body (raw, JSON)
{
"name": "John Doe",
"email": "john.doe@example.com"
}
Get All Users (GET μμ²)
URL: http://localhost:8080/users
Method: GET
Get User by ID (GET μμ²)
URL: http://localhost:8080/users/1
Method: GET
Delete User (DELETE μμ²)
URL: http://localhost:8080/users/1
Method: DELETE
μ΄μ κ°μ λ°©λ²μΌλ‘ Gradleκ³Ό MariaDBλ₯Ό μ¬μ©νμ¬ Spring Boot μ ν리μΌμ΄μ μ μ€μ νκ³ , JPAλ₯Ό νμ©νμ¬ κ°λ¨ν CRUD μ ν리μΌμ΄μ μ ꡬνν μ μλ€.
λμ€μλ μ€ν°λμμ ν νλ‘μ νΈλ‘ νμ©ν΄λ³΄κ² λ€.