반응형
저번 글에서는 firebase와 spring의 연결 방법과 fibasestore의 CURD중 C(create)를 하는 방법을 작성했습니다
이번 글에서는 U(update),R(read),D(delete) 나머지를 다 사용하는 방법을 알려드리겠습니다
설정방법과 Create 방법만 필요하시거나 모르신다면 하단 링크를 참조하세요!
https://chicken-coach.tistory.com/11
spring boot와 firebase 연결 방법 (1)
firebase 사용이유 어떤 아이디어가 있을 때 빠르게 프로토타입을 만들고 싶을 때 이용한다면 realtime database를 이용 할 수 있고 구글로 로그인하기 페이스북으로 로그인하기 휴대폰인증 알림 보내
chicken-coach.tistory.com
Read. 정보 조회하기
-service
public Phone getPhoneInfo(String phoneNm) throws ExecutionException, InterruptedException {
Firestore dbFireStore = FirestoreClient.getFirestore();
DocumentReference documentReference =
dbFireStore.collection(COL_NAME).document(phoneNm);
ApiFuture<DocumentSnapshot> future = documentReference.get();
DocumentSnapshot document = future.get();
Phone phone = null;
if (!document.exists()) {
return null;
}
phone = document.toObject(Phone.class);
return phone;
}
-test
@Test
public void getPhoneInfo() throws ExecutionException, InterruptedException {
Phone result = phoneService.getPhoneInfo("123123");
System.out.println(result.getPhoneName());
System.out.println(result.getCompany());
System.out.println(result.getTelecom());
}
//결과
123123
samsung
sk
이렇게 정보를 조회하는 것도 구현을 했습니다!
Update 정보 수정하기
-service
public String updatePhoneInfo(Phone phone) throws ExecutionException, InterruptedException {
Firestore dbFireStore = FirestoreClient.getFirestore();
ApiFuture<WriteResult> collectionsApiFuture =
dbFireStore.collection(COL_NAME).document(phone.getPhoneName()).set(phone);
return collectionsApiFuture.get().getUpdateTime().toString();
}
-test
@Test
public void updatePhoneInfo() throws ExecutionException, InterruptedException {
Phone phone = new Phone("123123","LG","kt");
String result = phoneService.updatePhoneInfo(phone);
System.out.println(result);
}
// 결과: 2022-05-07T07:28:38.273804000Z
정보가 잘 변경되었습니다
Delete 정보 제거
-service
public String deletePhoneInfo(String phoneName){
Firestore dbFireStore = FirestoreClient.getFirestore();
ApiFuture<WriteResult> writeResult =
dbFireStore.collection(COL_NAME).document(phoneName).delete();
return "삭제 완료";
}
-test
@Test
public void deletePhoneInfo(){
String result = phoneService.deletePhoneInfo("123123");
System.out.println(result);
}
이렇게 CRUD를 모두 사용해봤습니다!
처음보는 메소드들과 클래스들이 많이 있어서 어렵다고 생각하실수 있지만 일일히 설명하기엔 너무 길어질거 같아 하단에 링크를 남겨 놓겠습니다 설명이 아주 잘 되어 있습니다 부족한 부분이 있다면 댓글 주세요!
CollectionReference | Firebase
firebase.google.com
반응형
'spring(스프링)' 카테고리의 다른 글
spring scheduler 사용하기 (0) | 2022.05.26 |
---|---|
spring boot + FCM push하는 방법 (0) | 2022.05.11 |
spring boot와 firebase 연결 방법 (1) (1) | 2022.05.06 |
Querydsl 사용하기 (spring boot,maven) (2) | 2022.05.04 |
네이버 영화 검색 오픈 api 사용 방법(with spring) (0) | 2022.05.02 |