欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于MybatisPlus將百度天氣數(shù)據(jù)存儲(chǔ)至PostgreSQL數(shù)據(jù)庫

 更新時(shí)間:2025年08月21日 09:43:47   作者:夜郎king  
這篇文章主要為大家詳細(xì)介紹了如何基于MybatisPlus將百度天氣數(shù)據(jù)存儲(chǔ)至PostgreSQL數(shù)據(jù)庫,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

前言

在當(dāng)今數(shù)字化時(shí)代,數(shù)據(jù)的獲取、存儲(chǔ)與分析已成為推動(dòng)眾多領(lǐng)域發(fā)展的重要驅(qū)動(dòng)力。天氣數(shù)據(jù)作為一種極具價(jià)值的公共資源,廣泛應(yīng)用于農(nóng)業(yè)、交通、旅游、能源等諸多行業(yè)。百度天氣作為國內(nèi)知名的天氣信息服務(wù)平臺(tái),提供了豐富且實(shí)時(shí)的天氣數(shù)據(jù),若能將其有效存儲(chǔ)并加以利用,將為相關(guān)業(yè)務(wù)決策、研究分析等提供有力支持。將百度天氣數(shù)據(jù)存儲(chǔ)至 PostgreSQL 數(shù)據(jù)庫,這一過程涉及數(shù)據(jù)的獲取、解析、轉(zhuǎn)換以及最終的存儲(chǔ)等多個(gè)環(huán)節(jié)。通過 MybatisPlus 框架來實(shí)現(xiàn)這一目標(biāo),不僅可以充分利用其強(qiáng)大的 ORM(對(duì)象關(guān)系映射)能力,將天氣數(shù)據(jù)的實(shí)體類與數(shù)據(jù)庫表結(jié)構(gòu)進(jìn)行高效映射,還能借助其提供的便捷操作接口,輕松完成數(shù)據(jù)的增刪改查等操作。此外,MybatisPlus 的分頁插件、緩存機(jī)制等特性,也能夠在處理大量天氣數(shù)據(jù)時(shí),有效提升系統(tǒng)的性能和響應(yīng)速度,確保數(shù)據(jù)存儲(chǔ)過程的高效與穩(wěn)定。

這一實(shí)踐過程不僅涉及到技術(shù)層面的多種知識(shí)融合,如網(wǎng)絡(luò)編程、JSON 數(shù)據(jù)處理、數(shù)據(jù)庫操作以及框架應(yīng)用等,還面臨著諸如數(shù)據(jù)準(zhǔn)確性、存儲(chǔ)效率、系統(tǒng)穩(wěn)定性等諸多挑戰(zhàn)。通過對(duì)整個(gè)流程的深入研究和實(shí)踐探索,我們希望能夠?yàn)殚_發(fā)者提供一套完整、高效且可靠的解決方案,助力大家更好地利用百度天氣數(shù)據(jù),挖掘其潛在價(jià)值,同時(shí)也為類似的數(shù)據(jù)存儲(chǔ)項(xiàng)目提供有益的參考和借鑒。力求以清晰、易懂的方式,帶領(lǐng)讀者逐步深入這一實(shí)踐過程,共同探索數(shù)據(jù)存儲(chǔ)與應(yīng)用的無限可能。本文重點(diǎn)在于詳細(xì)得介紹在MybatisPlus框架中如何將百度天氣信息持久化到PG數(shù)據(jù)庫中,首先介紹具體的PG數(shù)據(jù)庫的設(shè)計(jì)與實(shí)現(xiàn),然后詳細(xì)介紹MybatisPlus的實(shí)體設(shè)計(jì),最后以源代碼的形式介紹如何將數(shù)據(jù)進(jìn)行持久化,期待為大家提供一種良好的解決方案和參考。

一、PG數(shù)據(jù)庫設(shè)計(jì)與實(shí)現(xiàn)

