springboot 단순 통합 mongodb

pom 파일 spring - boot - starter - data - mongodb 도입

<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.0modelVersion>

    <artifactId>spring-data-mongoartifactId>
    <version>1.0-SNAPSHOTversion>
    <packaging>jarpackaging>
    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <java.version>1.8java.version>
        <file.encoding>UTF8file.encoding>
        <spring-cloud.version>Finchley.SR2spring-cloud.version>
    properties>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.0.2.RELEASEversion>
    parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-data-mongodbartifactId>
        dependency>
    dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>${spring-cloud.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>

project>

application. properties 설정 파일 은 다음 과 같 습 니 다.
server.port=8081

#mongodb
spring.data.mongodb.host=localhost
spring.data.mongodb.port=17117
spring.data.mongodb.database=admin
spring.data.mongodb.username=username
spring.data.mongodb.password=password

새 모델
package top.littlematch.mongo.model;

/**
 * @ Description:${description}
 * @ Author     :Match
 * @ Date       :   18:07 2018/12/5 0005
 * @Version: $version$
 */
public class Hello {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}


controller 에 org. spring from work. data. mongodb. core 패 키 지 를 도입 합 니 다.
MongoTemplate 로 간단 한 조작 을 하 겠 습 니 다.
package top.littlematch.mongo.controller;

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.data.mongodb.core.query.Update;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import top.littlematch.mongo.model.Hello;

import javax.annotation.Resource;

/**
 * @ Description:${description}
 * @ Author     :Match
 * @ Date       :   18:05 2018/12/5 0005
 * @Version: $version$
 */
@RestController
@RequestMapping("hello")
public class HelloController {
    /**
     *   mongoTemplate   mongodb
     */
    @Resource
    private MongoTemplate mongoTemplate;

    /**
     * mongodb  
     */
    final static String HELLO = "HELLO";

    /**
     *     
     *
     * @return
     */
    @RequestMapping("/insert")
    public void insertHello(@RequestParam(name = "name") String name) {
        Hello hello = new Hello();
        hello.setName(name);
        mongoTemplate.insert(hello, HELLO);
    }

    /**
     *     
     *
     * @return
     */
    @RequestMapping("/get")
    public Hello getHello(@RequestParam(name = "name") String name) {
        Query query = new Query();
        query.addCriteria(Criteria.where("name").is(name));
        return mongoTemplate.findOne(query, Hello.class, HELLO);
    }

    /**
     *     
     *
     * @return
     */
    @RequestMapping("/update")
    public void updateHello(@RequestParam(name = "name") String name) {
        Query query = new Query();
        query.addCriteria(Criteria.where("name").is(name));
        Hello hello = new Hello();
        hello.setName(name);
        Update update = new Update();
        update.set("name", name);
        mongoTemplate.updateFirst(query, update, Hello.class, HELLO);
    }

    /**
     *     
     *
     * @return
     */
    @RequestMapping("/delete")
    public void deleteHello(@RequestParam(name = "name") String name) {
        Query query = new Query();
        query.addCriteria(Criteria.where("name").is(name));
        mongoTemplate.remove(query, Hello.class, HELLO);
    }

}


시작 하면 됩 니 다.
package top.littlematch.mongo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @ Description:${description}
 * @ Author     :Match
 * @ Date       :   15:45 2018/12/1 0001
 * @Version: $version$
 */

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

좋은 웹페이지 즐겨찾기