使用DataStax驱动程序进行查询构建

问题描述

我的桌子如下,

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

并尝试执行以下prepare语句,

 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来生成其工作所需的辅助类。但是您仍然可以使用它通过在DAO接口中用Row annotation声明方法来将GetEntry对象转换为POJO,如下所示:

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

但是,如果您要使用对象Mapper,我建议将其用于所有内容-在您的情况下,您可以使用Query annotation声明一个方法,并传递绑定参数

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...