*M1 Macํ๊ฒฝ
MongoDB ์ค์น
$ brew tap mongodb/brew
$ brew install mongodb-community
MongoDB ์คํ ๋ฐ ์ ์ง
- ์๋ฒ ์์
$ brew services start mongodb-community
- ์๋ฒ ์ข ๋ฃ
$ brew services stop mongodb-community
_์๋ฒ๋ฅผ ์์ํ๋ฉด MongoDB์ ๊ธฐ๋ณธ ํฌํธ์ธ *_27017** ํฌํธ๋ก ์ด๋ฆฐ๋ค.
๋ค์๊ณผ ๊ฐ์ด ๋จ๋ฉด ์๋ฒ ๊ตฌ๋์ ์ฑ๊ณตํ๊ฒ ์ด๋ค.
MongoDB ํฐ๋ฏธ๋ ํ์ฉํ๊ธฐ
$ mongo
mongo์๋ฒ๋ฅผ ๊ตฌ๋ํ๊ณ ,
ํฐ๋ฏธ๋์์ mongo๋ฅผ ์
๋ ฅํ๋ฉด ํฐ๋ฏธ๋์์ ์๋ฒ์ ์ ์ํ ์ ์๋ค.
*zsh: command not found: mongo์ ๊ฐ์ ์ค๋ฅ๊ฐ ๋ฐ์ํด์,
$ brew install mongodb-community-shell
community shell์ ์ค์นํด์ฃผ์๋ค.
- ๊ธฐ๋ณธ ๋ช ๋ น์ด
//DB์ฌ์ฉ use ๋ฐ์ดํฐ๋ฒ ์ด์ค //๋ฐ์ดํฐ๋ฒ ์ด์ค ๋ณด๊ธฐ show dbs; //์ปฌ๋ ์ ์์ฑ db.createCollection('์ปฌ๋ ์ ๋ช '); //์ปฌ๋ ์ ๋ณด๊ธฐ show collections; //์ปฌ๋ ์ ์ญ์ db.์ปฌ๋ ์ ๋ช .drop(); //๋ฐ์ดํฐ๋ฒ ์ด์ค ์ญ์ db.dropDatabase();
admin๊ณ์ ๋ ๋ง๋ค์ด ์ฃผ์๋ค.
compass - MongoDB GUI ๋ค์ด๋ก๋
https://www.mongodb.com/try/download/compass
Spring์ ์ ์ฉ์ํค๊ธฐ
- build.gradle ์์ฑ
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
์์ ๊ฐ์ด dependency๋ฅผ ์ถ๊ฐํด์ค๋ค.
2. application.yml ์ค์
spring:
data:
mongodb:
host: localhost
port: 27017
authentication-database: admin
database: my-database
username: DDonghyeo
password: 1234
- Model ์์ฑ
@Getter
@Setter
@Document(collection = "user")
public class User{
private Long id;
private String name;
private String email;
}
4. Repository ์์ฑ
public interface UserRepository extends MongoRepository<User, String> {
public String findByName(String name);
}
userRepository ์ถ์ ๋ฉ์๋๋ฅผ ์์ฑํ๋ค.
Spring์์ Data Repository์ ํํด Naming Convention์ด ๋ง๋ ๋ฉ์๋์ ๋ํด
๋์ ์ผ๋ก data access์ ๋ํ implementation์ ์ ๊ณตํ๋ค.
๋ฐ๋ผ์ ์ค์ ๋ก findByName์ ๊ตฌํํ์ง ์์์ง๋ง, ์กฐํ๊ฐ ์ ๋์ํ๋ค.
- Service ์์ฑ
๊ฐ๋จํ๊ฒ ์ด๋ฆ์ผ๋ก ์ ์ ์ฐพ๊ธฐ(findUserByName), ์ ์ ๋ฑ๋กํ๊ธฐ(addUser) ๋ฉ์๋๋ฅผ ๋ง๋ค์๋ค.
@Slf4j
@Component
public class UserSerivce {
@Autowired UserRepository userRepository;
public String findUserByName(String name){
ObjectMapper objectMapper = new ObjectMapper();
try {
if (userRepository.findByName(name) == null){
log.info("can't find user : {} ", name);
return String.format("can't find user : %s", name);
} else {
return userRepository.findByName(name);
}
} catch (JsonProcessingException e)
{ e.printStackTrace(); return "ERROR";
}
}
public void addUser(User user){
if(userRepository.findById(user.getId()) != null){
log.info("user already exists.");
} else {
userRepository.save(user);
log.info("user added completely");
}
}
}
- Controller ์์ฑ
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired UserSerivce userSerivce;
@GetMapping("/find")
public String findUser(@RequestParam String name){
return userSerivce.findUserByName(name);
}
@PostMapping("/add")
public void addUser(@RequestBody User user){
userSerivce.addUser(user);
}
}
/find์์๋ name์ผ๋ก ์ฌ์ฉ์๋ฅผ ์ฐพ๊ณ , /add์์๋ user๋ฅผ ๋ฑ๋กํ ์ ์๋ค.
์คํ ํ ์คํธ
- ์ ์ ๋ฑ๋ก
๋จผ์ ์๋ฒ๋ฅผ ๋๋ฆฌ๊ณ , postman์ผ๋ก ํ ์คํธ ํด๋ดค๋ค.
Body์ User๋ฅผ ๋ด์์ Postํ ๊ฒฐ๊ณผ
์ค์ ํด๋๋ ๋ก๊ทธ๊ฐ ์ ๋ด๊ณ , compass์์๋ ์ ํ์ธ๋๋ค.
*์ค๋ณต ์ ์ ๋ฑ๋ก ์
- ์ ์ ์ฐพ๊ธฐ
๋ชจ๋ ์คํ์ด ์ ๋๋ ๊ฒ์ ๋ณผ ์ ์๋ค.
์ด๋ก์จ ์คํ๋ง ํ๋ก์ ํธ์ NoSQL์ ์ ์ฉ์์ผ์ ์จ ๋ดค๋๋ฐ,
NoSQL์ด ํจ์ฌ ํธํ๊ณ ๋ณด๊ธฐ๋ ์ข์ ๊ฒ ๊ฐ๊ธฐ๋ ํ๋ค !!
'BackEnd > Spring Boot' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Spring] ๋ถ์ฐ ์์คํ ์์ ํจ๊ณผ์ ์ธ ๋ฐ์ดํฐ ์ ๋ฌ ๋ฐฉ๋ฒ (1) - RDB (1) | 2023.11.04 |
---|---|
[Spring] Docker์์ Static File ์ฒ๋ฆฌํ๊ธฐ (0) | 2023.09.02 |
Spring ํ๊ฒฝ์์ Redis ์ฌ์ฉํ๊ธฐ (0) | 2023.09.01 |
[Spring] ์ ํ ๋ก๊ทธ์ธ ๊ตฌํํ๊ธฐ (0) | 2023.09.01 |
[Java] Jwt๋ฅผ ์ด์ฉํ์ฌ Api์๋ฒ ๊ฐ์ธ์ ๋ณด ์ํธํํ๊ธฐ (0) | 2023.09.01 |