使用jdbc的方式同步数据到本地数据库(指定一次查询插入的数量)

16、使用jdbc的方式同步数据到本地数据库
    /**
     * 同步数据到本地数据库
     */
    @Override
    public void syncDataToLocalDB() {
        // 查询同步开关
        SysConfig configParam = new SysConfig();
        configParam.setConfigKey("DBSYNCKEY");
        List<SysConfig> configList = sysConfigMapper.queryList(configParam);
        if (CollectionUtils.isEmpty(configList) || !"on".equals(configList.get(0).getConfigValue())) {
            return;
        }

        // 查询所有未同步的资源目录
        TableInfo param = new TableInfo();
        // 同步db数据状态位
        param.setIsDbSynced(0);
        param.setDbSync(1);
        List<TableInfo> tableInfoList = tableInfoMapper.queryByParam(param);
        // 1.通过得到字节码对象的方式加载静态代码块,从而注册驱动程序
        try {
            Class.forName("com.mysql.jdbc.Driver");
            // 循环资源目录 新建jdbc连接并查询
            if (!CollectionUtils.isEmpty(tableInfoList)) {
                for (TableInfo tableInfo : tableInfoList) {
                    Connection conn = null;
                    Statement stmt = null;
                    ResultSet rs = null;
                    try {
                        // 查询表数据结果
                        List<TableFiled> tableFiledList = tableFiledMapper.selectAllList(tableInfo.getId());
                        String url = "jdbc:mysql://" + tableInfo.getRemoteIp() + tableInfo.getRemoteBase() + "?useUnicode=true&characterEncoding=UTF-8&useSSL=false&zeroDateTimeBehavior=convertToNull";
                        conn = DriverManager.getConnection(url, tableInfo.getRemoteUser(), tableInfo.getRemotePassword());
                        int pageIndex = 1;
                        int pageSize = 500;
                        stmt = conn.createStatement();

                        // 删除表内记录
                        publicMapper.publicDeleteBatch(tableInfo.getEname());

                        rs = stmt.executeQuery("select * from " + tableInfo.getEname() + " limit " + ((pageIndex - 1) * pageSize) + "," + pageSize);

                        while (rs.next()) {
                            List<List> insertList = Lists.newArrayList();
                            List<Object> temp = Lists.newArrayList();
                            for (TableFiled tableFiled : tableFiledList) {
                                temp.add(rs.getString(tableFiled.getEname()));
                            }
                            insertList.add(temp);
                            while (rs.next()) {
                                temp = Lists.newArrayList();
                                for (TableFiled tableFiled : tableFiledList) {
                                    temp.add(rs.getInt(tableFiled.getEname()));
                                }
                                insertList.add(temp);
                            }

                            // 编辑新增sql
                            publicMapper.publicInsertBatch(tableFiledList, insertList, tableInfo.getEname());

                            pageIndex++;
                            rs = stmt.executeQuery("select * from " + tableInfo.getEname() + " limit " + ((pageIndex - 1) * pageSize) + "," + pageSize);
                        }

                        // 更新es同步状态 es应该重新建个index模型DTO类吧
                        tableInfo.setIsSync(0);

                        // 更新数据库同步状态
                        tableInfo.setIsDbSynced(1);
                        tableInfoMapper.updateById(tableInfo);
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    } finally {
                        try {
                            assert rs != null;
                            rs.close();
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                        try {
                            stmt.close();
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                        try {
                            conn.close();
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值