使用Jorm进行批量操作_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 使用Jorm进行批量操作

使用Jorm进行批量操作

 2011/10/12 9:15:12  jstudio  http://jadmin.iteye.com  我要评论(0)
  • 摘要:直接上代码吧:>Demoonepublicvoidbatch_op_one(){session=Jorm.getSession();JdbcBatcherbatcher=session.createBatcher();batcher.addBatch("deletefromt_id_auto");batcher.addBatch("deletefromt_incre");batcher.addBatch("deletefromt_user");batcher.execute()
  • 标签:使用 操作
直接上代码吧:

> Demo one
public void batch_op_one() {

    session = Jorm.getSession();
    JdbcBatcher batcher = session.createBatcher();
    batcher.addBatch("delete from t_id_auto");
    batcher.addBatch("delete from t_incre");
    batcher.addBatch("delete from t_user");
    batcher.execute();
   
    session.beginTransaction();
    long start;
    try {
        start = System.currentTimeMillis();
        String sql = "INSERT INTO t_user(sex,age,career,name,id) VALUES(?,?,?,?,?)";
        for (int i = 0; i < 100000; i++) {
            batcher.addBatch(sql, new Object[] {"男", Numbers.random(98), Strings.random(10), Strings.fixed(6), (i+1) });}
            String sqlx = "INSERT INTO t_id_auto(name, id) VALUES(?, ?)";
            for (int i = 0; i < 100000; i++) {
                batcher.addBatch(sqlx, new Object[] {Strings.fixed(6), (i+1)});
                if(i > 200) {
                    //Integer.parseInt("kkk");
                }
            }
            batcher.execute();   
            System.out.println(System.currentTimeMillis() - start);
    } catch (Exception e) {
        session.rollback();
    } finally {
        session.endTransaction();
        session.close();
    }
}


> Demo two
public void batch_op_two() {

    session = Jorm.getSession();
    session.beginTransaction();
    session.clean(User.class);
    JdbcBatcher batcher = session.createBatcher();
    batcher.setBatchSize(500);// 指定每批处理的记录数
   
    User u;
    int times = 20 * 100;
    long start = System.currentTimeMillis();
    for(int i = 0; i < times; i++) {
     String sex = (i % 2 == 0 ? "男" : "女");
     u = new User(Strings.fixed(6), sex, Numbers.random(100), Strings.random(16));
     batcher.save(u);
    }
    batcher.execute();
    session.endTransaction();
    long cost = (System.currentTimeMillis() - start);
    System.out.println("Total:" + cost);
    System.out.println("Each:" + (float) cost / times);
    session.close();
}


项目地址: http://javaclub.sourceforge.net/jorm.html
下载地址: http://sourceforge.net/projects/javaclub/files/jorm
发表评论
用户名: 匿名