Search in sources :

Example 1 with BindBean

use of org.jdbi.v3.sqlobject.customizer.BindBean in project jdbi by jdbi.

the class TestOracleReturning method testReturningDmlNamedParams.

@Test
public void testReturningDmlNamedParams() {
    Handle h = dbRule.getSharedHandle();
    List<Integer> ids = h.createUpdate("insert into something(id, name) values (:id, :name) returning id into :result").bindBean(new Something(17, "Brian")).addCustomizer(returnParameters().register("result", OracleTypes.INTEGER)).execute(returningDml()).mapTo(int.class).list();
    assertThat(ids).containsExactly(17);
}
Also used : Something(org.jdbi.v3.core.Something) Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 2 with BindBean

use of org.jdbi.v3.sqlobject.customizer.BindBean in project jdbi by jdbi.

the class TestNamedParams method testBeanPropertyPrefixBinding.

@Test
public void testBeanPropertyPrefixBinding() throws Exception {
    Handle h = dbRule.openHandle();
    Something original = new Something(0, "Keith");
    assertThat(h.createUpdate("insert into something (id, name) values (:my.id, :my.name)").bindBean("my", original).execute()).isEqualTo(1);
    assertThat(h.select("select * from something where id = ?", original.getId()).mapToBean(Something.class).findOnly()).isEqualTo(original);
}
Also used : Something(org.jdbi.v3.core.Something) Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 3 with BindBean

use of org.jdbi.v3.sqlobject.customizer.BindBean in project jdbi by jdbi.

the class TestNamedParams method testBeanPropertyBinding.

@Test
public void testBeanPropertyBinding() throws Exception {
    Handle h = dbRule.openHandle();
    Something original = new Something(0, "Keith");
    assertThat(h.createUpdate("insert into something (id, name) values (:id, :name)").bindBean(original).execute()).isEqualTo(1);
    assertThat(h.select("select * from something where id = ?", original.getId()).mapToBean(Something.class).findOnly()).isEqualTo(original);
}
Also used : Something(org.jdbi.v3.core.Something) Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 4 with BindBean

use of org.jdbi.v3.sqlobject.customizer.BindBean in project jdbi by jdbi.

the class TestNamedParams method testBeanPropertyNestedBinding.

@Test
public void testBeanPropertyNestedBinding() throws Exception {
    Handle h = dbRule.openHandle();
    Something thing = new Something(0, "Keith");
    assertThat(h.createUpdate("insert into something (id, name) values (:my.nested.id, :my.nested.name)").bindBean("my", new Object() {

        @SuppressWarnings("unused")
        public Something getNested() {
            return thing;
        }
    }).execute()).isEqualTo(1);
    assertThat(h.select("select * from something where id = ?", thing.getId()).mapToBean(Something.class).findOnly()).isEqualTo(thing);
}
Also used : Something(org.jdbi.v3.core.Something) Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 5 with BindBean

use of org.jdbi.v3.sqlobject.customizer.BindBean in project jdbi by jdbi.

the class IntroductionTest method core.

@Test
public void core() {
    // tag::core[]
    // (H2 in-memory database)
    Jdbi jdbi = Jdbi.create("jdbc:h2:mem:test");
    List<User> users = jdbi.withHandle(handle -> {
        handle.execute("CREATE TABLE user (id INTEGER PRIMARY KEY, name VARCHAR)");
        // Inline positional parameters
        handle.execute("INSERT INTO user(id, name) VALUES (?, ?)", 0, "Alice");
        // Positional parameters
        handle.createUpdate("INSERT INTO user(id, name) VALUES (?, ?)").bind(0, // 0-based parameter indexes
        1).bind(1, "Bob").execute();
        // Named parameters
        handle.createUpdate("INSERT INTO user(id, name) VALUES (:id, :name)").bind("id", 2).bind("name", "Clarice").execute();
        // Named parameters from bean properties
        handle.createUpdate("INSERT INTO user(id, name) VALUES (:id, :name)").bindBean(new User(3, "David")).execute();
        // Easy mapping to any type
        return handle.createQuery("SELECT * FROM user ORDER BY name").mapToBean(User.class).list();
    });
    assertThat(users).containsExactly(new User(0, "Alice"), new User(1, "Bob"), new User(2, "Clarice"), new User(3, "David"));
// end::core[]
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)5 Handle (org.jdbi.v3.core.Handle)4 Something (org.jdbi.v3.core.Something)4 Jdbi (org.jdbi.v3.core.Jdbi)1