简体   繁体   English

Datastax java 驱动程序 4 上的 GenericDao

[英]GenericDao on Datastax java driver 4

Is the following approach to a GenericDao possible with the v4 java driver? v4 java 驱动程序是否可以使用以下 GenericDao 方法? How?如何?

public static <T> List<?> retrieve(Class<?> clazz, BoundStatement boundStatement) throws Exception {

        List<?> tList = new ArrayList<>();
        Result<?> result = null;
        MappingManager manager = null;

        Session session = CassandraUtil.getSession();

        ResultSet resultSet = session.execute(boundStatement);

        int totRows = resultSet.getAvailableWithoutFetching();

        if (totRows > 0) {

            manager = new MappingManager(session);
            Mapper<?> m = manager.mapper(clazz);
            result = (Result<?>) m.map(resultSet);
            tList = result.all();
        }

        return tList;
    }

This approach provided tremendous productivity by using a GenericDao across the entire application.这种方法通过在整个应用程序中使用 GenericDao 提供了巨大的生产力。

I am assessing the changes to move to the V4 of the javadriver but I am hitting the wall as it seems not possible to do the same with the java driver V4.我正在评估迁移到 javadriver V4 的更改,但我碰壁了,因为似乎不可能对 java 驱动程序 V4 做同样的事情。

Thanks谢谢

IPVP IPVP

You can use @GetEntity annotation on DAO to convert Row object into your POJO.您可以在 DAO 上使用@GetEntity注释Row object 转换为您的 POJO。 You can also convert ResultSet into POJO, iterable of POJOs, etc. - depending on return value of the function.您还可以将ResultSet转换为 POJO、POJO 的可迭代等 - 取决于 function 的返回值。 Here is example from documentation:以下是文档中的示例:

@Dao
public interface ProductDao {
  @GetEntity
  Product asProduct(Row row);

  @GetEntity
  PagingIterable<Product> asProducts(ResultSet resultSet);

  // ...
}

See documentation for more details.有关更多详细信息,请参阅文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM