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

新建一个model

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.springframework.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);
    }

}

你可能感兴趣的:(mongodb,springboot,Java,SpringBoot,mongodb)