From 66041721d9f2b01add7ffa68d83cbec923a0ec52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=AF=86=E7=A0=81=E6=BC=8F=E6=B4=9E?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=AE=8C=E6=88=90?= <> Date: Tue, 24 Sep 2024 00:07:17 +0800 Subject: [PATCH] mybatis to jpa finish --- 20240922/weather/install_oracle_driver.sh | 5 + 20240922/weather/pom.xml | 5 + .../config/dao/DataSourceConfiguration.java | 47 ------- .../config/dao/JuheWeatherProperties.java | 40 ------ .../weather/config/dao/RedisConfig.java | 52 -------- .../dao/SessionFactoryConfiguration.java | 50 -------- .../TransactionManagementConfiguration.java | 34 ----- .../weather/controller/StormController.java | 5 +- .../weather/controller/WeatherController.java | 15 +-- .../java/com/rehome/weather/dao/StormDao.java | 32 ----- .../weather/dao/StormDataRepository.java | 15 +++ .../weather/dao/StormForecastRepository.java | 18 +++ .../weather/dao/StormTrackJpaRepository.java | 17 +++ .../com/rehome/weather/dao/WeatherDao.java | 34 ----- .../rehome/weather/dao/WeatherTypeDao.java | 19 --- .../weather/dao/WeatherTypeRepository.java | 3 + .../rehome/weather/entity/WeatherType.java | 2 +- .../weather/service/ScheduledService.java | 10 +- .../weather/service/WeatherService.java | 2 +- .../service/impl/StormServiceImpl.java | 120 +++++++++++------- .../service/impl/WeatherServiceImpl.java | 54 ++++---- .../service/impl/WeatherTypeServiceImpl.java | 39 ++++-- .../src/main/resources/application.yml | 3 +- .../src/main/resources/mybatis-config.xml | 16 --- 24 files changed, 208 insertions(+), 429 deletions(-) create mode 100644 20240922/weather/install_oracle_driver.sh delete mode 100644 20240922/weather/src/main/java/com/rehome/weather/config/dao/DataSourceConfiguration.java delete mode 100644 20240922/weather/src/main/java/com/rehome/weather/config/dao/JuheWeatherProperties.java delete mode 100644 20240922/weather/src/main/java/com/rehome/weather/config/dao/RedisConfig.java delete mode 100644 20240922/weather/src/main/java/com/rehome/weather/config/dao/SessionFactoryConfiguration.java delete mode 100644 20240922/weather/src/main/java/com/rehome/weather/config/service/TransactionManagementConfiguration.java delete mode 100644 20240922/weather/src/main/java/com/rehome/weather/dao/StormDao.java create mode 100644 20240922/weather/src/main/java/com/rehome/weather/dao/StormDataRepository.java create mode 100644 20240922/weather/src/main/java/com/rehome/weather/dao/StormForecastRepository.java create mode 100644 20240922/weather/src/main/java/com/rehome/weather/dao/StormTrackJpaRepository.java delete mode 100644 20240922/weather/src/main/java/com/rehome/weather/dao/WeatherDao.java delete mode 100644 20240922/weather/src/main/java/com/rehome/weather/dao/WeatherTypeDao.java delete mode 100644 20240922/weather/src/main/resources/mybatis-config.xml diff --git a/20240922/weather/install_oracle_driver.sh b/20240922/weather/install_oracle_driver.sh new file mode 100644 index 0000000..e2e412f --- /dev/null +++ b/20240922/weather/install_oracle_driver.sh @@ -0,0 +1,5 @@ + + +#安装 人大金仓jar包 下载之后执行这个命令 + +mvn install:install-file -Dfile=/Users/edao/ojdbc6.jar -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=11.2.0.1.0 -Dpackaging=jar \ No newline at end of file diff --git a/20240922/weather/pom.xml b/20240922/weather/pom.xml index 62ed3cd..3b7c3c4 100644 --- a/20240922/weather/pom.xml +++ b/20240922/weather/pom.xml @@ -36,6 +36,11 @@ spring-boot-starter-test test + + mysql + mysql-connector-java + runtime + com.mchange diff --git a/20240922/weather/src/main/java/com/rehome/weather/config/dao/DataSourceConfiguration.java b/20240922/weather/src/main/java/com/rehome/weather/config/dao/DataSourceConfiguration.java deleted file mode 100644 index d2044d1..0000000 --- a/20240922/weather/src/main/java/com/rehome/weather/config/dao/DataSourceConfiguration.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.rehome.weather.config.dao; - - -import com.mchange.v2.c3p0.ComboPooledDataSource; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import java.beans.PropertyVetoException; - - -/** - * @author huangwenfei - * @version v1.0.0.0 - * Created DateTime 2021-04-25 9:35 - * @description: 数据库配置,数据源配置类 - */ -@Configuration -public class DataSourceConfiguration { - //驱动 - @Value("${jdbc.driver}") - private String jdbcDriver; - //数据库连接url - @Value("${jdbc.url}") - private String jdbcUrl; - //数据库名称 - @Value("${jdbc.username}") - private String jdbcUsername; - //数据库密码 - @Value("${jdbc.password}") - private String jdbcPassword; - /** - * @date 2021-04-29 10:24 - * @description: 创建数据源,加入连接池管理 - * @Param: null - */ - @Bean(name = "dataSouce") - public ComboPooledDataSource createDataSouce() throws PropertyVetoException { - ComboPooledDataSource dataSource = new ComboPooledDataSource(); - dataSource.setDriverClass(jdbcDriver); - dataSource.setJdbcUrl(jdbcUrl); - dataSource.setUser(jdbcUsername); - dataSource.setPassword(jdbcPassword); - //关闭连接后不自动commit - dataSource.setAutoCommitOnClose(false); - return dataSource; - } -} \ No newline at end of file diff --git a/20240922/weather/src/main/java/com/rehome/weather/config/dao/JuheWeatherProperties.java b/20240922/weather/src/main/java/com/rehome/weather/config/dao/JuheWeatherProperties.java deleted file mode 100644 index ec84bf2..0000000 --- a/20240922/weather/src/main/java/com/rehome/weather/config/dao/JuheWeatherProperties.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.rehome.weather.config.dao; - - -import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.PropertySource; -import org.springframework.stereotype.Component; -import lombok.Data; - -/** - * @author huangwenfei - * @version v1.0.0.0 - * Created DateTime 2021-04-28 9:35 - * @description: 读取配置文件,获取第三方平台 聚合数据 各接口的url key 查询天气的城市 - */ -@Configuration -@ConfigurationProperties(prefix = "weather", ignoreUnknownFields = false) -@PropertySource(value="classpath:config/juheweather.properties",encoding = "UTF-8") -@Data -@Component -public class JuheWeatherProperties { - //要查询天气的城市 - private String city; - //查询天汽url - private String weatherQueryUrl; - //聚合平台key - private String weatherKey; - //天气支持城市列表url - private String cityListUrl; - //查询天气种类列表url - private String weatherTypeUrl; - //和风开发平台台风key - private String heFengStormKey; - //和风天气开发平台 台风列表url - private String stormListUrl; - //和风天气开发平台 台风预报url - private String stormForecastUrl; - //和风天气开发平台 台风实况和路径url - private String stormTrackUrl; -} \ No newline at end of file diff --git a/20240922/weather/src/main/java/com/rehome/weather/config/dao/RedisConfig.java b/20240922/weather/src/main/java/com/rehome/weather/config/dao/RedisConfig.java deleted file mode 100644 index 6b7b07d..0000000 --- a/20240922/weather/src/main/java/com/rehome/weather/config/dao/RedisConfig.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.rehome.weather.config.dao; - - -import org.springframework.boot.autoconfigure.cache.CacheProperties; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.data.redis.cache.RedisCacheConfiguration; -import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; -import org.springframework.data.redis.serializer.RedisSerializationContext; - -import java.time.Duration; - - -/** - * @author huangwenfei - * @version v1.0.0.0 - * Created DateTime 2021-04-28 9:35 - * @description: Redis配置,把Redis默认的JDK序列化方式改为json 同时把redis自动失效时间改为20分钟 - */ -@Configuration -public class RedisConfig { - /** - * @date 2021-04-28 10:29 - * @description: Redis配置修改 - * @Param: cacheProperties - */ - @Bean - public RedisCacheConfiguration redisCacheConfiguration(CacheProperties cacheProperties) { - CacheProperties.Redis redisProperties = cacheProperties.getRedis(); - RedisCacheConfiguration config = RedisCacheConfiguration - .defaultCacheConfig(); - //把Redis默认的JDK序列化方式改为json - config = config.serializeValuesWith(RedisSerializationContext.SerializationPair - .fromSerializer(new GenericJackson2JsonRedisSerializer())); - - if (redisProperties.getTimeToLive() != null) { - config = config.entryTtl(redisProperties.getTimeToLive()); - } - if (redisProperties.getKeyPrefix() != null) { - config = config.prefixKeysWith(redisProperties.getKeyPrefix()); - } - if (!redisProperties.isCacheNullValues()) { - config = config.disableCachingNullValues(); - } - if (!redisProperties.isUseKeyPrefix()) { - config = config.disableKeyPrefix(); - } - // 20分钟缓存自动失效 - config.entryTtl(Duration.ofSeconds(60*3)); - return config; - } -} diff --git a/20240922/weather/src/main/java/com/rehome/weather/config/dao/SessionFactoryConfiguration.java b/20240922/weather/src/main/java/com/rehome/weather/config/dao/SessionFactoryConfiguration.java deleted file mode 100644 index a1830cc..0000000 --- a/20240922/weather/src/main/java/com/rehome/weather/config/dao/SessionFactoryConfiguration.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.rehome.weather.config.dao; - - -import org.mybatis.spring.SqlSessionFactoryBean; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.core.io.ClassPathResource; -import org.springframework.core.io.support.PathMatchingResourcePatternResolver; -import javax.sql.DataSource; -import java.io.IOException; - -/** - * @author huangwenfei - * @version v1.0.0.0 - * Created DateTime 2021-04-25 9:35 - * @description: 数据库配置,数据源sqlSession配置 - */ -@Configuration -public class SessionFactoryConfiguration { - //mapper映射路径 - @Value("${mapper_path}") - private String mapperPath; - //mybatis配置文件 - @Value("${mybatis_config_file}") - private String mybatisConfigFilePath; - //数据源 - @Autowired - private DataSource dataSouce; - //bean所在的包 - @Value("${entity_package}") - private String entityPackage; - /** - * @date 2021-04-29 10:35 - * @description: 创建SqlSessionFactoryBean - * @Param: null - */ - @Bean(name="sqlSessionFactory") - public SqlSessionFactoryBean createSqlSessionFactoryBean() throws IOException { - SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); - sqlSessionFactoryBean.setConfigLocation(new ClassPathResource(mybatisConfigFilePath)); - PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); - String packageSearchPath = PathMatchingResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX+mapperPath; - sqlSessionFactoryBean.setMapperLocations(resolver.getResources(packageSearchPath)); - sqlSessionFactoryBean.setDataSource(dataSouce); - sqlSessionFactoryBean.setTypeAliasesPackage(entityPackage); - return sqlSessionFactoryBean; - } -} \ No newline at end of file diff --git a/20240922/weather/src/main/java/com/rehome/weather/config/service/TransactionManagementConfiguration.java b/20240922/weather/src/main/java/com/rehome/weather/config/service/TransactionManagementConfiguration.java deleted file mode 100644 index 1236388..0000000 --- a/20240922/weather/src/main/java/com/rehome/weather/config/service/TransactionManagementConfiguration.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.rehome.weather.config.service; - - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Configuration; -import org.springframework.jdbc.datasource.DataSourceTransactionManager; -import org.springframework.transaction.PlatformTransactionManager; -import org.springframework.transaction.annotation.EnableTransactionManagement; -import org.springframework.transaction.annotation.TransactionManagementConfigurer; - -import javax.sql.DataSource; - -/** - * @author huangwenfei - * @version v1.0.0.0 - * Created DateTime 2021-04-25 9:35 - * @description: 事务配置类,不可缺少 - */ -@Configuration -@EnableTransactionManagement -public class TransactionManagementConfiguration implements TransactionManagementConfigurer{ - //数据源 - @Autowired - private DataSource dataSource; - /** - * @date 2021-04-25 10:45 - * @description: 数据库配置,把数据源加入事务管理 - * @Param: null - */ - @Override - public PlatformTransactionManager annotationDrivenTransactionManager() { - return new DataSourceTransactionManager(dataSource); - } -} diff --git a/20240922/weather/src/main/java/com/rehome/weather/controller/StormController.java b/20240922/weather/src/main/java/com/rehome/weather/controller/StormController.java index 7b58172..458348d 100644 --- a/20240922/weather/src/main/java/com/rehome/weather/controller/StormController.java +++ b/20240922/weather/src/main/java/com/rehome/weather/controller/StormController.java @@ -1,11 +1,11 @@ package com.rehome.weather.controller; -import com.rehome.weather.config.dao.JuheWeatherProperties; import com.rehome.weather.service.StormService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.web.bind.annotation.*; +import javax.annotation.Resource; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Map; @@ -19,10 +19,9 @@ import java.util.Map; @RestController @RequestMapping("/storm/service") -@EnableConfigurationProperties(JuheWeatherProperties.class) public class StormController { //台风服务 - @Autowired + @Resource private StormService stormService ; /** diff --git a/20240922/weather/src/main/java/com/rehome/weather/controller/WeatherController.java b/20240922/weather/src/main/java/com/rehome/weather/controller/WeatherController.java index 5ab769b..683bd36 100644 --- a/20240922/weather/src/main/java/com/rehome/weather/controller/WeatherController.java +++ b/20240922/weather/src/main/java/com/rehome/weather/controller/WeatherController.java @@ -1,12 +1,13 @@ package com.rehome.weather.controller; -import com.rehome.weather.config.dao.JuheWeatherProperties; import com.rehome.weather.entity.WeatherCity; import com.rehome.weather.service.WeatherService; import com.rehome.weather.service.WeatherTypeService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.web.bind.annotation.*; + +import javax.annotation.Resource; import java.util.Map; /** @@ -17,17 +18,15 @@ import java.util.Map; */ @RestController @RequestMapping("/weather/service") -@EnableConfigurationProperties(JuheWeatherProperties.class) public class WeatherController { //天气服务 - @Autowired + @Resource private WeatherService weatherService ; //天气种类服务 - @Autowired + @Resource private WeatherTypeService weatherTypeService ; - //聚合数据 配置文件相关参数 - @Autowired - JuheWeatherProperties juheWeatherProperties; + + /** @@ -53,7 +52,7 @@ public class WeatherController { * @description: 获取支持天气查询的城市列表 不对外开放 * @Param: null */ - @RequestMapping(value = "/getWeatherCitySupporList",method = RequestMethod.GET) + //@RequestMapping(value = "/getWeatherCitySupporList",method = RequestMethod.GET) public String getWeatherCitySupporList(){ return weatherService.getWeatherCitySupporList(); } diff --git a/20240922/weather/src/main/java/com/rehome/weather/dao/StormDao.java b/20240922/weather/src/main/java/com/rehome/weather/dao/StormDao.java deleted file mode 100644 index aac98b3..0000000 --- a/20240922/weather/src/main/java/com/rehome/weather/dao/StormDao.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.rehome.weather.dao; - -import com.rehome.weather.entity.StormData; -import com.rehome.weather.entity.StormForecast; -import com.rehome.weather.entity.StormTrack; -import org.apache.ibatis.annotations.Mapper; -import java.util.List; - -/** - * 功能描述 台风Dao层 - * @author huangwenfei - * Created DateTime 2021-05-08 14:08 - */ - -public interface StormDao { - //插入台风数据 - int insertStorm(StormData stormEntity); - //更新台风数据 - int updateStorm(StormData stormEntity); - //根据id查台风数据 - StormData getStormById(String id); - //查本地库台风列表数据 - List getLocalStorms(String year); - //插入台风预报数据 - int insertStormForecast(StormForecast stormForecast); - //根据stormid查台风预报数据 - StormForecast getStormForecastById(String stormid); - //插入台风实况和路径数据 - int insertStormTrack(StormTrack stormTrack); - //根据stormid查台风实况和路径数据 - StormTrack getStormTrackById(String stormid); -} diff --git a/20240922/weather/src/main/java/com/rehome/weather/dao/StormDataRepository.java b/20240922/weather/src/main/java/com/rehome/weather/dao/StormDataRepository.java new file mode 100644 index 0000000..313839c --- /dev/null +++ b/20240922/weather/src/main/java/com/rehome/weather/dao/StormDataRepository.java @@ -0,0 +1,15 @@ +package com.rehome.weather.dao; + + +import com.rehome.weather.entity.StormData; +import com.rehome.weather.entity.WeatherType; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.List; +import java.util.Optional; + +public interface StormDataRepository extends JpaRepository { + + Optional> findByYear(String year); +} + diff --git a/20240922/weather/src/main/java/com/rehome/weather/dao/StormForecastRepository.java b/20240922/weather/src/main/java/com/rehome/weather/dao/StormForecastRepository.java new file mode 100644 index 0000000..167a630 --- /dev/null +++ b/20240922/weather/src/main/java/com/rehome/weather/dao/StormForecastRepository.java @@ -0,0 +1,18 @@ +package com.rehome.weather.dao; + + +import com.rehome.weather.entity.StormForecast; +import com.rehome.weather.entity.WeatherFuture; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; + +import java.util.List; +import java.util.Optional; + +public interface StormForecastRepository extends JpaRepository { + + @Query(value = "select * from storm_forecast t where t.stormid = ?1 ORDER BY id DESC LIMIT 0,1", nativeQuery = true) + Optional findByIdOne(String stormid); + + +} \ No newline at end of file diff --git a/20240922/weather/src/main/java/com/rehome/weather/dao/StormTrackJpaRepository.java b/20240922/weather/src/main/java/com/rehome/weather/dao/StormTrackJpaRepository.java new file mode 100644 index 0000000..77f0a94 --- /dev/null +++ b/20240922/weather/src/main/java/com/rehome/weather/dao/StormTrackJpaRepository.java @@ -0,0 +1,17 @@ +package com.rehome.weather.dao; + + +import com.rehome.weather.entity.StormForecast; +import com.rehome.weather.entity.StormTrack; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; + +import java.util.Optional; + +public interface StormTrackJpaRepository extends JpaRepository { + + @Query(value = "select * from storm_track t where t.stormid = ?1 ORDER BY id DESC LIMIT 0,1", nativeQuery = true) + Optional findByIdOne(String stormid); + + +} diff --git a/20240922/weather/src/main/java/com/rehome/weather/dao/WeatherDao.java b/20240922/weather/src/main/java/com/rehome/weather/dao/WeatherDao.java deleted file mode 100644 index 36a69c8..0000000 --- a/20240922/weather/src/main/java/com/rehome/weather/dao/WeatherDao.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.rehome.weather.dao; - -import com.rehome.weather.entity.WeatherCity; -import com.rehome.weather.entity.WeatherFuture; -import com.rehome.weather.entity.WeatherRealtime; -import org.apache.ibatis.annotations.Mapper; - -import java.util.List; - -/** - * @author huangwenfei - * @version v1.0.0.0 - * Created DateTime 2021-04-25 14:35 - * @description: 天气Dao层 - */ - -public interface WeatherDao { - //根据id查城市 - WeatherCity getById(Integer id); - //插入支持天气查询的城市列表数据 - int insertCitys(List list); - //插入实时天气数据 - int insertRealtimeWeather(WeatherRealtime weatherRealtime); - //插入预报天气数据 - int insertFutrueWeather(WeatherFuture weatherFuture); - //更新预报天气数据 - int updateFutrueWeather(WeatherFuture weatherFuture); - //根据日期查预报天气数据 - WeatherFuture getFutrueByDate(String date); - //查本地库实时天气数据 - WeatherRealtime getLocalWeatherRealtime(String city); - //查本地库未来五天预报天气数据 - List getLocalWeatherFuture(String city); -} \ No newline at end of file diff --git a/20240922/weather/src/main/java/com/rehome/weather/dao/WeatherTypeDao.java b/20240922/weather/src/main/java/com/rehome/weather/dao/WeatherTypeDao.java deleted file mode 100644 index 05f3ba8..0000000 --- a/20240922/weather/src/main/java/com/rehome/weather/dao/WeatherTypeDao.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.rehome.weather.dao; - -import com.rehome.weather.entity.WeatherType; -import org.apache.ibatis.annotations.Mapper; -import java.util.List; - -/** - * @author huangwenfei - * @version v1.0.0.0 - * Created DateTime 2021-04-25 14:35 - * @description: 天气种类Dao层 - */ - -public interface WeatherTypeDao { - //根据天气种类标识查天气种类数据 - WeatherType getByWid(String wid); - //插入天气种类列表数据 - int insertWeatherTypes(List list); -} diff --git a/20240922/weather/src/main/java/com/rehome/weather/dao/WeatherTypeRepository.java b/20240922/weather/src/main/java/com/rehome/weather/dao/WeatherTypeRepository.java index bfb161c..521fa61 100644 --- a/20240922/weather/src/main/java/com/rehome/weather/dao/WeatherTypeRepository.java +++ b/20240922/weather/src/main/java/com/rehome/weather/dao/WeatherTypeRepository.java @@ -3,5 +3,8 @@ package com.rehome.weather.dao; import com.rehome.weather.entity.WeatherType; import org.springframework.data.jpa.repository.JpaRepository; +import java.util.Optional; + public interface WeatherTypeRepository extends JpaRepository { + Optional findByWid(String wid); } diff --git a/20240922/weather/src/main/java/com/rehome/weather/entity/WeatherType.java b/20240922/weather/src/main/java/com/rehome/weather/entity/WeatherType.java index f95c57d..4eacf71 100644 --- a/20240922/weather/src/main/java/com/rehome/weather/entity/WeatherType.java +++ b/20240922/weather/src/main/java/com/rehome/weather/entity/WeatherType.java @@ -14,7 +14,7 @@ import java.io.Serializable; */ @Data @Entity -public class WeatherType implements Serializable{WeatherTypeRepository +public class WeatherType implements Serializable{ @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @ApiModelProperty(value = "主键") diff --git a/20240922/weather/src/main/java/com/rehome/weather/service/ScheduledService.java b/20240922/weather/src/main/java/com/rehome/weather/service/ScheduledService.java index 73929ef..43bed60 100644 --- a/20240922/weather/src/main/java/com/rehome/weather/service/ScheduledService.java +++ b/20240922/weather/src/main/java/com/rehome/weather/service/ScheduledService.java @@ -2,8 +2,8 @@ package com.rehome.weather.service; import com.alibaba.fastjson.JSON; -import com.rehome.weather.config.dao.JuheWeatherProperties; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @@ -20,7 +20,6 @@ import java.util.Map; */ @Slf4j @Component -@EnableConfigurationProperties(JuheWeatherProperties.class) public class ScheduledService { //天气服务层 @Autowired @@ -28,9 +27,9 @@ public class ScheduledService { //台风服务层 @Autowired private StormService stormService ; - //聚合数据 配置文件相关参数 - @Autowired - JuheWeatherProperties juheWeatherProperties; + + @Value("${weather.city}") + private String city; /** * @date 2021-04-29 14:07 @@ -43,7 +42,6 @@ public class ScheduledService { public void scheduled(){ log.info("scheduled"); log.info("=====>>>>>使用cron:"+String.valueOf(System.currentTimeMillis())); - String city = juheWeatherProperties.getCity(); Map map = weatherService.getJuheWeatherByScheduled(city); String jsonString = JSON.toJSONString(map); log.info(jsonString); diff --git a/20240922/weather/src/main/java/com/rehome/weather/service/WeatherService.java b/20240922/weather/src/main/java/com/rehome/weather/service/WeatherService.java index f528998..71ce6d5 100644 --- a/20240922/weather/src/main/java/com/rehome/weather/service/WeatherService.java +++ b/20240922/weather/src/main/java/com/rehome/weather/service/WeatherService.java @@ -19,7 +19,7 @@ public interface WeatherService { //根据城市查询天气数据,然后把获取到的实时天气和预报天气入库 public Map getJuheWeatherByScheduled(String cityInput); //从本地数据库查实时天气和预报天气数据,然后返回给前端 - public Map getLocalWeatherByCity(String city); + public Map getLocalWeatherByCity(String cityParam); } /** diff --git a/20240922/weather/src/main/java/com/rehome/weather/service/impl/StormServiceImpl.java b/20240922/weather/src/main/java/com/rehome/weather/service/impl/StormServiceImpl.java index d2dae41..2217838 100644 --- a/20240922/weather/src/main/java/com/rehome/weather/service/impl/StormServiceImpl.java +++ b/20240922/weather/src/main/java/com/rehome/weather/service/impl/StormServiceImpl.java @@ -1,17 +1,15 @@ package com.rehome.weather.service.impl; import com.alibaba.fastjson.JSON; -import com.rehome.weather.config.dao.JuheWeatherProperties; -import com.rehome.weather.dao.StormDao; +import com.rehome.weather.dao.*; import com.rehome.weather.dto.BaseStormDto; import com.rehome.weather.dto.StormDto; import com.rehome.weather.entity.*; import com.rehome.weather.service.StormService; import com.rehome.weather.utils.WeatherUtil; -import lombok.extern.slf4j.Slf4j; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.cache.annotation.CacheConfig; import org.springframework.cache.annotation.CacheEvict; @@ -31,17 +29,44 @@ import java.util.*; */ @Service -@EnableConfigurationProperties(JuheWeatherProperties.class) @CacheConfig(cacheNames = "com.rehome.weather.service.impl.StormServiceImpl") public class StormServiceImpl implements StormService { private Logger log = LoggerFactory.getLogger(this.getClass()); //台风dao +// @Resource +// private StormDao stormDao ; + @Resource - private StormDao stormDao ; - //聚合数据 配置文件相关参数 + private StormDataRepository stormDataRepository; + @Resource - JuheWeatherProperties juheWeatherProperties; + private StormForecastRepository stormForecastRepository; + + @Resource + private StormTrackJpaRepository stormTrackJpaRepository; + + + + + + + + //聚合数据 配置文件相关参数 +// @Resource +// JuheWeatherProperties juheWeatherProperties; + + @Value("${weather.stormListUrl}") + private String stormListUrl; + + @Value("${weather.heFengStormKey}") + private String heFengStormKey; + + @Value("${weather.stormForecastUrl}") + private String stormForecastUrl; + + @Value("${weather.stormTrackUrl}") + private String stormTrackUrl; /** * 功能描述 从和风天气开发平台查询台风列表数据并入库 @@ -51,8 +76,7 @@ public class StormServiceImpl implements StormService { @Override @CacheEvict(cacheNames = "com.rehome.weather.service.impl.StormServiceImpl",allEntries = true) public Map getStormListByScheduled(String year) { - String stormListUrl = juheWeatherProperties.getStormListUrl(); - String heFengStormKey=juheWeatherProperties.getHeFengStormKey(); + String url=stormListUrl+"?key="+heFengStormKey+"&basin=NP&year="+year; String stormJson = WeatherUtil.analysisUrlGzip(url); log.info(url); @@ -62,33 +86,35 @@ public class StormServiceImpl implements StormService { if(stormDto!=null&&stormDto.getCode().equals("200")){ List storm=stormDto.getStorm(); if(storm.size()>0){ - for (StormData stormEntity : storm) { - stormEntity.setPlatform("hefeng"); - stormEntity.setPlatformdesc("和风天气开发平台"); - StormData stormEntityDb=stormDao.getStormById(stormEntity.getId()); - if(stormEntityDb==null){ + for (StormData stormData : storm) { + stormData.setPlatform("hefeng"); + stormData.setPlatformdesc("和风天气开发平台"); + Optional stormEntityDb=stormDataRepository.findById(stormData.getId()); + if(!stormEntityDb.isPresent()){ //数据库不存在这条台风数据 插入这条台风数据, //同时调用台风预报接口数据并入库, //同时调用台风实况和路径接口数据并入库, log.info("数据库不存在这条台风数据 插入这条台风数据,"); - int resultId=stormDao.insertStorm(stormEntity); - log.info("插入台风数据成功,id:"+String.valueOf(resultId)); - this.getStormForecastByScheduled(stormEntity.getId()); - this.getStormTrackByScheduled(stormEntity.getId()); + StormData resultStorm=stormDataRepository.save(stormData); + if(resultStorm!=null){ + log.info("插入台风数据成功,id:"+String.valueOf(resultStorm.getId())); + } + this.getStormForecastByScheduled(stormData.getId()); + this.getStormTrackByScheduled(stormData.getId()); }else{ //数据库存在这条台风数据 - if(stormEntity.getIsActive().equals("1")){ + if(stormData.getIsActive().equals("1")){ //台风处于活跃状态 //同时调用台风预报接口数据并入库, //同时调用台风实况和路径接口数据并入库 log.info("台风处于活跃状态"); - this.getStormForecastByScheduled(stormEntity.getId()); - this.getStormTrackByScheduled(stormEntity.getId()); + this.getStormForecastByScheduled(stormData.getId()); + this.getStormTrackByScheduled(stormData.getId()); } - if(stormEntity.getIsActive().equals("0")){ + if(stormData.getIsActive().equals("0")){ //台风已停止状态 - if(stormEntityDb.getIsActive().equals("1")){ + if(stormEntityDb.get().getIsActive().equals("1")){ //数据库里台风还处于活跃状态,更新台风状态 //同时调用台风预报接口数据并入库, //同时调用台风实况和路径接口数据并入库 @@ -99,11 +125,13 @@ public class StormServiceImpl implements StormService { String nowTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date); //把时间转换 Timestamp updatetime =Timestamp.valueOf(nowTime); - stormEntity.setUpdatetime(updatetime); - int resultId=stormDao.updateStorm(stormEntity); - log.info("更新台风数据成功,id:"+String.valueOf(resultId)); - this.getStormForecastByScheduled(stormEntity.getId()); - this.getStormTrackByScheduled(stormEntity.getId()); + stormData.setUpdatetime(updatetime); + StormData resultStorm=stormDataRepository.save(stormData); + if(resultStorm!=null){ + log.info("更新台风数据成功,id:"+String.valueOf(resultStorm.getId())); + } + this.getStormForecastByScheduled(stormData.getId()); + this.getStormTrackByScheduled(stormData.getId()); } } } @@ -130,8 +158,7 @@ public class StormServiceImpl implements StormService { */ @Override public String getStormForecastByScheduled(String stormid) { - String stormForecastUrl = juheWeatherProperties.getStormForecastUrl(); - String heFengStormKey=juheWeatherProperties.getHeFengStormKey(); + String url=stormForecastUrl+"?key="+heFengStormKey+"&stormid="+stormid; String stormJson = WeatherUtil.analysisUrlGzip(url); log.info(url); @@ -141,8 +168,10 @@ public class StormServiceImpl implements StormService { StormForecast stormForecast = new StormForecast(); stormForecast.setStormid(stormid); stormForecast.setForecast(stormJson); - int resultId=stormDao.insertStormForecast(stormForecast); - log.info("插入台风预报数据成功,id:"+String.valueOf(resultId)); + StormForecast resultStormForecast=stormForecastRepository.save(stormForecast); + if(resultStormForecast!=null){ + log.info("插入台风预报数据成功,id:"+String.valueOf(resultStormForecast.getId())); + } } return stormJson; } @@ -154,8 +183,7 @@ public class StormServiceImpl implements StormService { */ @Override public String getStormTrackByScheduled(String stormid) { - String stormTrackUrl = juheWeatherProperties.getStormTrackUrl(); - String heFengStormKey=juheWeatherProperties.getHeFengStormKey(); + String url=stormTrackUrl+"?key="+heFengStormKey+"&stormid="+stormid; String stormJson = WeatherUtil.analysisUrlGzip(url); log.info(url); @@ -165,8 +193,10 @@ public class StormServiceImpl implements StormService { StormTrack stormTrack = new StormTrack(); stormTrack.setStormid(stormid); stormTrack.setTrack(stormJson); - int resultId=stormDao.insertStormTrack(stormTrack); - log.info("插入台风实况和路径数据成功,id:"+String.valueOf(resultId)); + StormTrack resultStormTrack=stormTrackJpaRepository.save(stormTrack); + if(resultStormTrack!=null){ + log.info("插入台风实况和路径数据成功,id:"+String.valueOf(resultStormTrack.getId())); + } } return stormJson; } @@ -180,9 +210,9 @@ public class StormServiceImpl implements StormService { @Cacheable(cacheNames="com.rehome.weather.service.impl.StormServiceImpl",key="#year+'-getLocalStormList'") public Map getLocalStormList(String year) { Map map = new HashMap(); - List storm=stormDao.getLocalStorms(year); + Optional> storm=stormDataRepository.findByYear(year); List stormEmpty=new ArrayList(); - if(storm==null){ + if(!storm.isPresent()){ map.put("storm",stormEmpty); }else{ map.put("storm",storm); @@ -199,9 +229,9 @@ public class StormServiceImpl implements StormService { @Override @Cacheable(cacheNames="com.rehome.weather.service.impl.StormServiceImpl",key="#stormid+'-getLocalStormForecastByStormId'") public String getLocalStormForecastByStormId(String stormid) { - StormForecast stormForecast = stormDao.getStormForecastById(stormid); - if(stormForecast!=null){ - return stormForecast.getForecast(); + Optional stormForecast = stormForecastRepository.findByIdOne(stormid); + if(stormForecast.isPresent()){ + return stormForecast.get().getForecast(); } Map map = new HashMap(); map.put("code","10000"); @@ -218,9 +248,9 @@ public class StormServiceImpl implements StormService { @Override @Cacheable(cacheNames="com.rehome.weather.service.impl.StormServiceImpl",key="#stormid+'-getLocalStormTrackByStormId'") public String getLocalStormTrackByStormId(String stormid) { - StormTrack stormTrack = stormDao.getStormTrackById(stormid); - if (stormTrack!=null){ - return stormTrack.getTrack(); + Optional stormTrack = stormTrackJpaRepository.findByIdOne(stormid); + if (stormTrack.isPresent()){ + return stormTrack.get().getTrack(); } Map map = new HashMap(); map.put("code","10000"); diff --git a/20240922/weather/src/main/java/com/rehome/weather/service/impl/WeatherServiceImpl.java b/20240922/weather/src/main/java/com/rehome/weather/service/impl/WeatherServiceImpl.java index f4343a2..2fae5f1 100644 --- a/20240922/weather/src/main/java/com/rehome/weather/service/impl/WeatherServiceImpl.java +++ b/20240922/weather/src/main/java/com/rehome/weather/service/impl/WeatherServiceImpl.java @@ -1,7 +1,6 @@ package com.rehome.weather.service.impl; import com.alibaba.fastjson.JSON; -import com.rehome.weather.config.dao.JuheWeatherProperties; import com.rehome.weather.dao.*; import com.rehome.weather.dto.WeatherCityListDto; import com.rehome.weather.dto.WeatherQueryDto; @@ -9,18 +8,15 @@ import com.rehome.weather.dto.WeatherQueryResultDto; import com.rehome.weather.entity.*; import com.rehome.weather.service.WeatherService; import com.rehome.weather.utils.WeatherUtil; -import lombok.extern.slf4j.Slf4j; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.EnableConfigurationProperties; -import org.springframework.cache.annotation.CacheConfig; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; import javax.annotation.Resource; -import java.sql.Timestamp; import java.text.SimpleDateFormat; import java.util.*; @@ -32,10 +28,22 @@ import java.util.*; */ @Service -@EnableConfigurationProperties(JuheWeatherProperties.class) public class WeatherServiceImpl implements WeatherService { private Logger log = LoggerFactory.getLogger(this.getClass()); + @Value("${weather.weatherKey}") + private String weatherKey; + + @Value("${weather.city}") + private String city; + + @Value("${weather.weatherQueryUrl}") + private String weatherQueryUrl; + + @Value("${weather.cityListUrl}") + private String cityListUrl; + + //天气dao // @Resource // private WeatherDao weatherDao ; @@ -46,11 +54,14 @@ public class WeatherServiceImpl implements WeatherService { @Resource private WeatherFutureRepository weatherFutureRepository ; //聚合数据 配置文件相关参数 - @Resource - JuheWeatherProperties juheWeatherProperties; +// @Resource +// JuheWeatherProperties juheWeatherProperties; //天气种类dao +// @Resource +// private WeatherTypeDao weatherTypeDao; + @Resource - private WeatherTypeDao weatherTypeDao; + private WeatherTypeRepository weatherTypeRepository; /** * @date 2021-04-29 13:47 @@ -73,9 +84,6 @@ public class WeatherServiceImpl implements WeatherService { */ @Override public String getJuheWeather() { - String city = juheWeatherProperties.getCity(); - String weatherQueryUrl = juheWeatherProperties.getWeatherQueryUrl(); - String weatherKey=juheWeatherProperties.getWeatherKey(); String url=weatherQueryUrl+"?key="+weatherKey+"&city="+city; System.out.println(url); String weather = WeatherUtil.analysisUrl(url); @@ -89,8 +97,6 @@ public class WeatherServiceImpl implements WeatherService { */ @Override public String getWeatherCitySupporList() { - String cityListUrl = juheWeatherProperties.getCityListUrl(); - String weatherKey=juheWeatherProperties.getWeatherKey(); String url=cityListUrl+"?key="+weatherKey; System.out.println(url); String cityListJson = WeatherUtil.analysisUrl(url); @@ -125,9 +131,6 @@ public class WeatherServiceImpl implements WeatherService { @Override @CacheEvict(cacheNames="com.rehome.weather.service.impl.WeatherServiceImpl",key="#cityInput+'-getLocalWeatherByCity'") public Map getJuheWeatherByScheduled(String cityInput) { - String city = juheWeatherProperties.getCity(); - String weatherQueryUrl = juheWeatherProperties.getWeatherQueryUrl(); - String weatherKey=juheWeatherProperties.getWeatherKey(); String url=weatherQueryUrl+"?key="+weatherKey+"&city="+city; String weatherJson = WeatherUtil.analysisUrl(url); log.info(weatherJson); @@ -153,15 +156,15 @@ public class WeatherServiceImpl implements WeatherService { if(future.size()>0){ for (WeatherFuture weatherFuture : future) { weatherFuture.setCity(result.getCity()); - WeatherType weatherTypeDayDB=weatherTypeDao.getByWid(weatherFuture.getWid().getDay()); - WeatherType weatherTypeNightDB=weatherTypeDao.getByWid(weatherFuture.getWid().getNight()); - if(weatherTypeDayDB!=null){ + Optional weatherTypeDayDB=weatherTypeRepository.findByWid(weatherFuture.getWid().getDay()); + Optional weatherTypeNightDB=weatherTypeRepository.findByWid(weatherFuture.getWid().getNight()); + if(weatherTypeDayDB.isPresent()){ weatherFuture.setWidday(weatherFuture.getWid().getDay()); - weatherFuture.setWiddayDesc(weatherTypeDayDB.getWeather()); + weatherFuture.setWiddayDesc(weatherTypeDayDB.get().getWeather()); } - if(weatherTypeNightDB!=null){ + if(weatherTypeNightDB.isPresent()){ weatherFuture.setWidnight(weatherFuture.getWid().getNight()); - weatherFuture.setWidnightDesc(weatherTypeNightDB.getWeather()); + weatherFuture.setWidnightDesc(weatherTypeNightDB.get().getWeather()); } //插入单条预报数据 Optional weatherFutureDB=weatherFutureRepository.findByDate(weatherFuture.getDate()); @@ -190,10 +193,9 @@ public class WeatherServiceImpl implements WeatherService { */ @Override @Cacheable(cacheNames="com.rehome.weather.service.impl.WeatherServiceImpl",key="#city+'-getLocalWeatherByCity'") - public Map getLocalWeatherByCity(String city) { - String cityConfig = juheWeatherProperties.getCity(); + public Map getLocalWeatherByCity(String cityParam) { Map map = new HashMap(); - if(cityConfig.equals(city)){ + if(city.equals(cityParam)){ WeatherQueryResultDto resultToClient = new WeatherQueryResultDto(); resultToClient.setCity(city); Optional> weatherRealtimes = weatherRealtimeRepository.findAllByCityContainingOrderByIdDesc(city); diff --git a/20240922/weather/src/main/java/com/rehome/weather/service/impl/WeatherTypeServiceImpl.java b/20240922/weather/src/main/java/com/rehome/weather/service/impl/WeatherTypeServiceImpl.java index 8d29866..0a19b75 100644 --- a/20240922/weather/src/main/java/com/rehome/weather/service/impl/WeatherTypeServiceImpl.java +++ b/20240922/weather/src/main/java/com/rehome/weather/service/impl/WeatherTypeServiceImpl.java @@ -1,22 +1,21 @@ package com.rehome.weather.service.impl; import com.alibaba.fastjson.JSON; -import com.rehome.weather.config.dao.JuheWeatherProperties; -import com.rehome.weather.dao.WeatherTypeDao; +import com.rehome.weather.dao.WeatherTypeRepository; import com.rehome.weather.dto.WeatherTypeListDto; import com.rehome.weather.entity.WeatherType; import com.rehome.weather.service.WeatherTypeService; import com.rehome.weather.utils.WeatherUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.stereotype.Service; - import javax.annotation.Resource; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Optional; /** * @author huangwenfei @@ -25,16 +24,25 @@ import java.util.Map; * @description: 天气种类服务接口 */ @Service -@EnableConfigurationProperties(JuheWeatherProperties.class) public class WeatherTypeServiceImpl implements WeatherTypeService { private Logger log = LoggerFactory.getLogger(this.getClass()); //天气种类dao - @Resource - private WeatherTypeDao weatherTypeDao; +// @Resource +// private WeatherTypeDao weatherTypeDao; //聚合数据 配置文件相关参数 + //@Resource + //JuheWeatherProperties juheWeatherProperties; + @Resource - JuheWeatherProperties juheWeatherProperties; + private WeatherTypeRepository weatherTypeRepository; + + @Value("${weather.weatherTypeUrl}") + private String weatherTypeUrl; + + @Value("${weather.weatherKey}") + private String weatherKey; + /** * @date 2021-04-29 14:14 @@ -43,7 +51,11 @@ public class WeatherTypeServiceImpl implements WeatherTypeService { */ @Override public WeatherType getByWId(String wid) { - return weatherTypeDao.getByWid(wid); + Optional weatherType = weatherTypeRepository.findByWid(wid); + if(weatherType.isPresent()){ + return weatherType.get(); + } + return null; } /** @@ -53,8 +65,7 @@ public class WeatherTypeServiceImpl implements WeatherTypeService { */ @Override public String getWeatherTypeList() { - String weatherTypeUrl = juheWeatherProperties.getWeatherTypeUrl(); - String weatherKey=juheWeatherProperties.getWeatherKey(); + String url=weatherTypeUrl+"?key="+weatherKey; System.out.println(url); String weatherTypeListJson = WeatherUtil.analysisUrl(url); @@ -64,13 +75,13 @@ public class WeatherTypeServiceImpl implements WeatherTypeService { if(weatherTypes.size()>0){ WeatherType weatherType=weatherTypes.get(0); - WeatherType weatherTypeDB=weatherTypeDao.getByWid(weatherType.getWid()); - if(weatherTypeDB!=null){ + Optional weatherTypeDB=weatherTypeRepository.findByWid(weatherType.getWid()); + if(weatherTypeDB.isPresent()){ map.put("reason","查询成功!没有插入数据"); map.put("error_code",0); map.put("result",weatherTypes); }else{ - weatherTypeDao.insertWeatherTypes(weatherTypes); + weatherTypeRepository.saveAll(weatherTypes); map.put("reason","插入数据成功!"); map.put("error_code",0); map.put("result",weatherTypes); diff --git a/20240922/weather/src/main/resources/application.yml b/20240922/weather/src/main/resources/application.yml index 2ae0b5b..5a9a6a6 100644 --- a/20240922/weather/src/main/resources/application.yml +++ b/20240922/weather/src/main/resources/application.yml @@ -5,7 +5,8 @@ spring: type: com.zaxxer.hikari.HikariDataSource #driverClassName: com.mysql.jdbc.Driver #com.mysql.cj.jdbc.Driver com.mysql.jdbc.Driver driverClassName: com.mysql.cj.jdbc.Driver #com.mysql.cj.jdbc.Driver com.mysql.jdbc.Driver - url: jdbc:mysql://192.168.1.21:3306/weather?useUnicode=true&characterEncoding=utf-8&useSSL=false +# url: jdbc:mysql://192.168.1.21:3306/weather?useUnicode=true&characterEncoding=utf-8&useSSL=false + url: jdbc:mysql://127.0.0.1:3306/weather?useUnicode=true&characterEncoding=utf-8&useSSL=false username: root password: Skyinno251, jpa: diff --git a/20240922/weather/src/main/resources/mybatis-config.xml b/20240922/weather/src/main/resources/mybatis-config.xml deleted file mode 100644 index 6f2731b..0000000 --- a/20240922/weather/src/main/resources/mybatis-config.xml +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - - - - - - - - - \ No newline at end of file