PostgreSQL 作為一種功能強(qiáng)大的開源關(guān)系型數(shù)據(jù)庫管理系統(tǒng),以其出色的性能、高度的可擴(kuò)展性以及對(duì)復(fù)雜數(shù)據(jù)類型的良好支持,受到越來越多開發(fā)者的青睞。它能夠高效地存儲(chǔ)和管理大規(guī)模數(shù)據(jù),為數(shù)據(jù)的查詢、分析與挖掘提供了堅(jiān)實(shí)基礎(chǔ)。本節(jié)首先將以存儲(chǔ)百度天氣數(shù)據(jù)為背景,介紹如何進(jìn)行數(shù)據(jù)庫的物理表設(shè)計(jì),為了方便大家學(xué)習(xí),這里可以分享相關(guān)的建表語句。

1、PG數(shù)據(jù)庫模型設(shè)計(jì)

在前一篇的內(nèi)容中,我們?cè)敿?xì)的介紹了如何將百度天氣的返回?cái)?shù)據(jù)轉(zhuǎn)換為JavaBean,GSON 框架下百度天氣 JSON 數(shù)據(jù)轉(zhuǎn) JavaBean 的實(shí)戰(zhàn)攻略。在實(shí)際應(yīng)用系統(tǒng)查詢過程中,我們除了要定義JavaBean,也需要定義用來保存天氣數(shù)據(jù)的數(shù)據(jù)庫模型。根據(jù)接口文檔,我們可以按照業(yè)務(wù)維度設(shè)計(jì)以下數(shù)據(jù)庫模型,分別用來存儲(chǔ)天氣接口返回的數(shù)據(jù):

2、存儲(chǔ)天氣物理表結(jié)構(gòu)

這里將根據(jù)上面的物理模型創(chuàng)建相應(yīng)的天氣信息表,這里將分別給出這五張表的物理結(jié)構(gòu)語句。存儲(chǔ)實(shí)時(shí)天氣信息:

create table biz_weather_now (
   pk_id                INT8                 not null,
   location_code        VARCHAR(6)           not null default '',
   temp                 DECIMAL(8,2)         not null default 999999,
   feels_like           DECIMAL(8,2)         not null default 999999,
   rh                   DECIMAL(8,2)         not null default 999999,
   wind_class           VARCHAR(10)          null,
   wind_dir             VARCHAR(10)          null,
   text                 VARCHAR(50)          null,
   prec_1h              DECIMAL(8,2)         null default 999999,
   clouds               DECIMAL(8,2)         null default 999999,
   vis                  DECIMAL(8,2)         null default 999999,
   aqi                  DECIMAL(8,2)         null default 999999,
   pm25                 DECIMAL(8,2)         null default 999999,
   pm10                 DECIMAL(8,2)         null default 999999,
   no2                  DECIMAL(8,2)         null default 999999,
   so2                  DECIMAL(8,2)         null default 999999,
   o3                   DECIMAL(8,2)         null default 999999,
   co                   DECIMAL(8,2)         null default 999999,
   uptime               TIMESTAMP            null,
   constraint PK_BIZ_WEATHER_NOW primary key (pk_id)
);
comment on table biz_weather_now is
'存儲(chǔ)實(shí)時(shí)天氣信息';
comment on column biz_weather_now.pk_id is
'主鍵';
comment on column biz_weather_now.location_code is
'行政區(qū)劃code';
comment on column biz_weather_now.temp is
'溫度(℃)';
comment on column biz_weather_now.feels_like is
'體感溫度(℃)';
comment on column biz_weather_now.rh is
'相對(duì)濕度(%)';
comment on column biz_weather_now.wind_class is
'風(fēng)力等級(jí)';
comment on column biz_weather_now.wind_dir is
'風(fēng)向描述';
comment on column biz_weather_now.text is
'天氣現(xiàn)象';
comment on column biz_weather_now.prec_1h is
'1小時(shí)累計(jì)降水量(mm)';
comment on column biz_weather_now.clouds is
'云量(%)';
comment on column biz_weather_now.vis is
'能見度(m)';
comment on column biz_weather_now.aqi is
'空氣質(zhì)量指數(shù)數(shù)值';
comment on column biz_weather_now.pm25 is
'pm2.5濃度(μg/m3)';
comment on column biz_weather_now.pm10 is
'pm10濃度(μg/m3)';
comment on column biz_weather_now.no2 is
'二氧化氮濃度(μg/m3)';
comment on column biz_weather_now.so2 is
'二氧化硫濃度(μg/m3)';
comment on column biz_weather_now.o3 is
'臭氧濃度(μg/m3)';
comment on column biz_weather_now.co is
'一氧化碳濃度(mg/m3)';
comment on column biz_weather_now.uptime is
'數(shù)據(jù)更新時(shí)間,北京時(shí)間';

