@Test
@Transactional
@Rollback(false)
public void sync_table_test() throws SQLException, ParseException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//获取oracle的数据库配置信息
Connection connection = DruidJdbcUtils.getConnection("db0");
//查询TABLE_OLD中的数据
PreparedStatement preparedStatement = connection.prepareStatement("select * \n" +
"from TABLE_OLD \n");
ResultSet resultSet = preparedStatement.executeQuery();
int num = 0;
String tableUuid;
String tableRemark;
String tableCdate;
//循环list数据并获取数据
while (resultSet.next()) {
tableUuid= resultSet.getString(2) == null ? "" : resultSet.getString(2);
tableRemark= resultSet.getString(3) == null ? "" : resultSet.getString(3);
tableCdate= resultSet.getString(4) == null ? "" : resultSet.getString(4);
//要迁移到的表
TABLE_NEW table_new = new TABLE_NEW();
table_new.setTableUuid(tableUuid);
table_new.setTableRemark(tableRemark);
table_new.setTableCdate(format.parse(tableCdate));
tableNewDao.save(table_new);
num++;
System.out.println("第"+num+"条");
System.out.println("已同步数量" + num);
}
System.out.println("已同步");
preparedStatement.close();
}此种方法迁移对数据量较小的表比较友好,对于数据量庞大的表则会明显不足;本文另一篇姐妹篇讲述的是多线程模式的数据迁移,感兴趣的码友可以去看看