|
@@ -0,0 +1,51 @@
|
|
|
+package com.qmth.boot.data.upgrade.demo.service;
|
|
|
+
|
|
|
+import com.qmth.boot.data.upgrade.annotation.DataUpgradeVersion;
|
|
|
+import com.qmth.boot.data.upgrade.service.DataUpgradeService;
|
|
|
+import com.qmth.boot.data.upgrade.utils.ResourceFileHelper;
|
|
|
+import org.apache.commons.lang3.ArrayUtils;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
+import org.springframework.transaction.TransactionStatus;
|
|
|
+import org.springframework.transaction.support.TransactionCallbackWithoutResult;
|
|
|
+import org.springframework.transaction.support.TransactionTemplate;
|
|
|
+
|
|
|
+// @DataUpgradeVersion("1.0.5")
|
|
|
+public class DataUpgrade_1_0_5 implements DataUpgradeService {
|
|
|
+
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(DataUpgrade_1_0_5.class);
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void process(JdbcTemplate jdbcTemplate, TransactionTemplate transactionTemplate) throws Exception {
|
|
|
+ log.info("process...");
|
|
|
+
|
|
|
+ String[] sqlList = StringUtils.split(ResourceFileHelper.readContent("upgrade/1.0.5.sql"), ";");
|
|
|
+ if (ArrayUtils.isEmpty(sqlList)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ transactionTemplate.execute(new TransactionCallbackWithoutResult() {
|
|
|
+ @Override
|
|
|
+ protected void doInTransactionWithoutResult(TransactionStatus status) {
|
|
|
+ try {
|
|
|
+ for (String sql : sqlList) {
|
|
|
+ if (StringUtils.isBlank(sql)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ log.info(sql);
|
|
|
+ jdbcTemplate.execute(sql);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ // DML-SQL语句支持事务,执行失败时触发自动回滚。
|
|
|
+ status.setRollbackOnly();
|
|
|
+ // DDL-SQL语句不支持事务,执行失败时请采用其它补偿机制回滚!!!
|
|
|
+ throw new RuntimeException("SQL语句执行异常!", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+}
|