這里需要注意的是,在保存相應(yīng)的如氣溫或者指數(shù)時(shí),采用的數(shù)據(jù)庫類型不是Int,這也是為了兼容可能的出現(xiàn)浮點(diǎn)數(shù)的情況,而小數(shù)位,這里初步設(shè)計(jì)成2位,大家可以根據(jù)實(shí)際需求進(jìn)行調(diào)整。

天氣警報(bào)信息表:

create table biz_weather_alerts (
   pk_id                INT8                 not null,
   weather_pk_id        INT8                 null,
   type                 VARCHAR(20)          null,
   level                VARCHAR(10)          null,
   title                VARCHAR(128)         null,
   desc_info            VARCHAR(512)         null,
   constraint PK_BIZ_WEATHER_ALERTS primary key (pk_id)
);
comment on table biz_weather_alerts is
'天氣警報(bào)信息表';
comment on column biz_weather_alerts.pk_id is
'主鍵';
comment on column biz_weather_alerts.weather_pk_id is
'實(shí)時(shí)天氣信息主鍵';
comment on column biz_weather_alerts.type is
'預(yù)警事件類型,參考 天氣取值對(duì)照表中的預(yù)警類型';
comment on column biz_weather_alerts.level is
'預(yù)警事件等級(jí)';
comment on column biz_weather_alerts.title is
'預(yù)警標(biāo)題';
comment on column biz_weather_alerts.desc_info is
'預(yù)警詳細(xì)提示信息';

需要注意的是,在警報(bào)信息中,desc字段由于是數(shù)據(jù)庫的關(guān)鍵字,在原始接口中返回的desc我們需要變成desc_info字段,天氣指數(shù)信息表:

create table biz_weather_indexes (
   pk_id                INT8                 not null,
   weather_pk_id        INT8                 null,
   name                 VARCHAR(10)          null,
   brief                VARCHAR(10)          null,
   detail               VARCHAR(128)         null,
   constraint PK_BIZ_WEATHER_INDEXES primary key (pk_id)
);
comment on column biz_weather_indexes.pk_id is
'主鍵';
comment on column biz_weather_indexes.weather_pk_id is
'實(shí)時(shí)天氣信息主鍵';
comment on column biz_weather_indexes.name is
'生活指數(shù)中文名稱';
comment on column biz_weather_indexes.brief is
'生活指數(shù)概要說明';
comment on column biz_weather_indexes.detail is
'生活指數(shù)詳細(xì)說明';

天氣預(yù)報(bào)信息表:

create table biz_weather_forecasts (
   pk_id                INT8                 not null,
   weather_pk_id        INT8                 null,
   date                 TIMESTAMP            null,
   week                 VARCHAR(3)           null,
   high                 DECIMAL(8,2)         null default 999999,
   low                  DECIMAL(8,2)         null default 999999,
   wc_day               VARCHAR(10)          null,
   wc_night             VARCHAR(10)          null,
   wd_day               VARCHAR(10)          null,
   wd_night             VARCHAR(10)          null,
   text_day             VARCHAR(20)          null,
   text_night           VARCHAR(20)          null,
   constraint PK_BIZ_WEATHER_FORECASTS primary key (pk_id)
);
comment on column biz_weather_forecasts.pk_id is
'主鍵';
comment on column biz_weather_forecasts.weather_pk_id is
'實(shí)時(shí)天氣信息主鍵';
comment on column biz_weather_forecasts.date is
'日期,北京時(shí)區(qū)';
comment on column biz_weather_forecasts.week is
'星期,北京時(shí)區(qū)';
comment on column biz_weather_forecasts.high is
'最高溫度(℃)';
comment on column biz_weather_forecasts.low is
'最低溫度(℃)';
comment on column biz_weather_forecasts.wc_day is
'白天風(fēng)力';
comment on column biz_weather_forecasts.wc_night is
'晚上風(fēng)力';
comment on column biz_weather_forecasts.wd_day is
'白天風(fēng)向';
comment on column biz_weather_forecasts.wd_night is
'晚上風(fēng)向';
comment on column biz_weather_forecasts.text_day is
'白天天氣現(xiàn)象,參考天氣取值對(duì)照表';
comment on column biz_weather_forecasts.text_night is
'晚上天氣現(xiàn)象';

