栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot操作mongodb

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot操作mongodb

目录
  • 引入MongoDB依赖
  • 配置文件进行配置
  • 创建MongoDB实体类
  • 创建Dao
  • 创建service
  • 创建测试类进行测试

引入MongoDB依赖
        
            org.springframework.boot
            spring-boot-starter-data-mongodb
        
配置文件进行配置
spring:
  data:
    mongodb:
      host: 127.0.0.1
      database: articledb
      port: 27017
创建MongoDB实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
@document(collection = "comment")   //可以省略,默认为类名小写映射集合
@CompoundIndex(def = "{'userid': 1,'nickname': -1}")    //定义复合索引
public class Comment implements Serializable {
    @Id
    private String id;
    @Field("content") //属性和字段一致不用该注解
    private String content;
    private Date publishtime;
    @Indexed    //添加了一个索引
    private String userid;
    private String nickname;
    private LocalDateTime createdatetime;
    private Integer likenum;
    private Integer replynum;
    private String state;
    private String parentid;
    private String articleid;
}
创建Dao
//继承MongoRepository类,
public interface CommentRepository extends MongoRepository {
	//分页查询,会自动解析,只能叫findByParentid才会解析
    Page findByParentid(String parentid, Pageable pageable);  
}
创建service
@Service
public class CommentService {
	//有两种方式操作MongoDB,一种是spring data mongodb,另外一种是mongotemplate
    @Autowired
    private CommentRepository commentRepository;

    @Autowired
    private MongoTemplate mongoTemplate;

    public void saveComment(Comment comment){
        commentRepository.save(comment);
    }

    public void updateComment(Comment comment){
        commentRepository.save(comment);
    }

    public void  deleteCommentById(String id){
        commentRepository.deleteById(id);
    }

    public List findCommentList(){
        return commentRepository.findAll();
    }

    public Comment findCommentById(String id){
        return commentRepository.findById(id).get();
    }

    public Page findCommentListByParentif(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);
    }
}
创建测试类进行测试
@SpringBootTest
public class CommentServiceTest {
    @Autowired
    private CommentService commentService;

    @Test
    public void testSaveComment(){
//        Comment comment = new Comment("1","test1",new Date(),"1001","貂蝉", LocalDateTime.now(),0,0,"okk",null,"001");
//        Comment comment = new Comment("2","test2",new Date(),"1002","吕布", LocalDateTime.now(),0,0,"okk",null,"002");
        Comment comment = new Comment("3","test3",new Date(),"1003","刘禅", LocalDateTime.now(),0,0,"okk","1","003");
        commentService.saveComment(comment);
    }

    @Test
    public void testFindCommentList(){
        System.out.println(commentService.findCommentList());
    }

    @Test
    public void testFindById(){
        System.out.println(commentService.findCommentById("1"));
    }

    @Test
    public void testFindCommentListByParentid(){
        Page page = commentService.findCommentListByParentif("1", 1, 2);
        System.out.println(page.getTotalElements());
        System.out.println(page.getContent());
    }

    @Test
    public void testUpdateCommentLikenum(){
        commentService.updateCommentLikenum("1");
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/532026.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号