By GokiSoft.com|
15:56 11/08/2023|
Java Advanced
[Share Code] Tạo dự án MongoDB Java - C2209I
Nội dung kiến thức:
- Mongodb shell:
- CSDL
- Tạo
- Drop
- Collections
- Thêm
- Xóa (Drop)
- Thêm document
- Sửa document
- Xóa document
- Query:
==========================================================
Dự án bán hàng:
1) Hệ quản trị CSDL quan hệ (SQL)
- categories
id: tự tăng
name: tên danh mục
- products
id: tự tăng
title
price
thumbnail
content
updated_at
created_at
category_id: foreign key categories (id)
- orders
id: tự tăng
fullname
phone_number
email
address
order_date
total_price
- order_detail
id: tự tăng
product_id
order_id
price
num
====================================
categories:
[
{
id:
name:
},
{
id:
name:
}
]
products:
[
{
id:
category_id: ???
title:
price:
thumbnail:
content:
created_at:
updated_at:
}
]
orders:
[
{
id:
fullname:
phone_number:
email:
address:
order_date:
total_price:
productList: [
{
product_id
title
thumbnail
price
num
}, {
product_id
title
thumbnail
price
num
}
]
}
]
MongoDB Shell:
- https://www.mongodb.com/try/download/shell
- https://www.tutorialspoint.com/mongodb/mongodb_create_database.htm#:~:text=The%20use%20Command,will%20return%20the%20existing%20database.
#Main.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.gokisoft;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import org.bson.conversions.Bson;
/**
* https://mvnrepository.com/artifact/org.mongodb/bson/4.10.2
* https://mvnrepository.com/artifact/org.mongodb/mongo-java-driver/3.12.14
* @author teacher
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// dumpData();
insert();
}
/**
* https://www.mongodb.com/developer/languages/java/java-setup-crud-operations/
*/
static void insert() {
//B1. Ket noi vao CSDL
MongoClient mongoClient = new MongoClient(
new MongoClientURI(
"mongodb://localhost:27017/"
)
);
MongoDatabase database = mongoClient.getDatabase("webbanhang");
//B2. Insert du lieu vao Collection > categories
Document category = new Document();
category.append("name", "Test insert from Java");
database.getCollection("categories").insertOne(category);
}
static void dumpData() {
Bson filter = new Document();
MongoClient mongoClient = new MongoClient(
new MongoClientURI(
"mongodb://localhost:27017/"
)
);
MongoDatabase database = mongoClient.getDatabase("webbanhang");
MongoCollection<Document> collection = database.getCollection("categories");
FindIterable<Document> result = collection.find(filter);
for (Document document : result) {
System.out.println(document.toJson());
}
}
}
#Test.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.gokisoft;
import java.util.List;
import models.Category;
import mongodb.CategoryCRUD;
/**
*
* @author teacher
*/
public class Test {
public static void main(String[] args) {
List<Category> dataList = CategoryCRUD.getList();
for (Category category : dataList) {
System.out.println(category);
}
}
}
#Category.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package models;
import org.bson.Document;
import org.bson.types.ObjectId;
/**
*
* @author teacher
*/
public class Category {
ObjectId _id;
String name;
public Category() {
}
public Category(ObjectId _id, String name) {
this._id = _id;
this.name = name;
}
public ObjectId getId() {
return _id;
}
public void setId(ObjectId _id) {
this._id = _id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void readDocument(Document doc) {
this._id = doc.getObjectId("_id");
this.name = doc.getString("name");
}
@Override
public String toString() {
return "Category{" + "_id=" + _id.toString() + ", name=" + name + '}';
}
}
#BaseCRUD.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package mongodb;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.client.MongoDatabase;
/**
*
* @author teacher
*/
public class BaseCRUD {
static MongoClient mongoClient;
static MongoDatabase database;
static void open() {
//B1. Ket noi vao CSDL
mongoClient = new MongoClient(
new MongoClientURI(
"mongodb://localhost:27017/"
)
);
database = mongoClient.getDatabase("webbanhang");
}
}
#CategoryCRUD.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package mongodb;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import java.util.ArrayList;
import java.util.List;
import models.Category;
import org.bson.Document;
import org.bson.conversions.Bson;
/**
*
* @author teacher
*/
public class CategoryCRUD extends BaseCRUD{
static final String COLLECTION_NAME = "categories";
public static void insert(Category item) {
open();
Document doc = new Document();
doc.append("name", item.getName());
database.getCollection(COLLECTION_NAME).insertOne(doc);
}
public static List<Category> getList() {
List<Category> dataList = new ArrayList<>();
open();
Bson filter = new Document();
MongoCollection<Document> collection = database.getCollection(COLLECTION_NAME);
FindIterable<Document> result = collection.find(filter);
for (Document document : result) {
Category item = new Category();
item.readDocument(document);
dataList.add(item);
}
return dataList;
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)