[Spring Boot] Mongo DB
1. MongoDB
- JSON 기반의 도큐먼트 데이터베이스
- 스키마가 없음
- Redis와 비슷하게 콘솔에 접근해서 확인 가능
2. 의존성 추가
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
3. MongoDB 설치 및 실행 (도커)
① MongoDB 설치
docker run -p 27017:27017 --name mongo_boot -d mongo
② 실행
docker exec -i -t mongo_boot bash
mongo
4. 스프링 데이터 몽고DB
① collection
: table이름 정도라고 보면 됨
@Document(collection = "accounts")
public class Account {
@Id
private String id;
private String username;
private String email;
// getter, setter 생략
}
② MongoTemplate
@SpringBootApplication
public class SpringBootMongoApplication {
@Autowired
MongoTemplate mongoTemplate;
public static void main(String[] args) {
SpringApplication.run(SpringBootMongoApplication.class, args);
}
@Bean
public ApplicationRunner applicationRunner() {
return args -> {
Account account = new Account();
account.setEmail("aaa@bbb");
account.setUsername("aaa");
mongoTemplate.insert(account);
System.out.println("finished");
};
}
}
- 실행 전
- 실행 후
③ MongoRepository
public interface AccountRepository extends MongoRepository<Account, String> {
}
@SpringBootApplication
public class SpringBootMongoApplication {
@Autowired
AccountRepository accountRepository;
public static void main(String[] args) {
SpringApplication.run(SpringBootMongoApplication.class, args);
}
@Bean
public ApplicationRunner applicationRunner() {
return args -> {
Account account = new Account();
account.setEmail("hhlin@email.com");
account.setUsername("hhlin");
accountRepository.insert(account);
System.out.println("finished");
};
}
}
- 실행 후
④ 내장형 MongoDB (테스트용)
<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo</artifactId>
<scope>test</scope>
</dependency>
⑤ @DataMongoTest
: Mongo Repository 관련 빈들만 등록이 됨
-> AccountRepository 주입받아서 사용 가능
public interface AccountRepository extends MongoRepository<Account, String> {
Optional<Account> findByEmail(String email);
}
@RunWith(SpringRunner.class)
@DataMongoTest
@TestPropertySource(properties = "spring.mongodb.embedded.version=3.5.5")
public class AccountRepositoryTest {
@Autowired
AccountRepository accountRepository;
@Test
public void findByEmail() {
Account account = new Account();
account.setUsername("hyelin");
account.setEmail("hyelin@email.com");
accountRepository.save(account);
Optional<Account> byId = accountRepository.findById(account.getId());
assertThat(byId).isNotEmpty();
Optional<Account> byEmail = accountRepository.findByEmail(account.getEmail());
assertThat(byEmail).isNotEmpty();
assertThat(byEmail.get().getUsername()).isEqualTo("hhlin");
}
}
- 실행 후 : 운영 db는 변화 없음
https://www.inflearn.com/course/%EC%8A%A4%ED%94%84%EB%A7%81%EB%B6%80%ED%8A%B8/dashboard
스프링 부트 개념과 활용 - 인프런 | 강의
스프링 부트의 원리 및 여러 기능을 코딩을 통해 쉽게 이해하고 보다 적극적으로 사용할 수 있는 방법을 학습합니다., - 강의 소개 | 인프런...
www.inflearn.com
MongoDB: The Application Data Platform
Get your ideas to market faster with an application data platform built on the leading modern database. MongoDB makes working with data easy.
www.mongodb.com
https://www.samsungsds.com/kr/insights/1232564_4627.html
NoSQL이란 무엇인가? 대량데이터 동시처리위한 DBMS 종류와 특징
NoSQL이란 무엇인가? 대량데이터 동시처리위한 DBMS 종류와 특징
www.samsungsds.com