-
-
Save mp911de/3603c9c78d6326f3cc8ea912ceeb1506 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.demo; | |
import java.time.Instant; | |
import java.util.ArrayList; | |
import java.util.Date; | |
import java.util.List; | |
import org.joda.time.DateTime; | |
import org.springframework.boot.CommandLineRunner; | |
import org.springframework.boot.SpringApplication; | |
import org.springframework.boot.autoconfigure.SpringBootApplication; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.core.convert.converter.Converter; | |
import org.springframework.data.cassandra.core.CassandraTemplate; | |
import org.springframework.data.cassandra.core.convert.CassandraCustomConversions; | |
@SpringBootApplication | |
public class DemoApplication { | |
public static void main(String[] args) { | |
SpringApplication.run(DemoApplication.class, args); | |
} | |
@Bean | |
CassandraCustomConversions cassandraCustomConversions() { | |
List<Converter> converters = new ArrayList<>(); | |
converters.add(new InstantToDateTimeConverter()); | |
converters.add(new DateTimeToInstantConverter()); | |
CassandraCustomConversions ccv = new CassandraCustomConversions(converters); | |
return ccv; | |
} | |
class DateTimeToInstantConverter implements Converter<DateTime, java.time.Instant> { | |
@Override | |
public java.time.Instant convert(DateTime source) { | |
return source.toDate().toInstant(); | |
} | |
} | |
class InstantToDateTimeConverter implements Converter<Instant, DateTime> { | |
@Override | |
public DateTime convert(java.time.Instant source) { | |
return new DateTime(Date.from(source)); | |
} | |
} | |
@Bean | |
CommandLineRunner runner(CassandraTemplate cassandraTemplate){ | |
return args -> { | |
List<MyEntity> select = cassandraTemplate.select("SELECT * FROM myentity", MyEntity.class); | |
System.out.println(select); | |
}; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.demo; | |
import org.joda.time.DateTime; | |
import org.springframework.data.annotation.Id; | |
/** | |
* create table myentity (id varchar PRIMARY KEY, ts timestamp); | |
*/ | |
public class MyEntity { | |
@Id String id; | |
DateTime ts; | |
public String getId() { | |
return id; | |
} | |
public void setId(String id) { | |
this.id = id; | |
} | |
public DateTime getTs() { | |
return ts; | |
} | |
public void setTs(DateTime ts) { | |
this.ts = ts; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<parent> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-parent</artifactId> | |
<version>3.2.2</version> | |
<relativePath/> <!-- lookup parent from repository --> | |
</parent> | |
<groupId>com.sample</groupId> | |
<artifactId>demo</artifactId> | |
<version>0.0.1-SNAPSHOT</version> | |
<name>demo</name> | |
<description>demo</description> | |
<properties> | |
<java.version>17</java.version> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-data-cassandra</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-test</artifactId> | |
<scope>test</scope> | |
</dependency> | |
<dependency> | |
<groupId>joda-time</groupId> | |
<artifactId>joda-time</artifactId> | |
<version>2.12.7</version> | |
</dependency> | |
</dependencies> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-maven-plugin</artifactId> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment