Search in sources :

Example 1 with SqlBoxContextConfig

use of com.github.drinkjava2.jsqlbox.SqlBoxContextConfig in project jSqlBox by drinkjava2.

the class UsuageAndSpeedTest method xXxxStyle_BasicTemplate.

@Test
public void xXxxStyle_BasicTemplate() {
    SqlBoxContextConfig config = new SqlBoxContextConfig();
    config.setTemplateEngine(BasicSqlTemplate.instance());
    SqlBoxContext ctx = new SqlBoxContext(dataSource, config);
    for (int i = 0; i < REPEAT_TIMES; i++) {
        UserAR user = new UserAR("Sam", "Canada");
        UserAR tom = new UserAR("Tom", "China");
        put0("user", user);
        ctx.xExecute("insert into users (name, address) values(#{user.name},#{user.address})");
        put0("user", tom);
        ctx.xExecute("update users set name=#{user.name}, address=#{user.address}");
        Assert.assertEquals(1L, ctx.xQueryForObject("select count(*) from users where ${col}=#{name} and address=#{addr}", put0("name", "Tom"), put("addr", "China"), replace("col", "name")));
        ctx.xExecute("delete from users where name=#{u.name} or address=#{u.address}", put0("u", tom));
    }
}
Also used : SqlBoxContextConfig(com.github.drinkjava2.jsqlbox.SqlBoxContextConfig) SqlBoxContext(com.github.drinkjava2.jsqlbox.SqlBoxContext) Test(org.junit.Test)

Example 2 with SqlBoxContextConfig

use of com.github.drinkjava2.jsqlbox.SqlBoxContextConfig in project jSqlBox by drinkjava2.

the class Initializer method contextInitialized.

@Override
public void contextInitialized(ServletContextEvent context) {
    // Initialize BeanBox
    BeanBox.regAopAroundAnnotation(TX.class, TxBox.class);
    BeanBox.regAopAroundAnnotation(Transaction.class, TxBox.class);
    // Initialize Global SqlBoxContext
    SqlBoxContextConfig config = new SqlBoxContextConfig();
    config.setConnectionManager(TinyTxConnectionManager.instance());
    SqlBoxContext ctx = new SqlBoxContext((DataSource) BeanBox.getBean(DataSourceBox.class), config);
    SqlBoxContext.setGlobalSqlBoxContext(ctx);
    // Initialize database
    String[] ddls = ctx.toDropAndCreateDDL(Team.class);
    for (String ddl : ddls) ctx.quiteExecute(ddl);
    for (int i = 0; i < 5; i++) new Team().put("name", "Team" + i, "rating", i * 10).insert();
    Assert.assertEquals(5, ctx.nQueryForLongValue("select count(*) from teams"));
    System.out.println("========== com.jsqlboxdemo.init.Initializer initialized=====");
}
Also used : SqlBoxContextConfig(com.github.drinkjava2.jsqlbox.SqlBoxContextConfig) SqlBoxContext(com.github.drinkjava2.jsqlbox.SqlBoxContext) Team(model.Team)

Aggregations

SqlBoxContext (com.github.drinkjava2.jsqlbox.SqlBoxContext)2 SqlBoxContextConfig (com.github.drinkjava2.jsqlbox.SqlBoxContextConfig)2 Team (model.Team)1 Test (org.junit.Test)1