1 前置工作
创建maven工程,并导入依赖的jar包
<!-- 声明此项目是springboot的子项目 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
</parent>
<dependencies>
<!-- 导入web依赖包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 导入lombok工具包 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- Java操作MongoDB -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<!-- SpringBoot test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2 配置文件
application.yml
spring:
data:
mongodb:
host: mongodb服务器的ip地址
database: blog
3 基本操作
3.1 集合/表相关
3.1.1 创建集合
@Autowired
private MongoTemplate mongoTemplate;
/**
* 创建集合
*/
@Test
public void test() {
MongoCollection<Document> mongoCollection = mongoTemplate.createCollection("bl_comment");
}
创建前
创建后
3.1.2 查看集合
/**
* 查看集合
*/
@Test
public void testShowCollection() {
Set<String> collections = mongoTemplate.getCollectionNames();
for (String collection : collections) {
System.out.println(collection);
}
}
3.1.3 修改集合
暂时没看到相应的api
3.1.4 删除集合
/**
* 删除集合
*/
@Test
public void testRemoveCollection() {
mongoTemplate.dropCollection("bl_comment");
}
3.2 文档/表记录相关
前置工作
先创建一个文档对应的实体类
@Data
public class Comment implements Serializable {
package per.blacksnow.pojo;
import java.io.Serializable;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import lombok.Data;
/**
* 评论实体类
* @author XiLongJin
*
*/
@Data
@Document(collection = "bl_comment")
public class Comment implements Serializable {
/**
* 评论id
*/
@Id
private String id;
/**
* 评论内容
*/
private String commentContent;
/**
* 评价人
*/
private Integer commentUser;
/**
* 评论帖子id
*/
private String commentBlog;
/**
* 点赞数
*/
private Integer commentGood;
/**
* 评论时间
*/
private String createdTime;
}
3.2.1 插入文档
/**
* 插入文档
*/
@Test
public void testInsertDoc() {
Comment comment = new Comment();
comment.setCommentContent("我是评论的内容");
comment.setCommentUser(123456);
comment.setCommentBlog("1301794986045005824 ");
comment.setCommentGood(0);
comment.setCreatedTime("2020-09-21 15:30:30");
mongoTemplate.insert(comment);
}
或者通过controller-service-dao的方式
a) controller替换为test类
@Autowired
private CommentService commentService;
/**
* 插入文档
*/
@Test
public void testInsertDoc2() {
Comment comment = new Comment();
comment.setCommentContent("我是评论的内容");
comment.setCommentUser(123456);
comment.setCommentBlog("1301794986045005824 ");
comment.setCommentGood(0);
comment.setCreatedTime("2020-09-21 15:30:30");
commentService.save(comment);
}
b) Service
public interface CommentService {
void save(Comment comment);
}
c) Service具体实现
@Service
public class CommentServiceImpl implements CommentService {
@Autowired
private CommentDao commentDao;
@Override
public void save(Comment comment) {
commentDao.save(comment);
}
}
d) dao/repository
public interface CommentDao extends MongoRepository<Comment, String>{
}
3.2.2 查询文档
查看所有文档
/**
* 查询文档 查看所有文档
*/
@Test
public void testQueryDoc() {
List<Comment> comments = mongoTemplate.findAll(Comment.class);
for (Comment comment : comments) {
System.out.println(comment);
}
}
或者
/**
* 查询文档 查看所有文档
*/
@Test
public void testQueryDoc2() {
List<Comment> comments = commentService.findAll();
for (Comment comment : comments) {
System.out.println(comment);
}
}
public interface CommentService {
List<Comment> findAll();
}
@Service
public class CommentServiceImpl implements CommentService {
@Autowired
private CommentDao commentDao;
@Override
public List<Comment> findAll() {
List<Comment> comments = commentDao.findAll();
return comments;
}
}
public interface CommentDao extends MongoRepository<Comment, String>{
}
根据id查询
@Test
public void testQueryById() {
Comment comment = mongoTemplate.findById("5f6c317cfb15a94e6fd1d833", Comment.class);
System.out.println(comment);
}
查找指定记录
再加两条测试数据
@Test
public void testInsertDoc22() {
Comment comment = new Comment();
comment.setCommentContent("我是评论的内容2");
comment.setCommentUser(222222);
comment.setCommentBlog("1301794986045005825 ");
comment.setCommentGood(1);
comment.setCreatedTime("2020-09-21 15:30:31");
mongoTemplate.insert(comment);
}
@Test
public void testInsertDoc3() {
Comment comment = new Comment();
comment.setCommentContent("我是评论的内容3");
comment.setCommentUser(333333);
comment.setCommentBlog("1301794986045005826 ");
comment.setCommentGood(2);
comment.setCreatedTime("2020-09-21 15:30:32");
mongoTemplate.insert(comment);
}
/**
* 查找指定记录
*/
@Test
public void testQueryDoc4() {
Query query = new Query(Criteria.where("commentGood").is(2));
List<Comment> comments = mongoTemplate.find(query, Comment.class);
for (Comment comment : comments) {
System.out.println(comment);
}
}
and查询
/**
* and查询
*/
@Test
public void testQueryDoc5() {
Query query = new Query(Criteria
.where("commentGood").is(2)
.and("commentUser").is(333333));
List<Comment> comments = mongoTemplate.find(query, Comment.class);
for (Comment comment : comments) {
System.out.println(comment);
}
}
or查询
/**
* or查询
*/
@Test
public void testQueryDoc6() {
Criteria criteria = new Criteria();
Query query = new Query(criteria.orOperator(
Criteria.where("commentGood").is(2),
Criteria.where("commentUser").is(222222)));
List<Comment> comments = mongoTemplate.find(query, Comment.class);
for (Comment comment : comments) {
System.out.println(comment);
}
}
条件查询
/**
* 条件查询
*/
@Test
public void testQueryDoc7() {
Query query = new Query(Criteria.where("commentGood").gt(1));
List<Comment> comments = mongoTemplate.find(query, Comment.class);
for (Comment comment : comments) {
System.out.println(comment);
}
}
模糊查询
/**
* 模糊查询
*/
@Test
public void testQueryDoc8() {
Pattern pattern = Pattern.compile(".*内容3.*");
Query query = new Query(Criteria.where("commentContent").regex(pattern));
List<Comment> comments = mongoTemplate.find(query, Comment.class);
for (Comment comment : comments) {
System.out.println(comment);
}
}
3.2.3 修改文档
/**
* 修改文档
*/
@Test
public void testUpdateDoc() {
Update update = new Update();
update.set("commentGood", 888);
update.set("commentContent", "我是评论的内容");
Query query = new Query(Criteria.where("commentGood").is(2));
UpdateResult updateResult = mongoTemplate.upsert(query, update, Comment.class);
if (updateResult.getModifiedCount() > 0) {
System.out.println("更新成功");
}
}
3.2.4 删除文档
/**
* 删除文档
*/
@Test
public void testDeleteDoc() {
Query query = new Query();
query.addCriteria(Criteria.where("commentUser").is(333333));
DeleteResult deleteResult = mongoTemplate.remove(query,Comment.class);
if (deleteResult.getDeletedCount() > 0) {
System.out.println("删除成功");
}
}