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

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

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

Query.mapToBean介绍

暂无

代码示例

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

@Test
public void testFirstWithNoResult() {
  Optional<Something> s = h.createQuery("select id, name from something").mapToBean(Something.class).findFirst();
  assertThat(s.isPresent()).isFalse();
}

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

@Test
public void testBindOptionalPresent() {
  Something result = handle.createQuery(SELECT_BY_NAME)
      .bind("name", Optional.of("brian"))
      .mapToBean(Something.class)
      .findOnly();
  assertThat(result).isEqualTo(new Something(2, "brian"));
}

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

@Test
public void testSimpleUpdate() {
  h.execute("insert into something (id, name) values (1, 'eric')");
  h.execute("update something set name = 'cire' where id = 1");
  Something eric = h.createQuery("select * from something where id = 1").mapToBean(Something.class).list().get(0);
  assertThat(eric.getName()).isEqualTo("cire");
}

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

@Test
public void testMappedQueryObjectWithNullForPrimitiveIntField() {
  h.execute("insert into something (id, name, intValue) values (1, 'eric', null)");
  ResultIterable<Something> query = h.createQuery("select * from something order by id").mapToBean(Something.class);
  List<Something> r = query.list();
  Something eric = r.get(0);
  assertThat(eric).isEqualTo(new Something(1, "eric"));
  assertThat(eric.getIntValue()).isZero();
}

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

@Test
public void testDynamicBindOptionalEmpty() {
  List<Something> result = handle.createQuery(SELECT_BY_NAME)
      .bindByType("name", Optional.empty(), new GenericType<Optional<String>>() {})
      .mapToBean(Something.class)
      .list();
  assertThat(result).containsExactly(new Something(1, "eric"), new Something(2, "brian"));
}

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

@Test
public void testFirstResult() {
  h.execute("insert into something (id, name) values (1, 'eric')");
  h.execute("insert into something (id, name) values (2, 'brian')");
  Something r = h.createQuery("select * from something order by id")
          .mapToBean(Something.class)
          .findFirst()
          .get();
  assertThat(r.getName()).isEqualTo("eric");
}

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

@Test
public void testUpdate() {
  h.execute("insert into something (id, name) values (1, 'eric')");
  h.createUpdate("update something set name = 'ERIC' where id = 1").execute();
  Something eric = h.createQuery("select * from something where id = 1").mapToBean(Something.class).list().get(0);
  assertThat(eric.getName()).isEqualTo("ERIC");
}

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

@Test
public void testSetPositionalString() {
  h.execute("insert into something (id, name) values (1, 'eric')");
  h.execute("insert into something (id, name) values (2, 'brian')");
  Something eric = h.createQuery("select * from something where name = ?")
      .bind(0, "eric")
      .mapToBean(Something.class)
      .list()
      .get(0);
  assertThat(eric.getId()).isEqualTo(1);
}

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

@Test
public void testSetPositionalInteger() {
  h.execute("insert into something (id, name) values (1, 'eric')");
  h.execute("insert into something (id, name) values (2, 'brian')");
  Something eric = h.createQuery("select * from something where id = ?")
      .bind(0, 1)
      .mapToBean(Something.class)
      .list().get(0);
  assertThat(eric.getId()).isEqualTo(1);
}

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

@Test
public void testGetOptionShouldReturnCorrectRow() {
  Something result = dbRule.getSharedHandle().createQuery(SELECT_BY_NAME)
      .bind("name", Option.of("eric"))
      .mapToBean(Something.class)
      .findOnly();
  assertThat(result).isEqualTo(ERICSOMETHING);
}

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

@Test
public void testGetLazyShouldReturnCorrectRow() {
  Something result = dbRule.getSharedHandle().createQuery(SELECT_BY_NAME)
      .bind("name", Lazy.of(() -> "brian"))
      .mapToBean(Something.class)
      .findOnly();
  assertThat(result).isEqualTo(BRIANSOMETHING);
}

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

@Test
public void testGetValidationInvalidShouldReturnAllRows() {
  List<Something> result = dbRule.getSharedHandle().createQuery(SELECT_BY_NAME)
      .bind("name", Validation.invalid("eric"))
      .mapToBean(Something.class)
      .list();
  assertThat(result).hasSize(2);
}

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

@Test
public void testGetOptionEmptyShouldReturnAllRows() {
  List<Something> result = dbRule.getSharedHandle().createQuery(SELECT_BY_NAME)
      .bind("name", Option.none())
      .mapToBean(Something.class)
      .list();
  assertThat(result).hasSize(2);
}

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

@Test
public void testGetEitherLeftShouldReturnAllRows() {
  List<Something> result = dbRule.getSharedHandle().createQuery(SELECT_BY_NAME)
      .bind("name", Either.left("eric"))
      .mapToBean(Something.class)
      .list();
  assertThat(result).hasSize(2);
}

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

@Test
public void testStatementCustomizersPersistAfterMap() {
  h.execute("insert into something (id, name) values (?, ?)", 1, "hello");
  h.execute("insert into something (id, name) values (?, ?)", 2, "world");
  List<Something> rs = h.createQuery("select id, name from something")
             .setMaxRows(1)
             .mapToBean(Something.class)
             .list();
  assertThat(rs).hasSize(1);
}

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

@Test
public void testMapInvalidEnumValue() {
  Handle h = dbRule.openHandle();
  h.createUpdate("insert into something (id, name) values (1, 'joe')").execute();
  assertThatThrownBy(() -> h.createQuery("select * from something order by id")
    .mapToBean(SomethingElse.class)
    .findFirst()).isInstanceOf(UnableToProduceResultException.class);
}

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

@Test
public void testBindOptionalOfUnregisteredCustomType() {
  exception.expect(UnsupportedOperationException.class);
  handle.createQuery(SELECT_BY_NAME)
      .bind("name", Optional.of(new Name("eric")))
      .mapToBean(Something.class)
      .list();
}

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

@Test
public void testBatchInsertWithKeyGenerationAndExplicitColumnNames() {
  PreparedBatch batch = h.prepareBatch("insert into something (name) values (?) ");
  batch.add("Brian");
  batch.add("Thom");
  List<Integer> ids = batch.executeAndReturnGeneratedKeys("id").mapTo(Integer.class).list();
  assertThat(ids).containsExactly(1, 2);
  List<Something> somethings = h.createQuery("select id, name from something")
      .mapToBean(Something.class)
      .list();
  assertThat(somethings).containsExactly(new Something(1, "Brian"), new Something(2, "Thom"));
}

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

@Test
public void testDynamicBindOptionalOfCustomType() {
  handle.registerArgument(new NameArgumentFactory());
  handle.createQuery(SELECT_BY_NAME)
      .bindByType("name", Optional.of(new Name("eric")), new GenericType<Optional<Name>>() {})
      .mapToBean(Something.class)
      .list();
}

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

@Test
public void testBasics() {
  Handle h = dbRule.openHandle();
  Batch b = h.createBatch();
  b.add("insert into something (id, name) values (0, 'Keith')");
  b.add("insert into something (id, name) values (1, 'Eric')");
  b.add("insert into something (id, name) values (2, 'Brian')");
  b.execute();
  List<Something> r = h.createQuery("select * from something order by id").mapToBean(Something.class).list();
  assertThat(r).hasSize(3);
}

相关文章