mybatis批量分批次插入oracle数据库,报ORA-01745: 无效的主机/绑定变量名...

方法一:循环调用插入单条记录的方法,效率真心让人捉急 (3万条数据,快三分钟)
  

 public int saveGwghidlist1(List<Gwghid> list) {
        
        int xh=0;
        deleteGwghidByCondition("");
        if(list.size()>0){
            for (Gwghid gwghid: list) {
                xh++;
                gwghidDAO.saveGwghid(gwghid);
                System.out.println(xh);
            }
        }

        return xh;
     }

方法二:在mapper.xml中循环插入<foreach>  (可行,但是要要分批插入,因为oracle,有sql限制64k?不确定哈)

serviceimpl

	public int saveGwghidlist(List<Gwghid> list) {
    	
		int xh=0;
		deleteGwghidByCondition("");
		if(list.size()>0){
			for (Gwghid gwghid: list) {
				xh++;
				gwghidDAO.saveGwghid(gwghid);
				System.out.println(xh);
			}
		}

		return xh;
     }

mapper.xml

	<insert id="saveListGwghid" parameterType="java.util.List">
		insert into GWGHID (<include refid="bf" />) 
			select S_GWGHID.nextval,a.* from (
			<foreach collection="list" item="item" index="index" separator="union all" >
			select
				#{item.wlbm,jdbcType=VARCHAR},
				#{item.wlms,jdbcType=VARCHAR},
				#{item.dlms,jdbcType=VARCHAR},
				#{item.zlms,jdbcType=VARCHAR},
				#{item.xlms,jdbcType=VARCHAR},
				#{item.tzbm,jdbcType=VARCHAR},
				#{item.kzms,jdbcType=VARCHAR},
				#{item.kzmszt,jdbcType=VARCHAR},
				#{item.jsgfsid,jdbcType=VARCHAR},
				#{item.gdidzt,jdbcType=VARCHAR},
				#{item.lx,jdbcType=VARCHAR},
				#{item.bz,jdbcType=VARCHAR},
				#{item.fbsj,jdbcType=TIMESTAMP},
				#{item.color,jdbcType=VARCHAR},
				#{item.drsj,jdbcType=TIMESTAMP},
				#{item.xh,jdbcType=NUMERIC},
				#{item.bs,jdbcType=NUMERIC}
			from dual
			</foreach>
			) a
	</insert>

终于按照要求把maper.xml写好了,接下来测试吧,结果呜呜~~~~~数据量超过2万就报错。

刚开始,需打印出每次插入的返回值,一直报  TOMCAT内存溢出问题Exception in thread ""http-bio-8080"-exec-6  问题,后来查看

https://blog.csdn.net/wangyang1354/article/details/49754891?utm_source=blogxgwz1 

MyEclipse中打开window-->preferences-->Tomacat -->jdk   加入了这句话,再次批量插入返回值打印出来了。

   -Xms128m -Xmx512m -XX:PermSize=128m -XX:MaxPermSize=128m

再次导入三万条测试,

报 oracle ORA-01745: 无效的主机/绑定变量名

查了不少资料,主要原因有两种:

1.绑定变量用了oracle关键字导致的  2.绑定变量中间少了分割符导致的

仔细审查了一遍mapperx.xml发现并没有上边两种情况,抓狂啊

也会报这个错  java.sql.SQLException: code [17410]无法从套接字读取更多的数据  (自行百度,说是要改数据库,表空间自动扩展的值,按照要求改了,确实不报这个错了,改报上边的错了,好吧,我也是够了)

再次开启搜索模式,看到了一篇文章(链接忘哈),上边说是插入的数据量过大,超过oracle64k限制,用批次插入方式

说干就干吧,批次插入一次插入1000条,因为项目需求,采用这种简单方式即可

serviceimpl

	@Override
	public int saveBatGwckjg(List<Gwckjg> list) {
    	int xh=0;
		gwckjgDAO.deleteAllGwckjg();
		List<Gwckjg> insertList =new ArrayList<Gwckjg>();
		for (Gwckjg gwckjg : list) {
			insertList.add(gwckjg);
			if(insertList.size() ==1000){
				 xh +=gwckjgDAO.saveBatGwckjg(insertList);
				 insertList.clear();
				 //System.out.println(xh);
			}
		}
		if(insertList.size()>0){
			 int k=gwckjgDAO.saveBatGwckjg(insertList);
			 System.out.println(k+"ok");
		}
         return xh;
     }

测试,完美运行,三万条,时间35秒左右。(加上导入验证15秒左右,实际上就20左右的样子)。

分批次插入还有其他方法,https://blog.csdn.net/hardworking0323/article/details/51105191

至此结束,睡觉去了。

猜你喜欢

转载自blog.csdn.net/emsoc/article/details/83094541