mongodb快速入门及其springboot实战
mongodb简介
mongodb 是一个基于分布式文件存储的数据库。由 c++ 语言编写。旨在为 web 应用提供可扩展的高性能数据存储解决方案。
mongodb是一个开源、高性能、无模式的文档型数据库,当初的设计就是用于简化开发和方便扩展,是nosql数据库产品中的一种。是最像关系型数据库(mysql)的非关系型数据库。
它支持的数据结构非常松散,是一种类似于json的格式叫bson,所以它既可以存储比较复杂的数据类型,又相当的灵活。
mongodb概念解析
sql术语/概念 | mongodb术语/概念 | 解释/说明 |
---|---|---|
database | database | 数据库 |
table | collection | 数据库表/集合 |
row | document | 数据记录行/文档 |
column | field | 数据字段/域 |
index | index | 索引 |
table joins | 表连接,mongodb不支持 | |
primary key | primary key | 主键,mongodb自动将_id字段设置为主键 |
sql与mongodb数据存储形式对比如下图所示:
mongodb数据类型
数据类型 | 描述 |
---|---|
string | 字符串。存储数据常用的数据类型。在 mongodb 中,utf-8 编码的字符串才是合法的。 |
integer | 整型数值。用于存储数值。根据你所采用的服务器,可分为 32 位或 64 位。 |
boolean | 布尔值。用于存储布尔值(真/假)。 |
double | 双精度浮点值。用于存储浮点值。 |
min/max keys | 将一个值与 bson(二进制的 json)元素的最低值和最高值相对比。 |
array | 用于将数组或列表或多个值存储为一个键。 |
timestamp | 时间戳。记录文档修改或添加的具体时间。 |
object | 用于内嵌文档。 |
null | 用于创建空值。 |
symbol | 符号。该数据类型基本上等同于字符串类型,但不同的是,它一般用于采用特殊符号类型的语言。 |
date | 日期时间。用 unix 时间格式来存储当前日期或时间。你可以指定自己的日期时间:创建 date 对象,传入年月日信息。 |
object id | 对象 id。用于创建文档的 id。 |
binary data | 二进制数据。用于存储二进制数据。 |
code | 代码类型。用于在文档中存储 javascript 代码。 |
regular expression | 正则表达式类型。用于存储正则表达式。 |
mongodb特点
- 高性能:mongodb提供高性能的数据持久性。特别是,对嵌入式数据模型的支持减少了数据库系统上的i/o活动。索引支持更快的查询。
- 高可用性:mongodb的复制工具称为副本集(replica set),它可提供自动故障转移和数据冗余。
- 高扩展性:mongodb提供了水平可扩展性作为其核心功能的一部分。分片将数据分布在一组集群的机器上。(海量数据存储,服务能力水平扩展)
- 丰富的查询支持:mongodb支持丰富的查询语言,支持读和写操作(crud),比如数据聚合、文本搜索和地理空间查询等。
mongodb下载与安装
mongodb下载网址:https://www.mongodb.com/try/download/community
图形化界面mongodb compass下载网址: https://www.mongodb.com/try/download/compass
创建数据目录
mongodb 将数据目录存储在 db 目录下。但是这个数据目录不会主动创建,我们在安装完成后需要创建它。
例如:在d盘创建一个 data 的目录,然后在 data 目录里创建 db 目录。
启动mongodb
在mongodb 目录的 bin 目录中执行 mongod.exe 文件
d:\mongodb\bin>mongod --dpath d:\data\db
mongodb启动成功后,默认端口是27017
compass连接mongodb
连接成功后界面如下:
springboot实战
功能需求
实现文章评论的增删改查,参考示例如图所示:
表结构分析
数据库:articledb
字段名称 | 字段含义 | 字段类型 | 备注 |
---|---|---|---|
_id | id | objectid或string | mongo的主键的字段 |
articleid | 文章id | string | |
content | 评论内容 | string | |
userid | 评论人id | string | |
nickname | 评论人昵称 | string | |
createdatetime | 评论的日期时间 | date | |
likenum | 点赞数 | int32 | |
replynum | 回复数 | int32 | |
state | 状态 | string | 0:不可见;1:可见; |
parentid | 上级id | string | 如果为0表示文章的顶级评论 |
文章微服务模块搭建
搭建项目工程article,项目目录结构如下
引入mongodb依赖
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-data-mongodb</artifactid> </dependency>
创建application.yml
注意,需先在mongondb中创建articledb数据库
spring: data: mongodb: host: 127.0.0.1 database: articledb port: 27017
创建启动类
@springbootapplication public class articleapplication { public static void main(string[] args) { springapplication.run(articleapplication.class, args); } }
启动项目,看能否正常运行。
文章实体类的创建
@data @document(collection = "comment") // 指定为comment集合 @compoundindex(def = "{'userid':1}") // 在userid上建立升序索引 public class comment implements serializable { @id private string id;//主键 //该属性对应mongodb的字段的名字,如果一致,则无需该注解 @field("content") private string content;//评论内容 private date publishtime;//发布日期 //添加了一个单字段的索引 @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; }
文章评论持久层的创建
创建持久层时,需继承mongorepository接口
public interface commentrepository extends mongorepository<comment, string> { }
文章评论service层的创建
@service public class commentservice { @autowired private commentrepository commentrepository; /** * 保存评论 * @param comment */ public void savecomment(comment comment){ commentrepository.save(comment); } /** * 更新评论 * @param comment */ public void updatecomment(comment comment){ commentrepository.save(comment); } /** * 根据id删除评论 * @param id */ public void deletecommentbyid(string id){ commentrepository.deletebyid(id); } /** * 查询所有评论 * @return */ public list<comment> findcommentlist(){ return commentrepository.findall(); } /** * 根据id查询评论 * @param id * @return */ public comment findcommentbyid(string id){ return commentrepository.findbyid(id).get(); } /** * 文章评论点赞,点赞数+1 * @param id */ public void updatecommentlikenum(string id){ query query = new query(criteria.where("_id").is(id)); update update = new update(); update.inc("likenum"); mongotemplate.updatefirst(query, update, comment.class); } }
文章评论微服务测试
@springboottest(classes = articleapplication.class) @runwith(springrunner.class) public class commentservicetest { @autowired private commentservice commentservice; @test public void testfindcomment(){ list<comment> commentlist = commentservice.findcommentlist(); system.out.println(commentlist); } @test public void testfindcommentbyid(){ comment comment = commentservice.findcommentbyid("1"); system.out.println(comment); } @test public void testsavecomment(){ comment comment = new comment(); comment.setarticleid("100002"); comment.setcontent("樊神yyds"); comment.setcreatedatetime(localdatetime.now()); comment.setuserid("1003"); comment.setnickname("随缘夏沫"); comment.setstate("1"); comment.setlikenum(0); comment.setreplynum(0); commentservice.savecomment(comment); } @test public void testfindcommentlistbyparentid(){ page<comment> page = commentservice.findcommentlistbyparentid("1", 1, 2); system.out.println(page.getcontent()); } @test public void testupdatecommentlikenum(){ commentservice.updatecommentlikenum("2"); } }
到此这篇关于mongodb快速入门及其springboot实战的文章就介绍到这了,更多相关mongodb入门内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论