美文网首页
SpringBoot整合MongoDB

SpringBoot整合MongoDB

作者: 攻城老狮 | 来源:发表于2021-01-12 09:28 被阅读0次
  1. 导入pom依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
</dependency>
  1. 编写配置文件,连接mongodb
spring:
  data:
    mongodb:
      uri: mongodb://192.168.44.142:27017/articledb
  1. 编写主启动类
@SpringBootApplication
public class MongoDBApplication {
    public static void main(String[] args) {
        SpringApplication.run(MongoDBApplication.class,args);
    }
}
  1. 编写实体类
/**
 * 文章评论实体类
 */
@AllArgsConstructor
@NoArgsConstructor
@Data
//把一个java类声明为mongodb的文档,可以通过collection参数指定这个类对应的文档。
@Document(collection="comment")//可以省略,如果省略,则默认使用类名小写映射集合
//复合索引
//@CompoundIndex( def = "{'userid': 1, 'nickname': -1}")
public class Comment implements Serializable {
    //主键标识,该属性的值会自动对应mongodb的主键字段"_id",如果该属性名就叫“id”,则该注解可以省略,否则必须写
    @Id
    private String id;//主键
    //该属性对应mongodb的字段的名字,如果一致,则无需该注解
    @Field("content")
    private String content;//吐槽内容
    private Date publishtime;//发布日期
    //添加了一个单字段的索引,索引的添加可以通过Mongo的命令来添加,也可以在Java的实体类中通过注解添加
    //@Indexed
    private String userid;//发布人ID
    private String nickname;//昵称
    private LocalDateTime createdatetime;//评论的日期时间
    private Integer likenum;//点赞数
    private Integer replynum;//回复数
    private String state;//状态
    private String parentid;//上级ID
    private String articleid;
}
  1. 编写操作mongodb的dao接口
//评论的持久层接口
public interface CommentDao extends MongoRepository<Comment,String> {
}
  1. 编写业务层
//评论的业务层
@Service
public class CommentService {

    @Autowired
    private CommentDao commentDao;

    //保存一个评论
    public void saveComment(Comment comment) {
        commentDao.save(comment);
    }

    //更新评论
    public void updateComment(Comment comment) {
        commentDao.save(comment);
    }

    //根据id删除评论
    public void deleteComment(String id) {
        commentDao.deleteById(id);
    }

    //查询所有评论
    public List<Comment> findCommentList() {
        return commentDao.findAll();
    }

    //根据id查询评论
    public Comment findCommentById(String id) {
        return commentDao.findById(id).get();
    }
}
  1. 测试
@SpringBootTest(classes = MongoDBApplication.class)
@RunWith(SpringRunner.class)
public class CommentServiceTest {

    @Autowired
    private CommentService commentService;

    @Test
    public void testSaveComment(){
        Comment comment=new Comment();
        comment.setArticleid("100000");
        comment.setContent("测试添加的数据");
        comment.setCreatedatetime(LocalDateTime.now());
        comment.setUserid("1003");
        comment.setNickname("凯撒大帝");
        comment.setState("1");
        comment.setLikenum(0);
        comment.setReplynum(0);
        commentService.saveComment(comment);
    }
}

注:

  • MongoDB服务所在的服务器关闭防火墙,或者开放对应端口
  • SpringBoot从2.2.X开始不建议在实体类中添加@Indexed的索引注解,推荐在MongoDB中直接添加索引

相关文章

网友评论

      本文标题:SpringBoot整合MongoDB

      本文链接:https://www.haomeiwen.com/subject/cmsjaktx.html