【问题标题】:Query Build using DataStax Driver使用 DataStax 驱动程序构建查询
【发布时间】:2020-12-07 10:30:40
【问题描述】:

我的桌子如下,

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 开始,根据我的理解,它与映射的类型不同,我无法从谷歌得到答案。有什么建议吗?


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

【问题讨论】:

    标签: datastax datastax-java-driver


    【解决方案1】:

    对象映射器在 Java 驱动程序 4.x 中发生了显着变化,因此它需要setting up of the compile-time processor 来生成它工作所必需的辅助类。但是您仍然可以使用它将Row 对象转换为您的POJO,方法是在您的DAO 接口中使用GetEntry annotation 声明方法,如下所示:

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

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

    【讨论】:

      猜你喜欢
      • 2014-03-31
      • 2017-08-17
      • 2020-11-23
      • 2023-03-21
      • 2017-01-02
      • 2018-09-26
      • 2013-09-09
      • 2016-09-21
      • 2017-03-08
      相关资源
      最近更新 更多