use of org.apache.commons.dbutils.handlers.MapListHandler in project jSqlBox by drinkjava2.
the class ActiveRecordDemoTest method doTest.
@Test
public void doTest() {
// DataSource
HikariDataSource ds = new HikariDataSource();
// H2 is a memory database
ds.setDriverClassName("org.h2.Driver");
ds.setJdbcUrl("jdbc:h2:mem:DBName;MODE=MYSQL;DB_CLOSE_DELAY=-1;TRACE_LEVEL_SYSTEM_OUT=0");
ds.setUsername("sa");
ds.setPassword("");
// MySQL
// ds.setDriverClassName("com.mysql.jdbc.Driver");
// ds.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/test?rewriteBatchedStatements=true&useSSL=false");
// ds.setUsername("root");
// ds.setPassword("root888");
// MS-SqlServer
// ds.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// ds.setJdbcUrl("jdbc:sqlserver://localhost:1433;databaseName=test");
// ds.setUsername("sa");
// ds.setPassword("root888");
// ORACLE
// ds.setDriverClassName("oracle.jdbc.OracleDriver");
// ds.setJdbcUrl("jdbc:oracle:thin:@127.0.0.1:1521:XE");
// ds.setUsername("root");
// ds.setPassword("root888");
SqlBoxContext ctx = new SqlBoxContext(ds);
SqlBoxContext.setGlobalSqlBoxContext(ctx);
String[] ddlArray = ctx.getDialect().toDropAndCreateDDL(UserEntity.class);
for (String ddl : ddlArray) ctx.quiteExecute(ddl);
for (int i = 1; i <= 100; i++) {
UserEntity u = new UserEntity();
u.setFirstName("Foo" + i);
u.setLastName("Bar" + i);
u.setAge(i);
u.insert();
}
Assert.assertEquals(100L, ((Number) ctx.nQueryForObject("select count(*) from users")).longValue());
List<Map<String, Object>> users = ctx.nQuery(new MapListHandler(), ctx.pagin(2, 10, "select concat(firstName, ' ', lastName) as UserName, age from users where age>?"), 50);
Assert.assertEquals(10, users.size());
for (Map<String, Object> map : users) System.out.println("UserName=" + map.get("USERNAME") + ", age=" + map.get("AGE"));
ds.close();
}
Aggregations