org.jdbi.v3.core.statement.Query.map()方法的使用及代码示例

x33g5p2x  于2022-01-28 转载在 其他  
字(8.8k)|赞(0)|评价(0)|浏览(119)

本文整理了Java中org.jdbi.v3.core.statement.Query.map方法的一些代码示例,展示了Query.map的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Query.map方法的具体详情如下:
包路径:org.jdbi.v3.core.statement.Query
类名称:Query
方法名:map

Query.map介绍

暂无

代码示例

代码示例来源:origin: jdbi/jdbi

@Test
public void inlineRowMapper() {
  // tag::inlineRowMapper[]
  List<User> users = handle.createQuery("SELECT id, name FROM user ORDER BY id ASC")
      .map((rs, ctx) -> new User(rs.getInt("id"), rs.getString("name")))
      .list();
  // end::inlineRowMapper[]
  assertThat(users).hasSize(4);
  assertThat(users.get(3).name).isEqualTo("Data");
}

代码示例来源:origin: jdbi/jdbi

@Override
void configureReturner(Query q, SqlObjectStatementConfiguration cfg) {
  UseRowMapper useRowMapper = getMethod().getAnnotation(UseRowMapper.class);
  UseRowReducer useRowReducer = getMethod().getAnnotation(UseRowReducer.class);
  if (useRowReducer != null && useRowMapper != null) {
    throw new IllegalStateException("Cannot declare @UseRowMapper and @UseRowReducer on the same method.");
  }
  cfg.setReturner(() -> {
    StatementContext ctx = q.getContext();
    QualifiedType<?> elementType = magic.elementType(ctx);
    if (useRowReducer != null) {
      return magic.reducedResult(q.reduceRows(rowReducerFor(useRowReducer)), ctx);
    }
    ResultIterable<?> iterable = useRowMapper == null
        ? q.mapTo(elementType)
        : q.map(rowMapperFor(useRowMapper));
    return magic.mappedResult(iterable, ctx);
  });
}

代码示例来源:origin: jdbi/jdbi

@Test
  public void testConcurrentUpdateableResultSet() {
    handle.execute("create table something (id identity primary key, name varchar(50))");
    handle.execute("insert into something (id, name) values (7, 'Tim')");
    handle.createQuery("select id, name from something where id = :id")
        .bind("id", 7)
        .concurrentUpdatable()
        .map((r, ctx) -> {
          r.updateString("name", "Tom");
          r.updateRow();
          return null;
        }).list();

    final String name = handle.createQuery("select name from something where id = :id")
        .bind("id", 7)
        .mapTo(String.class)
        .findOnly();

    assertThat(name).isEqualTo("Tom");
  }
}

代码示例来源:origin: jdbi/jdbi

@Test
public void rowMapper() {
  // tag::rowMapper[]
  List<User> users = handle.createQuery("SELECT id, name FROM user ORDER BY id ASC")
    .map(new UserMapper())
    .list();
  // end::rowMapper[]
  assertThat(users).hasSize(4);
  assertThat(users.get(3).name).isEqualTo("Data");
}

代码示例来源:origin: jdbi/jdbi

@Test
public void testCartesianProduct() {
  Multimap<User, Article> product = HashMultimap.create();
  h.createQuery("SELECT * FROM user, article")
    .map(JoinRowMapper.forTypes(User.class, Article.class))
    .forEach(jr -> product.put(jr.get(User.class), jr.get(Article.class)));
  Multimap<User, Article> expected = HashMultimap.create();
  IntStream.rangeClosed(1, 3).forEach(u ->
    IntStream.rangeClosed(1, 3).forEach(a ->
      expected.put(u(u), a(a))));
  assertThat(product).isEqualTo(expected);
}

代码示例来源:origin: jdbi/jdbi

public Optional<User> findUserById(long id) {
  RowMapper<User> userMapper =
      (rs, ctx) -> new User(rs.getInt("id"), rs.getString("name"));
  return handle.createQuery("SELECT * FROM user WHERE id=:id")
    .bind("id", id)
    .map(userMapper)
    .findFirst();
}
// end::headlineExample[]

代码示例来源:origin: prestodb/presto

public MaterializedResult execute(Session session, @Language("SQL") String sql, List<? extends Type> resultTypes)
{
  MaterializedResult materializedRows = new MaterializedResult(
      handle.setSqlParser(new RawSqlParser())
          .setTemplateEngine((template, context) -> template)
          .createQuery(sql)
          .map(rowMapper(resultTypes))
          .list(),
      resultTypes);
  return materializedRows;
}

代码示例来源:origin: jdbi/jdbi

@Test
public void testColumnNameAnnotation() {
  Handle handle = dbRule.getSharedHandle();
  handle.execute("insert into something (id, name) values (1, 'foo')");
  ColumnNameThing thing = handle.createQuery("select * from something")
      .map(FieldMapper.of(ColumnNameThing.class))
      .findOnly();
  assertThat(thing.i).isEqualTo(1);
  assertThat(thing.s).isEqualTo("foo");
}

代码示例来源:origin: jdbi/jdbi

private Optional<IdValue> select() {
  return dbRule.getJdbi().withHandle(
    h -> h.createQuery("SELECT id, value FROM test")
      .map((rs, ctx) -> new IdValue(rs.getLong("id"), rs.getString("value")))
      .findFirst());
}

代码示例来源:origin: jdbi/jdbi

