use of com.github.drinkjava2.jsqlbox.SqlBoxContext in project jSqlBox by drinkjava2.
the class UsuageAndSpeedTest method sqlMapperSqlAnnotaion.
@Test
public void sqlMapperSqlAnnotaion() {
SqlBoxContext ctx = new SqlBoxContext(dataSource);
// use global default context
SqlBoxContext.setGlobalSqlBoxContext(ctx);
UserMapper user = new UserMapper();
for (int i = 0; i < REPEAT_TIMES; i++) {
user.insertOneUser("Sam", "Canada");
user.updateAllUser("Tom", "China");
List<Map<String, Object>> users = user.selectUsers("Tom", "China");
Assert.assertEquals(1, users.size());
user.deleteUsers("Tom", "China");
Assert.assertEquals(0, user.ctx().nQueryForLongValue("select count(*) from users"));
}
}
use of com.github.drinkjava2.jsqlbox.SqlBoxContext in project jSqlBox by drinkjava2.
the class BeetlSqlTemplateDemo method doTest.
@Test
public void doTest() {
HikariDataSource ds = new HikariDataSource();
// H2 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("");
ConnectionSource source = ConnectionSourceHelper.getSingle(ds);
DBStyle dbstyle = new H2Style();
SQLLoader loader = new ClasspathLoader("/sql");
UnderlinedNameConversion nc = new UnderlinedNameConversion();
SQLManager sqlManager = new SQLManager(dbstyle, loader, source, nc, new Interceptor[] {});
// Done BeetlSQL engine
SqlBoxContext.setGlobalTemplateEngine(new BeetlSqlTempalte(sqlManager));
// Log output
SqlBoxContext.setGlobalAllowShowSql(true);
SqlBoxContext ctx = new SqlBoxContext(ds);
SqlBoxContext.setGlobalSqlBoxContext(ctx);
String[] ddlArray = ctx.toDropAndCreateDDL(User.class);
for (String ddl : ddlArray) ctx.quiteExecute(ddl);
for (int i = 1; i <= 100; i++) {
User u = new User();
u.setName("Foo" + i);
u.setAge(i);
u.insert();
}
Assert.assertEquals(100, ctx.nQueryForLongValue("select count(*) from users"));
Map<String, Object> params = new HashMap<>();
params.put("age", 50);
List<Map<String, Object>> result = ctx.tQueryForMapList("user.select", params);
Assert.assertEquals(50, result.size());
ds.close();
}
use of com.github.drinkjava2.jsqlbox.SqlBoxContext in project jSqlBox by drinkjava2.
the class Initializer method onStartup.
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(WebAppConfig.class);
servletContext.addListener(new ContextLoaderListener(ctx));
ctx.setServletContext(servletContext);
Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
servlet.addMapping("/");
servlet.setLoadOnStartup(1);
// force refresh
ctx.refresh();
// SqlBoxContext.setGlobalAllowShowSql(true);
SqlBoxContext sqlCtx = ctx.getBean(SqlBoxContext.class);
SqlBoxContext.setGlobalSqlBoxContext(sqlCtx);
String[] ddls = sqlCtx.toDropAndCreateDDL(Team.class);
for (String ddl : ddls) sqlCtx.quiteExecute(ddl);
for (int i = 0; i < 5; i++) new Team().put("name", "Team" + i, "rating", i * 10).insert();
System.out.println("========== com.jsqlboxdemo.init.Initializer initialized=====");
}
use of com.github.drinkjava2.jsqlbox.SqlBoxContext in project jSqlBox by drinkjava2.
the class EntityNetFactory method createEntityNet.
/**
* Create a EntityNet instance, load data from database buy given loadKeyOnly
* and configObjects parameters
*
* @param ctx
* A SqlBoxContext instance
* @param loadKeyOnly
* If true will only load PKey and FKeys field, otherwise load all
* columns
* @param configObjects
* netConfigs array, can be entity class, entity, SqlBox or
* TableModel instance
* @return The EntityNet
*/
public static EntityNet createEntityNet(SqlBoxContext ctx, boolean loadKeyOnly, Object... configObjects) {
if (configObjects == null || configObjects.length == 0)
throw new EntityNetException("LoadNet() does not support empty netConfigs parameter");
TableModel[] models = EntityNetUtils.objectConfigsToModels(ctx, configObjects);
EntityNet net = new EntityNet();
String starOrSharp = loadKeyOnly ? ".##" : ".**";
for (TableModel t : models) {
List<Map<String, Object>> mapList = null;
String alias = t.getAlias();
if (StrUtils.isEmpty(alias))
alias = t.getTableName();
try {
mapList = ctx.nQuery(new EntitySqlMapListHandler(t), "select " + alias + starOrSharp + " from " + t.getTableName() + " as " + alias);
} finally {
EntityNetUtils.removeBindedTableModel(mapList);
}
net.addMapList(mapList, t);
}
return net;
}
use of com.github.drinkjava2.jsqlbox.SqlBoxContext 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=====");
}
Aggregations