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修改即可
时间: 2024-10-16 03:27:46