MysqlSyncConfig.java 1011 B

12345678910111213141516171819202122232425262728293031323334
  1. package com.xy.config;
  2. import com.xy.utils.DataTime;
  3. import lombok.AllArgsConstructor;
  4. import lombok.SneakyThrows;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.core.io.ClassPathResource;
  7. import org.springframework.core.io.Resource;
  8. import org.springframework.core.io.support.EncodedResource;
  9. import org.springframework.jdbc.datasource.init.ScriptUtils;
  10. import org.springframework.stereotype.Service;
  11. import javax.sql.DataSource;
  12. /**
  13. * 执行sql脚本
  14. */
  15. @Service
  16. @AllArgsConstructor
  17. public class MysqlSyncConfig {
  18. private DataSource dataSource;
  19. @Bean
  20. @SneakyThrows
  21. public Boolean syncSql() {
  22. String time = DataTime.getSring("yyyyMMdd");
  23. Resource classPathResource = new ClassPathResource(String.format("prod/%s/%s.sql", time, time));
  24. EncodedResource encodedResource = new EncodedResource(classPathResource, "utf-8");
  25. ScriptUtils.executeSqlScript(dataSource.getConnection(), encodedResource);
  26. return true;
  27. }
  28. }