Search in sources :

Example 1 with SqlObjectPlugin

use of org.jdbi.v3.sqlobject.SqlObjectPlugin in project jdbi by jdbi.

the class TestTransactional method setUp.

@Before
public void setUp() throws Exception {
    final JdbcDataSource ds = new JdbcDataSource() {

        private static final long serialVersionUID = 1L;

        @Override
        public Connection getConnection() throws SQLException {
            final Connection real = super.getConnection();
            return (Connection) Proxy.newProxyInstance(real.getClass().getClassLoader(), new Class<?>[] { Connection.class }, new TxnIsolationCheckingInvocationHandler(real));
        }
    };
    // in MVCC mode h2 doesn't shut down immediately on all connections closed, so need random db name
    ds.setURL(String.format("jdbc:h2:mem:%s;MVCC=TRUE", UUID.randomUUID()));
    db = Jdbi.create(ds);
    db.installPlugin(new SqlObjectPlugin());
    db.registerRowMapper(new SomethingMapper());
    handle = db.open();
    handle.execute("create table something (id int primary key, name varchar(100))");
}
Also used : SomethingMapper(org.jdbi.v3.core.mapper.SomethingMapper) JdbcDataSource(org.h2.jdbcx.JdbcDataSource) Connection(java.sql.Connection) Before(org.junit.Before)

Example 2 with SqlObjectPlugin

use of org.jdbi.v3.sqlobject.SqlObjectPlugin in project dropwizard by dropwizard.

the class JdbiFactory method configure.

/**
 * Overridable function to allow extra customization of the created {@link Jdbi}
 * instance.
 *
 * <p>
 * If this is overridden it is strongly recommend that
 * {@code super.configure(jdbi, configuration)} is invoked before any other
 * changes are made if you intend to use the default as a base so that the
 * customized settings will supersede the defaults
 * </p>
 *
 * @param jdbi
 */
protected void configure(final Jdbi jdbi) {
    jdbi.installPlugin(new SqlObjectPlugin());
    jdbi.installPlugin(new JodaTimePlugin());
    jdbi.installPlugin(new GuavaPlugin());
}
Also used : JodaTimePlugin(org.jdbi.v3.jodatime2.JodaTimePlugin) SqlObjectPlugin(org.jdbi.v3.sqlobject.SqlObjectPlugin) GuavaPlugin(org.jdbi.v3.guava.GuavaPlugin)

Example 3 with SqlObjectPlugin

use of org.jdbi.v3.sqlobject.SqlObjectPlugin in project jdbi by jdbi.

the class IntroductionTest method sqlObject.

// end::sqlobject-declaration[]
@Test
public void sqlObject() {
    // tag::sqlobject-usage[]
    Jdbi jdbi = Jdbi.create("jdbc:h2:mem:test");
    jdbi.installPlugin(new SqlObjectPlugin());
    // Jdbi implements your interface based on annotations
    List<User> userNames = jdbi.withExtension(UserDao.class, dao -> {
        dao.createTable();
        dao.insertPositional(0, "Alice");
        dao.insertPositional(1, "Bob");
        dao.insertNamed(2, "Clarice");
        dao.insertBean(new User(3, "David"));
        return dao.listUsers();
    });
    assertThat(userNames).containsExactly(new User(0, "Alice"), new User(1, "Bob"), new User(2, "Clarice"), new User(3, "David"));
// end::sqlobject-usage[]
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi) SqlObjectPlugin(org.jdbi.v3.sqlobject.SqlObjectPlugin) Test(org.junit.Test)

Example 4 with SqlObjectPlugin

use of org.jdbi.v3.sqlobject.SqlObjectPlugin in project jdbi by jdbi.

the class TestNewApiOnDbiAndHandle method setUp.

@Before
public void setUp() throws Exception {
    JdbcDataSource ds = new JdbcDataSource();
    ds.setURL("jdbc:h2:mem:" + UUID.randomUUID());
    db = Jdbi.create(ds);
    db.installPlugin(new SqlObjectPlugin());
    db.registerRowMapper(new SomethingMapper());
    handle = db.open();
    handle.execute("create table something (id int primary key, name varchar(100))");
}
Also used : SomethingMapper(org.jdbi.v3.core.mapper.SomethingMapper) JdbcDataSource(org.h2.jdbcx.JdbcDataSource) Before(org.junit.Before)

Example 5 with SqlObjectPlugin

use of org.jdbi.v3.sqlobject.SqlObjectPlugin in project jdbi by jdbi.

the class TestReentrancy method setUp.

@Before
public void setUp() throws Exception {
    JdbcDataSource ds = new JdbcDataSource();
    // in MVCC mode h2 doesn't shut down immediately on all connections closed, so need random db name
    ds.setURL(String.format("jdbc:h2:mem:%s;MVCC=TRUE", UUID.randomUUID()));
    db = Jdbi.create(ds);
    db.installPlugin(new SqlObjectPlugin());
    db.registerRowMapper(new SomethingMapper());
    handle = db.open();
    handle.execute("create table something (id int primary key, name varchar(100))");
}
Also used : SomethingMapper(org.jdbi.v3.core.mapper.SomethingMapper) JdbcDataSource(org.h2.jdbcx.JdbcDataSource) Before(org.junit.Before)

Aggregations

SomethingMapper (org.jdbi.v3.core.mapper.SomethingMapper)6 Jdbi (org.jdbi.v3.core.Jdbi)5 SqlObjectPlugin (org.jdbi.v3.sqlobject.SqlObjectPlugin)4 Before (org.junit.Before)4 JdbcDataSource (org.h2.jdbcx.JdbcDataSource)3 BeforeClass (org.junit.BeforeClass)3 Something (org.jdbi.v3.core.Something)2 Connection (java.sql.Connection)1 GuavaPlugin (org.jdbi.v3.guava.GuavaPlugin)1 JodaTimePlugin (org.jdbi.v3.jodatime2.JodaTimePlugin)1 Test (org.junit.Test)1