您好,登錄后才能下訂單哦!
這篇文章主要講解了Spring Boot如何集成mongodb數據庫,內容清晰明了,對此有興趣的小伙伴可以學習一下,相信大家閱讀完之后會有幫助。
一.認識mongodb
MongoDB是一個介于關系數據庫和非關系數據庫之間的產品,是非關系數據庫當中功能最豐富,最像關系數據庫的。它支持的數據結構非常松散,是類似json的bson格式,因此可以存儲比較復雜的數據類型。Mongo最大的特點是它支持的查詢語言非常強大,其語法有點類似于面向對象的查詢語言,幾乎可以實現類似關系數據庫單表查詢的絕大部分功能,而且還支持對數據建立索引。
二.Spring boot項目集成mongodb
1.添加mongodb依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency>
2.配置mongodb的連接
spring:
data:
mongodb:
#uri: mongodb://localhost:27017/data_exploration
uri: mongodb://root:dhcc-mongodb@192.168.100.87:27017/data_exploration authSource=admin&authMechanism=SCRAM-SHA-1
解析:以上uri分別代表本地配置和遠程連接
3.操作數據庫
(1)保存
@Repository public class ExplorationJobDao { @Autowired MongoTemplate mongoTemplate; public void save(ExplorationJob explorationJob) { mongoTemplate.save(explorationJob); } }
(2)根據ID修改一條數據(其原理先符合ID的數據,然后刪除查詢結果的第一條)
public void updateExecutionStatusById(int executionStatus, String jobId) { Query query = new Query(Criteria.where("jobId").is(jobId)); Update update = new Update().set("executionStatus", executionStatus); mongoTemplate.updateFirst(query, update, ExplorationJob.class); }
(3)根據條修改多條數據(查詢符合ID的所有數據,然后將所有數據修改)
public void update(BusinessExploration businessExploration) { Query query = new Query(Criteria.where("_id").is(businessExploration.getId())); Update update = new Update().set("sourceUnit", businessExploration.getSourceUnit()) .set("appSystem", businessExploration.getAppSystem()) .set("businessImplication", businessExploration.getBusinessImplication()) .set("safetyRequire", businessExploration.getSafetyRequire()); mongoTemplate.updateMulti(query, update, TableExploration.class); }
(4)刪除(根據ID刪除)
public void delExplorationJobById(String jobId) { Query query=new Query(Criteria.where("jobId").is(jobId)); mongoTemplate.remove(query,ExplorationJob.class); }
(5)根據條件查詢(根據ID查詢)
public ExplorationJob getExplorationJobByJobId(String jobId) { Query query = new Query(Criteria.where("jobId").is(jobId)); ExplorationJob explorationJob = mongoTemplate.findOne(query, ExplorationJob.class); return explorationJob; }
(6)查詢所有
mongoTemplate.findAll(TableExploration.class);
(7)多條件動態查詢
public List<ExplorationJob> getExplorationByCondition(ExplorationJob explorationJob) { Query query = new Query(); if (explorationJob.getJobName() != null) { Pattern pattern = Pattern.compile("^.*" + explorationJob.getJobName() + ".*$", Pattern.CASE_INSENSITIVE); query.addCriteria(Criteria.where("jobName").regex(pattern)); } if (explorationJob.getDsType() != null) { query.addCriteria(Criteria.where("dsType").is(explorationJob.getDsType())); } if (explorationJob.getExecutionStatus() != null) { query.addCriteria(Criteria.where("executionStatus").lte(explorationJob.getExecutionStatus())); } List<ExplorationJob> explorationJobs=mongoTemplate.find(query, ExplorationJob.class); return explorationJobs; }
(8)查詢最大值
public Date getMaxExplorationDate(String tableName) { FindIterable<Document> iterable = mongoTemplate.getCollection("tableExploration") .find(new BasicDBObject("tableName", tableName)).sort(new BasicDBObject("explorationDate", -1)).skip(0) .limit(1); Document doc =null; if (getDocuments(iterable).size()>0) { doc=getDocuments(iterable).get(0); Date date = doc.getDate("explorationDate"); return date; }else { return null; } } /** * 工具方法 * * @param iterable * @return */ public static List<Document> getDocuments(FindIterable<Document> iterable) { List<Document> results = new ArrayList<Document>(); if (null != iterable) { MongoCursor<Document> cursor = iterable.iterator(); Document doc = null; while (cursor.hasNext()) { doc = cursor.next(); results.add(doc); } } return results; }
(9)分組查詢(這里還是用到了排序)
public List<TableExploration> getAllTableExplorationGroupByTableName(String jobId){ Aggregation aggregation = Aggregation.newAggregation( Aggregation.match(Criteria.where("jobId").is(jobId)), Aggregation.sort(new Sort(Direction.DESC,"explorationDate")), Aggregation.group("tableName") .first("_id").as("tableName") .first("databaseType").as("databaseType") .first("databaseName").as("databaseName") .first("networkSituation").as("networkSituation") .first("userName").as("userName") .first("password").as("password") .first("url").as("url") .first("dataStorage").as("dataStorage") .first("dataIncrement").as("dataIncrement") .first("explorationDate").as("explorationDate") //.push("columnExplorations").as("columnExplorations") .first("jobId").as("jobId") ); AggregationResults<TableExploration> aggregationResults= mongoTemplate.aggregate(aggregation, "tableExploration", TableExploration.class); List<TableExploration> tableExplorations=aggregationResults.getMappedResults(); return tableExplorations;
看完上述內容,是不是對Spring Boot如何集成mongodb數據庫有進一步的了解,如果還想學習更多內容,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。