欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 美景 > # 基于MongoDB实现商品管理系统(2)

# 基于MongoDB实现商品管理系统(2)

2024/10/24 3:27:04 来源:https://blog.csdn.net/qfyh_djh/article/details/140960935  浏览:    关键词:# 基于MongoDB实现商品管理系统(2)

基于MongoDB实现商品管理系统(2)

基于 mongodb 实现商品管理系统之准备工作

1、案例需求

这里使用的不是前端页面,而是控制台来完成的。

具体的需求如下所示:

  • 运行

  2-1.png(img/51.jpg)

  • 查询所有

  2-2.png(img/52.jpg)

  • 通过id查询详情

  2-3.png (img/53.jpg)

  • 添加

  2-4.png(img/54.jpg)

- 通过id删除

  2-5.png(img/56.jpg)

  2-6.png (img/55.jpg)

2、案例分析

  • 程序将划分层次

    ​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层

3、准备工作:向 mongodb 数据库中插入数据。

创建数据库并插入数据:


# 显示所有数据库,并使用 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<productArr.length;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 }
>

4、使用图形界面工具 Compass 连接 mongodb 数据库,并查看插入的数据。

插入后的数据如下所示:

   2-8.png (img/58.jpg)

5、打开 idea,创建 productManager 的 maven 工程。

--> 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	

6、在工程 productManager (模块)中的 pom.xml 中导入依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>productManager</artifactId><version>1.0-SNAPSHOT</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.6.RELEASE</version><relativePath></relativePath></parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-mongodb</artifactId></dependency></dependencies></project>
<!-- productManager\pom.xml -->

7、在工程 productManager (模块)中,创建工具类 MongoDBUtils.java

/***  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;}
} 

8、在工程 productManager (模块)中,创建 Product.java 实体类。

/***  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;//商品idprivate 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;}@Overridepublic String toString() {return "Product [obj_id=" + obj_id + ", pid=" + pid + ", pname=" + pname + ", price=" + price + "]";}
}

9、在工程 productManager (模块)中,创建 配置文件 application.yml。

#  productManager\src\main\resources\application.ymlspring:# 数据源配置data:mongodb:# 主机地址host: 127.0.0.1# 数据库database: mymongodb# 默认端口27017port: 27017# 也可以使用uri连接# uri mongodb //172.18.30.110:27017/articledb

10、在工程 productManager (模块)中,创建启动类 ProductApplication.java。

/***   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)

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com