IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Java知识库 -> Spring Boot实现数据同步 -> 正文阅读

[Java知识库]Spring Boot实现数据同步

问题描述:

实际项目开发过程中,需要将第三方数据库中的部门同步到当前部门下,两个数据库部门表的字段有很多不同。

方法一

Controller层:

@PostMapping("/syncDepartmentMsg")
    public ResultNoEncryptVo<Object> syncDepartmentMsg() {

        Integer line = departmentService.syncDepartmentMsg();
        return ResultNoEncryptVo.ok(line);
    }

Service层:

   Integer syncDepartmentMsg();

ServiceImpl层:

@Override
    public Integer syncDepartmentMsg() {

        HttpResultDepartmentVo httpResultDepartmentVo;
        int isSuccess = 1;

        try {
            String departmentInfo = HttpClient.httpGet(syncDepartmentInfo,"29");
            httpResultDepartmentVo = JSONObject.parseObject(departmentInfo, HttpResultDepartmentVo.class);
            List<DepartmentRemoteVo> data = httpResultDepartmentVo.getData();
            departmentMapper.insertOrUpdateDepartmentBatch(data);
        } catch (Exception e) {
            e.printStackTrace();
            isSuccess = 0;
            log.error("同步用户数据异常: orgId -> {}, catch ->",  e);
        }

        return isSuccess;
    }

Mapper层

@Mapper
public interface DepartmentMapper extends BaseMapper<DepartmentVo> {

    void insertOrUpdateDepartmentBatch(@Param("departmentRemoteVo") List<DepartmentRemoteVo> data);
}

xml:

<insert id="insertOrUpdateDepartmentBatch">
        insert into `answer_department`( id,....
        )
        values
        <foreach collection="departmentRemoteVo" item="departmentVo" separator=",">
            (
            #{departmentVo.id},
           ......
            )
        </foreach>
        ON DUPLICATE KEY UPDATE sort = values(sort), bind_user_id = values(bind_user_id), cate_id = values(cate_id),
       .....

方法二

@RequestMapping("/dataSynchronization")
public class DataSynchronizationController extends BaseManageController{
    /**
     * 部门同步接口
     */
//    @Scheduled(cron = "0 */3 * * * ?") // 三分钟执行一次
//    @Scheduled(cron = "0/10 * * * * ?") // 三分钟执行一次  定时任务
    @Transactional(rollbackFor = Throwable.class)
    public void pullDataNew() {
        String result = HttpClient.httpGet("http://app.cq.qiludev.com/v1/osps/app/department/getGovaskDepartment?fieldType=all","29");
        JSONObject jsonObject = JSON.parseObject(result);
        log.info(jsonObject.toJSONString());
        if (Objects.nonNull(jsonObject)) {
            JSONArray data = jsonObject.getJSONArray("data");
            List<DepartmentRemoteVo> list = data.toJavaList(DepartmentRemoteVo.class);
            if (CollectionUtils.isNotEmpty(list)) {

                //查询数据库已有的数据
                List<DepartmentVo> existList = departmentService.list();
                if (CollectionUtils.isNotEmpty(existList)) {
                    //存在map
                    Map<Integer, DepartmentVo> existMap = existList.stream().collect(Collectors.toMap(DepartmentVo::getId,DepartmentVo->DepartmentVo));
                    //传输map
                    Map<Integer, DepartmentRemoteVo> dataMap = list.stream().collect(Collectors.toMap(DepartmentRemoteVo::getId, DepartmentRemoteVo->DepartmentRemoteVo));

                    List<DepartmentVo> variable = existList.stream().filter(department -> dataMap.get(department.getId()) != null
                            && !dataMap.get(department.getId()).getDepartment().equals(department.getDepartment())
                    ).collect(Collectors.toList());
                    if (CollectionUtils.isNotEmpty(variable)) {
                        variable.forEach(department -> {
                            departmentService.removeById(department.getId());
                            log.info("删除的id");
                        });
                    }
                    //增量
                    List<DepartmentRemoteVo> increment = list.stream().filter(department -> existMap.get(department.getId()) == null).collect(Collectors.toList());
                    if (CollectionUtils.isNotEmpty(increment)) {
                        departmentRemoteService.saveBatch(increment);
                    }
                } else {
                    departmentRemoteService.saveBatch(list);
                }
            }
        }

    }

工具类:
HTTPClient连接池

@Component
public class HttpClient {

    /**
     * 池化管理
     */
    private static PoolingHttpClientConnectionManager poolConnManager = null;
    private static CloseableHttpClient httpClient;
    /**
     * 请求器配置
     */
    private static RequestConfig requestConfig;

