NutzCN Logo
问答 无pojo怎么批量一次导入或更新数据库
发布于 2830天前 作者 qq_9ad759a9 2358 次浏览 复制 上一个帖子 下一个帖子
标签:

无pojo怎么批量一次导入或更新多条数据到数据库

7 回复

插入的话,dao.fastInsert(List)

更新一般不会有批量一说吧?根据条件更新就是了

@wendal 我们是excel导数据,用户身份证相同的执行更新,没有的执行新增,还有你那个是有pojo的情况才能用吧

我哪个只有pojo才能用?

dao.fastInsert(List)

@wendal
dao.fastInsert(List)是有pojo的情况才能用吧?

不是的, map也可以的

        List<NutMap> list = new ArrayList<NutMap>();
        for (int i = 0; i < 10; i++) {
            NutMap map = new NutMap();
            map.setv("+*id", 0);
            map.put("name", "abc_" + i);
            list.add(map);
        }
        list.get(0).setv(".table", "t_pet");
        dao.fastInsert(list);
        for (NutMap map : list) {
            log.debugf("id=%d, name=%s", map.get("id"), map.get("name"));
        }

输出

2017-03-24 17:30:09,472 org.nutz.dao.impl.sql.run.NutDaoExecutor.printSQL(NutDaoExecutor.java:388) DEBUG - INSERT INTO t_pet(name) VALUES(?) 
    |     1 |
    |-------|
    | abc_0 |
    | abc_1 |
    | abc_2 |
    | abc_3 |
    | abc_4 |
    | abc_5 |
    | abc_6 |
    | abc_7 |
    | abc_8 |
    | abc_9 |
  For example:> "INSERT INTO t_pet(name) VALUES('abc_0') "
2017-03-24 17:30:09,489 org.nutz.dao.impl.interceptor.DaoTimeInterceptor.filter(DaoTimeInterceptor.java:28) DEBUG - time=16ms, sql=INSERT INTO t_pet(name) VALUES('abc_0') 
2017-03-24 17:30:09,489 org.nutz.dao.test.normal.SimpleDaoTest.test_issue_1235(SimpleDaoTest.java:864) DEBUG - id=1, name=abc_0
2017-03-24 17:30:09,490 org.nutz.dao.test.normal.SimpleDaoTest.test_issue_1235(SimpleDaoTest.java:864) DEBUG - id=2, name=abc_1
2017-03-24 17:30:09,490 org.nutz.dao.test.normal.SimpleDaoTest.test_issue_1235(SimpleDaoTest.java:864) DEBUG - id=3, name=abc_2
2017-03-24 17:30:09,490 org.nutz.dao.test.normal.SimpleDaoTest.test_issue_1235(SimpleDaoTest.java:864) DEBUG - id=4, name=abc_3
2017-03-24 17:30:09,490 org.nutz.dao.test.normal.SimpleDaoTest.test_issue_1235(SimpleDaoTest.java:864) DEBUG - id=5, name=abc_4
2017-03-24 17:30:09,490 org.nutz.dao.test.normal.SimpleDaoTest.test_issue_1235(SimpleDaoTest.java:864) DEBUG - id=6, name=abc_5
2017-03-24 17:30:09,490 org.nutz.dao.test.normal.SimpleDaoTest.test_issue_1235(SimpleDaoTest.java:864) DEBUG - id=7, name=abc_6
2017-03-24 17:30:09,491 org.nutz.dao.test.normal.SimpleDaoTest.test_issue_1235(SimpleDaoTest.java:864) DEBUG - id=8, name=abc_7
2017-03-24 17:30:09,491 org.nutz.dao.test.normal.SimpleDaoTest.test_issue_1235(SimpleDaoTest.java:864) DEBUG - id=9, name=abc_8
2017-03-24 17:30:09,491 org.nutz.dao.test.normal.SimpleDaoTest.test_issue_1235(SimpleDaoTest.java:864) DEBUG - id=10, name=abc_9

表结构

id 主键,自增
name 唯一

添加回复
请先登陆
回到顶部