use of org.nutz.dao.impl.NutDao in project nutz by nutzam.
the class SimpleDaoTest method test_fastinsert_speed.
// @Test
// public void test_map_blob() throws FileNotFoundException {
// if (dao.exists("t_test_map_blob")) {
// dao.drop("t_test_map_blob");
// Lang.quiteSleep(1000);
// }
// dao.execute(Sqls.create("create table t_test_map_blob(id
// VARCHAR(60),filecontent blob)"));
//
// NutMap map = new NutMap().setv(".table", "t_test_map_blob");
// map.put("id", R.UU32());
// map.put("filecontent", new
// FileInputStream("W:\\usb3.0_intel_1.0.10.255_w7.zip"));
//
// dao.insert(map);
//
// Record re = dao.fetch("t_test_map_blob", Cnd.NEW());
// assertNotNull(re);
// System.out.println(re.get("filecontent").getClass());
// System.out.println(new String((byte[])re.get("filecontent")));
//
//// assertEquals("你好", new String((byte[])re.get("filecontent")));
// }
//@Test
public void test_fastinsert_speed() {
SimpleDataSource ds = new SimpleDataSource();
ds.setJdbcUrl("jdbc:mysql://localhost/nutztest");
ds.setUsername("root");
ds.setPassword("root");
dao = new NutDao(ds);
// 删表重建
dao.create(Pet.class, true);
Lang.sleep(1000);
Stopwatch sw = Stopwatch.begin();
// 生成10*2000个对象
List<List<Pet>> list = new ArrayList<List<Pet>>();
for (int i = 0; i < 10; i++) {
List<Pet> pets = new ArrayList<Pet>();
for (int j = 0; j < 2000; j++) {
Pet pet = Pet.create(R.UU32());
pets.add(pet);
}
list.add(pets);
}
sw.stop();
System.out.println("生成对象的耗时: " + sw);
for (final List<Pet> tmp : list) {
sw = Stopwatch.begin();
Trans.exec(new Atom() {
public void run() {
dao.fastInsert(tmp);
}
});
sw.stop();
System.out.println("fastInsert插入2000个对象的耗时" + sw);
}
dao.create(Pet.class, false);
for (int i = 0; i < 10; i++) {
try {
final int t = i;
Connection conn = ds.getConnection();
conn.setAutoCommit(false);
sw = Stopwatch.begin();
System.out.println(System.currentTimeMillis());
PreparedStatement ps = conn.prepareStatement("INSERT INTO t_pet(name,alias) VALUES(?,?)");
System.out.println(System.currentTimeMillis());
for (int j = 0; j < 2000; j++) {
ps.setString(1, "pet_" + t + "_" + j);
ps.setString(2, "");
// ps.setInt(3, 30);
// ps.setInt(4, 0);
// ps.setDate(5, null);
// ps.setFloat(6, 0);
ps.addBatch();
}
System.out.println(System.currentTimeMillis());
ps.executeBatch();
conn.commit();
sw.stop();
System.out.println(sw);
ps.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
use of org.nutz.dao.impl.NutDao in project nutz by nutzam.
the class SimpleDaoTest method test_get_sql.
@Test
public void test_get_sql() throws Throwable {
NutDao dao = new NutDao(ioc.get(javax.sql.DataSource.class));
final List<String> sqls = new ArrayList<String>();
final Method m = NutStatement.class.getDeclaredMethod("toStatement", Object[][].class, String.class);
m.setAccessible(true);
dao.setExecutor(new DaoExecutor() {
public void exec(Connection conn, DaoStatement st) {
String psql = st.toPreparedStatement();
sqls.add(psql);
// 事实上根据参数矩阵(getParamMatrix)和Sql语句(toPreparedStatement)就能逐一替换生成
try {
String ssql = (String) m.invoke(st, st.getParamMatrix(), psql);
sqls.add(ssql);
} catch (Exception e) {
e.printStackTrace();
}
}
});
dao.insert(Pet.create("hi"));
for (String sql : sqls) {
System.out.println(sql);
}
}
use of org.nutz.dao.impl.NutDao in project nutz by nutzam.
the class SimpleDaoTest method test_migration.
@Test
public void test_migration() {
dao.execute(Sqls.create("drop table t_pet"));
Entity<Pet> en = dao.getEntity(Pet.class);
NutDao dao = (NutDao) this.dao;
JdbcExpert expert = dao.getJdbcExpert();
MappingField mf = en.getField("age");
String str = "create table t_pet (" + mf.getColumnName() + " " + expert.evalFieldType(mf) + "," + mf.getColumnName() + "_2" + " " + expert.evalFieldType(mf) + ")";
dao.execute(Sqls.create(str));
Daos.migration(dao, Pet.class, !dao.meta().isSQLite(), !dao.meta().isSQLite());
}
use of org.nutz.dao.impl.NutDao in project nutz by nutzam.
the class FastClassFactoryTest method test_fastclass_for_datasource.
@Test
public void test_fastclass_for_datasource() {
FastClassFactory.get(Dao.class);
FastClassFactory.get(DruidDataSource.class);
FastClass fc = FastClassFactory.get(DaoSupport.class);
fc = FastClassFactory.get(NutDao.class);
fc.invoke(new NutDao(), "execute", new Class[] { Sql.class }, new Object[] { null });
}
Aggregations