1、搭建测试环境 步骤一:创建 Maven 项目 父项目的pom文件 4.0.0 com.tqylxuecheng xc_parent pom 1.0-SNAPSHOT
父项目的pom文件
4.0.0
com.tqylxuecheng
xc_parent
pom
1.0-SNAPSHOT
xc_test_parent
org.springframework.boot
spring-boot-starter-parent
2.1.4.RELEASE
UTF-8
1.8
Greenwich.RELEASE
1.3.2
2.0.3
1.2.3
1.1.9
5.1.32
1.16.20
2.6
1.3.2
1.3.3
1.10
3.6
3.9.1
8.18.0
2.7.0
1.2.9
1.27.0.0
24.0-jre
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud-release.version}
pom
import
org.mybatis.spring.boot
mybatis-spring-boot-starter
${mybatis.starter.version}
tk.mybatis
mapper-spring-boot-starter
${mapper.starter.version}
com.GitHub.pagehelper
pagehelper-spring-boot-starter
${pageHelper.starter.version}
com.alibaba
druid-spring-boot-starter
${druid.starter.version}
Mysql
mysql-connector-java
${mysql-connector-java.version}
com.squareup.okHttp3
okhttp
${okhttp.version}
com.netflix.feign
feign-okhttp
${feign-okhttp.version}
commons-io
commons-io
${commons-io.version}
org.apache.commons
commons-io
${org.apache.commons.io.version}
commons-fileupload
commons-fileupload
${commons-fileupload.version}
commons-codec
commons-codec
${commons-codec.version}
org.apache.commons
commons-lang3
${commons-lang3.version}
io.springfox
springfox-swagger2
${springfox-swagger.version}
io.springfox
springfox-swagger-ui
${springfox-swagger.version}
net.oschina.zcx7878
fastdfs-client-java
${fastdfs-client-java.version}
com.Google.guava
guava
${guava.version}
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false
步骤三:子模块测试
test_mongo 的 pom文件添加依赖
org.springframework.boot
spring-boot-starter-test
org.mongoDB
mongo-java-driver
3.4.3
方式1:连接本地数据库
@Test
public void testConnection() {
//获得本地连接
MongoClient mongoClient = new MongoClient("localhost", 27017);
System.out.println(mongoClient);
}
方式2:采用连接字符串
@Test
public void testConnection2() {
//获得连接
//拼凑连接字符串
MongoClientURI connectionString = new MongoClientURI("mongodb://root:1234@localhost:27017");
//获得连接
MongoClient mongoClient = new MongoClient(connectionString);
System.out.println(mongoClient);
}
@Test
public void testFindOne(){
//查询一个
//1 获得连接
MongoClientURI mongoClientURI = new MongoClientURI("mongodb://root:1234@localhost:27017");
MongoClient mongoClient = new MongoClient(mongoClientURI);
// 2 获得数据库
MongoDatabase database = mongoClient.getDatabase("demo");
//3 获得集合
MongoCollection studentCollection = database.getCollection("student");
//4 查询操作
Document document = studentCollection.find().first();
//5 将文档转换JSON,并输出
System.out.println(document.tojson());
}
@Test
public void testCreateColl(){
//1 获得连接
MongoClientURI mongoClientURI = new MongoClientURI("mongodb://root:1234@localhost:27017");
MongoClient mongoClient = new MongoClient(mongoClientURI);
//2 获得数据库
MongoDatabase database = mongoClient.getDatabase("demo");
//3 创建集合
database.createCollection("teacher");
}
@Test
public void testInsertDocument(){
//1 获得连接
MongoClientURI mongoClientURI = new MongoClientURI("mongodb://root:1234@localhost:27017");
MongoClient mongoClient = new MongoClient( mongoClientURI );
//2 获得数据库
MongoDatabase database = mongoClient.getDatabase("demo");
//3 获得集合
MongoCollection collection = database.getCollection("teacher");
//4 准备文档
Document document = new Document();
document.append("username","jack");
document.append("passWord","1234");
document.append("age", 18 );
//5 录入文档
collection.insertOne(document);
}
@Test
public void testInsertManyDocument(){
//批量插入
//1 获得连接
MongoClientURI mongoClientURI = new MongoClientURI("mongodb://root:1234@localhost:27017");
MongoClient mongoClient = new MongoClient(mongoClientURI);
//2 获得数据库
MongoDatabase database = mongoClient.getDatabase("demo");
//3 获得集合
MongoCollection collection = database.getCollection("teacher");
//4 准备一组数据
Document doc = new Document();
doc.append("username","rose");
doc.append("password","1234");
doc.append("age", 21 );
Document doc2 = new Document();
doc2.append("username","tom");
doc2.append("password","666");
doc2.append("age", 25 );
List list = new ArrayList<>();
list.add( doc );
list.add( doc2 );
//5 批量插入
collection.insertMany( list );
}
@Test
public void testFindAll(){
//1 获得连接
MongoClientURI mongoClientURI = new MongoClientURI("mongodb://root:1234@localhost:27017");
MongoClient mongoClient = new MongoClient(mongoClientURI);
//2 获得数据库
MongoDatabase database = mongoClient.getDatabase("demo");
//3 获得集合
MongoCollection collection = database.getCollection("teacher");
//4 查询所有
FindIterable findIterable = collection.find();
//5 处理数据(遍历迭代器)
MongoCursor it = findIterable.iterator();
while( it.hasNext() ){
Document document = it.next();
String username = document.get("username", String.class);
String password = document.get("password", String.class);
Integer age = document.get("age", Integer.class);
System.out.println(username + "_" + password + "_" + age);
}
@Test
public void testUpdate() {
// 采用连接字符串
MongoClientURI connectionString = new MongoClientURI("mongodb://root:1234@localhost:27017");
MongoClient mongoClient = new MongoClient(connectionString);
// 连接数据库
MongoDatabase database = mongoClient.getDatabase("demo");
// 获得集合
MongoCollection collection = database.getCollection("teacher");
// 更新
collection.updateOne(Filters.eq("age",20), new Document("$set", new Document("name","YY老师")));
}
@Test
public void testDelete() {
// 采用连接字符串
MongoClientURI connectionString = new MongoClientURI("mongodb://root:1234@localhost:27017");
MongoClient mongoClient = new MongoClient(connectionString);
// 连接数据库
MongoDatabase database = mongoClient.getDatabase("demo");
// 获得集合
MongoCollection collection = database.getCollection("teacher");
// 删除
collection.deleteOne(Filters.eq("age",20));
}
--结束END--
本文标题: MongoDB学习(三) --- MongoDB Java入门
本文链接: https://lsjlt.com/news/6680.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-04-01
2024-04-03
2024-04-03
2024-01-21
2024-01-21
2024-01-21
2024-01-21
2023-12-23
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0