上一篇:MongoDB(二)备份恢复、导入导出、主从复制、副本集集群、分片存储.
一、启动MongoDB1、创建文件夹:D:Javamongo_db
2、启动mongodb
// mongod --dbpath D:Javamongo_db二、新建项目及配置(MongoRepository方式)
- 1、新建如下项目:
- 2、pom依赖
4.0.0 com.mongo mongodb_demo 1.0-SNAPSHOT org.springframework.boot spring-boot-starter-parent 2.0.3.RELEASE org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-data-mongodb org.apache.commons commons-lang3 3.4
- 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{ }
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 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 example = Example.of(user);
Optional one = mongoUserDAO.findOne(example);
System.out.println(one.get());
return "success";
}
}
- 5、启动项目测试
调用保存接口成功:
连接数据库查询数据,数据已经保存到数据库
创建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";
}
}