小時(shí)天氣預(yù)報(bào)表:

create table biz_weather_forecast_hours (
   pk_id                INT8                 not null,
   weather_pk_id        INT8                 null,
   text                 VARCHAR(20)          null,
   temp_fc              DECIMAL(8,2)         null,
   wind_class           VARCHAR(10)          null,
   wind_dir             VARCHAR(10)          null,
   rh                   DECIMAL(8,2)         null,
   prec_1h              NUMERIC(8,2)         null,
   clouds               DECIMAL(8,2)         null,
   data_time            TIMESTAMP            null,
   constraint PK_BIZ_WEATHER_FORECAST_HOURS primary key (pk_id)
);
comment on column biz_weather_forecast_hours.pk_id is
'主鍵';
comment on column biz_weather_forecast_hours.weather_pk_id is
'實(shí)時(shí)天氣信息主鍵';
comment on column biz_weather_forecast_hours.text is
'天氣現(xiàn)象';
comment on column biz_weather_forecast_hours.temp_fc is
'溫度(℃)';
comment on column biz_weather_forecast_hours.wind_class is
'風(fēng)力等級(jí)';
comment on column biz_weather_forecast_hours.wind_dir is
'風(fēng)向描述';
comment on column biz_weather_forecast_hours.rh is
'相對(duì)濕度';
comment on column biz_weather_forecast_hours.prec_1h is
'1小時(shí)累計(jì)降水量(mm)';
comment on column biz_weather_forecast_hours.clouds is
'云量(%)';
comment on column biz_weather_forecast_hours.data_time is
'數(shù)據(jù)時(shí)間';

二、MybatisPlus中實(shí)體實(shí)現(xiàn)

MybatisPlus 則是基于 Mybatis 的增強(qiáng)版本,它不僅繼承了 Mybatis 的靈活性和易用性,還通過一系列插件和擴(kuò)展功能,極大地簡化了數(shù)據(jù)庫操作流程,提高了開發(fā)效率,尤其在處理復(fù)雜業(yè)務(wù)邏輯和數(shù)據(jù)持久化方面表現(xiàn)出色。在之前的博客中已經(jīng)介紹了相關(guān)類的設(shè)計(jì),下面將結(jié)合MybatisPlus來深入講解這些實(shí)體的設(shè)計(jì),分別從類圖和具體實(shí)現(xiàn)兩個(gè)方面展開。

1、MybatisPlus實(shí)體類圖設(shè)計(jì)

通過分析原來的接口以及梳理對(duì)象的關(guān)系,可以整理出相關(guān)的數(shù)據(jù)關(guān)系,比如實(shí)時(shí)天氣信息與預(yù)警信息、生活指數(shù)信息、逐日預(yù)報(bào)信息和逐小時(shí)預(yù)報(bào)信息均是一對(duì)多的情況。

2、MybatisPlus中相關(guān)類實(shí)現(xiàn)

由于篇幅有限,這里將前面的內(nèi)容中剩下的幾個(gè)JavaBean的代碼給出,在之前的內(nèi)容中已經(jīng)對(duì)BdWeatherDTO.java、WeatherInfoDTO.java、WeatherIndexes.java進(jìn)行了說明。預(yù)警信息JavaBean如下:

package com.yelang.project.weather.domain;
import java.io.Serializable;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.google.gson.annotations.SerializedName;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
@TableName(value = "biz_weather_alerts")
@NoArgsConstructor
@AllArgsConstructor
@Setter
@Getter
@ToString
public class WeatherAlerts implements Serializable{
	private static final long serialVersionUID = 9180445236146165608L;
	@TableId(value ="pk_id")
	private Long pkId ;//主鍵
	@TableField(value="weather_pk_id")
	private Long weatherPkId;//實(shí)時(shí)天氣信息主鍵
	private String type;//預(yù)警事件類型,參考 天氣取值對(duì)照表中的預(yù)警類型
	private String level;//預(yù)警事件等級(jí)
	private String title;//預(yù)警標(biāo)題
	@TableField(value="desc_info")
	@SerializedName("desc")
	private String descInfo;//預(yù)警詳細(xì)提示信息	
}

