最近接手一个Springboot项目,需要在原项目上增加一些需求,用到了mongodb。一起看下集成之路吧!
1、首先pom.xml中引入mongodbDe依赖jar包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
2、创建实体类
@Document(collection = "spiderConfig")
public class SpiderConfig implements Serializable {
@Id
private String id;
private String spiderConfig;
private long updateTime;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getSpiderConfig() {
return spiderConfig;
}
public void setSpiderConfig(String spiderConfig) {
this.spiderConfig = spiderConfig;
}
public long getUpdateTime() {
return updateTime;
}
public void setUpdateTime(long updateTime) {
this.updateTime = updateTime;
}
}
3、dao层操作mongodb的代码
@Component
public class SpiderConfigDaoImpl implements ISpiderConfigDao{
@Autowired
private MongoTemplate mongoTemplate;
@Override
public SpiderConfig findById(String id) {
Query query=new Query(Criteria.where("_id").is(id));
SpiderConfig user = mongoTemplate.findOne(query , SpiderConfig.class);
return user;
}
@Override
public void saveSpiderConfig(SpiderConfig spiderConfig) {
mongoTemplate.save(spiderConfig);
}
}