oracle下使用Db.batchSave()会提示无效的序列,解决方法直接上代码
#### 集成DbPro,覆盖batchSave方法
- package com.seven.common;
- import com.jfinal.plugin.activerecord.Config;
- import com.jfinal.plugin.activerecord.DbKit;
- import com.jfinal.plugin.activerecord.DbPro;
- import com.jfinal.plugin.activerecord.Record;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Map;
- import java.util.Set;
- /**
- * @author xuyanfeng
- * @Description: TODO
- * @date 2019/5/23
- */
- public class RenzhiDbPro extends DbPro {
- @Override
- public int[] batchSave(String tableName, List<Record> recordList, int batchSize) {
- if (recordList != null && recordList.size() != 0) {
- Record record = recordList.get(0);
- Map<String, Object> cols = record.getColumns();
- int index = 0;
- String pks = "";
- StringBuilder columns = new StringBuilder();
- Set<Map.Entry<String, Object>> entries = cols.entrySet();
- for (Map.Entry<String, Object> e : entries) {
- if (config.getDialect().isOracle()){
- Object value = e.getValue();
- if (value instanceof String && ((String)value).endsWith(".nextval")) {
- //主键
- pks = pks.concat(e.getKey()).concat(",");
- continue ;
- }
- }
- if (index++ > 0) {
- columns.append(',');
- }
- columns.append(e.getKey());
- }
- if (pks.length() > 0){
- pks = pks.replaceFirst(",", "");
- }
- StringBuilder sql = new StringBuilder();
- List<Object> parasNoUse = new ArrayList();
- config.getDialect().forDbSave(tableName, pks.split(","), record, sql, parasNoUse);
- return this.batch(sql.toString(), columns.toString(), recordList, batchSize);
- } else {
- return new int[0];
- }
- }
- }
- #### 创建MyDbProFactory实现IDbProFactory
- package com.seven.common;
- import com.jfinal.plugin.activerecord.DbPro;
- import com.jfinal.plugin.activerecord.IDbProFactory;
- /**
- * @author xuyanfeng
- * @date 2019/5/23
- */
- public class RenzhiDbProFactory implements IDbProFactory {
- @Override
- public DbPro getDbPro(String configName) {
- return new RenzhiDbPro();
- }
- }
#### Arp设置
- arp.setDbProFactory(new RenzhiDbProFactory())