SpringBoot整合MongoDB(1)

SpringBoot整合MongoDB(1)

springboot整合mongodb

gradle配置

buildscript {
    ext {
        springBootVersion = '2.0.0.RC2'
    }
    repositories {
        mavenCentral()
        maven { url "https://repo.spring.io/snapshot" }
        maven { url "https://repo.spring.io/milestone" }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group = 'com.mongo'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
    maven { url "https://repo.spring.io/snapshot" }
    maven { url "https://repo.spring.io/milestone" }
}


dependencies {
    compile('org.springframework.boot:spring-boot-starter-data-mongodb')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

mongodb自动配置

启动类

测试类

先来感受一下

总结

有多种方式:

  1. 使用原生的驱动api,也就是使用内嵌mongodb

  2. 使用springboot-data-jpa

    2-1. 使用extends MongoRepository

    2-2. 使用mongoTemplate

    2-3. 使用@query注解

Last updated

Was this helpful?