@Test
public void testJoin() {
  // tag::multimap[]
  Multimap<User, Article> joined = HashMultimap.create();
  h.createQuery("SELECT * FROM user NATURAL JOIN author NATURAL JOIN article")
    .map(JoinRowMapper.forTypes(User.class, Article.class))
    .forEach(jr -> joined.put(jr.get(User.class), jr.get(Article.class)));
  // end::multimap[]
  assertThat(joined).isEqualTo(getExpected());
}

代码示例来源:origin: jdbi/jdbi

@Test
public void testMapper() {
  h.execute("insert into something (id, name) values (1, 'eric')");
  h.execute("insert into something (id, name) values (2, 'brian')");
  ResultIterable<String> query = h.createQuery("select name from something order by id").map((r, ctx) -> r.getString(1));
  List<String> r = query.list();
  assertThat(r).startsWith("eric");
}

代码示例来源:origin: jdbi/jdbi

@Test
public void testFold() {
  h.prepareBatch("insert into something (id, name) values (?, ?)")
   .add(1, "Brian")
   .add(2, "Keith")
   .execute();
  Map<String, Integer> rs = h.createQuery("select id, name from something")
      .<Entry<String, Integer>>map((r, ctx) -> Maps.immutableEntry(r.getString("name"), r.getInt("id")))
      .collect(toMap(Entry::getKey, Entry::getValue));
  assertThat(rs).containsOnly(entry("Brian", 1), entry("Keith", 2));
}

代码示例来源:origin: jdbi/jdbi

@Test
public void testMapOptional() {
  final Handle h = db.getSharedHandle();
  h.execute("insert into something(intValue, name) values(1, 'Duke')");
  h.execute("insert into something(intValue, name) values(null, null)");
  assertThat(h.createQuery("select * from something order by id")
      .map(ConstructorMapper.of(OptionalBean.class))
      .list())
    .extracting("intValue", "name")
    .containsExactly(
      tuple(OptionalInt.of(1), Optional.of("Duke")),
      tuple(OptionalInt.empty(), Optional.empty()));
}

代码示例来源:origin: jdbi/jdbi

@Test
public void registerFactory() {
  TestDao dao = handle.attach(TestDao.class);
  dao.insertStringValue(StringValue.of("foo"));
  dao.insertStringValue(StringValue.of("bar"));
  List<StringValue> values = handle.select("select string_value from serialized_types")
      .map((rs, ctx) -> (StringValue) rs.getObject("string_value"))
      .list();
  assertThat(values).containsExactly(StringValue.of("foo"), StringValue.of("bar"));
}

代码示例来源:origin: jdbi/jdbi

@Test
public void useCustomMapper() {
  // tag::useCustomMapper[]
  List<Contact> contacts = handle.createQuery("select * from contacts")
                  .map(new ContactMapper())
                  .list();
  assertThat(contacts).extracting("id", "name")
            .contains(tuple(1, "Alice"),
                 tuple(2, "Bob"));
  // end::useCustomMapper[]
}

代码示例来源:origin: jdbi/jdbi

@SqlQuery("select * from something")
  @RegisterRowMapper(SomethingMapper.class)
  default List<Something> list() {
    return getHandle().createQuery("select * from something")
        .map(new SomethingMapper())
        .list();
  }
}

代码示例来源:origin: jdbi/jdbi

@Test
public void registerFactories() {
  TestDao dao = handle.attach(TestDao.class);
  dao.insertValues(StringValue.of("foo"), LongValue.of(10));
  dao.insertValues(StringValue.of("bar"), LongValue.of(20));
  List<ValueTypeEntity> values = handle.select("select * from serialized_types")
      .map((rs, ctx) -> ValueTypeEntity.of(
          (StringValue) rs.getObject("string_value"),
          (LongValue) rs.getObject("long_value")))
      .list();
  assertThat(values).containsExactly(
      ValueTypeEntity.of(StringValue.of("foo"), LongValue.of(10)),
      ValueTypeEntity.of(StringValue.of("bar"), LongValue.of(20)));
}

代码示例来源:origin: org.trellisldp.ext/trellis-db

private Stream<Quad> fetchQuadsFromTable(final String tableName, final IRI graphName) {
  final String query = "SELECT subject, predicate, object, lang, datatype "
            + "FROM " + tableName + " WHERE resource_id = ?";
  return jdbi.withHandle(handle -> handle.select(query, data.getId())
      .map((rs, ctx) -> rdf.createQuad(graphName, rdf.createIRI(rs.getString(SUBJECT)),
          rdf.createIRI(rs.getString(PREDICATE)),
          getObject(rs.getString(OBJECT), rs.getString(LANG), rs.getString(DATATYPE))))
      .stream());
}

代码示例来源:origin: prestosql/presto

public MaterializedResult execute(Session session, @Language("SQL") String sql, List<? extends Type> resultTypes)
{
  MaterializedResult materializedRows = new MaterializedResult(
      handle.setSqlParser(new RawSqlParser())
          .setTemplateEngine((template, context) -> template)
          .createQuery(sql)
          .map(rowMapper(resultTypes))
          .list(),
      resultTypes);
  return materializedRows;
}

代码示例来源:origin: org.jdbi/jdbi3-sqlobject

@SqlQuery("select * from something")
  @RegisterRowMapper(SomethingMapper.class)
  default List<Something> list() {
    return getHandle().createQuery("select * from something")
        .map(new SomethingMapper())
        .list();
  }
}

相关文章