亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

SpringBoot MongoDB詳細使用教程

 更新時間:2022年10月27日 11:40:35   作者:showswoller  
這篇文章主要介紹了SpringBoot整合Mongodb實現(xiàn)簡單的增刪查改,MongoDB是一個以分布式數(shù)據(jù)庫為核心的數(shù)據(jù)庫,因此高可用性、橫向擴展和地理分布是內(nèi)置的,并且易于使用。況且,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)文章

  • C語言指針數(shù)組案例詳解

    C語言指針數(shù)組案例詳解

    這篇文章主要介紹了C語言指針數(shù)組案例詳解,本文通過案例詳細的解釋了指針與數(shù)組的初始化還有關(guān)系與應(yīng)用,需要的朋友可以參考下這篇文章
    2021-07-07
  • 關(guān)于Tomcat出現(xiàn)The origin server did not find a current representation for the target resourc...的問題

    關(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-08
  • IDEA如何切換JDK版本

    IDEA如何切換JDK版本

    本文主要介紹了IDEA如何切換JDK版本,JDK版本之間的關(guān)系是一個向后兼容的關(guān)系,所以我們需要切換JDK的版本號,具有一定的參考價值,感興趣的可以了解一下
    2024-01-01
  • Spring配置文件中parent與abstract的使用

    Spring配置文件中parent與abstract的使用

    這篇文章主要介紹了Spring配置文件中parent與abstract的使用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • 詳解Java設(shè)計模式之備忘錄模式的使用

    詳解Java設(shè)計模式之備忘錄模式的使用

    這篇文章主要介紹了Java設(shè)計模式之備忘錄模式的使用,備忘錄模式中的發(fā)起者和管需要的朋友可以參考下
    2016-02-02
  • java圖形界面之加法計算器

    java圖形界面之加法計算器

    這篇文章主要為大家詳細介紹了java圖形界面之加法計算器,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • Java實現(xiàn)猜數(shù)字小游戲代碼

    Java實現(xiàn)猜數(shù)字小游戲代碼

    大家好,本篇文章主要講的是Java實現(xiàn)猜數(shù)字小游戲代碼,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-01-01
  • 聊聊RabbitMQ發(fā)布確認高級問題

    聊聊RabbitMQ發(fā)布確認高級問題

    這篇文章主要介紹了RabbitMQ發(fā)布確認高級問題,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-01-01
  • SpringBoot中的ExpiringMap代碼實例

    SpringBoot中的ExpiringMap代碼實例

    這篇文章主要介紹了SpringBoot中的ExpiringMap代碼實例,ExpiringMap是一個可以設(shè)置過期策略、可變條目過期、延遲條目加載和過期偵聽器的線程安全存儲容器,需要的朋友可以參考下
    2023-08-08
  • Java使用FutureTask實現(xiàn)預(yù)加載的示例詳解

    Java使用FutureTask實現(xiàn)預(yù)加載的示例詳解

    基于FutureTask的特性,通??梢允褂肍utureTask做一些預(yù)加載工作,比如一些時間較長的計算等,本文就來和大家講講具體實現(xiàn)方法吧,感興趣的可以了解一下
    2023-06-06

最新評論