逐日天氣預(yù)報(bào)JavaBean代碼如下:

package com.yelang.project.weather.domain;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.google.gson.annotations.SerializedName;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
@TableName(value = "biz_weather_forecasts")
@NoArgsConstructor
@AllArgsConstructor
@Setter
@Getter
@ToString
public class WeatherForecasts implements Serializable{
	private static final long serialVersionUID = 4929733391294381721L;
	@TableId(value ="pk_id")
	private Long pkId ;//主鍵
	@TableField(value="weather_pk_id")
	private Long weatherPkId;//實(shí)時(shí)天氣信息主鍵
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	private Date date;//日期,北京時(shí)區(qū)
	private String week;//星期,北京時(shí)區(qū)
	private BigDecimal high = new BigDecimal(999999);//最高溫度(℃)
	private BigDecimal low = new BigDecimal(999999);//最低溫度(℃)
	@TableField(value="wc_day")
	@SerializedName("wc_day")
	private String wcDay;//白天風(fēng)力
	@TableField(value="wc_night")
	@SerializedName("wc_night")
	private String wcNight;//晚上風(fēng)力
	@TableField(value="wd_day")
	@SerializedName("wd_day")
	private String wdDay;//白天風(fēng)向
	@TableField(value="wd_night")
	@SerializedName("wd_night")
	private String wdNight;//晚上風(fēng)向
	@TableField(value="text_day")
	@SerializedName("text_day")
	private String textDay;//白天天氣現(xiàn)象,參考天氣取值對(duì)照表
	@TableField(value="text_night")
	@SerializedName("text_night")
	private String textNight;//晚上天氣現(xiàn)象
}

最后還有逐小時(shí)的天氣預(yù)報(bào)信息JavaBean:

package com.yelang.project.weather.domain;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.google.gson.annotations.SerializedName;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
@TableName(value = "biz_weather_forecast_hours")
@NoArgsConstructor
@AllArgsConstructor
@Setter
@Getter
@ToString
public class WeatherForecastHours implements Serializable{
	private static final long serialVersionUID = -18102820381727782L;
	@TableId(value ="pk_id")
	private Long pkId ;//主鍵
	@TableField(value="weather_pk_id")
	private Long weatherPkId;//實(shí)時(shí)天氣信息主鍵
	private String text;//天氣現(xiàn)象
	@TableField(value="temp_fc")
	@SerializedName("temp_fc")
	private BigDecimal tempFc;//溫度(℃)
	@TableField(value="wind_class")
	@SerializedName("wind_class")
	private String windClass;//風(fēng)力等級(jí)
	@TableField(value="wind_dir")
	@SerializedName("wind_dir")
	private String windDir;//風(fēng)向描述
	private BigDecimal rh;//相對(duì)濕度
	@TableField(value="prec_1h")
	@SerializedName("prec_1h")
	private BigDecimal prec1h;//1小時(shí)累計(jì)降水量(mm)
	private BigDecimal clouds;//云量(%)
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	@TableField(value="data_time")
	@SerializedName("data_time")
	private Date dataTime;//數(shù)據(jù)時(shí)間
}

經(jīng)過以上的步驟,我們就完成了全部的百度天氣相關(guān)的JavaBean的定義,接下來我們將詳細(xì)介紹如何實(shí)現(xiàn)將數(shù)據(jù)持久化到PG數(shù)據(jù)庫中。

三、PostgreSQL持久化實(shí)現(xiàn)

