mybatis to jpa finish

master
修改密码漏洞修复完成 1 year ago
parent cf5a0e2000
commit 66041721d9

@ -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

@ -36,6 +36,11 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--线程池-->
<dependency>
<groupId>com.mchange</groupId>

@ -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;
}
}

@ -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;
}

@ -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: RedisRedisJDKjson redis20
*/
@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;
}
}

@ -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;
}
}

@ -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);
}
}

@ -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 ;
/**

@ -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();
}

@ -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<StormData> getLocalStorms(String year);
//插入台风预报数据
int insertStormForecast(StormForecast stormForecast);
//根据stormid查台风预报数据
StormForecast getStormForecastById(String stormid);
//插入台风实况和路径数据
int insertStormTrack(StormTrack stormTrack);
//根据stormid查台风实况和路径数据
StormTrack getStormTrackById(String stormid);
}

@ -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<StormData, String> {
Optional<List<StormData>> findByYear(String year);
}

@ -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<StormForecast, Integer> {
@Query(value = "select * from storm_forecast t where t.stormid = ?1 ORDER BY id DESC LIMIT 0,1", nativeQuery = true)
Optional<StormForecast> findByIdOne(String stormid);
}

@ -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<StormTrack, Integer> {
@Query(value = "select * from storm_track t where t.stormid = ?1 ORDER BY id DESC LIMIT 0,1", nativeQuery = true)
Optional<StormTrack> findByIdOne(String stormid);
}

@ -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<WeatherCity> list);
//插入实时天气数据
int insertRealtimeWeather(WeatherRealtime weatherRealtime);
//插入预报天气数据
int insertFutrueWeather(WeatherFuture weatherFuture);
//更新预报天气数据
int updateFutrueWeather(WeatherFuture weatherFuture);
//根据日期查预报天气数据
WeatherFuture getFutrueByDate(String date);
//查本地库实时天气数据
WeatherRealtime getLocalWeatherRealtime(String city);
//查本地库未来五天预报天气数据
List<WeatherFuture> getLocalWeatherFuture(String city);
}

@ -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<WeatherType> list);
}

@ -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<WeatherType, Integer> {
Optional<WeatherType> findByWid(String wid);
}

@ -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 = "主键")

@ -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);

@ -19,7 +19,7 @@ public interface WeatherService {
//根据城市查询天气数据,然后把获取到的实时天气和预报天气入库
public Map getJuheWeatherByScheduled(String cityInput);
//从本地数据库查实时天气和预报天气数据,然后返回给前端
public Map getLocalWeatherByCity(String city);
public Map getLocalWeatherByCity(String cityParam);
}
/**

@ -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<StormData> 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<StormData> 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<String,Object>();
List<StormData> storm=stormDao.getLocalStorms(year);
Optional<List<StormData>> storm=stormDataRepository.findByYear(year);
List stormEmpty=new ArrayList<StormData>();
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> stormForecast = stormForecastRepository.findByIdOne(stormid);
if(stormForecast.isPresent()){
return stormForecast.get().getForecast();
}
Map map = new HashMap<String,Object>();
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> stormTrack = stormTrackJpaRepository.findByIdOne(stormid);
if (stormTrack.isPresent()){
return stormTrack.get().getTrack();
}
Map map = new HashMap<String,Object>();
map.put("code","10000");

@ -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<WeatherType> weatherTypeDayDB=weatherTypeRepository.findByWid(weatherFuture.getWid().getDay());
Optional<WeatherType> 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<WeatherFuture> 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<String,Object>();
if(cityConfig.equals(city)){
if(city.equals(cityParam)){
WeatherQueryResultDto resultToClient = new WeatherQueryResultDto();
resultToClient.setCity(city);
Optional<List<WeatherRealtime>> weatherRealtimes = weatherRealtimeRepository.findAllByCityContainingOrderByIdDesc(city);

@ -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> 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<WeatherType> 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);

@ -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:

@ -1,16 +0,0 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<!-- 配置文件的根元素 -->
<configuration>
<!--配置全局属性-->
<settings>
<!--使用jdbc的getGeneratedKeys获取数据库自增主键值-->
<setting name="useGeneratedKeys" value="true"/>
<!--使用列标签替换列别名 默认未true-->
<setting name="useColumnLabel" value="true" />
<!--开启驼峰式命名转换Table{create_time} -> Entity{createTime}-->
<setting name="mapUnderscoreToCamelCase" value="true" />
</settings>
</configuration>
Loading…
Cancel
Save