Skip to content

Proxy Gates

ZorT edited this page Apr 18, 2023 · 11 revisions

This is one of the latest and best things you can do with this library. You can create gate for queries so you will have prepared settings for queries on methods and then you can only run those methods to invoke queries.

Create gate structure

@Table("users")
public interface UsersGate {

    @Save
    QueryResult saveUser(User user); // Upsert

    @Select
    @Where(@Where.Condition(column = "firstname", value = "{First Name}"))
    Optional<User> getUser(@Placeholder("First Name") String firstName);

    @Select
    @Limit(10)
    List<User> selectFirstTen();

    @Delete
    void deleteAll();
}

The entity model needs to follow constructor requirements, as stated in Mapping to Objects

Create gate instance and make queries

UsersGate gate = connection.createGate(UsersGate.class); // Save this somewhere for repeated usage
QueryResult result = gate.saveUser(new User("John", "Doe"));
User user = gate.getUser("John").orElse(null);
Dynamically set table name (Optional)
UsersGate gate = connection.createGate(UsersGate.class, new StatementMappingOptions.Builder()
    .table("users")
    .build());

Clone this wiki locally