在實(shí)際開發(fā)過程中,我們首先需要通過百度天氣 API 獲取原始的天氣數(shù)據(jù),這些數(shù)據(jù)通常以 JSON 格式返回,包含了城市、日期、溫度、濕度、風(fēng)力、天氣狀況等諸多信息。隨后,利用 Java 等編程語言對(duì)這些 JSON 數(shù)據(jù)進(jìn)行解析,將其轉(zhuǎn)換為符合數(shù)據(jù)庫存儲(chǔ)要求的結(jié)構(gòu)化數(shù)據(jù)。接著,借助 MybatisPlus 框架,將這些結(jié)構(gòu)化數(shù)據(jù)映射到對(duì)應(yīng)的實(shí)體類中,并通過配置 MybatisPlus 的 SQL 會(huì)話工廠、數(shù)據(jù)源以及相關(guān)映射文件,實(shí)現(xiàn)與 PostgreSQL 數(shù)據(jù)庫的連接和數(shù)據(jù)的持久化存儲(chǔ)。關(guān)于如何采集數(shù)據(jù)、JSON數(shù)據(jù)轉(zhuǎn)換的內(nèi)容在之前的博客中都有所涉及,接下來我們來講解最后一個(gè)環(huán)節(jié),即如何將采集和轉(zhuǎn)換后的數(shù)據(jù)持久到PG數(shù)據(jù)庫中。

1、天氣數(shù)據(jù)入庫時(shí)序圖

上圖詳細(xì)的展示了如何在可運(yùn)行程序中集成百度接口,采用時(shí)序圖的方式進(jìn)行了詳細(xì)交互過程的闡述。整個(gè)過程一共分為11個(gè)步驟,從入口程序調(diào)用開始,首先調(diào)用百度地圖的接口,然后百度地圖返回JSON結(jié)果,程序解析JSON反序列化成DTO的JavaBean,然后調(diào)用天氣服務(wù)類,分別實(shí)現(xiàn)實(shí)時(shí)天氣、預(yù)警信息、生活指數(shù)信息、逐日預(yù)報(bào)信息、逐小時(shí)預(yù)報(bào)信息的PG入庫,最后返回寫入狀態(tài)。時(shí)序圖在繪制時(shí),為了美觀在Service的名稱上有一定省略,希望不影響閱讀。

2、調(diào)用程序執(zhí)行

關(guān)于MybatisPlus中涉及的Mapper、Service及其實(shí)現(xiàn)類在此不再進(jìn)行贅述,通用的程序都比較簡單,這里將核心的天氣程序入口和持久化入口以及具體的天氣信息入庫方法在這里跟大家分享。入口核心程序如下所示:

package com.yelang.project.unihttp;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.burukeyou.uniapi.http.core.response.HttpResponse;
import com.google.gson.Gson;
import com.yelang.project.thridinterface.BaiduWeatherApiServcie;
import com.yelang.project.weather.domain.BdWeatherDTO;
import com.yelang.project.weather.domain.WeatherInfoDTO;
import com.yelang.project.weather.service.IWeatherNowService;
@SpringBootTest
@RunWith(SpringRunner.class)
public class BaiduWeather2DBCase {
	private static final String DATA_TYPE = "all";
	@Autowired
	private BaiduWeatherApiServcie baiduWeatherApiService;
	@Autowired
	private IWeatherNowService weatherService;
	@Test
	public void bdWeather2PG() {
		String district_id = "430626";//表示具體的行政區(qū)劃代號(hào)
		HttpResponse<String> result  = baiduWeatherApiService.getWeather(district_id, DATA_TYPE);
		System.out.println(result.getBodyResult());
		Gson gson = new Gson();
		BdWeatherDTO bdWeatherInfo = gson.fromJson(result.getBodyResult(), BdWeatherDTO.class);
		WeatherInfoDTO bdResult = bdWeatherInfo.getResult();
		//將天氣信息持久化到數(shù)據(jù)庫中
		if(null != bdResult) {
			Long weatherId = IdWorker.getId();
			bdResult.getWeatherNow().setPkId(weatherId);
			bdResult.getWeatherNow().setLocationCode(district_id);
			weatherService.insertWeatherInfo(bdResult);
		}
	}
}

為了實(shí)現(xiàn)天氣數(shù)據(jù)的入庫,在保存相關(guān)的生活指數(shù)信息、預(yù)警信息、逐日/小時(shí)天氣預(yù)報(bào)信息時(shí),我們還要將實(shí)時(shí)天氣主鍵給相關(guān)的子表賦值,這里給出天氣信息的持久化詳細(xì)方法:

