|
@@ -11,10 +11,16 @@ import com.elab.core.dao.row.ThirdRowMapper;
|
|
|
import com.elab.core.exception.CoreException;
|
|
|
import com.elab.core.spring.method.CheckSqlProcess;
|
|
|
import com.elab.core.spring.method.DefaultCheckSQLProcess;
|
|
|
+import com.elab.core.spring.method.SQLEventHandler;
|
|
|
+import com.elab.core.sql.config.SqlCommandType;
|
|
|
import com.elab.core.utils.BeanPropertyUtils;
|
|
|
import org.apache.commons.lang3.ArrayUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.BeansException;
|
|
|
+import org.springframework.beans.factory.InitializingBean;
|
|
|
+import org.springframework.context.ApplicationContext;
|
|
|
+import org.springframework.context.ApplicationContextAware;
|
|
|
import org.springframework.dao.DataAccessException;
|
|
|
import org.springframework.dao.EmptyResultDataAccessException;
|
|
|
import org.springframework.jdbc.core.JdbcTemplate;
|
|
@@ -26,10 +32,7 @@ import org.springframework.jdbc.core.namedparam.SqlParameterSource;
|
|
|
import org.springframework.jdbc.support.GeneratedKeyHolder;
|
|
|
|
|
|
import javax.persistence.Column;
|
|
|
-import java.util.Arrays;
|
|
|
-import java.util.LinkedHashMap;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
+import java.util.*;
|
|
|
|
|
|
/**
|
|
|
* 增强的持久层操作
|
|
@@ -38,7 +41,8 @@ import java.util.Map;
|
|
|
* @create 2018-04-20 15:36
|
|
|
* @email liukx@elab-plus.com
|
|
|
**/
|
|
|
-public class BasicBaseDao {
|
|
|
+public class BasicBaseDao implements ApplicationContextAware,
|
|
|
+ InitializingBean {
|
|
|
|
|
|
private Logger logger = LoggerFactory.getLogger(BasicBaseDao.class);
|
|
|
|
|
@@ -46,8 +50,46 @@ public class BasicBaseDao {
|
|
|
|
|
|
private CheckSqlProcess checkSqlProcess = new DefaultCheckSQLProcess();
|
|
|
|
|
|
- public void setCheckSqlProcess(CheckSqlProcess checkSqlProcess) {
|
|
|
- this.checkSqlProcess = checkSqlProcess;
|
|
|
+ private List<SQLEventHandler> sqlEventHandlerList;
|
|
|
+
|
|
|
+ private ApplicationContext applicationContext;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void afterPropertiesSet() throws Exception {
|
|
|
+ Map<String, CheckSqlProcess> beansOfType = this.applicationContext.getBeansOfType(CheckSqlProcess.class);
|
|
|
+ Map<String, SQLEventHandler> SQLEventHandlerMap = this.applicationContext.getBeansOfType(SQLEventHandler.class);
|
|
|
+ if (SQLEventHandlerMap != null && SQLEventHandlerMap.size() > 0) {
|
|
|
+ this.sqlEventHandlerList = new ArrayList<>();
|
|
|
+ SQLEventHandlerMap.forEach((K, V) -> {
|
|
|
+ this.sqlEventHandlerList.add(V);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+// @Override
|
|
|
+// public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
|
|
|
+//
|
|
|
+// }
|
|
|
+
|
|
|
+// @Override
|
|
|
+// public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
|
|
|
+// Map<String, CheckSqlProcess> beansOfType = this.applicationContext.getBeansOfType(CheckSqlProcess.class);
|
|
|
+// Map<String, SQLEventHandler> SQLEventHandlerMap = this.applicationContext.getBeansOfType(SQLEventHandler.class);
|
|
|
+// if (SQLEventHandlerMap != null && SQLEventHandlerMap.size() > 0) {
|
|
|
+// this.sqlEventHandlerList = new ArrayList<>();
|
|
|
+// SQLEventHandlerMap.forEach((K, V) -> {
|
|
|
+// this.sqlEventHandlerList.add(V);
|
|
|
+// });
|
|
|
+// }
|
|
|
+// }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
|
|
+ this.applicationContext = applicationContext;
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<SQLEventHandler> getSqlEventHandlerList() {
|
|
|
+ return sqlEventHandlerList;
|
|
|
}
|
|
|
|
|
|
public JdbcTemplate getJdbcTemplate() {
|
|
@@ -134,11 +176,11 @@ public class BasicBaseDao {
|
|
|
* @param param 参数
|
|
|
* @return
|
|
|
*/
|
|
|
- private JdbcParamsModel commonParseSql(String sql, Object param) throws Exception {
|
|
|
+ public JdbcParamsModel commonParseSql(String sql, Object param) throws Exception {
|
|
|
return this.commonParseSql(sql, param, null);
|
|
|
}
|
|
|
|
|
|
- private JdbcParamsModel commonParseSql(String sql, Object param, ListDynamicSearch search) throws Exception {
|
|
|
+ public JdbcParamsModel commonParseSql(String sql, Object param, ListDynamicSearch search) throws Exception {
|
|
|
long start = System.currentTimeMillis();
|
|
|
JdbcParamsModel model = new JdbcParamsModel();
|
|
|
if (param instanceof String) {
|
|
@@ -153,10 +195,7 @@ public class BasicBaseDao {
|
|
|
// 解析参数类型 , 这里会根据Map和Object对象做装换
|
|
|
SqlParameterSource sqlParameterSource = getSqlParameterSource(param);
|
|
|
// 获取有效的参数,将为null和""的参数给替换掉
|
|
|
- long k = System.currentTimeMillis();
|
|
|
Object[] data = NamedParameterUtils2.buildValueArray(parsedSql, sqlParameterSource, null);
|
|
|
- long k1 = System.currentTimeMillis() - k;
|
|
|
- logger.debug("--------------------" + k1 + "-----------------------");
|
|
|
// 正常解析sql为jdbc与参数对应
|
|
|
String s = NamedParameterUtils2.substituteNamedParameters(parsedSql, sqlParameterSource);
|
|
|
// 校验第一个参数为空的情况下
|
|
@@ -175,7 +214,9 @@ public class BasicBaseDao {
|
|
|
logger.debug(" 对应参数 - " + Arrays.toString(model.getObjects()));
|
|
|
|
|
|
// 校验SQL是否符合规范
|
|
|
- checkSqlProcess.checkProcess(model, param);
|
|
|
+ if (this.checkSqlProcess != null) {
|
|
|
+ this.checkSqlProcess.checkProcess(model, param);
|
|
|
+ }
|
|
|
long time = System.currentTimeMillis() - start;
|
|
|
logger.debug(" sql 语句处理耗时 : " + time);
|
|
|
return model;
|
|
@@ -201,7 +242,7 @@ public class BasicBaseDao {
|
|
|
|
|
|
/**
|
|
|
* @param sql
|
|
|
- * @param o
|
|
|
+ * @param obj
|
|
|
* @param search
|
|
|
* @param elementType
|
|
|
* @param <T>
|
|
@@ -431,14 +472,16 @@ public class BasicBaseDao {
|
|
|
public int insert(String sql, Object o) {
|
|
|
SqlParameterSource sqlParameterSource = getSqlParameterSource(o);
|
|
|
GeneratedKeyHolder keyHolder = new GeneratedKeyHolder();
|
|
|
+ sqlInvokeBefore(SqlCommandType.INSERT, sql, o);
|
|
|
long start = System.currentTimeMillis();
|
|
|
logger.debug("sql : " + sql);
|
|
|
this.getNamedParameterJdbcTemplate().update(sql, sqlParameterSource, keyHolder);
|
|
|
- int i = keyHolder.getKey().intValue();
|
|
|
- logger.debug(" params : " + i);
|
|
|
+ int result = keyHolder.getKey().intValue();
|
|
|
+ logger.debug(" params : " + result);
|
|
|
long time = System.currentTimeMillis() - start;
|
|
|
logger.debug(" SQL 执行耗时 : " + time);
|
|
|
- return i;
|
|
|
+ sqlInvokeAfter(SqlCommandType.INSERT, sql, o, result);
|
|
|
+ return result;
|
|
|
}
|
|
|
|
|
|
|
|
@@ -496,6 +539,24 @@ public class BasicBaseDao {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private void sqlInvokeBefore(SqlCommandType event, String sql, Object o) {
|
|
|
+ if (this.sqlEventHandlerList != null && this.sqlEventHandlerList.size() > 0) {
|
|
|
+ for (int i = 0; i < sqlEventHandlerList.size(); i++) {
|
|
|
+ SQLEventHandler sqlEventHandler = sqlEventHandlerList.get(i);
|
|
|
+ sqlEventHandler.sqlInvokeBefore(event, sql, o);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void sqlInvokeAfter(SqlCommandType event, String sql, Object request, Object result) {
|
|
|
+ if (this.sqlEventHandlerList != null && this.sqlEventHandlerList.size() > 0) {
|
|
|
+ for (int i = 0; i < sqlEventHandlerList.size(); i++) {
|
|
|
+ SQLEventHandler sqlEventHandler = sqlEventHandlerList.get(i);
|
|
|
+ sqlEventHandler.sqlInvokeAfter(event, sql, request, result);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 修改数据操作
|
|
|
*
|
|
@@ -505,9 +566,11 @@ public class BasicBaseDao {
|
|
|
*/
|
|
|
public int update(String sql, Object o) throws Exception {
|
|
|
JdbcParamsModel jdbcParamsModel = commonParseSql(sql, o);
|
|
|
+ sqlInvokeBefore(SqlCommandType.UPDATE, sql, o);
|
|
|
long start = System.currentTimeMillis();
|
|
|
int update = this.jdbcTemplate.update(jdbcParamsModel.getSql(), jdbcParamsModel.getObjects());
|
|
|
long time = System.currentTimeMillis() - start;
|
|
|
+ sqlInvokeAfter(SqlCommandType.UPDATE, sql, o, update);
|
|
|
logger.debug(" SQL 执行耗时 : " + time);
|
|
|
return update;
|
|
|
}
|