一 安装MongoDB
1 Docker安装
安装完成后,执行下面命令:
docker run -d -p 27017:27017 mongo
2 VirtualBox下做一次端口映射

3 下载MongoDB数据库管理软件Robomongo
下载地址
二 新建Spring Boot项目
新增依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
三 新建领域模型
1 Person源码
package com.wisely.ch8_6_1.domain;
import java.util.Collection;
import java.util.LinkedHashSet;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;
@Document //该注解映射领域模型和MongoDB的文档
public class Person {
//表明这个属性为文档的Id
@Id
private String id;
private String name;
private Integer age;
//此属性在文档中的名称为locs,locations属性将以数组形式存在当前数据记录中
@Field("locs")
private Collection<Location> locations = new LinkedHashSet<Location>();
public Person(String name, Integer age) {
super();
this.name = name;
this.age = age;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Collection<Location> getLocations() {
return locations;
}
public void setLocations(Collection<Location> locations) {
this.locations = locations;
}
}
2 Location源码
package com.wisely.ch8_6_1.domain;
public class Location {
private String place;
private String year;
public Location(String place, String year) {
super();
this.place = place;
this.year = year;
}
public String getPlace() {
return place;
}
public void setPlace(String place) {
this.place = place;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
}
四 数据访问
package com.wisely.ch8_6_1.dao;
import java.util.List;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query;
import com.wisely.ch8_6_1.domain.Person;
public interface PersonRepository extends MongoRepository<Person, String> {
//支持方法名查询
Person findByName(String name);
//支持@Query查询,查询参数构造JSON字符串即可
@Query("{'age': ?0}")
List<Person> withQueryFindByAge(Integer age);
}
五 控制器
package com.wisely.ch8_6_1.web;
import java.util.Collection;
import java.util.LinkedHashSet;
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.wisely.ch8_6_1.dao.PersonRepository;
import com.wisely.ch8_6_1.domain.Location;
import com.wisely.ch8_6_1.domain.Person;
@RestController
public class DataController {
@Autowired
PersonRepository personRepository;
//测试保存数据
@RequestMapping("/save")
public Person save(){
Person p = new Person("wyf",32);
Collection<Location> locations = new LinkedHashSet<Location>();
Location loc1 = new Location("上海","2009");
Location loc2 = new Location("合肥","2010");
Location loc3 = new Location("广州","2011");
Location loc4 = new Location("马鞍山","2012");
locations.add(loc1);
locations.add(loc2);
locations.add(loc3);
locations.add(loc4);
p.setLocations(locations);
return personRepository.save(p);
}
//测试方法名查询
@RequestMapping("/q1")
public Person q1(String name){
return personRepository.findByName(name);
}
//测试@Query查询
@RequestMapping("/q2")
public List<Person> q2(Integer age){
return personRepository.withQueryFindByAge(age);
}
}
六 主类
@SpringBootApplication
public class Ch861Application {
public static void main(String[] args) {
SpringApplication.run(Ch861Application.class, args);
}
}
@Configuration
//启动MongoDB的支持
@EnableMongoRepositories
class AppConfig{
}
七 测试
1 测试保存数据
浏览器输入:http://localhost:8080/save
页面显示结果如下:

2 测试方法名查询
浏览器输入:http://localhost:8080/q1?name=wyf
页面显示结果如下:

3 测试@Query查询
浏览器输入:http://localhost:8080/q2?age=32
页面显示结果如下:

本文详细介绍了如何使用Spring Boot框架整合MongoDB数据库,包括安装配置、领域模型设计、数据访问层实现、控制器编写及测试等关键步骤,旨在帮助开发者快速上手并掌握这一技能。

296

被折叠的 条评论
为什么被折叠?



