数据迁移

@Component
public class DBDao {
    private static final Logger logger = LoggerFactory.getLogger(DBDao.class);

    public List> getMemberList(Integer index, String db, String table, Connection conn) throws Exception {
        //创建查询
        String sql = MessageFormat.format("select * from {0}.{1} order by id asc limit ?,?", db, table);
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setInt(1, index * Config.batch);
        ps.setInt(2, Config.batch);

        //执行查询
        long start = System.currentTimeMillis();
        ResultSet rs = ps.executeQuery();
        long end = System.currentTimeMillis();
        logger.debug("db:{}, table:{}, batch:{}, sql gap:{}", db, table, index, end-start);


        //处理结果
        List> list = new LinkedList<>();
        while (rs.next()) {
            Map member = new HashMap<>();
            member.put("id",rs.getLong("id"));
            member.put("avatar",rs.getString("avatar"));
            member.put("birthday",rs.getString("birthday"));
            member.put("sex",rs.getString("sex"));
            list.add(member);
        }
        rs.close();
        ps.close();
        return list;
    }
}

你可能感兴趣的:(数据迁移)