使用SpringBoot将中国地震台网数据保存PostGIS数据库实践
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
public class EarthquakeService {
private final EarthquakeRepository earthquakeRepository;
@Autowired
public EarthquakeService(EarthquakeRepository earthquakeRepository) {
this.earthquakeRepository = earthquakeRepository;
}
@Transactional
public void saveEarthquakes(List<Earthquake> earthquakes) {
earthquakeRepository.saveAll(earthquakes);
}
}
这段代码定义了一个服务层类EarthquakeService
,它使用@Autowired
注解自动装配EarthquakeRepository
。saveEarthquakes
方法通过传入的地震列表使用EarthquakeRepository
的saveAll
方法来保存地震数据。@Transactional
注解确保了在保存过程中如果出现异常可以进行回滚。
评论已关闭