1、搭建测试环境
- 步骤一:创建 maven 项目
-
父项目的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
2、基本操作
- 创建测试类 Test01
2.1、获得连接
-
方式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); }
2.2、查询第一个
@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());
}
2.3、创建集合
@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");
}
2.4、插入一个文档
@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);
}
2.5、批量插入文档
@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 );
}
2.6、查询所有
@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);
}
2.7、更新文档
@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老师")));
}
2.8、删除文档
@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));
}