项目总结52: Linux安装mongodb和Springboot集成mongodb
项目总结52: Linux安装mongodb和Springboot集成mongodb
Linux安装mongodb
如何在Linux上安装mongodb,可以参考这篇文章:https://blog.csdn.net/E_Eric12138/article/details/89606950
Springboot集成mongodb
第一步:POM文件中引入jar包
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency>
第二步:设计保存到mongodb的数据格式实体类
package com.hs.web.entity.mongodb; import com.hs.api.commom.bizservice.thirdparty.express100.LastResultBean; import io.swagger.annotations.ApiModel; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.index.Indexed; import org.springframework.data.mongodb.core.mapping.Document; import java.sql.Timestamp; import java.util.Date; /* * *@Description: *@Author:TYJ *@Date: create in 2020/1/13 15:04 */ @Document //标明由mongo来维护该表 @ApiModel("mongodb 快递100返回数据") public class Express100NotifyVO { @Id //自增主键 private String id; @Indexed //索引 private Long orderId; private String orderCode; private String logisticsNum; private String jsonData; private Date dataTime; ......省略getter和setter }
第三步:设计service层,实现数据的保存和获取
import com.hs.web.entity.mongodb.Express100NotifyVO; import org.springframework.beans.factory.annotation.Autowired; 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.stereotype.Component; import java.util.List; @Component public class MongodbBizService { @Autowired private MongoTemplate mongoTemplate;//MongoTemplate类,和RedisTemplate类似,对mongodb数据库的XRUD操作进行封装 //1-将快递100的数据保存到mongodb public void saveExpress100Data(Express100NotifyVO data){ mongoTemplate.save(data);//保存数据 } //2-读取快递100数据 public List<Express100NotifyVO> listExpress100Data(Long orderId){ Query query = new Query();//实例化一个查询条件 query.addCriteria(Criteria.where("orderId").is(orderId));//封装查询条件-根据orderId查询数据 List<Express100NotifyVO> dataList = mongoTemplate.find(query, Express100NotifyVO.class);//mongoTemplate.find查询mongodb数据据 return dataList; } }
第四步:在application.properties文件中进行配置
#mongodb://username::port/database spring.data.mongodb.uri=mongodb://xg::27017/xg
第五步:写controller,正常调用service层方法;
本人遇到的坑:
1-本地测试完功能后,将mongodb用到的jar上传到Linux,没有把bson-3.4.3.jar上传到服务器,导致启动的时候一直无法加载decoder方法
相关推荐
大秦铁骑 2020-08-19
thatway 2020-08-19
lovecodeblog 2020-08-19
codetyper 2020-08-16
MongoDB数据库 2020-08-16
csuzxm000 2020-08-02
flyDeDog 2020-06-14
lbyd0 2020-11-17
BigYellow 2020-11-16
sushuanglei 2020-11-12
我心似明月 2020-11-09
zhushenghan 2020-11-09
sunnnyduan 2020-10-16
不要皱眉 2020-10-14
xiaohai 2020-09-29
songxiugongwang 2020-09-22
萌亖 2020-09-17
LuckyLXG 2020-09-08