0


【SpringBoot整合MongoDB】

文章目录

Linux宝塔安装MongoDB

  • 进入宝塔页面,找到软件商店,搜索mongoDB,点击安装

在这里插入图片描述

  • 修改Mongo DB配置文件,将ip换成0.0.0.0,之后重启mongo DB在这里插入图片描述
  • 配置php,在安装扩展中安装mongo db,重启php。在这里插入图片描述
  • 使用工具测试连接在这里插入图片描述

SpringBoot整合MongoDB

  • 引入依赖
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-mongodb</artifactId></dependency>
  • 编写配置文件
data:mongodb:host: xxx.xx.xxx.xx
      database: 数据库
      port:27017
  • 编写实体类
@Document(collection ="article")@DatapublicclassArticleimplementsSerializable{@IdprivateString id;privateString articleId;privateString content;privateInteger likeNum;@IndexedprivateString userName;privateDate createTime;privateDate updateTime;}
  • Controller层的增删改查
@RestController@RequestMapping("/idol/article")@Api("发布文章")publicclassArticleController{@ResourceprivateArticleService articleService;@PostMapping("/getAll")@ApiOperation("查询全部")publicResultgetList(){List<Article> list = articleService.getList();returnResult.success(list);}@PostMapping("/issueArticle")@ApiOperation("发布文章、动态")publicResultissueArticle(@RequestBodyArticle article){Result res = articleService.issueArticle(article);returnResult.success(res);}@GetMapping("/getArticleById")@ApiOperation("根据id查询")publicResultgetArticleById(@RequestParamString id){Article article = articleService.getArticleById(id);returnResult.success(article);}@PostMapping("/getArticleByParam")@ApiOperation("根据条件查询")publicResultgetArticleByParam(@RequestBodyArticleParam articleParam){List<Article> articleList = articleService.getArticleByParam(articleParam);returnResult.success(articleList);}@GetMapping("/deleteArticle")publicResultdeleteArticle(@RequestParamString id){Result result = articleService.deleteArticle(id);returnResult.success(result);}}
  • service接口
publicinterfaceArticleService{List<Article>getList();ResultissueArticle(Article article);ArticlegetArticleById(String id);List<Article>getArticleByParam(ArticleParam articleParam);ResultdeleteArticle(String id);}
  • serviceImpl,这里我用了两个方式,一个是直接继承MongoRepository,一个是使用MongoTemplate
@Service@Slf4jpublicclassArticleServiceImplimplementsArticleService{@ResourceprivateArticleMapper articleMapper;@ResourceprivateMongoTemplate mongoTemplate;@OverridepublicList<Article>getList(){List<Article> all = articleMapper.findAll();return all;}@OverridepublicResultissueArticle(Article article){
    article.setCreateTime(newDate());
    article.setUpdateTime(newDate());Article insert = articleMapper.insert(article);returnResult.success(insert);}@OverridepublicArticlegetArticleById(String id){Article article = mongoTemplate.findById(id,Article.class);return article;}@OverridepublicList<Article>getArticleByParam(ArticleParam articleParam){Query query =newQuery();if(!StringUtils.isEmpty(articleParam.getKey())){
      query.addCriteria(Criteria.where("content").regex(articleParam.getKey()));}if(!StringUtils.isEmpty(articleParam.getUserName())){
      query.addCriteria(Criteria.where("userName").is(articleParam.getUserName()));}List<Article> articleList = mongoTemplate.find(query,Article.class);return articleList;}@OverridepublicResultdeleteArticle(String id){DeleteResult result = mongoTemplate.remove(newQuery(Criteria.where("_id").is(id)),Article.class,"article");returnResult.success(result);}}
  • 自定义mapper
@MapperpublicinterfaceArticleMapperextendsMongoRepository<Article,String>{}

结尾

这是第一次使用MongoDB,从搭建环境到写个小Demo吧,大佬和新手小白有建议或者问题,评论区理性发言。


本文转载自: https://blog.csdn.net/zhang0305/article/details/125706321
版权归原作者 IDEA上的操作工 所有, 如有侵权,请联系我们删除。

“【SpringBoot整合MongoDB】”的评论:

还没有评论