πŸ¦λ©‹μŸμ΄μ‚¬μžμ²˜λŸΌ λ°±μ—”λ“œ μŠ€ν„°λ”” (1)

μœ€μ€€μƒΒ·2024λ…„ 6μ›” 2일
0

λ©‹μŸμ΄μ‚¬μžμ²˜λŸΌ

λͺ©λ‘ 보기
2/4
post-thumbnail

μ΄λ²ˆλ…„μ— λ‚΄ 미래λ₯Ό μœ„ν•΄ 뭐라도 ν•΄μ•Όν•œλ‹€λŠ” μƒκ°μœΌλ‘œ
μž‘λ…„μ— κΈ°μˆ™μ‚¬ λ£Έλ©”μ΄νŠΈλ‘œ λ§Œλ‚œ 친ꡬ 덕뢄에 λ©‹μ‚¬λΌλŠ” 동아리λ₯Ό μ•Œκ²Œλ˜κ³ ,
많이 λΆ€μ‘±ν•œ μ‹€λ ₯μ΄μ§€λ§Œ λ“€μ–΄κ°€κ²Œ λ˜μ—ˆλ‹€.
아무것도 λͺ¨λ₯΄κ³  λ°©ν™©ν•˜λ˜ λ‚˜μ—κ² κ·Έ μΉœκ΅¬κ°€ μ‹œμž‘μ μ΄ 된 λŠλ‚Œμ΄μ—¬μ„œ κ³ λ§ˆμ› λ‹€.

μ—¬νŠΌ κ·Έλž˜μ„œ 3월달뢀터 μ‹œμž‘λœ λ°±μ—”λ“œ μŠ€ν„°λ””μ— λŒ€ν•΄μ„œ 기둝, λ³΅μŠ΅μ„ μœ„ν•΄ velog에 κΈ°λ‘ν•˜λ €κ³  ν•œλ‹€.
λ‚΄ 미래, 꿈, 직업같은 것듀은 velog에 κΈ°λ‘ν•˜κ³ , 취미인 클라이밍등등은 instagram, 일상은 blog에 μ €μž₯ν•˜μžλŠ” λͺ©ν‘œκ°€ μžˆλ‹€. μΆ”μ–΅ν•˜κ³  κ²½ν—˜μ€ 기둝으둜 λ‚¨κΈ°λŠ”κ²Œ 쒋을 것 κ°™λ‹€κ³  생각을 ν–ˆλ‹€.

λ°±μ—”λ“œ μŠ€ν„°λ”” (1)


μš©μ–΄

API

API (Application Programing Interface)λŠ” λ‹€λ₯Έ μ†Œν”„νŠΈμ›¨μ–΄ μ‹œμŠ€ν…œκ³Ό ν†΅μ‹ ν•˜κΈ° μœ„ν•΄ 따라야 ν•˜λŠ” κ·œμΉ™μ„ μ •μ˜ν•˜λŠ” 것을 λ§ν•œλ‹€.

ν•œλ§ˆλ””λ‘œ μ–΄λ– ν•œ μ‘μš©ν”„λ‘œκ·Έλž¨μ—μ„œ 데이터λ₯Ό μ£Όκ³  λ°›κΈ° μœ„ν•œ 방법을 μ˜λ―Έν•˜λŠ” 것 κ°™λ‹€.

μΈν„°νŽ˜μ΄μŠ€

μΈν„°νŽ˜μ΄μŠ€λŠ” μ–΄λ–€ κΈ°κ³„κ°„μ˜ μž₯치끼리 정보λ₯Ό κ΅ν™˜ν•˜κΈ° μœ„ν•œ μˆ˜λ‹¨μ΄λ‚˜ 방법을 μ˜λ―Έν•œλ‹€.
μΌμ’…μ˜ 맀개체둜 슀마트폰과 μ‚¬λžŒκ°„μ˜ μ–΄λ– ν•œ μ‹ ν˜Έλ₯Ό μ£Όκ³  λ°›λŠ” λ§€κ°œμ²΄κ°€ λ˜λŠ” μ†Œν”„νŠΈμ›¨μ–΄μΈκ²ƒκ°™λ‹€.
UI(USER INTERFACE) μš°λ¦¬κ°€ 눈으둜 λ³΄λŠ” 화면을 UI라 ν•œλ‹€.

JPA

JPA (Java Persistence API)λŠ” μžλ°” μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ—μ„œ κ΄€κ³„ν˜• λ°μ΄ν„°λ² μ΄μŠ€λ₯Ό μ‚¬μš©ν•˜λŠ” ν‘œμ€€λ°©λ²•μ„ μ œκ³΅ν•˜λŠ” API이닀.
JPAλŠ” 객체-관계 맀핑(ORM) κΈ°μˆ μ„ μ‚¬μš©ν•΄μ„œ μžλ°” 객체λ₯Ό λ°μ΄ν„°λ² μ΄μŠ€ ν…Œμ΄λΈ”μ— λ§€ν•‘ν•œλ‹€.

JDBC

JDBC (Java Database Connectivity)λŠ” μžλ°” μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ΄ λ°μ΄ν„°λ² μ΄μŠ€μ— μ—°κ²°ν•˜κ³  SQL 쿼리λ₯Ό μ‹€ν–‰ν•˜μ—¬ 데이터λ₯Ό κ²€μƒ‰ν•˜κ³  μ‘°μž‘ν•  수 있게 ν•˜λŠ” μ €μˆ˜μ€€ API λΌκ³ ν•œλ‹€.
JDBCλŠ” λ°μ΄ν„°λ² μ΄μŠ€μ— μ €μž₯된 데이터λ₯Ό μžλ°” μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ—μ„œ μ‚¬μš©ν•  수 있게 ν•œλ‹€.

