package com.xy.config; import com.xy.utils.DataTime; import lombok.AllArgsConstructor; import lombok.SneakyThrows; import org.springframework.context.annotation.Bean; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.core.io.support.EncodedResource; import org.springframework.jdbc.datasource.init.ScriptUtils; import org.springframework.stereotype.Service; import javax.sql.DataSource; /** * 执行sql脚本 */ @Service @AllArgsConstructor public class MysqlSyncConfig { private DataSource dataSource; @Bean @SneakyThrows public Boolean syncSql() { String time = DataTime.getSring("yyyyMMdd"); Resource classPathResource = new ClassPathResource(String.format("prod/%s/%s.sql", time, time)); EncodedResource encodedResource = new EncodedResource(classPathResource, "utf-8"); ScriptUtils.executeSqlScript(dataSource.getConnection(), encodedResource); return true; } }