package com.yelang.project.weather.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yelang.common.utils.StringUtils;
import com.yelang.project.weather.domain.WeatherAlerts;
import com.yelang.project.weather.domain.WeatherForecastHours;
import com.yelang.project.weather.domain.WeatherForecasts;
import com.yelang.project.weather.domain.WeatherIndexes;
import com.yelang.project.weather.domain.WeatherInfoDTO;
import com.yelang.project.weather.domain.WeatherNow;
import com.yelang.project.weather.mapper.WeatherNowMapper;
import com.yelang.project.weather.service.IWeatherAlertsService;
import com.yelang.project.weather.service.IWeatherForecastHoursService;
import com.yelang.project.weather.service.IWeatherForecastsService;
import com.yelang.project.weather.service.IWeatherIndexesService;
import com.yelang.project.weather.service.IWeatherNowService;
@Service
public class WeatherNowServiceImpl extends ServiceImpl<WeatherNowMapper, WeatherNow> implements IWeatherNowService{
	@Autowired
	private IWeatherAlertsService alertsService;
	@Autowired
	private IWeatherForecastHoursService forecastHoursService;
	@Autowired
	private IWeatherForecastsService forecastsService;
	@Autowired
	private IWeatherIndexesService indexesService;
	@Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class)
	@Override
	public void insertWeatherInfo(WeatherInfoDTO infoDto) {
		WeatherNow now = infoDto.getWeatherNow();
		//step1、保存實(shí)時(shí)天氣信息
		this.baseMapper.insert(now);
		Long weatherId = now.getPkId();
		//step2、保存生活指數(shù)信息
		List<WeatherAlerts> alerts = infoDto.getAlerts();
		if(StringUtils.isNotEmpty(alerts)) {
			for(WeatherAlerts alert : alerts) {
				alert.setWeatherPkId(weatherId);
			}
			alertsService.saveBatch(alerts);
		}
		//step3、保存預(yù)警信息
		List<WeatherIndexes> indexes = infoDto.getIndexes();
		if(StringUtils.isNotEmpty(indexes)) {
			for(WeatherIndexes index : indexes) {
				index.setWeatherPkId(weatherId);
			}
			indexesService.saveBatch(indexes);
		}
		//step4、保存天氣預(yù)報(bào)
		List<WeatherForecasts> forecasts = infoDto.getForecasts();
		if(StringUtils.isNotEmpty(forecasts)) {
			for(WeatherForecasts forecast : forecasts) {
				forecast.setWeatherPkId(weatherId);
			}
			forecastsService.saveBatch(forecasts);
		}
		//step5、保存逐小時(shí)天氣預(yù)報(bào)
		List<WeatherForecastHours> forecastHours = infoDto.getForecastHours();
		if(StringUtils.isNotEmpty(forecastHours)) {
			for(WeatherForecastHours forecastHour : forecastHours) {
				forecastHour.setWeatherPkId(weatherId);
			}
			forecastHoursService.saveBatch(forecastHours);
		}
	}
}

至此基本完成了調(diào)用程序和入口程序的編寫,接下來在PG數(shù)據(jù)庫中看一下實(shí)際的效果。

3、數(shù)據(jù)入庫結(jié)果

執(zhí)行以上程序可以在控制臺(tái)中看到以下輸出,說明正常的向PG數(shù)據(jù)庫進(jìn)行數(shù)據(jù)持久化。

最后我們到數(shù)據(jù)庫中查詢一下是否將數(shù)據(jù)成功的寫入,執(zhí)行的SQL如下:

select * from biz_weather_now;

select * from biz_weather_alerts;

select * from biz_weather_forecasts 
where weather_pk_id = 1955642733221658626;

實(shí)時(shí)天氣數(shù)據(jù)

逐小時(shí)天氣預(yù)報(bào)

四、總結(jié)

