๐Ÿ”ฅ TIL - Day 16

Kim Dae Hyunยท2021๋…„ 9์›” 30์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
18/93

๐Ÿ“Œ Springboot + MongoDB

1์ฐจ ํ”„๋กœ์ ํŠธ๊ฐ€ ๋๋‚˜์„œ ์˜ค๋žœ๋งŒ์— ๊ฐœ์ธ๊ณต๋ถ€๋ฅผ ํ•œ๋‹ค.

์บ ํ”„ ๊ธฐ๊ฐ„๋™์•ˆ ๋ชจ๋“  ํ”„๋กœ์ ํŠธ์—์„œ mongoDB๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค. ๋น ๋ฅธ ๊ฐœ๋ฐœ์— ์žˆ์–ด์„œ ์ •๋ง ์ข‹์€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ผ๋Š” ์ƒ๊ฐ์ด ๋“ ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€ RDBMS๋ฅผ ์ด์šฉํ•ด์„œ ์ง„ํ–‰ํ•œ ํ”„๋กœ์ ํŠธ๋Š” ๋ณธ์ธ์ด ์Šคํ‚ค๋งˆ ๊ตฌ์„ฑ์— ์„œํˆด๋Ÿฌ์„œ ๊ต‰์žฅํžˆ ๋งŽ์€ ์‹œ๊ฐ„์„ ERD๋ฅผ ๊ทธ๋ฆฌ๋Š”๋ฐ ์†Œ๋น„ํ–ˆ๋Š”๋ฐ mongoDB๋Š” ์ผ๋‹จ ์ง„ํ–‰ํ•˜๊ณ  ์ดํ›„ ์œ ์—ฐํ•˜๊ฒŒ ๋ณ€๊ฒฝ์ด ๊ฐ€๋Šฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฐœ๋ฐœ์ด ๋งค์šฐ ๋น ๋ฅด๋‹ค.

์ด๋ ‡๊ฒŒ mongoDB์— ๊ด€์‹ฌ์ด ์ƒ๊ฒจ์„œ ์ง€๊ธˆ๊ป ์ญ‰ ๊ณต๋ถ€ ์ค‘์ธ springboot์— ์—ฐ๋™์‹œ์ผœ๋ณด๊ณ  ์‹ถ์—ˆ๋‹ค.

์ผ๋‹จ ์จ๋ณด์ž.

โœ”๏ธ ํ”„๋กœ์ ํŠธ ์˜์กด์„ฑ์€ web๊ณผ mongoDB๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.

implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
implementation 'org.springframework.boot:spring-boot-starter-web'

โœ”๏ธ ์„ค์ •ํŒŒ์ผ์„ ์ด์šฉํ•ด์„œ mongoDB์™€ ์—ฐ๊ฒฐ์„ ์„ค์ •ํ•œ๋‹ค. (.yml)

spring:
  data:
    mongodb:
      host: localhost
      port: 27017
      database: testdb

โœ”๏ธ ์—”ํ‹ฐํ‹ฐ ์—ญํ• ์„ ํ•ด์ค„ Document Schema๋ฅผ ์ •์˜ํ•œ๋‹ค.

@Document("user")
@Data
@NoArgsConstructor
public class UserDoc {

    @Id
    private String id; // ObjectId
    private String name;
    private int age;

    public UserDoc(String name, int age) {
        this.name = name;
        this.age = age;
    }
}

โœ”๏ธ Repository ์ƒ์„ฑ (wow....)

JPA๋ž‘ ๋˜‘๊ฐ™๋‹ค ... ์ฉ๋‹ค ...
MongoRepository๋ฅผ ํƒ€๊ณ  ์˜ฌ๋ผ๊ฐ€๋ฉด CrudRepository๊ฐ€ ๋‚˜์˜จ๋‹ค. wooooow....

public interface UserDocRepository extends MongoRepository<UserDoc, String> {

    public Optional<UserDoc> findByName(String name);
}

โœ”๏ธ ํ…Œ์ŠคํŠธ์šฉ ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ๋งŒ๋“ค์–ด์„œ ํ…Œ์ŠคํŠธ (Create)

@Slf4j
@RestController
@RequiredArgsConstructor
public class UserApiController {

    private final UserDocRepository userDocRepository;

    @GetMapping("/api/save")
    public List<UserDoc> save() {
        List<UserDoc> userList = List.of(new UserDoc("kim", 26), new UserDoc("lee", 25));

        return userDocRepository.saveAll(userList);
    }
}

๐Ÿ”Ž ํ…Œ์ŠคํŠธ ๊ฒฐ๊ณผ

MongoRepository ์ธํ„ฐํŽ˜์ด์Šค์˜ ๋ถ€๋ชจ ํด๋ž˜์Šค๋ฅผ ๋ณด๊ณ  ๋ฏธ์†Œ๊ฐ€ ์ง€์–ด์กŒ๋‹ค.
์ด๊ฒŒ ๋ฐ”๋กœ ์ธํ„ฐํŽ˜์ด์Šค ์ถ”์ƒํ™”์˜ ํž˜์ธ ๊ฒƒ์ธ๊ฐ€.. ๋‚˜๋Š” mongoDB๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์ง€๋งŒ mysql์„ ์‚ฌ์šฉํ•  ๋•Œ์™€ ํฌ๊ฒŒ ๋‹ค๋ฅธ๊ฒŒ ์—†๋‹ค.

๋‚ด๊ฐ€ ๊ธฐ์–ตํ•  ๊ฒƒ์€ @Document์™€ Stringํƒ€์ž… id, DB์— document ํด๋ž˜์Šค์˜ ๊ฒฝ๋กœ๊ฐ€ _class๋กœ ์ €์žฅ๋œ๋‹ค๋Š” ๊ฒƒ !

โœ”๏ธ Document์— ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด๋ณด์ž (Read)

UserDocRepository์— ์ •์˜ํ•œ findByName์„ ์‚ฌ์šฉํ•ด์„œ ์ €์žฅ๋œ UserDoc์„ ์ฐพ์•„๋ณธ๋‹ค.

์ฟผ๋ฆฌ์ŠคํŠธ๋ง์œผ๋กœ name๊ฐ’์„ ๋ฐ›์•„ ์กฐํšŒํ•˜๋Š” API๋ฅผ ๊ตฌํ˜„ํ–ˆ๋‹ค.

@GetMapping("/api/read")
public UserDoc read(@RequestParam("name") String name) {
    return userDocRepository.findByName(name).orElseThrow(IllegalArgumentException::new);
}

๐Ÿ”Ž ํ…Œ์ŠคํŠธ ๊ฒฐ๊ณผ

V~ery good


ํ”„๋ ˆ์ž„์›Œํฌ์— ๊ฐ์‚ฌ ๋˜ ๊ฐ์‚ฌ ^^ ๐Ÿ˜

profile
์ข€ ๋” ์ฒœ์ฒœํžˆ ๊นŒ๋จน๊ธฐ ์œ„ํ•ด ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค. ๐Ÿง

0๊ฐœ์˜ ๋Œ“๊ธ€