ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
批量操作 ~~~ @Resource(name = "sqlSessionDao") private Dao dao; //批量插入 List<TfNList> list = new ArrayList(); TfNList n1 = new TfNList(); n1.setListId("111"); n1.setListName("测试"); list.add(n1); TfNList n2 = new TfNList(); n2.setListId("112"); n2.setListName("测试2"); list.add(n2); dao.insertBatch(list); //批量插入另一种写法 dao.insertBatch(new ArrayList(){{ add(new TfNList(){{ setListId("111"); setListName("测试"); }}); add(new TfNList(){{ setListId("112"); setListName("测试2"); }}); }}); //批量更新 List<TfNList> list = new ArrayList(); TfNList n1 = new TfNList(); n1.setListId("111").asCondition(); n1.setListName("测试"); list.add(n1); TfNList n2 = new TfNList(); n2.setListId("112").asCondition(); n2.setListName("测试2"); list.add(n2); dao.updateBatch(list); //批量更新另一种写法 dao.updateBatch(new ArrayList(){{ add(new TfNList(){{ setListId("111").asCondition();; setListName("测试"); }}); add(new TfNList(){{ setListId("112").asCondition();; setListName("测试2"); }}); }}); //设置条件时,调用每个实体类asCondition()方法是一种方式,还有另外一种方法,可指定条件列,方法定义如下: updateBatch(List<? extends BaseEntity> list, String... conditionColumns); //用法如下 dao.updateBatch(list, "LIST_ID", "CUST_TYPE"); //批量删除 List<TfNList> list = new ArrayList(); TfNList n1 = new TfNList(); n1.setListId("111").asCondition(); list.add(n1); TfNList n2 = new TfNList(); n2.setListId("112").asCondition(); list.add(n2); dao.deleteBatch(list); //批量删除另一种写法 dao.deleteBatch(new ArrayList(){{ add(new TfNList(){{ setListId("111").asCondition();; }}); add(new TfNList(){{ setListId("112").asCondition();; }}); }}); //技巧:此种写法目的是减少遍历次数,某些值可不用事先设置,在BatchEachHandler里进行处理即可,insertBatch内部循环时会回调BatchEachHandler dao.insertBatch(list, new BatchEachHandler<TfNList>(){ @Override public void onEach(TfNList n){ n.setCreateTime(new Date()); } }); //另外也可自定义sql进行批量处理 void executeBatch(String statementId, List<?> list); void executeBatch(String statementId, List<?> list, BatchEachHandler batchEachHandler); ~~~