本文重點(diǎn)在于詳細(xì)得介紹在MybatisPlus框架中如何將百度天氣信息持久化到PG數(shù)據(jù)庫中,首先介紹具體的PG數(shù)據(jù)庫的設(shè)計(jì)與實(shí)現(xiàn),然后詳細(xì)介紹MybatisPlus的實(shí)體設(shè)計(jì),最后以源代碼的形式介紹如何將數(shù)據(jù)進(jìn)行持久化,期待為大家提供一種良好的解決方案和參考。這一實(shí)踐過程不僅涉及到技術(shù)層面的多種知識(shí)融合,如網(wǎng)絡(luò)編程、JSON 數(shù)據(jù)處理、數(shù)據(jù)庫操作以及框架應(yīng)用等,還面臨著諸如數(shù)據(jù)準(zhǔn)確性、存儲(chǔ)效率、系統(tǒng)穩(wěn)定性等諸多挑戰(zhàn)。通過對(duì)整個(gè)流程的深入研究和實(shí)踐探索,我們希望能夠?yàn)殚_發(fā)者提供一套完整、高效且可靠的解決方案,助力大家更好地利用百度天氣數(shù)據(jù),挖掘其潛在價(jià)值,同時(shí)也為類似的數(shù)據(jù)存儲(chǔ)項(xiàng)目提供有益的參考和借鑒。

以上就是基于MybatisPlus將百度天氣數(shù)據(jù)存儲(chǔ)至PostgreSQL數(shù)據(jù)庫的詳細(xì)內(nèi)容,更多關(guān)于MybatisPlus數(shù)據(jù)存儲(chǔ)至PostgreSQL數(shù)據(jù)庫的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • SpringBoot項(xiàng)目為何引入大量的starter?如何自定義starter?

    SpringBoot項(xiàng)目為何引入大量的starter?如何自定義starter?

    這篇文章主要介紹了SpringBoot項(xiàng)目為何引入大量的starter?如何自定義starter?文章基于這兩個(gè)問題展開全文,需要的小伙伴可以參考一下
    2022-04-04
  • Java數(shù)組的定義、初始化、及二維數(shù)組用法分析

    Java數(shù)組的定義、初始化、及二維數(shù)組用法分析

    這篇文章主要介紹了Java數(shù)組的定義、初始化、及二維數(shù)組用法,結(jié)合具體實(shí)例形式分析了java數(shù)組概念、功能、數(shù)組定義、靜態(tài)數(shù)組、動(dòng)態(tài)數(shù)組、二維數(shù)組等相關(guān)使用技巧,需要的朋友可以參考下
    2019-01-01
  • 淺析SpringBoot自動(dòng)化配置原理實(shí)現(xiàn)

    淺析SpringBoot自動(dòng)化配置原理實(shí)現(xiàn)

    這篇文章主要介紹了淺析SpringBoot自動(dòng)化配置原理實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • Spring Data JPA 設(shè)置字段默認(rèn)值方式

    Spring Data JPA 設(shè)置字段默認(rèn)值方式

    這篇文章主要介紹了Spring Data JPA設(shè)置字段默認(rèn)值方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • Java8語法糖之Lambda表達(dá)式的深入講解

    Java8語法糖之Lambda表達(dá)式的深入講解

    這篇文章主要給大家介紹了關(guān)于Java8語法糖之Lambda表達(dá)式的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • 教你如何使用JAVA POI

    教你如何使用JAVA POI

    今天教大家怎么學(xué)習(xí)JAVA POI的用法,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴們有很好地幫助,需要的朋友可以參考下
    2021-05-05
  • Java8內(nèi)存模型PermGen Metaspace實(shí)例解析

    Java8內(nèi)存模型PermGen Metaspace實(shí)例解析

    這篇文章主要介紹了Java8內(nèi)存模型PermGen Metaspace實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-03-03
  • java解析JSON數(shù)據(jù)詳解

    java解析JSON數(shù)據(jù)詳解

    這篇文章主要介紹了java解析JSON數(shù)據(jù)詳解,具有一定借鑒價(jià)值,需要的朋友可以參考下。
    2017-12-12
  • 代碼實(shí)例Java IO判斷目錄和文件是否存在

    代碼實(shí)例Java IO判斷目錄和文件是否存在

    本篇文章給大家分享了Java IO判斷目錄和文件是否存在的代碼,對(duì)此有需要的讀者們可以跟著小編一起學(xué)習(xí)下。
    2018-02-02
  • java多線程-讀寫鎖原理

    java多線程-讀寫鎖原理

    本文主要介紹java多線程的知識(shí),這里整理了相關(guān)資料及簡單示例代碼,有興趣的小伙伴可以參考下
    2016-09-09

最新評(píng)論