Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Developer guide: implementing a generic DAO interface #825

Closed
qualidafial opened this issue Jun 15, 2017 · 1 comment
Closed

Developer guide: implementing a generic DAO interface #825

qualidafial opened this issue Jun 15, 2017 · 1 comment
Labels
Milestone

Comments

@qualidafial
Copy link
Member

qualidafial commented Jun 15, 2017

package com.app;

public interface CrudDao<T, ID> {
    @SqlUpdate
    @UseClasspathSqlLocator
    void insert(T entity);

    @SqlQuery
    @UseClasspathSqlLocator
    T getById(ID id);

    @SqlQuery
    @UseClasspathSqlLocator
    List<T> list();

    @SqlUpdate
    @UseClasspathSqlLocator
    void update(T entity);

    @SqlUpdate
    @UseClasspathSqlLocator
    void delete(ID id);
}

@RegisterBeanMapper(Contact.class)
public interface ContactDao extends CrudDao<Contact, Long> {}

@RegisterBeanMapper(Account.class)
public interface AccountDao extends CrudDao<Account, UUID> {}

In this example, SQL is loaded from files on the classpath, depending on the method called and which DAO subclass is used.

  • com/app/ContactDao/insert.sql
  • com/app/ContactDao/getById.sql
  • com/app/ContactDao/list.sql
  • com/app/ContactDao/update.sql
  • com/app/ContactDao/delete.sql
  • com/app/AccountDao/insert.sql
  • com/app/AccountDao/getById.sql
  • com/app/AccountDao/list.sql
  • com/app/AccountDao/update.sql
  • com/app/AccountDao/delete.sql

Note that @UseClasspathSqlLocator is repeated on every method instead of once on the CrudDao type. This is because right now, we only look for annotations on the SQL object type itself, and not supertypes (see #438). If a DAO subtype overrides a method from a supertype, we only look at the annotations present in the subtype.

In the future we may change this to look for annotations in the supertype, in which case we can update this example.

@qualidafial qualidafial added this to the JDBI 3 Post-release (non-blocker) milestone Jun 15, 2017
@qualidafial
Copy link
Member Author

Tracked in #956

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

1 participant