1.创建工具类
import org.apache.ibatis.builder.StaticSqlSource;
import org.apache.ibatis.exceptions.TooManyResultsException;
import org.apache.ibatis.mapping.*;
import org.apache.ibatis.scripting.LanguageDriver;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.Configuration;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* @author honghh
* @version 2017/11/9
*/
public class SqlMapper {
private final MSUtils msUtils;
private final SqlSession sqlSession;
/**
* 构造方法,默认缓存MappedStatement
*
* @param sqlSession
*/
public SqlMapper(SqlSession sqlSession) {
this.sqlSession = sqlSession;
this.msUtils = new MSUtils(sqlSession.getConfiguration());
}
/**
* 获取List中最多只有一个的数据
*
* @param list List结果
* @param 泛型类型
* @return
*/
private T getOne(List list) {
if (list.size() == 1) {
return list.get(0);
} else if (list.size() > 1) {
throw new TooManyResultsException("Expected one result (or null) to be returned by selectOne(), but found: " + list.size());
} else {
return null;
}
}
/**
* 查询返回一个结果,多个结果时抛出异常
*
* @param sql 执行的sql
* @return
*/
public Map selectOne(String sql) {
List
2,创建service
@Service
public class DataMaintainService {
private Logger logger = LoggerFactory.getLogger(DataMaintainService.class);
@Autowired
DataMaintainHandler dataMaintainHandler;
@Autowired
protected SqlMapper sqlMapper;
private static final String SQL_EXECUTE_CONFIG = "SqlExecuteConfig";
private static final String DO_SQL_CODE = "DoSqlCode";
/**
* 执行sql
* 此方法允许在归档库或生产库执行,执行的sql会被记录相应的maintain_log表中。 即执行生产执行sql记录生产maintain_log表
*
* @param
* @return
*/
public List execSql(LoginBean user, String orgSql) throws DataException {
if (StringUtil.isBlank(orgSql)) {
return null;
}
String sqls = orgSql.replaceAll("\\r|\\t|\\v|\\e|\\f", "");
String[] arr = TextUtils.split(sqls, ";\\n");
if (arr == null || arr.length < 1) {
return null;
}
List result = new ArrayList();
List maintainLogs = new ArrayList();
boolean isException = false;
Exception resultException = null;
for (int i = 0; i < arr.length; i++) {
// 如果sql为空不执行
String sql = arr[i];
if (StringUtil.isBlank(sql)) {
continue;
}
sql = sql.replaceAll("\\n", "").trim().replaceAll(";$", "");
try {
// 无异常时需要执行sql
if (!isException) {
int count = 0;
if (sql.startsWith("insert")) {
count = sqlMapper.insert(sql);
} else if (sql.startsWith("update")) {
count = sqlMapper.update(sql);
} else if (sql.startsWith("delete")) {
count = sqlMapper.delete(sql);
} else {
result.add(TextUtils.joinString("[ ", sql, " ]", " 语句异常"));
isException = true;
}
result.add(TextUtils.joinString("[ ", sql, " ]", " 语句影响数据行数:", String.valueOf(count)));
maintainLogs.add(getMaintainLog(user, count, sql));
} else {
// 提醒此sql没有被执行
result.add(TextUtils.joinString("[ ", sql, " ]", " 语句不执行"));
}
} catch (Exception ex) {
logger.error("bulkUpdateBySql", ex);
// 执行时异常了需要返回
result.add(TextUtils.joinString("[ ", sql, " ]", " 执行异常:", ex.getMessage()));
isException = true;
resultException = ex;
}
}
// 如果有异常需要回滚所有数据
if (isException) {
result.add("因为存在异常,此次执行sql将被回滚");
throw new DataException(result, resultException);
}
if (CollectionUtils.isNotEmpty(maintainLogs)) {
dataMaintainHandler.saveMaintainLogs(maintainLogs);
}
return result;
}
}
3.注入SQLMapper