Jdbi: 记录 SqlObject mixins 以及如何使用它们

创建于 2016-12-16  ·  5评论  ·  资料来源: jdbi/jdbi

缺乏这方面的文件让我想知道

最有用的评论

@Shujito

mixin 是一个接口,您可以在 SQL 对象类型中实现/扩展它,以向 SQL 对象添加额外的方法。 有两个 mixin, GetHandleTransactional

GetHandle提供了一个方法Handle getHandle() 。 如果您有一个比注​​释更复杂的查询(例如,带有折叠的多表连接),那么实现/扩展GetHandle ,并使您的 SQL 方法成为具体/默认方法。 在方法内部,您可以调用getHandle()来获取与 SQL 对象关联的Handle

interface ContactDao extends GetHandle {
  default List<Contact> getFullContacts() {
    return getHandle().createQuery(
        "select * from contact c left join phones p on c.id = p.contactId")
        .fold(new LinkedHashMap<Integer,Contact>(), (map, rs, ctx) -> {
          // etc
        })
        .values()
        .stream()
        .collect(toList());
}

Transactional提供了几种管理事务的方法:

interface ContactDao extends Transactional {
  default void createFullContact(Contact contact) {
    begin();
    try {
      int contactId = createContact(contact.getName());
      createPhones(contact.getPhones());
      commit();
    }
    catch (Exception e) {
      rollback();
    }
  }

  @SqlUpdate("insert into contact(id, name) values (nextval('contact_seq'), :name)")
  <strong i="20">@GetGeneratedKeys</strong>
  int createContact(<strong i="21">@BindBean</strong> Contact contact);

  @SqlBatch("insert into phone(id, contact_id, phone_type, phone_number) " +
      "values (nextval('phone_seq'), :contactId, :phoneType, :phoneNumber)")
  void createPhones(<strong i="22">@BindBean</strong> List<Phone> phones);
}

所有5条评论

@Shujito

mixin 是一个接口,您可以在 SQL 对象类型中实现/扩展它,以向 SQL 对象添加额外的方法。 有两个 mixin, GetHandleTransactional

GetHandle提供了一个方法Handle getHandle() 。 如果您有一个比注​​释更复杂的查询(例如,带有折叠的多表连接),那么实现/扩展GetHandle ,并使您的 SQL 方法成为具体/默认方法。 在方法内部,您可以调用getHandle()来获取与 SQL 对象关联的Handle

interface ContactDao extends GetHandle {
  default List<Contact> getFullContacts() {
    return getHandle().createQuery(
        "select * from contact c left join phones p on c.id = p.contactId")
        .fold(new LinkedHashMap<Integer,Contact>(), (map, rs, ctx) -> {
          // etc
        })
        .values()
        .stream()
        .collect(toList());
}

Transactional提供了几种管理事务的方法:

interface ContactDao extends Transactional {
  default void createFullContact(Contact contact) {
    begin();
    try {
      int contactId = createContact(contact.getName());
      createPhones(contact.getPhones());
      commit();
    }
    catch (Exception e) {
      rollback();
    }
  }

  @SqlUpdate("insert into contact(id, name) values (nextval('contact_seq'), :name)")
  <strong i="20">@GetGeneratedKeys</strong>
  int createContact(<strong i="21">@BindBean</strong> Contact contact);

  @SqlBatch("insert into phone(id, contact_id, phone_type, phone_number) " +
      "values (nextval('phone_seq'), :contactId, :phoneType, :phoneNumber)")
  void createPhones(<strong i="22">@BindBean</strong> List<Phone> phones);
}

所以,有了这些,我就可以查询更复杂的对象。
对于这里的这个例子,我可以获得联系人以及他们拥有的尽可能多的电话,对吧?

就是这个想法,是的。 不过,这完全取决于您如何制定查询。

现在一切都清楚了,我会在看到需要时立即滥用此功能。
顺便在这里填一下就好了:http: //jdbi.org/sql_object_mixins/
谢谢

在 #802 中跟踪

此页面是否有帮助?
0 / 5 - 0 等级