ORM

ORM (Objcect-Relational Mapping)λŠ” 객체 지ν–₯ ν”„λ‘œκ·Έλž˜λ° μ–Έμ–΄μ˜ 객체λ₯Ό κ΄€κ³„ν˜• λ°μ΄ν„°λ² μ΄μŠ€μ˜ ν…Œμ΄λΈ”μ— λ§€ν•‘ν•˜λŠ” κΈ°μˆ μ΄λ‹€.
λ°μ΄ν„°λ² μ΄μŠ€μ˜ 데이터λ₯Ό μžλ°” 객체둜 λ§€ν•‘ν•˜κ³ , μžλ°” 객체λ₯Ό λ°μ΄ν„°λ² μ΄μŠ€μ˜ λ°μ΄ν„°λ‘œ λ§€ν•‘ν•˜μ—¬ ν”„λ‘œκ·Έλž˜λ° νŒ¨λŸ¬λ‹€μž„μ˜ 뢈일치λ₯Ό ν•΄κ²°ν•œλ‹€.


JPA의 μ‚¬μš©μ΄μœ 

  1. νŒ¨λŸ¬λ‹€μž„μ˜ 뢈일치 ν•΄κ²°

    • 객체 지ν–₯ ν”„λ‘œκ·Έλž˜λ° 언어와 κ΄€κ³„ν˜• λ°μ΄ν„°λ² μ΄μŠ€μ˜ 데이터 λͺ¨λΈ κ°„μ˜ 뢈일치λ₯Ό ν•΄μ†Œν•œλ‹€.
  2. 객체 쀑심적 개발

    • SQL μ€‘μ‹¬μ˜ 개발 λ°©μ‹μ—μ„œ λ²—μ–΄λ‚˜ 객체 μ€‘μ‹¬μœΌλ‘œ κ°œλ°œν•  수 있게 ν•œλ‹€.
  3. 생산성 ν–₯상

    • 반볡적인 SQL μ½”λ“œ μž‘μ„±μ—μ„œ ν•΄λ°©λ˜μ–΄ λΉ„μ¦ˆλ‹ˆμŠ€ λ‘œμ§μ— 집쀑할 수 있게 ν•œλ‹€.
  4. μ„±λŠ₯ κ°œμ„ 

    • 1μ°¨ μΊμ‹œ, μ§€μ—°λ‘œλ”©, μ“°κΈ° μ§€μ—°λ“±μ˜ μ΅œμ ν™” κΈ°λŠ₯을 톡해 μ„±λŠ₯을 κ°œμ„ ν•œλ‹€.
  5. μœ μ§€λ³΄μˆ˜ 용이

    • 객체 지ν–₯ λͺ¨λΈμ„ μ‚¬μš©ν•˜μ—¬ μ½”λ“œμ˜ 가독성과 μœ μ§€λ³΄μˆ˜μ„±μ„ 높인닀.

ꡬ동방식 (GPTμ˜ˆμ‹œ)

1. ν”„λ‘œμ νŠΈ μ„€μ •

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()
}

2. application.properties μ„€μ •

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

3. μ—”ν‹°ν‹° 클래슀 μ •μ˜

λ°μ΄ν„°λ² μ΄μŠ€ ν…Œμ΄λΈ”μ— 맀핑될 μžλ°” 클래슀λ₯Ό μ •μ˜ν•œλ‹€.

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;
    }
}

4. 리포지토리 μΈν„°νŽ˜μ΄μŠ€ 생성

JPAλ₯Ό μ‚¬μš©ν•˜μ—¬ CRUD μž‘μ—…μ„ μˆ˜ν–‰ν•  리포지토리λ₯Ό μ •μ˜ν•œλ‹€.

UserRepository.java:

package com.example.demo;

import org.springframework.data.jpa.repository.JpaRepository;

public interface UserRepository extends JpaRepository<User, Long> {
}

5. μ„œλΉ„μŠ€ 클래슀 κ΅¬ν˜„

λΉ„μ¦ˆλ‹ˆμŠ€ λ‘œμ§μ„ μ²˜λ¦¬ν•˜λŠ” μ„œλΉ„μŠ€ 클래슀λ₯Ό κ΅¬ν˜„ν•œλ‹€.

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);
    }
}

6. 컨트둀러 클래슀 κ΅¬ν˜„

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);
    }
}

7. μ• ν”Œλ¦¬μΌ€μ΄μ…˜ μ‹€ν–‰

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 μž‘μ—…μ„ μˆ˜ν–‰ν•  수 μžˆλ‹€.

Postman을 μ‚¬μš©ν•œ 예제

  1. Create User (POST μš”μ²­)

    URL: http://localhost:8080/users
    Method: POST
    Body (raw, JSON)

    {
      "name": "John Doe",
      "email": "john.doe@example.com"
    }
    
  2. 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 μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ„ κ΅¬ν˜„ν•  수 μžˆλ‹€.

λ‚˜μ€‘μ—λŠ” μŠ€ν„°λ””μ—μ„œ ν•œ ν”„λ‘œμ νŠΈλ‘œ ν™œμš©ν•΄λ³΄κ² λ‹€.

profile
ν˜λŸ¬κ°€λ˜ μ›ν•˜λŠ” λ°©ν–₯으둜

0개의 λŒ“κΈ€