解决 JFInal Db.batchSave 方法 在oracle下 序列无效

  • • 发表于2019-05-23 21:57:39.0
  • • 作者 小徐同学
  • • 540 次浏览
  • • 0 条评论
  • • 最后编辑时间 2019-05-23 21:57:39.0
  • • 来自 [JFinal]

原创声明:本文为作者原创,未经允许不得转载,经授权转载需注明作者和出处

集成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
 * @Description: TODO
 * @date 2019/5/23
 */
public class RenzhiDbProFactory implements IDbProFactory {




    @Override
    public DbPro getDbPro(String configName) {
        return new RenzhiDbPro();
    }
}

Arp设置

        arp.setDbProFactory(new RenzhiDbProFactory());
评论区(共0条评论)
0条评论
Ctrl+Enter
作者

Michael

小徐同学

帖子:36 回复:0

全栈搬运工

作者详情》
Top