Skip to content
Zsolt Herpai edited this page Feb 7, 2015 · 6 revisions

###Select queries####

query.select(sqlQuery)...

####Parameters#### Both positional and named parameters are supported

query
    .select("SELECT * FROM CUSTOMER WHERE NAME = ?")
    .params("John Doe")
    ...
Map<String, Object> params = ...
query
    .select("SELECT * FROM CUSTOMER WHERE NAME = :name")
    .namedParams(params)
    ...

####Query customization####

query
    .select("SELECT * FROM CUSTOMER WHERE NAME = :name")
    .namedParams(params)
    .fetchSize(50)
    .maxRows(200)
    ...

####Results#### #####Mappers##### All result rows of a select query will be mapped to single java object by a Mapper. Mappers can be implemented manually

Mapper<Customer> manualCustomerMapper = resultSet -> {
	return new Customer(resultSet.getString("NAME"));
}

Or generated based on object field name / column name matching (case-insensitive, ignoring '_')

ObjectMappers objectMappers = ObjectMappers.builder().build();
...
Mapper<Customer> generatedCustomerMapper = objectMappers.forClass(Customer.class);

Note: some convenience mappers can be found #####List#####

List<Customer> customers = query
    .select("SELECT * FROM CUSTOMER WHERE NAME = ?")
    .params("John Doe")
    .listResult(customerMapper);

#####Set#####

Set<Customer> customers = query
    .select("SELECT * FROM CUSTOMER WHERE NAME = ?")
    .params("John Doe")
    .setResult(customerMapper);

#####Single result##### There must be a result.

Long count = query
    .select("SELECT COUNT(*) FROM CUSTOMER WHERE NAME = ?")
    .params("John Doe")
    .singleResult(Mappers.singleLong());

#####First result##### May not have a result

Optional<Customer> customer = query
    .select("SELECT * FROM CUSTOMER WHERE NAME = ?")
    .params("John Doe")
    .firstResult(customerMapper);

#####Consuming large results#####

query
    .select("SELECT * FROM CUSTOMER")
    .iterateResult(customerMapper, (customer) -> {
        // do something with the customer
    });

#####Filter results in java#####

List<Customer> customer = query
    .select("SELECT * FROM CUSTOMER WHERE NAME = ?")
    .params("John Doe")
    .filter(customer::isActive)
    .listResult(customerMapper);

Note: results will be fetched from the DB, this should be used conciously.

Clone this wiki locally