Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
小雨青年
Java Demo
提交
1d1d89c6
J
Java Demo
项目概览
小雨青年
/
Java Demo
通知
53
Star
2
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
J
Java Demo
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
1d1d89c6
编写于
2月 15, 2022
作者:
小雨青年
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
MongoDB 增删改查
上级
739a460d
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
106 addition
and
1 deletion
+106
-1
SpringBootWithMongoDB/src/test/java/com/example/springbootwithmongodb/entry/CatTest.java
...java/com/example/springbootwithmongodb/entry/CatTest.java
+106
-1
未找到文件。
SpringBootWithMongoDB/src/test/java/com/example/springbootwithmongodb/entry/CatTest.java
浏览文件 @
1d1d89c6
package
com.example.springbootwithmongodb.entry
;
import
com.mongodb.client.result.DeleteResult
;
import
com.mongodb.client.result.UpdateResult
;
import
org.junit.jupiter.api.Test
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.data.mongodb.core.MongoTemplate
;
import
org.springframework.data.mongodb.core.query.Criteria
;
import
org.springframework.data.mongodb.core.query.Query
;
import
org.springframework.data.mongodb.core.query.Update
;
import
java.util.List
;
@SpringBootTest
public
class
CatTest
{
...
...
@@ -12,12 +19,110 @@ public class CatTest {
@Autowired
private
MongoTemplate
mongoTemplate
;
private
final
Class
<
Cat
>
entityClass
=
Cat
.
class
;
@Test
public
void
createTest
(){
public
void
createTest
()
{
Cat
cat
=
new
Cat
();
cat
.
setName
(
"糖果"
);
cat
.
setAge
(
3
);
cat
.
setLikeCount
(
100
);
mongoTemplate
.
insert
(
cat
);
}
@Test
public
void
deleteTest
()
throws
NoSuchFieldException
{
String
id
=
"620a27c3db5ca321f4cc5754"
;
Criteria
criteria
=
(
new
Criteria
(
entityClass
.
getDeclaredField
(
"id"
).
getName
())).
is
(
id
);
//根据条件删除
Cat
cat
=
mongoTemplate
.
findAndRemove
(
new
Query
(
criteria
),
entityClass
);
System
.
out
.
println
(
cat
);
//直接删除
Cat
cat2
=
mongoTemplate
.
findById
(
"620a27c3db5ca321f4cc5754"
,
entityClass
);
assert
cat2
!=
null
;
DeleteResult
result
=
mongoTemplate
.
remove
(
cat2
);
System
.
out
.
println
(
result
);
//findAllAndRemove 和 findAndRemove 的区别在于,前者删除全部匹配的数据,后者只删除一个
}
@Test
public
void
findTest
()
throws
NoSuchFieldException
{
//查询全部
List
<
Cat
>
cats
=
mongoTemplate
.
findAll
(
entityClass
);
System
.
out
.
println
(
cats
);
//根据条件查询
Criteria
criteria
=
(
new
Criteria
(
entityClass
.
getDeclaredField
(
"likeCount"
).
getName
())).
is
(
cats
.
get
(
0
).
getLikeCount
());
List
<
Cat
>
cats2
=
mongoTemplate
.
find
(
new
Query
(
criteria
),
entityClass
);
System
.
out
.
println
(
cats2
);
//根据ID查询
Cat
cat
=
mongoTemplate
.
findById
(
cats
.
get
(
1
).
getId
(),
entityClass
);
System
.
out
.
println
(
cat
);
//查询单条记录
Criteria
criteria2
=
(
new
Criteria
(
entityClass
.
getDeclaredField
(
"name"
).
getName
())).
is
(
cats
.
get
(
0
).
getName
());
Cat
cat2
=
mongoTemplate
.
findOne
(
new
Query
(
criteria2
),
entityClass
);
System
.
out
.
println
(
cat2
);
}
@Test
public
void
updateTest
()
throws
NoSuchFieldException
{
String
id
=
"620b1b46d6e42202392f5653"
;
Criteria
criteria
=
(
new
Criteria
(
entityClass
.
getDeclaredField
(
"id"
).
getName
())).
is
(
id
);
Update
update
=
new
Update
();
update
.
set
(
entityClass
.
getDeclaredField
(
"name"
).
getName
(),
"炸酱233"
);
update
.
inc
(
"likeCount"
);
//单条更新
UpdateResult
updateResult
=
mongoTemplate
.
updateFirst
(
new
Query
(
criteria
),
update
,
entityClass
);
System
.
out
.
println
(
updateResult
);
Criteria
criteria2
=
(
new
Criteria
(
entityClass
.
getDeclaredField
(
"age"
).
getName
())).
is
(
3
);
Update
update2
=
new
Update
();
update2
.
set
(
entityClass
.
getDeclaredField
(
"name"
).
getName
(),
"炸酱"
);
update2
.
inc
(
"age"
);
//批量更新
UpdateResult
updateResult2
=
mongoTemplate
.
updateMulti
(
new
Query
(
criteria2
),
update2
,
entityClass
);
System
.
out
.
println
(
updateResult2
);
}
@Test
public
void
countTest
()
throws
NoSuchFieldException
{
Criteria
criteria
=
(
new
Criteria
(
entityClass
.
getDeclaredField
(
"age"
).
getName
())).
gte
(
3
);
//计数
long
count
=
mongoTemplate
.
count
(
new
Query
(
criteria
),
entityClass
);
System
.
out
.
println
(
count
);
}
@Test
public
void
otherTest
(){
//获取集合名称
String
collectionName
=
mongoTemplate
.
getCollectionName
(
entityClass
);
System
.
out
.
println
(
collectionName
);
//判断集合是不是存在 false
boolean
check
=
mongoTemplate
.
collectionExists
(
"dog"
);
System
.
out
.
println
(
check
);
//判断集合是不是存在 true
boolean
checkCat
=
mongoTemplate
.
collectionExists
(
"cat"
);
System
.
out
.
println
(
checkCat
);
//创建集合
mongoTemplate
.
createCollection
(
"bug2"
);
//删除集合
mongoTemplate
.
dropCollection
(
"bug2"
);
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录