前言
MongoDB是一个基于分布式文件存储的NoSQL数据库,由C++语言编写,旨在为Web应用提供可扩展的高性能数据存储解决方案。
MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库中功能最丰富最像关系数据库的,它支持的数据结构非常松散,是类似JSON的BSON格式,因此可以存储比较复杂的数据类型。Mongo最大的特点是它支持的查询语言非常强大,其语法有点类似与 面向对象的查询语言,几乎可以实现类似关系数据库单表查询的绝大部分功能,而且还支持对数据建立索引。
安装MongoDB
官方网站https://www.mongodb.com/downlad-center/community下载
Spring Boot整合MongoDB
Spring对MongoDB的支持主要是通过Spring Data MongoDB实现的,Spring Data MongoDB提供了如下功能
1:对象/文档映射注解
2:MongoTemplate
提供了数据访问的方法
3:Repository
public interface PersonRepository extends MongoRepository<Person,String>{
}
实战进行增删改查
1:创建基于spring-boot-starter-data-mongodb依赖的Spring Boot应用
2:配置application.properties文件
server.servlet.context-path=/ch6_8
#让控制器输出的JSON字符串格式更美观
spring.jackson.serialization.indent-output=true
3:创建领域模型
创建名为com.ch.ch6_8.domain的包 并在该包中创建领域模型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:创建数据访问接口
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);//支持方法名查询,方法名命名规范参照表6.1
@Query("{'psex':?0}")//JSON字符串
List<Person> selectPersonsByPsex(String psex);
}
5:创建控制器层
创建控制器类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打开查看已保存的数据
到此这篇关于SpringBoot MongoDB详细使用教程的文章就介绍到这了,更多相关SpringBoot MongoDB内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!