上一篇:MongoDB(二)备份恢复、导入导出、主从复制、副本集集群、分片存储.
一、启动MongoDB
1、创建文件夹:D:\Java\mongo_db
2、启动mongodb
//
mongod --dbpath D:\Java\mongo_db
二、新建项目及配置(MongoRepository方式)
- 1、新建如下项目:
- 2、pom依赖
<?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.mongo</groupId>
<artifactId>mongodb_demo</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
</dependencies>
</project>
- 3、配置application.properties
#端口号
server.port=8001
#上下文路径
server.servlet.context-path=/mongo_demo
#mongodb数据库配置
spring.data.mongodb.uri=mongodb://127.0.0.1:27017/test1
- 4、创建DemoApplication、RepositoryController、UserEntity、MongoUserDAO继承MongoRepository
DemoApplication启动类:
package com.mongodb.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
UserEntity:
package com.mongodb.demo.entity;
import org.springframework.data.mongodb.core.mapping.Document;
@Document(collection = "user")// 指定对应的集合名称,如果不存在会自动创建
public class UserEntity {
private String _id;
private String name;
private Integer age;
public String get_id() {
return _id;
}
public void set_id(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;
}
@Override
public String toString() {
return "UserEntity{" +
"_id='" + _id + '\'' +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
MongoUserDAO :
package com.mongodb.demo.dao;
import com.mongodb.demo.entity.UserEntity;
import org.springframework.data.mongodb.repository.MongoRepository;
public interface MongoUserDAO extends MongoRepository<UserEntity,String> {
}
RepositoryController:
package com.mongodb.demo.controller;
import com.mongodb.demo.dao.MongoUserDAO;
import com.mongodb.demo.entity.UserEntity;
import org.springframework.data.domain.Example;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.Optional;
@RestController
@RequestMapping("/repository")
public class RepositoryController {
@Resource
private MongoUserDAO mongoUserDAO;
@RequestMapping("/save")
public String save(){
UserEntity userEntity = new UserEntity();
userEntity.setName("李四");
userEntity.setAge(18);
UserEntity user = mongoUserDAO.save(userEntity);
System.out.println(user);
return "success";
}
@RequestMapping("/findById")
public String findById(String id){
Optional<UserEntity> op = mongoUserDAO.findById(id);
System.out.println(op.get());
return "success";
}
@RequestMapping("/findOne")
public String findOne(String name){
UserEntity user = new UserEntity();
user.setName(name);
Example<UserEntity> example = Example.of(user);
Optional<UserEntity> one = mongoUserDAO.findOne(example);
System.out.println(one.get());
return "success";
}
}
- 5、启动项目测试
调用保存接口成功:
连接数据库查询数据,数据已经保存到数据库
三、MongoTemplate方式
创建MongoTemplate测试:
package com.mongodb.demo.controller;
import com.mongodb.demo.entity.UserEntity;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
@RequestMapping("/template")
public class MongoTemplateController {
@Resource
private MongoTemplate mongoTemplate;
@RequestMapping("/save")
public String save(){
UserEntity userEntity = new UserEntity();
userEntity.setName("王五");
userEntity.setAge(20);
mongoTemplate.save(userEntity);
return "success";
}
@RequestMapping("/findById")
public String findById(String id){
UserEntity userEntity = mongoTemplate.findById(id, UserEntity.class);
System.out.println(userEntity);
return "success";
}
@RequestMapping("/findOne")
public String findOne(String name){
Query query = new Query(Criteria.where("name").is(name));
UserEntity userEntity = mongoTemplate.findOne(query, UserEntity.class);
System.out.println(userEntity);
return "success";
}
}