SpringBoot结合MongoDB简单应用
来源:互联网 发布:商务部零售数据 编辑:程序博客网 时间:2024/06/05 06:38
1.docker安装MongoDB
本案例在windows环境下安装。
命令:
docker pull mongodb
2.运行docker容器
docker run -d -p 27017:27017 mongo
打开virtualBox进行端口映射。如图所示:
3. 下载安装mongoDB数据库管理软件
Robomongo
4.搭建springBoot项目
pom.xml
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.wangh</groupId> <artifactId>springboot_mongodb</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springboot_mongodb</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>
5.新建实体类(领域模型)
package com.wangh.springboot_mongodb.model;import java.util.Collection;import java.util.LinkedList;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; @Field("locs") //此属性在文档中的名称为locs,locations属性将以数组形式存在当前数据记录中 private Collection<Location> locations = new LinkedList<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; }}package com.wangh.springboot_mongodb.model;/** * 工作过的地方 * @author Wanghao * */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; }}
6. 数据访问接口
package com.wangh.springboot_mongodb.repository;import java.util.List;import org.springframework.data.mongodb.repository.MongoRepository;import org.springframework.data.mongodb.repository.Query;import com.wangh.springboot_mongodb.model.Person;public interface PersonRepository extends MongoRepository<Person, String> { //1.支持方法名查询 Person findByName(String name); //2.支持@Query查询,查询参数构造JSON字符串即可 @Query("{'age' : ?0}") List<Person> withQueryFindByAge(Integer age);}
7.控制器
package com.wangh.springboot_mongodb.controller;import java.util.ArrayList;import java.util.Collection;import java.util.LinkedList;import java.util.List;import javax.annotation.Resource;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import com.wangh.springboot_mongodb.model.Location;import com.wangh.springboot_mongodb.model.Person;import com.wangh.springboot_mongodb.repository.PersonRepository;@RestControllerpublic class PersonController { @Resource private PersonRepository personRepository; @RequestMapping("/save") public List<Person> save(){ Person p1 = new Person("wanghao", 27); Collection<Location> locations1 = new LinkedList<Location>(); Location l1 = new Location("北京", "2017"); Location l2 = new Location("上海", "2015"); Location l3 = new Location("广州", "2010"); Location l4 = new Location("西安", "2008"); locations1.add(l1); locations1.add(l2); locations1.add(l3); locations1.add(l4); p1.setLocations(locations1); Person p2 = new Person("wangzhen", 27); Collection<Location> locations2 = new LinkedList<Location>(); locations2.add(l1); locations2.add(l2); locations2.add(l3); locations2.add(l4); p2.setLocations(locations2); List<Person> ps = new ArrayList<Person>(); ps.add(p1); ps.add(p2); return personRepository.save(ps); } @RequestMapping("/all") public List<Person> list(){ return personRepository.findAll(); } @RequestMapping("/q1") public Person q1(String name){ return personRepository.findByName(name); } @RequestMapping("/q2") public List<Person> q2(Integer age){ return personRepository.withQueryFindByAge(age); }}
8.测试
访问 http://localhost:8080/all
访问 http://localhost:8080/q1?name=wangtuo
访问 http://localhost:8080/q2?age=21
阅读全文
0 0
- SpringBoot结合MongoDB简单应用
- SpringBoot结合Redis简单应用
- SpringBoot中简单使用mongodb
- 结合MongoDB开发LBS应用
- 结合MongoDB开发LBS应用
- 结合MongoDB开发LBS应用
- 结合MongoDB开发LBS应用
- 结合MongoDB开发LBS应用
- 结合MongoDB开发LBS应用
- 结合MongoDB开发LBS应用
- 结合MongoDB开发LBS应用
- 结合MongoDB开发LBS应用
- 结合MongoDB开发LBS应用
- 结合MongoDB开发LBS应用
- 结合MongoDB开发LBS应用
- 结合MongoDB开发LBS应用
- MongoDB简单应用
- 深入浅出Symfony2 - 结合MongoDB开发LBS应用
- Leetcode67. Add Binary
- Android源码解析四大组件系列(五)---广播的注册过程
- pandas处理字符串1
- CodeForces
- Five-In-a-Row CodeForces
- SpringBoot结合MongoDB简单应用
- Github收藏之awesome-linux
- 在rviz里尝试先锋机器人的导航仿真
- 数据库事务处理
- POJ 3709 K-Anonymous Sequence(斜率DP)
- hpu 瞎子与瘸子的故事 【bfs +优先队列】
- JDBC自定义套件封装
- 大理石在哪(Where is the Marble?, uva 10474)
- .NET Framework 与 .NET Core 的区别与联系