차근차근
[Firebase Cloud Firestore] 스프링 파이어베이스 데이터 베이스 연동 예제 본문
1. 파이어베이스 데이터 베이스 생성
2. 접속 KEY생성
3.자바 소스 작성
-pom.xml dependencies 추가
<dependency> |
-Key json파일 패키지에 이동
-BtsFireBase.java
package egovframework.com.firebase;
import java.io.File; import java.io.FileInputStream;
import com.google.api.core.ApiFuture; import com.google.auth.oauth2.GoogleCredentials; import com.google.cloud.firestore.DocumentReference; import com.google.cloud.firestore.DocumentSnapshot; import com.google.cloud.firestore.Firestore; import com.google.cloud.firestore.WriteResult; import com.google.firebase.FirebaseApp; import com.google.firebase.FirebaseOptions; import com.google.firebase.cloud.FirestoreClient;
public class BtsFireBase {
public static void main(String[] args) { // TODO Auto-generated method stub initialize();
try { //삽입 //BtsVideoVO btsVideoVO = new BtsVideoVO(); //btsVideoVO.setAge(1); //btsVideoVO.setId("test"); //btsVideoVO.setName("testName"); //btsVideoVO.setTel("010-1234-1234"); //insertMember(btsVideoVO);
//호출 getMemberDetail("test"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }
}
public static void initialize() { try { String path = BtsFireBase.class.getResource("").getPath();
FileInputStream serviceAccount = new FileInputStream(path+"serviceAccountKey.json"); FirebaseOptions options = new FirebaseOptions.Builder() .setCredentials(GoogleCredentials.fromStream(serviceAccount)) .setDatabaseUrl("FireBase Console에 사용자 계정에서 가린 DataBase URL삽입") .build(); FirebaseApp.initializeApp(options); System.out.println("성공");
} catch (Exception e) {
e.printStackTrace();
}
}
public static final String COLLECTION_NAME="member";
public static String insertMember(BtsVideoVO member) throws Exception {
Firestore firestore = FirestoreClient.getFirestore();
ApiFuture<WriteResult> apiFuture = firestore.collection(COLLECTION_NAME).document(member.getId()).set(member);
return apiFuture.get().getUpdateTime().toString();
}
public static void getMemberDetail(String id) throws Exception {
Firestore firestore = FirestoreClient.getFirestore(); DocumentReference documentReference = firestore.collection(COLLECTION_NAME).document(id); ApiFuture<DocumentSnapshot> apiFuture = documentReference.get(); DocumentSnapshot documentSnapshot = apiFuture.get(); //System.out.println(documentSnapshot.toString());
BtsVideoVO member = null;
if(documentSnapshot.exists()) {
member = documentSnapshot.toObject(BtsVideoVO.class); System.out.println(member.toString());
} else {
}
}
} |
-BtsVideoVO.java
package egovframework.com.firebase;
public class BtsVideoVO { String id; String name; int age; String tel;
@Override public String toString() { return "BtsVideoVO [id=" + id + ", name=" + name + ", age=" + age + ", tel=" + tel + "]"; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getTel() { return tel; } public void setTel(String tel) { this.tel = tel; }
} |
4. 테스트 결과
'개발' 카테고리의 다른 글
[YouTube Data API v3] YouTube API 사용해서 검색 데이터 가져오기 (0) | 2020.09.27 |
---|---|
[Firebase Realtime Database] 자바스크립트 파이어베이스 리얼타임데이터베이스 연동 예제 (0) | 2020.09.26 |
egov 프로젝트 세팅 (0) | 2020.09.16 |
glide 4 https Image URL Not Working API 16~19 (0) | 2020.08.30 |
맥 키보드 한/영 단축키 변환 (0) | 2020.08.05 |