使用 Apache Cassandra 设置 SpringData 项目

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

欢迎加入小哈的星球 ,你将获得:专属的项目实战 / 1v1 提问 / Java 学习路线 / 学习打卡 / 每月赠书 / 社群讨论

  • 新项目:《从零手撸:仿小红书(微服务架构)》 正在持续爆肝中,基于 Spring Cloud Alibaba + Spring Boot 3.x + JDK 17...点击查看项目介绍 ;
  • 《从零手撸:前后端分离博客项目(全栈开发)》 2 期已完结,演示链接: http://116.62.199.48/ ;

截止目前, 星球 内专栏累计输出 63w+ 字,讲解图 2808+ 张,还在持续爆肝中.. 后续还会上新更多项目,目标是将 Java 领域典型的项目都整一波,如秒杀系统, 在线商城, IM 即时通讯,权限管理,Spring Cloud Alibaba 微服务等等,已有 2200+ 小伙伴加入学习 ,欢迎点击围观

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

为了跑就跑

相关文章