SpringBoot MongoDB詳細使用教程
前言
MongoDB是一個基于分布式文件存儲的NoSQL數(shù)據(jù)庫,由C++語言編寫,旨在為Web應(yīng)用提供可擴展的高性能數(shù)據(jù)存儲解決方案。
MongoDB是一個介于關(guān)系數(shù)據(jù)庫和非關(guān)系數(shù)據(jù)庫之間的產(chǎn)品,是非關(guān)系數(shù)據(jù)庫中功能最豐富最像關(guān)系數(shù)據(jù)庫的,它支持的數(shù)據(jù)結(jié)構(gòu)非常松散,是類似JSON的BSON格式,因此可以存儲比較復(fù)雜的數(shù)據(jù)類型。Mongo最大的特點是它支持的查詢語言非常強大,其語法有點類似與 面向?qū)ο蟮牟樵冋Z言,幾乎可以實現(xiàn)類似關(guān)系數(shù)據(jù)庫單表查詢的絕大部分功能,而且還支持對數(shù)據(jù)建立索引。
安裝MongoDB
官方網(wǎng)站https://www.mongodb.com/downlad-center/community下載
Spring Boot整合MongoDB
Spring對MongoDB的支持主要是通過Spring Data MongoDB實現(xiàn)的,Spring Data MongoDB提供了如下功能
1:對象/文檔映射注解
2:MongoTemplate
提供了數(shù)據(jù)訪問的方法
3:Repository
public interface PersonRepository extends MongoRepository<Person,String>{ }
實戰(zhàn)進行增刪改查
1:創(chuàng)建基于spring-boot-starter-data-mongodb依賴的Spring Boot應(yīng)用
2:配置application.properties文件
server.servlet.context-path=/ch6_8
#讓控制器輸出的JSON字符串格式更美觀
spring.jackson.serialization.indent-output=true
3:創(chuàng)建領(lǐng)域模型
創(chuàng)建名為com.ch.ch6_8.domain的包 并在該包中創(chuàng)建領(lǐng)域模型Person以及人去過的Loation
Person代碼如下
package com.ch.ch6_8.domain; import java.util.ArrayList; import java.util.List; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; import org.springframework.data.mongodb.core.mapping.Field; @Document public class Person { @Id private String pid; private String pname; private Integer page; private String psex; @Field("plocs") private List<Location> locations = new ArrayList<Location>(); public Person() { super(); } public Person(String pname, Integer page, String psex) { super(); this.pname = pname; this.page = page; this.psex = psex; } public String getPid() { return pid; } public void setPid(String pid) { this.pid = pid; } public String getPname() { return pname; } public void setPname(String pname) { this.pname = pname; } public Integer getPage() { return page; } public void setPage(Integer page) { this.page = page; } public String getPsex() { return psex; } public void setPsex(String psex) { this.psex = psex; } public List<Location> getLocations() { return locations; } public void setLocations(List<Location> locations) { this.locations = locations; } }
Location代碼如下
package com.ch.ch6_8.domain; public class Location { private String locName; private String year; public Location() { super(); } public Location(String locName, String year) { super(); this.locName = locName; this.year = year; } public String getLocName() { return locName; } public void setLocName(String locName) { this.locName = locName; } public String getYear() { return year; } public void setYear(String year) { this.year = year; } }
4:創(chuàng)建數(shù)據(jù)訪問接口
package com.ch.ch6_8.repository; import java.util.List; import org.springframework.data.mongodb.repository.MongoRepository; import org.springframework.data.mongodb.repository.Query; import com.ch.ch6_8.domain.Person; public interface PersonRepository extends MongoRepository<Person, String>{ Person findByPname(String pname);//支持方法名查詢,方法名命名規(guī)范參照表6.1 @Query("{'psex':?0}")//JSON字符串 List<Person> selectPersonsByPsex(String psex); }
5:創(chuàng)建控制器層
創(chuàng)建控制器類TestMongoDBController
package com.ch.ch6_8.controller; import java.util.ArrayList; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.ch.ch6_8.domain.Location; import com.ch.ch6_8.domain.Person; import com.ch.ch6_8.repository.PersonRepository; @RestController public class TestMongoDBController { @Autowired private PersonRepository personRepository; @RequestMapping("/save") public List<Person> save() { List<Location> locations1 = new ArrayList<Location>(); Location loc1 = new Location("北京","2019"); Location loc2 = new Location("上海","2018"); locations1.add(loc1); locations1.add(loc2); List<Location> locations2 = new ArrayList<Location>(); Location loc3 = new Location("廣州","2017"); Location loc4 = new Location("深圳","2016"); locations2.add(loc3); locations2.add(loc4); List<Person> persons = new ArrayList<Person>(); Person p1 = new Person("陳恒1", 88, "男"); p1.setLocations(locations1); Person p2 = new Person("陳恒2", 99, "女"); p2.setLocations(locations2); persons.add(p1); persons.add(p2); return personRepository.saveAll(persons); } @RequestMapping("/findByPname") public Person findByPname(String pname) { return personRepository.findByPname(pname); } @RequestMapping("/selectPersonsByPsex") public List<Person> selectPersonsByPsex(String psex) { return personRepository.selectPersonsByPsex(psex); } @RequestMapping("/updatePerson") public Person updatePerson(String oldPname, String newPname) { Person p1 = personRepository.findByPname(oldPname); if(p1 != null) p1.setPname(newPname); return personRepository.save(p1); } @RequestMapping("/deletePerson") public void updatePerson(String pname) { Person p1 = personRepository.findByPname(pname); personRepository.delete(p1); } }
運行主類后 可以使用MongoDB的圖形界面管理工具MongoDB Compass打開查看已保存的數(shù)據(jù)
到此這篇關(guān)于SpringBoot MongoDB詳細使用教程的文章就介紹到這了,更多相關(guān)SpringBoot MongoDB內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于Tomcat出現(xiàn)The origin server did not find a current represent
這篇文章主要介紹了關(guān)于Tomcat出現(xiàn)The origin server did not find a current representation for the target resourc...的問題,感興趣的小伙伴們可以參考一下2020-08-08Java使用FutureTask實現(xiàn)預(yù)加載的示例詳解
基于FutureTask的特性,通??梢允褂肍utureTask做一些預(yù)加載工作,比如一些時間較長的計算等,本文就來和大家講講具體實現(xiàn)方法吧,感興趣的可以了解一下2023-06-06