merge into可以使oracle数据表中已有数据进行合并。即有则update,否则更新
下边的示例是在jdbc数据源连接后测试通过的
1、单条数据
Long companyAuthId = SeqkeyUtil.getSeqKey(vids, "COMPANY_AUTH_ID"); CompanyAuth comtmp = companyList.get(0); String sql = "merge into COMPANY_AUTH auth using (select ? MOBILE from dual) tmp on (auth.mobile=tmp.mobile)"+ " WHEN MATCHED THEN"+ " UPDATE SET auth.reserve=?"+ " WHEN NOT MATCHED THEN"+ " INSERT (id,MOBILE) VALUES (?, ?)"; vids.update(sql, new Object[]{comtmp.getMobile(),comtmp.getReserve(),companyAuthId+"",comtmp.getMobile()});
2、多条数据(批处理)
Long companyAuthId = SeqkeyUtil.getSeqKey(vids, "COMPANY_AUTH_ID");//seqs 表主键使用 List<Object[]> batch = new ArrayList<Object[]>(); for (CompanyAuth company : companyList) { Object[] values = new Object[] { company.getMobile(), company.getReserve(), companyAuthId+"", company.getMobile() }; batch.add(values); } String sql = "merge into COMPANY_AUTH auth using (select ? MOBILE from dual) tmp on (auth.mobile=tmp.mobile)"+ " WHEN MATCHED THEN"+ " UPDATE SET auth.reserve=?"+ " WHEN NOT MATCHED THEN"+ " INSERT (id,MOBILE) VALUES (?, ?)"; vids.batchUpdate(sql, batch);
如果出现多张表的话只需要针对using (select ? MOBILE from dual) tmp on修改即可
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。