在这篇文章中,我们将使用 Gradle 和 spring boot 来创建一个集成了 spring-mvc 和 Apache Cassandra 数据库的项目。
首先,我们将从我们的 Gradle 配置开始:
group 'com.gkatzioura'
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.5.RELEASE")
}
}
jar {
baseName = 'gs-serving-web-content'
version = '0.1.0'
}
repositories {
mavenCentral()
}
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile "org.springframework.boot:spring-boot-starter-thymeleaf"
compile "org.springframework.data:spring-data-cassandra:1.2.2.RELEASE"
compile 'org.slf4j:slf4j-api:1.6.6'
compile 'ch.qos.logback:logback-classic:1.0.13'
testCompile "junit:junit"
}
task wrapper(type: Wrapper) {
gradleVersion = '2.3'
}
我们将在我们的 Cassandra 数据库上创建键空间和表:
group 'com.gkatzioura'
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.5.RELEASE")
}
}
jar {
baseName = 'gs-serving-web-content'
version = '0.1.0'
}
repositories {
mavenCentral()
}
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile "org.springframework.boot:spring-boot-starter-thymeleaf"
compile "org.springframework.data:spring-data-cassandra:1.2.2.RELEASE"
compile 'org.slf4j:slf4j-api:1.6.6'
compile 'ch.qos.logback:logback-classic:1.0.13'
testCompile "junit:junit"
}
task wrapper(type: Wrapper) {
gradleVersion = '2.3'
}
我们可以使用 cqlsh 运行包含 cql 语句的文件:
group 'com.gkatzioura'
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.5.RELEASE")
}
}
jar {
baseName = 'gs-serving-web-content'
version = '0.1.0'
}
repositories {
mavenCentral()
}
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile "org.springframework.boot:spring-boot-starter-thymeleaf"
compile "org.springframework.data:spring-data-cassandra:1.2.2.RELEASE"
compile 'org.slf4j:slf4j-api:1.6.6'
compile 'ch.qos.logback:logback-classic:1.0.13'
testCompile "junit:junit"
}
task wrapper(type: Wrapper) {
gradleVersion = '2.3'
}
Cassandra 连接信息将驻留在 META-INF/cassandra.properties 中:
group 'com.gkatzioura'
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.5.RELEASE")
}
}
jar {
baseName = 'gs-serving-web-content'
version = '0.1.0'
}
repositories {
mavenCentral()
}
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile "org.springframework.boot:spring-boot-starter-thymeleaf"
compile "org.springframework.data:spring-data-cassandra:1.2.2.RELEASE"
compile 'org.slf4j:slf4j-api:1.6.6'
compile 'ch.qos.logback:logback-classic:1.0.13'
testCompile "junit:junit"
}
task wrapper(type: Wrapper) {
gradleVersion = '2.3'
}
现在我们可以使用 spring 注释继续进行 Cassandra 配置。
group 'com.gkatzioura'
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.5.RELEASE")
}
}
jar {
baseName = 'gs-serving-web-content'
version = '0.1.0'
}
repositories {
mavenCentral()
}
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile "org.springframework.boot:spring-boot-starter-thymeleaf"
compile "org.springframework.data:spring-data-cassandra:1.2.2.RELEASE"
compile 'org.slf4j:slf4j-api:1.6.6'
compile 'ch.qos.logback:logback-classic:1.0.13'
testCompile "junit:junit"
}
task wrapper(type: Wrapper) {
gradleVersion = '2.3'
}
然后我们创建 Greeting 实体。
group 'com.gkatzioura'
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.5.RELEASE")
}
}
jar {
baseName = 'gs-serving-web-content'
version = '0.1.0'
}
repositories {
mavenCentral()
}
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile "org.springframework.boot:spring-boot-starter-thymeleaf"
compile "org.springframework.data:spring-data-cassandra:1.2.2.RELEASE"
compile 'org.slf4j:slf4j-api:1.6.6'
compile 'ch.qos.logback:logback-classic:1.0.13'
testCompile "junit:junit"
}
task wrapper(type: Wrapper) {
gradleVersion = '2.3'
}
为了访问数据,应该创建一个存储库。在我们的例子中,我们将通过添加一些查询来向存储库添加一些额外的功能。
group 'com.gkatzioura'
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.5.RELEASE")
}
}
jar {
baseName = 'gs-serving-web-content'
version = '0.1.0'
}
repositories {
mavenCentral()
}
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile "org.springframework.boot:spring-boot-starter-thymeleaf"
compile "org.springframework.data:spring-data-cassandra:1.2.2.RELEASE"
compile 'org.slf4j:slf4j-api:1.6.6'
compile 'ch.qos.logback:logback-classic:1.0.13'
testCompile "junit:junit"
}
task wrapper(type: Wrapper) {
gradleVersion = '2.3'
}
现在我们可以实现控制器以便通过 http 访问数据。
通过邮寄,我们可以保存一个 Greeting 实体。
通过 get 我们可以获取所有收到的问候语。
通过指定用户,我们可以使用 Cassandra 查询来获取特定用户的问候语。
group 'com.gkatzioura'
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.5.RELEASE")
}
}
jar {
baseName = 'gs-serving-web-content'
version = '0.1.0'
}
repositories {
mavenCentral()
}
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile "org.springframework.boot:spring-boot-starter-thymeleaf"
compile "org.springframework.data:spring-data-cassandra:1.2.2.RELEASE"
compile 'org.slf4j:slf4j-api:1.6.6'
compile 'ch.qos.logback:logback-classic:1.0.13'
testCompile "junit:junit"
}
task wrapper(type: Wrapper) {
gradleVersion = '2.3'
}
最后但同样重要的是我们的应用程序类
group 'com.gkatzioura'
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.5.RELEASE")
}
}
jar {
baseName = 'gs-serving-web-content'
version = '0.1.0'
}
repositories {
mavenCentral()
}
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile "org.springframework.boot:spring-boot-starter-thymeleaf"
compile "org.springframework.data:spring-data-cassandra:1.2.2.RELEASE"
compile 'org.slf4j:slf4j-api:1.6.6'
compile 'ch.qos.logback:logback-classic:1.0.13'
testCompile "junit:junit"
}
task wrapper(type: Wrapper) {
gradleVersion = '2.3'
}
为了跑就跑