Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

JAVA Developer Training

18. StudentDB 본문

트레이닝

18. StudentDB

Romenest 2021. 9. 1. 17:46
package com.example.model;



import java.util.ArrayList;
import java.util.List;

import com.example.config.Setting;
import com.example.entity.Student;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.Updates;
import com.mongodb.client.result.InsertOneResult;

import org.bson.Document;
import org.bson.conversions.Bson;

public class StudentDB {

    private MongoDatabase db = null;
    private MongoCollection<Document> studentCollection = null;
    private MongoCollection<Document> seqCollection = null;

    private static StudentDB studentDB = new StudentDB();

    private StudentDB() {
        MongoClient client = MongoClients.create(Setting.URL);
        this.db = client.getDatabase(Setting.DBNAME);
        this.studentCollection = db.getCollection(Setting.STUDENT_COLLECTION);
        this.seqCollection = db.getCollection(Setting.SEQ_STUDENT_COLLECTION);
    }

    public static StudentDB getInstance() {
        return studentDB;
    }

    public int insertStudent(Student student) {

        Bson queryBson = Filters.eq("_id","SEQ_STUDENT_NO");
        Bson updateBson = Updates.inc("seq",1);
        
        Document doc = seqCollection.findOneAndUpdate(queryBson, updateBson);

        
        long stdid = doc.getLong("seq");

        

        Document doc1 = new Document();
        doc1.append("_id", stdid);
        doc1.append("name", student.getStdname());
        doc1.append("major", student.getStdmajor());
        doc1.append("stddate", student.getStddate());

        
        InsertOneResult result = studentCollection.insertOne(doc1);
        System.out.println(doc1);
      
        // insertOne crtl+ space 시 반환될 값 보여진다 insertOneResult
        // 따라서 collection.insertOne으로 보고 앞에 작성

        // result는 doc 즉 학생의 학번과 이름, 전공, 등록날짜의 정보를 담고있다

        // result의 insertedId(입력받은)를 문자로 변환한 값이 학생의 학번과 동일한 경우
        if(result.getInsertedId().asInt64().getValue() == stdid){
            return 1;
        }
        
        return 0;
    }

    public List<Student> selectStudent() throws Exception {
		MongoCursor<Document> cursor = this.studentCollection.find().sort(Filters.eq("_id", 1)).cursor();

		List<Student> list = new ArrayList<>();
		while (cursor.hasNext()) {
			Document doc = cursor.next();
			Student student = new Student();
            
			student.setStdid(doc.getLong("_id"));
			student.setStdname(doc.getString("name"));
			student.setStdmajor(doc.getString("major"));
			student.setStddate(doc.getDate("date"));

           
			list.add(student);
		}
		return list;
	}
}