使用 Apache Cassandra 设置 SpringData 项目

一则或许对你有用的小广告

欢迎加入小哈的星球 ,你将获得:专属的项目实战 / Java 学习路线 / 一对一提问 / 学习打卡/ 赠书活动

目前,正在 星球 内带小伙伴们做第一个项目:全栈前后端分离博客项目,采用技术栈 Spring Boot + Mybatis Plus + Vue 3.x + Vite 4手把手,前端 + 后端全栈开发,从 0 到 1 讲解每个功能点开发步骤,1v1 答疑,陪伴式直到项目上线,目前已更新了 204 小节,累计 32w+ 字,讲解图:1416 张,还在持续爆肝中,后续还会上新更多项目,目标是将 Java 领域典型的项目都整上,如秒杀系统、在线商城、IM 即时通讯、权限管理等等,已有 870+ 小伙伴加入,欢迎点击围观

在这篇文章中,我们将使用 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' }

为了跑就跑