本章我们通过SpringBoot集成mongodb,Java,Kotlin开发一个极简社区文章博客系统。
为什么是mongodb?Mongo 的主要目标是在键/值存储方式(提供了高性能和高度伸缩性)和传统的RDBMS 系统(具有丰富的功能)之间架起一座桥梁,它集两者的优势于一身。Mongo 的BSON 数据格式非常适合文档化格式的存储及查询。[1]
关于nosql和rdbms的对比以及选择,我参考了不少资料,关键一点在于:nosql可以轻易扩展表的列,对于业务快速变化的应用场景非常适合;rdbms则需要安装关系型数据库模式对业务进行建模,适合业务场景已经成熟的系统。我目前的这个项目——dailyReport,我暂时没法确定的是,对于一个report,它的属性应该有哪些:date、title、content、address、images等等,基于此我选择mongodb作为该项目的持久化存储。
11.1 系统基本功能1.支持markdown编辑器
2.写文章,编辑文章,阅读文章基础博客功能
3.文章列表排序,搜索
MacOS Sierra
IDEA 2017.1
JDK 1.8.0_40
mongod-3.2.4
Gradle 3.5-rc-2
开发语言:Java 混合Kotlin语言开发
开发框架:
kotlinVersion = '1.1.0'
SpringBootVersion = '1.5.2.RELEASE'
Spring-data-mongodb
Javascript、html、css
requirejs
jquery
bootstrap
dataTables
meditor
Blog
前后端分层后端Controller
jsp
js
11.4 数据库环境配置1.build.gradle配置
添加mongodb starter
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-data-mongodb')添加mongo-java-driver
compile('org.mongodb:mongo-java-driver:3.4.2')完整配置如下:
group = 'com.restfeel'version = '0.0.1-SNAPSHOT'description = ""apply {
plugin "kotlin"
plugin "kotlin-spring"
plugin "kotlin-jpa"
plugin "org.springframework.boot"
plugin 'java'
plugin 'eclipse'
plugin 'idea'
plugin 'war'
plugin 'maven'}
sourceCompatibility = 1.8
targetCompatibility = 1.8
sourceSets {
main {
kotlin { srcDir "src/main/kotlin" }
java { srcDir "src/main/java" }
} test {
kotlin { srcDir "src/test/kotlin" }
java { srcDir "src/test/java" }
}
}
jar {
baseName = 'restfeel'
version = '0.0.1'}
buildscript {
ext {
kotlinVersion = '1.1.0'
springBootVersion = '1.5.2.RELEASE'
}
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:$springBootVersion"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
classpath "org.jetbrains.kotlin:kotlin-noarg:$kotlinVersion"
classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlinVersion"
}
repositories {
mavenLocal()
mavenCentral()
maven { url "http://oss.jfrog.org/artifactory/oss-release-local" }
maven { url "http://jaspersoft.artifactoryonline.com/jaspersoft/jaspersoft-repo/" }
maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
}
}
dependencies {
compile("org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion")
compile("org.jetbrains.kotlin:kotlin-reflect:$kotlinVersion")
compile("com.fasterxml.jackson.module:jackson-module-kotlin:2.8.4")
compile('org.springframework.boot:spring-boot-starter')
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-data-mongodb')
compile('org.springframework.boot:spring-boot-starter-actuator')
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework.boot:spring-boot-starter-security')
compile('org.springframework.boot:spring-boot-starter-remote-shell')
compile('org.springframework.boot:spring-boot-starter-aop')
providedCompile('org.springframework.boot:spring-boot-starter-tomcat')
compile('javax.servlet:jstl')
providedCompile('org.apache.tomcat.embed:tomcat-embed-jasper')
//thymeleaf
// compile("org.springframework.boot:spring-boot-starter-thymeleaf")
compile('org.hibernate:hibernate-validator:5.1.3.Final')
compile('org.mongodb:mongo-java-driver:3.4.2')
compile('org.hsqldb:hsqldb:2.3.2')
compile('org.apache.httpcomponents:httpclient:4.5.1')
compile('org.apache.httpcomponents:httpmime:4.5.1')
compile('org.apache.commons:commons-lang3:3.3.2')
compile('com.sendgrid:sendgrid-java:2.1.0')
compile('com.ryantenney.metrics:metrics-spring:3.0.0')
compile('net.sf.jasperreports:jasperreports:6.0.0') {
exclude module: 'jdtcore'
exclude module: 'jackson-annotations'
}
compile('com.mangofactory:swagger-springmvc:0.9.4')
compile('org.ajar:swagger-spring-mvc-ui:0.4')
compile('com.google.oauth-client:google-oauth-client:1.19.0')
compile('com.jayway.jsonpath:json-path:2.0.0')
compile('io.swagger:swagger-compat-spec-parser:1.0.12')
compile('org.raml:raml-parser:0.8.12') {
exclude module: 'slf4j-log4j12'
exclude module: 'log4j'
}
testCompile('org.springframework.boot:spring-boot-starter-test')
// https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools
compile group: 'org.springframework.boot', name: 'spring-boot-devtools'}
compileJava {
//options.fork = true
options.incremental = true}
repositories {
mavenLocal()
mavenCentral()
maven { url "http://oss.jfrog.org/artifactory/oss-release-local" }
maven { url "http://jaspersoft.artifactoryonline.com/jaspersoft/jaspersoft-repo/" }
maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
}2.实现AbstractMongoConfiguration类
package com.restfeel.configimport com.mongodb.Mongoimport com.mongodb.MongoClientimport com.mongodb.MongoCredentialimport com.mongodb.ServerAddressimport org.springframework.beans.factory.annotation.Autowiredimport org.springframework.context.annotation.Beanimport org.springframework.context.annotation.Configurationimport org.springframework.core.env.Environmentimport org.springframework.data.mongodb.config.AbstractMongoConfigurationimport org.springframework.data.mongodb.repository.config.EnableMongoRepositories@Configuration
@EnableMongoRepositories(*arrayOf("com.restfeel.dao", "com.restfeel.service"))class PersistenceConfig : AbstractMongoConfiguration() {
@Autowired private val env: Environment? = null
override fun getDatabaseName(): String { return env!!.getProperty("mongodb.name")
}
@Bean
@Throws(Exception::class)
override fun mongo(): Mongo { return MongoClient(listOf(ServerAddress(env!!.getProperty("mongodb.host"), env!!.getProperty("mongodb.port", Int::class.java))),
listOf(MongoCredential
.createCredential(env!!.getProperty("mongodb.username"), env!!.getProperty("mongodb.name"),
env!!.getProperty("mongodb.password").toCharArray())))
}// override fun getMappingbasePackage(): String {// return "com.restfiddle.dao"// }
override fun getMappingbasePackages(): Collection { return setOf("com.restfeel.dao", "com.restfeel.service")
}
}
作者:东海陈光剑
链接:https://www.jianshu.com/p/33aa23a085a8



