这里使用的不是前端页面,而是控制台来完成的。
具体的需求如下所示:




- 通过id删除


程序将划分层次
djh.it.sh.domain javaBean实体类
djh.it.sh.utils 工具类
djh.it.sh.web web层,使用main方法代替。
djh.it.sh.service service层(业务层)
djh.it.sh.dao dao层
创建数据库并插入数据:
# 显示所有数据库,并使用 mymongodb 数据库。 > show dbs local 0.03125GB mymongodb 0.0625GB > use mymongodb switched to db mymongodb # 插入数据 var productArr = [{ pid:1, pname:"lenovo", price:5000 }, { pid:2, pname:"Haier", price:3000 }, { pid:3, pname:"Thor", price:5000 }, { pid:4, pname:"Nike", price:800 }, { pid:5, pname:"Dior", price:2000 }, { pid:6, pname:"HERMES", price:2400 }, { pid:7, pname:"MK", price:4000 }, { pid:8, pname:"CHANEL", price:800 }, { pid:9, pname:"BMW", price:20000 }] for(var i = 0;i db.products.insert(productArr[i]) } # 查询数据 > db.products.find() { "_id" : ObjectId("66b1897c57b300f386ae6a43"), "pid" : 1, "pname" : "lenovo", "price" : 5000 } { "_id" : ObjectId("66b1897c57b300f386ae6a44"), "pid" : 2, "pname" : "Haier", "price" : 3000 } { "_id" : ObjectId("66b1897c57b300f386ae6a45"), "pid" : 3, "pname" : "Thor", "price" : 5000 } { "_id" : ObjectId("66b1897c57b300f386ae6a46"), "pid" : 4, "pname" : "Nike", "price" : 800 } { "_id" : ObjectId("66b1897c57b300f386ae6a47"), "pid" : 5, "pname" : "Dior", "price" : 2000 } { "_id" : ObjectId("66b1897c57b300f386ae6a48"), "pid" : 6, "pname" : "HERMES", "price" : 2400 } { "_id" : ObjectId("66b1897c57b300f386ae6a49"), "pid" : 7, "pname" : "MK", "price" : 4000 } { "_id" : ObjectId("66b1897c57b300f386ae6a4a"), "pid" : 8, "pname" : "CHANEL", "price" : 800 } { "_id" : ObjectId("66b1897c57b300f386ae6a4b"), "pid" : 9, "pname" : "BMW", "price" : 20000 } > 插入后的数据如下所示:

--> idea --> File --> New --> Project --> Maven Project SDK: ( 1.8(java version "1.8.0_131" ) --> Next --> Groupld : ( djh.it ) Artifactld : ( productManager ) Version : 1.0-SNAPSHOT --> Name: ( productManager ) Location: ( ...\productManager\ ) --> Finish 4.0.0 org.example productManager 1.0-SNAPSHOT org.springframework.boot spring-boot-starter-parent 2.1.6.RELEASE org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-starter-data-mongodb /** * productManager\src\main\java\djh\it\sh\utils\MongoDBUtils.java * * 2024-8-6 创建工具类 MongoDBUtils.java */ package djh.it.sh.utils; import com.mongodb.DB; import com.mongodb.Mongo; public class MongoDBUtils { // 1.使用connection用来保存Mongo的数据库连接对象 static Mongo connection = null; // 2.使用db接收具体的数据库连接 static DB db = null; public static DB getDB(String dbName) throws Exception { //创建一个Mongo的数据库连接对象 connection = new Mongo("127.0.0.1:27017"); //通过获取数据库的连接对象connection根据传递的数据库名dbName来连接具体的数据库 db = connection.getDB(dbName); //将具体的数据库连接返回给调用者 return db; } } /** * D:\java_test\Intesij_idea\productManager\src\main\java\djh\it\sh\domain\Product.java * * 2024-8-6 创建 Product.java 实体类。 */ package djh.it.sh.domain; import org.bson.types.ObjectId; public class Product { private ObjectId obj_id; private int pid;//商品id private String pname;//商品名字 private int price;//商品价格 public Product() { super(); // TODO Auto-generated constructor stub } public Product(ObjectId obj_id, int pid, String pname, int price) { super(); this.obj_id = obj_id; this.pid = pid; this.pname = pname; this.price = price; } public ObjectId getObj_id() { return obj_id; } public void setObj_id(ObjectId obj_id) { this.obj_id = obj_id; } public int getPid() { return pid; } public void setPid(int pid) { this.pid = pid; } public String getPname() { return pname; } public void setPname(String pname) { this.pname = pname; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } @Override public String toString() { return "Product [obj_id=" + obj_id + ", pid=" + pid + ", pname=" + pname + ", price=" + price + "]"; } } # productManager\src\main\resources\application.yml spring: # 数据源配置 data: mongodb: # 主机地址 host: 127.0.0.1 # 数据库 database: mymongodb # 默认端口27017 port: 27017 # 也可以使用uri连接 # uri mongodb //172.18.30.110:27017/articledb /** * productManager\src\main\java\djh\it\sh\ProductApplication.java * * 2024-8-6 创建启动类 ProductApplication.java */ package djh.it.sh; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class ProductApplication { public static void main(String[] args) { SpringApplication.run(ProductApplication.class, args); } } 上一节关联链接请点击
# 基于MongoDB实现商品管理系统(1)