    static {
        SSLContextBuilder builder = new SSLContextBuilder();
        try {
            builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build());

            // 配置同时支持HTTP和HTTPS协议
            Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
                    .register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", sslsf).build();
            // 初始化连接管理器
            poolConnManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
            // 设置最大连接数
            poolConnManager.setMaxTotal(200);
            // 设置路由
            poolConnManager.setDefaultMaxPerRoute(50);
            // 设置默认超时限制初始化requestConfig
            int socketTimeout = 3000;
            int connectTimeout = 3000;
            int connectionRequestTimeout = 3000;
            requestConfig = RequestConfig.custom().setConnectionRequestTimeout(connectionRequestTimeout)
                    .setSocketTimeout(socketTimeout).setConnectTimeout(connectTimeout).build();
            // 初始化HttpClient
            httpClient = getConnection();
            log.info("HttpClient init ..");
        } catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException e) {
            e.printStackTrace();
        }

    }

    /**
     * 创建HttpClient对象
     *
     */
    private static CloseableHttpClient getConnection() {
        return HttpClients.custom().setConnectionManager(poolConnManager)
                // 设置请求配置
                .setDefaultRequestConfig(requestConfig)
                // 设置重试次数
                .setRetryHandler(new DefaultHttpRequestRetryHandler(0, false))
                .build();
    }

    //  get  //

    /**
     * HttpGet请求方法 - 基本
     *
     */
    public static String httpGet(String url) {

        HttpGet httpGet = new HttpGet(url);
        CloseableHttpResponse response = null;
        String result = "";
        try {
            response = httpClient.execute(httpGet);
            HttpEntity entity = response.getEntity();
            result = EntityUtils.toString(entity, "utf-8");
            EntityUtils.consume(entity);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (response != null) {
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    /**
     * HttpGet请求方法 - 携带Token
     *
     */
    public static String httpGet(String url, String orgid) {

        HttpGet httpGet = new HttpGet(url);
        httpGet.setHeader("orgId", orgid);
        CloseableHttpResponse response = null;
        String result = "";
        try {
            response = httpClient.execute(httpGet);
            HttpEntity entity = response.getEntity();
            result = EntityUtils.toString(entity, "utf-8");
            EntityUtils.consume(entity);
            log.info("请求(httpGet-token) -> {}", url);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (response != null) {
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    /**
     * HttpGet请求方法 - 请求一县一端
     *
     */
    public static String doGet(String url) {

        HttpGet httpGet = new HttpGet(url);
        httpGet.setHeader("Dubbo-Attachments", "appkey=comment,appsecret=d2ff9b3583507c7e48ed3b1769b89f7d");
        httpGet.setHeader("Accept", "application/json,*/*");
        CloseableHttpResponse response = null;
        String result = "";
        long start = System.currentTimeMillis();
        try {
            response = httpClient.execute(httpGet);
            HttpEntity entity = response.getEntity();
            result = EntityUtils.toString(entity, "utf-8");
            EntityUtils.consume(entity);
            log.info("请求(httpGet) -> {}", url);
        } catch (IOException e) {
            long end = System.currentTimeMillis() - start;
            log.error("请求报错(httpGet) -> {}, 耗时 -> {}ms", url, end);
            e.printStackTrace();
        } finally {
            if (response != null) {
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    /**
     * HttpGet请求方法 
     *
     */
    public static String doGet(String url,String orgid,String token) {

        HttpGet httpGet = new HttpGet(url);
        httpGet.setHeader("Dubbo-Attachments", "appkey=comment,appsecret=d2ff9b3583507c7e48ed3b1769b89f7d");
        /// 后期用这个, 目前测试赞用评论
///        httpGet.setHeader("Dubbo-Attachments", "appkey=listen,appsecret=d36e4ac13a1e8b7eacab7a952578a938");
        httpGet.setHeader("Accept", "application/json,*/*");
        httpGet.setHeader("orgid", orgid);
        httpGet.setHeader("CQ-TOKEN", token);
        CloseableHttpResponse response = null;
        String result = "";
        try {
            response = httpClient.execute(httpGet);
            HttpEntity entity = response.getEntity();
            result = EntityUtils.toString(entity, "utf-8");
            EntityUtils.consume(entity);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (response != null) {
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    //  POST  //

    /**
     * HttpPost请求
     *
     */
    public static String doPost(String url, Map<String, Object> paramsMap) {

        CloseableHttpResponse closeableHttpResponse = null;
        //配置连接超时时间
        RequestConfig requestConfig = RequestConfig.custom()
                .setConnectTimeout(3000)
                .setConnectionRequestTimeout(3000)
                .setSocketTimeout(3000)
                .setRedirectsEnabled(true)
                .build();
        HttpPost httpPost = new HttpPost(url);
        //设置超时时间
        httpPost.setConfig(requestConfig);

        //装配post请求参数
        List<NameValuePair> list = new ArrayList<>();
        for (String key : paramsMap.keySet()) {
            list.add(new BasicNameValuePair(key, String.valueOf(paramsMap.get(key))));
        }

        try {
            //将参数进行编码为合适的格式,如将键值对编码为param1=value1&param2=value2
            UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(list, "utf-8");
            httpPost.setEntity(urlEncodedFormEntity);

            //执行 post请求
            closeableHttpResponse = httpClient.execute(httpPost);
            String strRequest = "";
            if (null != closeableHttpResponse) {
///                System.out.println(closeableHttpResponse.getStatusLine().getStatusCode());
                if (closeableHttpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                    HttpEntity httpEntity = closeableHttpResponse.getEntity();
                    strRequest = EntityUtils.toString(httpEntity);
                } else {
                    strRequest = "Error Response" + closeableHttpResponse.getStatusLine().getStatusCode();
                }
            }
            return strRequest;

        } catch (ClientProtocolException e) {
            e.printStackTrace();
            return "协议异常";
        } catch (ParseException e) {
            e.printStackTrace();
            return "解析异常";
        } catch (IOException e) {
            e.printStackTrace();
            return "传输异常";
        } finally {
            if (closeableHttpResponse != null) {
                try {
                    closeableHttpResponse.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }

}

  Java知识库 最新文章
计算距离春节还有多长时间
系统开发系列 之WebService(spring框架+ma
springBoot+Cache(自定义有效时间配置)
SpringBoot整合mybatis实现增删改查、分页查
spring教程
SpringBoot+Vue实现美食交流网站的设计与实
虚拟机内存结构以及虚拟机中销毁和新建对象
SpringMVC---原理
小李同学: Java如何按多个字段分组
打印票据--java
上一篇文章      下一篇文章      查看所有文章
加:2022-04-07 22:29:52  更:2022-04-07 22:32:52 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/24 4:24:36-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码