使用 DataStax 驱动程序查询构建

Query Build using DataStax Driver

我的 table 如下,

create table contact( id uuid primary key, 
                      personName text, 
                      updatedTime timestamp
                    );

并尝试执行下面的准备语句,

 String query = "SELECT * FROM CONTACT WHERE personName IN (:personNameList) " +
                "AND updatedTime > ':startTime' AND  updatedTime < :endTime ALLOW FILTERING;";


        SimpleStatement simpleStatement = SimpleStatement.builder(query)
                                            .setConsistencyLevel(DefaultConsistencyLevel.QUORUM)
                                            .build();

        PreparedStatement preparedStatement = cqlSession.prepare(simpleStatement);
        BoundStatement boundStatement = preparedStatement.bind();

        personList = ["John","Alex"];

        boundStatement.setString("startTime", "2020-08-16 14:44:32+0000");  // Issue with setting
        boundStatement.setString("endTime", "2020-08-16 14:60:32+0000");  // Issue with setting
        boundStatement.setList("personNameList", personList, String.class); //  Codec not found for requested operation: [TEXT <-> java.util.List<java.lang.String>]

        ResultSet execute = cqlSession.execute(boundStatement);


        // List<Person> personList = // Mapping

从驱动映射的4.7.2开始,根据我的理解,它是不同类型的映射,我无法从Google中得到答案。有什么建议吗?


 <dependency>
            <groupId>com.datastax.oss</groupId>
            <artifactId>java-driver-mapper-processor</artifactId>
            <version>4.7.2</version>
            <scope>test</scope>
        </dependency>

对象映射器在 Java 驱动程序 4.x 中发生了重大变化,因此它需要 setting up of the compile-time processor to generate auxiliary classes that are necessary for it work. But you can still use it to convert Row object into your POJO, by declaring the method with GetEntry annotation 在您的 DAO 接口中,如下所示:

@Dao
public interface PersonDao {
  @GetEntity
  Person asPerson(Row row);
}

但是如果您打算使用对象映射器,我建议您将它用于所有事情 - 在您的情况下,您可以使用 Query annotation 声明一个方法并传递参数用于绑定。