栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

MongoDB(三)SpringBoot整合MongoDB,使用MongoRepository和MongoTemplate

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

MongoDB(三)SpringBoot整合MongoDB,使用MongoRepository和MongoTemplate

上一篇:MongoDB(二)备份恢复、导入导出、主从复制、副本集集群、分片存储.

一、启动MongoDB

1、创建文件夹: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方式

创建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";
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/462827.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号