电子商务平台网站推广拼团小程序代理

当前位置: 首页 > news >正文

电子商务平台网站推广,拼团小程序代理,套网站模板软件,台州网页设计招聘文章目录 项目搭建文章评论实体类的编写文章评论的基本增删改查根据上级ID查询文章评论的分页列表MongoTemplate实现评论点赞 GITHUB 项目搭建 ?xml version1.0 encodingUTF-8?project xmlnshttp://maven.apache.org/POM/4.0.0?xml version1.0 encodingUTF-8?project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.1.6.RELEASE/versionrelativePath/ !– lookup parent from repository –/parentgroupIditcast/groupIdartifactIdarticle/artifactIdversion1.0-SNAPSHOT/versiondependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-mongodb/artifactId/dependency/dependencies/project2创建application.yml spring:#数据源配置data:mongodb:# 主机地址host: 192.168.218.131# 数据库database: articledb# 默认端口是27017port: 27017username: rootpassword: 123456authentication-database: admin #必须设置设置权限认证的数据库3创建启动类 onenewcode.article.ArticleApplication package onenewcode.article;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;SpringBootApplicationpublic class ArticleApplication {public static void main(String[] args) {SpringApplication.run(ArticleApplication.class, args);}}4启动项目看是否能正常启动控制台没有错误。 文章评论实体类的编写 创建实体类 创建包onenewcode.article包下建包po用于存放实体类创建实体类 onenewcode.article.po.Comment package onenewcode.article.service;import onenewcode.article.dao.CommentRepository; import onenewcode.article.po.Comment; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; 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 org.springframework.stereotype.Service;import java.util.List;Service public class CommentService {Autowiredprivate CommentRepository commentRepository;Autowiredprivate MongoTemplate mongoTemplate;/*** 保存一个评论* param comment/public void saveComment(Comment comment){//如果需要自定义主键可以在这里指定主键如果不指定主键MongoDB会自动生成主键//设置一些默认初始值。。。//调用daocommentRepository.save(comment);}/** 更新评论* param comment/public void updateComment(Comment comment){//调用daocommentRepository.save(comment);}/** 根据id删除评论* param id/public void deleteCommentById(String id){//调用daocommentRepository.deleteById(id);}/** 查询所有评论* return/public ListComment findCommentList(){//调用daoreturn commentRepository.findAll();}/** 根据id查询评论* param id* return/public Comment findCommentById(String id){//调用daoreturn commentRepository.findById(id).get();}public PageComment findCommentListByParentid(String parentid,int page,int size) {return commentRepository.findByParentid(parentid,PageRequest.of(page-1,size));}public void updateCommentLikenum(String id){// 查询条件Query query Query.query(Criteria.where(_id).is(id));// 更新条件Update update new Update();update.inc(likenum);mongoTemplate.updateFirst(query,update,Comment.class);} } 说明索引可以大大提升查询效率一般在查询字段上添加索引索引的添加可以通过Mongo的命令来添加也可以在Java的实体类中通过注解添加。 1单字段索引注解Indexed org.springframework.data.mongodb.core.index.Indexed.class 声明该字段需要索引建索引可以大大的提高查询效率。 Mongo命令参考 db.comment.createIndex({“userid”:1}) 2复合索引注解CompoundIndex org.springframework.data.mongodb.core.index.CompoundIndex.class 复合索引的声明建复合索引可以有效地提高多字段的查询效率。 db.comment.createIndex({“userid”:1,“nickname”:-1}) 文章评论的基本增删改查 创建数据访问接口 onenewcode.article包下创建dao包包下创建接口 onenewcode.article.dao.CommentRepository package onenewcode.article.dao;import onenewcode.article.po.Comment; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.mongodb.repository.MongoRepository;//评论的持久层接口 public interface CommentRepository extends MongoRepositoryComment,String {}创建业务逻辑类 onenewcode.article包下创建service包包下创建类 import onenewcode.article.po.Comment; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;import java.util.List;//评论的业务层 Servicepublic class CommentService {//注入daoAutowiredprivate CommentRepository commentRepository;/** 保存一个评论* param comment/public void saveComment(Comment comment){//如果需要自定义主键可以在这里指定主键如果不指定主键MongoDB会自动生成主键//设置一些默认初始值。。。//调用daocommentRepository.save(comment);}/** 更新评论* param comment/public void updateComment(Comment comment){//调用daocommentRepository.save(comment);}/** 根据id删除评论* param id/public void deleteCommentById(String id){//调用daocommentRepository.deleteById(id);}/** 查询所有评论* return/public ListComment findCommentList(){//调用daoreturn commentRepository.findAll();}/** 根据id查询评论* param id* return/public Comment findCommentById(String id){//调用daoreturn commentRepository.findById(id).get();}}新建Junit测试类测试保存和查询所有 package onenewcode.article.service;import onenewcode.article.ArticleApplication; import onenewcode.article.po.Comment; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.domain.Page; import org.springframework.test.context.junit4.SpringRunner; import java.time.LocalDateTime; import java.util.List; //测试评论的业务层 //SpringBoot的Junit集成测试//SpringBoot的测试环境初始化参数启动类 SpringBootTest public class CommentServiceTest {//注入ServiceAutowiredprivate CommentService commentService;/** 保存一个评论/Testpublic void testSaveComment(){Comment commentnew 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);}/** 查询所有数据/Testpublic void testFindAll(){ListComment list commentService.findCommentList();System.out.println(list);}/** 测试根据id查询/Testpublic void testFindCommentById(){Comment comment commentService.findCommentById(5d6a27b81b8d374798cf0b41);System.out.println(comment);} }添加结果
根据上级ID查询文章评论的分页列表 CommentRepository新增方法定义 //根据父id查询子评论的分页列表 Page findByParentid(String parentid, Pageable pageable); CommentService新增方法 /**
根据父id查询分页列表* param parentid* param page* param size* return/public PageComment findCommentListPageByParentid(String parentid,int page ,int size){return commentRepository.findByParentid(parentid, PageRequest.of(page-1,size));}junit测试用例 /** 测试根据父id查询子评论的分页列表/Testpublic void testFindCommentListPageByParentid(){PageComment pageResponse commentService.findCommentListPageByParentid(3, 1, 2);System.out.println(—-总记录数pageResponse.getTotalElements());System.out.println(—-当前页数据pageResponse.getContent());}MongoTemplate实现评论点赞 以下点赞的临时示例代码 CommentService 新增updateThumbup方法 /** 点赞-效率低* param id/public void updateCommentThumbupToIncrementingOld(String id){Comment comment CommentRepository.findById(id).get();comment.setLikenum(comment.getLikenum()1);CommentRepository.save(comment);}以上方法虽然实现起来比较简单但是执行效率并不高因为我只需要将点赞数加1就可以了没必要查询出所有字段修改后再更新所有字 段。(蝴蝶效应) 我们可以使用MongoTemplate类来实现对某列的操作。 1修改CommentService //注入MongoTemplateAutowiredprivate MongoTemplate mongoTemplate;/** 点赞数1* param id/public void updateCommentLikenum(String id){//查询对象 Query queryQuery.query(Criteria.where(_id).is(id));//更新对象 Update updatenew Update();//局部更新相当于\(set// update.set(key,value)//递增\)inc//
update.inc(likenum,1);update.inc(likenum);}//参数1查询对象 //参数2更新对象 //参数3集合的名字或实体类的类型Comment.classmongoTemplate.updateFirst(query,update,comment); }测试用例 /**
点赞数1*/Testpublic void testUpdateCommentLikenum(){//对3号文档的点赞数1commentService.updateCommentLikenum(3);}GITHUB 代码仓库 https://github.com/onenewcode/MyMongoDB.git