Search in sources :

Example 26 with Pet

use of org.nutz.dao.test.meta.Pet 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();
        }
    }
}
Also used : NutDao(org.nutz.dao.impl.NutDao) SQLException(java.sql.SQLException) Stopwatch(org.nutz.lang.Stopwatch) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Atom(org.nutz.trans.Atom) SimpleDataSource(org.nutz.dao.impl.SimpleDataSource) List(java.util.List) ArrayList(java.util.ArrayList) Issue1163Pet(org.nutz.dao.test.meta.issue1163.Issue1163Pet) Pet(org.nutz.dao.test.meta.Pet)

Example 27 with Pet

use of org.nutz.dao.test.meta.Pet in project nutz by nutzam.

the class SimpleDaoTest method test_fetchLinks.

@Test
public void test_fetchLinks() {
    dao.create(Master.class, true);
    Master master = new Master();
    master.setName("wendal");
    Pet pet = Pet.create("asdfs");
    Pet pet2 = Pet.create("zzzz");
    List<Pet> pets = new ArrayList<Pet>();
    pets.add(pet);
    pets.add(pet2);
    master.setPets(pets);
    dao.insertWith(master, null);
    List<Master> list = dao.query(Master.class, null);
    dao.fetchLinks(list, null, Cnd.where("1", "=", 1));
}
Also used : Issue1163Master(org.nutz.dao.test.meta.issue1163.Issue1163Master) Issue396Master(org.nutz.dao.test.meta.issue396.Issue396Master) Master(org.nutz.dao.test.meta.Master) ArrayList(java.util.ArrayList) Issue1163Pet(org.nutz.dao.test.meta.issue1163.Issue1163Pet) Pet(org.nutz.dao.test.meta.Pet) Test(org.junit.Test)

Example 28 with Pet

use of org.nutz.dao.test.meta.Pet in project nutz by nutzam.

the class SimpleDaoTest method test_fetch_by_condition_in_special_char.

@Test
public void test_fetch_by_condition_in_special_char() {
    dao.insert(Pet.create("a@b").setNickName("ABC"));
    Pet pet = dao.fetch(Pet.class, Cnd.where("name", "=", "a@b"));
    assertEquals("a@b", pet.getName());
    assertEquals("ABC", pet.getNickName());
}
Also used : Issue1163Pet(org.nutz.dao.test.meta.issue1163.Issue1163Pet) Pet(org.nutz.dao.test.meta.Pet) Test(org.junit.Test)

Example 29 with Pet

use of org.nutz.dao.test.meta.Pet in project nutz by nutzam.

the class SimpleDaoTest method insertRecords.

private void insertRecords(int len) {
    for (int i = 0; i < len; i++) {
        Pet pet = Pet.create("pet" + i);
        pet.setNickName("alias_" + i);
        pet.setPrice((float) (R.random(30, 100) / Math.PI));
        dao.insert(pet);
    }
}
Also used : Issue1163Pet(org.nutz.dao.test.meta.issue1163.Issue1163Pet) Pet(org.nutz.dao.test.meta.Pet)

Example 30 with Pet

use of org.nutz.dao.test.meta.Pet in project nutz by nutzam.

the class SimpleDaoTest method test_insert_ignore_null.

@Test
public void test_insert_ignore_null() {
    Pet pet = Pet.create(R.UU32());
    dao.insert(pet, true, true, true);
    assertTrue(pet.getId() > 0);
    dao.insert(Pet.create(1000), true, true, true);
}
Also used : Issue1163Pet(org.nutz.dao.test.meta.issue1163.Issue1163Pet) Pet(org.nutz.dao.test.meta.Pet) Test(org.junit.Test)

Aggregations

Pet (org.nutz.dao.test.meta.Pet)48 Test (org.junit.Test)45 Issue1163Pet (org.nutz.dao.test.meta.issue1163.Issue1163Pet)13 Atom (org.nutz.trans.Atom)8 Sql (org.nutz.dao.sql.Sql)7 ArrayList (java.util.ArrayList)6 Record (org.nutz.dao.entity.Record)5 List (java.util.List)4 HashMap (java.util.HashMap)3 NutDao (org.nutz.dao.impl.NutDao)3 NutSql (org.nutz.dao.impl.sql.NutSql)3 SimpleCriteria (org.nutz.dao.util.cri.SimpleCriteria)3 Connection (java.sql.Connection)2 SQLException (java.sql.SQLException)2 Timestamp (java.sql.Timestamp)2 Cnd (org.nutz.dao.Cnd)2 Criteria (org.nutz.dao.sql.Criteria)2 Master (org.nutz.dao.test.meta.Master)2 Stopwatch (org.nutz.lang.Stopwatch)2 Molecule (org.nutz.trans.Molecule)2