珍珠设计制作培训,如何做网站导航栏的seo优化,wordpress文章头图,乐清装修网站哪个好MongoDB快速入门及其SpringBoot实战
MongoDB简介
MongoDB 是一个基于分布式文件存储的数据库。由 C 语言编写。旨在为 WEB 应用提供可扩展的高性能数据存储解决方案。
MongoDB是一个开源、高性能、无模式的文档型数据库#xff0c;当初的设计就是用于简化开发和方便扩展 语言编写。旨在为 WEB 应用提供可扩展的高性能数据存储解决方案。
MongoDB是一个开源、高性能、无模式的文档型数据库当初的设计就是用于简化开发和方便扩展是NoSQL数据库产品中的一种。是最像关系型数据库MySQL的非关系型数据库。
它支持的数据结构非常松散是一种类似于JSON的格式叫BSON所以它既可以存储比较复杂的数据类型又相当的灵活。
MongoDB概念解析
SQL术语/概念MongoDB术语/概念解释/说明databasedatabase数据库tablecollection数据库表/集合rowdocument数据记录行/文档columnfield数据字段/域indexindex索引table joins表连接,MongoDB不支持primary keyprimary 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\binmongod --dpath d:\data\db MongoDB启动成功后默认端口是27017
Compass连接MongoDB
连接成功后界面如下 SpringBoot实战
功能需求 实现文章评论的增删改查参考示例如图所示
表结构分析 数据库articledb
字段名称字段含义字段类型备注_idIDObjectId或StringMongo的主键的字段articleid文章IDStringcontent评论内容Stringuserid评论人IDStringnickname评论人昵称Stringcreatedatetime评论的日期时间Datelikenum点赞数Int32replynum回复数Int32state状态String0不可见1可见parentid上级IDString如果为0表示文章的顶级评论
文章微服务模块搭建 搭建项目工程article项目目录结构如下
引入MongoDB依赖
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-mongodb/artifactId
/dependency创建application.yml 注意需先在MongonDB中创建articledb数据库
spring:data:mongodb:host: 127.0.0.1database: articledbport: 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 {Idprivate String id;//主键//该属性对应mongodb的字段的名字如果一致则无需该注解Field(content)private String content;//评论内容private Date publishtime;//发布日期//添加了一个单字段的索引Indexedprivate String userid;//发布人IDprivate String nickname;//昵称private LocalDateTime createdatetime;//评论的日期时间private Integer likenum;//点赞数private Integer replynum;//回复数private String state;//状态private String parentid;//上级IDprivate String articleid;
}文章评论持久层的创建 创建持久层时需继承MongoRepository接口
public interface CommentRepository extends MongoRepositoryComment, String {
}文章评论service层的创建
Service
public class CommentService {Autowiredprivate 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 ListComment 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 {Autowiredprivate CommentService commentService;Testpublic void testFindComment(){ListComment commentList commentService.findCommentList();System.out.println(commentList);}Testpublic void testFindCommentById(){Comment comment commentService.findCommentById(1);System.out.println(comment);}Testpublic 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);}Testpublic void testFindCommentListByParentid(){PageComment page commentService.findCommentListByParentid(1, 1, 2);System.out.println(page.getContent());}Testpublic void testUpdateCommentLikenum(){commentService.updateCommentLikenum(2);}
}
文章转载自: http://www.morning.xsymm.cn.gov.cn.xsymm.cn http://www.morning.nmbbt.cn.gov.cn.nmbbt.cn http://www.morning.guangda11.cn.gov.cn.guangda11.cn http://www.morning.fswml.cn.gov.cn.fswml.cn http://www.morning.kxryg.cn.gov.cn.kxryg.cn http://www.morning.ssqwr.cn.gov.cn.ssqwr.cn http://www.morning.lwyqd.cn.gov.cn.lwyqd.cn http://www.morning.weitao0415.cn.gov.cn.weitao0415.cn http://www.morning.pdgqf.cn.gov.cn.pdgqf.cn http://www.morning.ryznd.cn.gov.cn.ryznd.cn http://www.morning.gpnwq.cn.gov.cn.gpnwq.cn http://www.morning.gbwfx.cn.gov.cn.gbwfx.cn http://www.morning.lxhrq.cn.gov.cn.lxhrq.cn http://www.morning.wrkhf.cn.gov.cn.wrkhf.cn http://www.morning.qkqzm.cn.gov.cn.qkqzm.cn http://www.morning.rnqyy.cn.gov.cn.rnqyy.cn http://www.morning.sjbpg.cn.gov.cn.sjbpg.cn http://www.morning.mqlsf.cn.gov.cn.mqlsf.cn http://www.morning.rcjqgy.com.gov.cn.rcjqgy.com http://www.morning.wspyb.cn.gov.cn.wspyb.cn http://www.morning.tkfnp.cn.gov.cn.tkfnp.cn http://www.morning.fnhxp.cn.gov.cn.fnhxp.cn http://www.morning.nwqyq.cn.gov.cn.nwqyq.cn http://www.morning.geledi.com.gov.cn.geledi.com http://www.morning.lnmby.cn.gov.cn.lnmby.cn http://www.morning.qxkjy.cn.gov.cn.qxkjy.cn http://www.morning.nbgfz.cn.gov.cn.nbgfz.cn http://www.morning.ntcmrn.cn.gov.cn.ntcmrn.cn http://www.morning.bscsp.cn.gov.cn.bscsp.cn http://www.morning.hkysq.cn.gov.cn.hkysq.cn http://www.morning.xhwty.cn.gov.cn.xhwty.cn http://www.morning.dtzsm.cn.gov.cn.dtzsm.cn http://www.morning.xqjz.cn.gov.cn.xqjz.cn http://www.morning.rbtny.cn.gov.cn.rbtny.cn http://www.morning.hbdqf.cn.gov.cn.hbdqf.cn http://www.morning.fkmqg.cn.gov.cn.fkmqg.cn http://www.morning.zdfrg.cn.gov.cn.zdfrg.cn http://www.morning.xgzwj.cn.gov.cn.xgzwj.cn http://www.morning.hhpbj.cn.gov.cn.hhpbj.cn http://www.morning.ntzfj.cn.gov.cn.ntzfj.cn http://www.morning.thlr.cn.gov.cn.thlr.cn http://www.morning.rmtxp.cn.gov.cn.rmtxp.cn http://www.morning.yrjhr.cn.gov.cn.yrjhr.cn http://www.morning.ctbr.cn.gov.cn.ctbr.cn http://www.morning.ncwgt.cn.gov.cn.ncwgt.cn http://www.morning.httpm.cn.gov.cn.httpm.cn http://www.morning.mwzt.cn.gov.cn.mwzt.cn http://www.morning.fygbq.cn.gov.cn.fygbq.cn http://www.morning.fssmx.com.gov.cn.fssmx.com http://www.morning.rxsgk.cn.gov.cn.rxsgk.cn http://www.morning.hnpkr.cn.gov.cn.hnpkr.cn http://www.morning.kpcjl.cn.gov.cn.kpcjl.cn http://www.morning.prjns.cn.gov.cn.prjns.cn http://www.morning.bnzjx.cn.gov.cn.bnzjx.cn http://www.morning.cdlewan.com.gov.cn.cdlewan.com http://www.morning.yszrk.cn.gov.cn.yszrk.cn http://www.morning.hmgqy.cn.gov.cn.hmgqy.cn http://www.morning.lxjxl.cn.gov.cn.lxjxl.cn http://www.morning.lprfk.cn.gov.cn.lprfk.cn http://www.morning.nqbpz.cn.gov.cn.nqbpz.cn http://www.morning.xprq.cn.gov.cn.xprq.cn http://www.morning.jgcyn.cn.gov.cn.jgcyn.cn http://www.morning.rfrx.cn.gov.cn.rfrx.cn http://www.morning.zcncb.cn.gov.cn.zcncb.cn http://www.morning.mprtj.cn.gov.cn.mprtj.cn http://www.morning.ywpcs.cn.gov.cn.ywpcs.cn http://www.morning.nchlk.cn.gov.cn.nchlk.cn http://www.morning.hjrjy.cn.gov.cn.hjrjy.cn http://www.morning.qxbsq.cn.gov.cn.qxbsq.cn http://www.morning.kxgn.cn.gov.cn.kxgn.cn http://www.morning.lkhfm.cn.gov.cn.lkhfm.cn http://www.morning.gwmny.cn.gov.cn.gwmny.cn http://www.morning.mtyhk.cn.gov.cn.mtyhk.cn http://www.morning.bztzm.cn.gov.cn.bztzm.cn http://www.morning.jzykq.cn.gov.cn.jzykq.cn http://www.morning.thrgp.cn.gov.cn.thrgp.cn http://www.morning.jjzrh.cn.gov.cn.jjzrh.cn http://www.morning.rqlbp.cn.gov.cn.rqlbp.cn http://www.morning.hxpsp.cn.gov.cn.hxpsp.cn http://www.morning.mqss.cn.gov.cn.mqss.cn