@Target(value=METHOD) @Retention(value=RUNTIME) public @interface DaoFactory
Mapper
interface.
Example:
@Mapper public interface InventoryMapper { @DaoFactory ProductDao productDao(); }The return type of the method must be a
Dao
-annotated interface.
If the method takes no arguments, the DAO operates on the session's default keyspace (assuming that one was set), and the entity's default table:
// Example 1: the session has a default keyspace CqlSession session = CqlSession.builder().withKeyspace("test").build(); InventoryMapper inventoryMapper = new InventoryMapperBuilder(session).build(); ProductDao dao = inventoryMapper.productDao(); Product product = dao.selectById(1); // => success (selects from test.product) // Example 2: the session has no default keyspace CqlSession session = CqlSession.builder().build(); InventoryMapper inventoryMapper = new InventoryMapperBuilder(session).build(); ProductDao dao = inventoryMapper.productDao(); Product product = dao.selectById(1); // => CQL error (No keyspace has been specified. USE a keyspace, or explicitly specify keyspace.tablename)You can also have the method take the keyspace and table as arguments (annotated respectively with
DaoKeyspace
and DaoTable
):
@Mapper public interface InventoryMapper { @DaoFactory ProductDao productDao(@DaoKeyspace String keyspace); @DaoFactory ProductDao productDao(@DaoKeyspace String keyspace, @DaoTable String table); }This allows you to reuse the same DAO interface to operate on different tables:
ProductDao dao1 = inventoryMapper.productDao("keyspace1"); Product product = dao1.selectById(1); // selects from keyspace1.product ProductDao dao2 = inventoryMapper.productDao("keyspace2"); Product product = dao2.selectById(1); // selects from keyspace2.product ProductDao dao3 = inventoryMapper.productDao("keyspace3", "table3"); Product product = dao3.selectById(1); // selects from keyspace3.table3In all cases, DAO instances are initialized lazily and cached for future calls:
ProductDao dao1 = inventoryMapper.productDao("keyspace1", "product"); ProductDao dao2 = inventoryMapper.productDao("keyspace1", "product"); assert dao1 == dao2; // same arguments, same instanceNote that the cache is a simple map with no eviction mechanism.
Copyright © 2017–2019. All rights reserved.