|
@@ -4,16 +4,14 @@ import com.alibaba.fastjson.JSON;
|
|
|
import com.elab.core.bean.PageModel;
|
|
|
import com.elab.core.dao.model.JdbcParamsModel;
|
|
|
import com.elab.core.dao.model.ListDynamicSearch;
|
|
|
+import com.elab.core.dao.model.SqlResult;
|
|
|
import com.elab.core.dao.params.BeanSqlParameterSource;
|
|
|
import com.elab.core.dao.params.MapDataSqlParameterSource;
|
|
|
import com.elab.core.dao.params.NamedParameterUtils2;
|
|
|
import com.elab.core.dao.params.ParsedSql2;
|
|
|
import com.elab.core.dao.row.ThirdRowMapper;
|
|
|
import com.elab.core.exception.CoreException;
|
|
|
-import com.elab.core.spring.method.BatchPreparedStatementCreator;
|
|
|
-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.spring.method.*;
|
|
|
import com.elab.core.sql.config.SqlCommandType;
|
|
|
import com.elab.core.utils.BeanPropertyUtils;
|
|
|
import com.google.common.collect.Sets;
|
|
@@ -45,8 +43,7 @@ import java.util.*;
|
|
|
* @create 2018-04-20 15:36
|
|
|
* @email liukx@elab-plus.com
|
|
|
**/
|
|
|
-public class BasicBaseDao implements ApplicationContextAware,
|
|
|
- InitializingBean {
|
|
|
+public class BasicBaseDao implements ApplicationContextAware, InitializingBean {
|
|
|
|
|
|
private Logger logger = LoggerFactory.getLogger(BasicBaseDao.class);
|
|
|
|
|
@@ -56,13 +53,15 @@ public class BasicBaseDao implements ApplicationContextAware,
|
|
|
|
|
|
private List<SQLEventHandler> sqlEventHandlerList;
|
|
|
|
|
|
+ private List<SQLPostProcess> sqlPostProcessorList;
|
|
|
+
|
|
|
private ApplicationContext applicationContext;
|
|
|
|
|
|
/**
|
|
|
* 默认的基础类型
|
|
|
*/
|
|
|
- private Set<Class> basicTypes = Sets.newHashSet(String.class, Integer.class, Double.class, Long.class, Float.class, Date.class);
|
|
|
-
|
|
|
+ private Set<Class> basicTypes =
|
|
|
+ Sets.newHashSet(String.class, Integer.class, Double.class, Long.class, Float.class, Date.class);
|
|
|
|
|
|
@Override
|
|
|
public void afterPropertiesSet() throws Exception {
|
|
@@ -74,24 +73,32 @@ public class BasicBaseDao implements ApplicationContextAware,
|
|
|
this.sqlEventHandlerList.add(V);
|
|
|
});
|
|
|
}
|
|
|
+
|
|
|
+ Map<String, SQLPostProcess> sqlPostProcessMap = this.applicationContext.getBeansOfType(SQLPostProcess.class);
|
|
|
+ if (sqlPostProcessMap != null && sqlPostProcessMap.size() > 0) {
|
|
|
+ this.sqlPostProcessorList = new ArrayList<>();
|
|
|
+ sqlPostProcessMap.forEach((K, V) -> {
|
|
|
+ this.sqlPostProcessorList.add(V);
|
|
|
+ });
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
-// @Override
|
|
|
-// public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
|
|
|
-//
|
|
|
-// }
|
|
|
+ // @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 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 {
|
|
@@ -134,12 +141,12 @@ public class BasicBaseDao implements ApplicationContextAware,
|
|
|
|
|
|
@SuppressWarnings("serial")
|
|
|
private final Map<String, ParsedSql2> parsedSqlCache =
|
|
|
- new LinkedHashMap<String, ParsedSql2>(cacheLimit, 0.75f, true) {
|
|
|
- @Override
|
|
|
- protected boolean removeEldestEntry(Map.Entry<String, ParsedSql2> eldest) {
|
|
|
- return size() > getCacheLimit();
|
|
|
- }
|
|
|
- };
|
|
|
+ new LinkedHashMap<String, ParsedSql2>(cacheLimit, 0.75f, true) {
|
|
|
+ @Override
|
|
|
+ protected boolean removeEldestEntry(Map.Entry<String, ParsedSql2> eldest) {
|
|
|
+ return size() > getCacheLimit();
|
|
|
+ }
|
|
|
+ };
|
|
|
|
|
|
/**
|
|
|
* 参数转换 , 参数类型分别为 Map,Model
|
|
@@ -150,7 +157,7 @@ public class BasicBaseDao implements ApplicationContextAware,
|
|
|
private SqlParameterSource getSqlParameterSource(Object params) {
|
|
|
SqlParameterSource sqlParameterSource = null;
|
|
|
if (params instanceof Map) {
|
|
|
- sqlParameterSource = new MapSqlParameterSource((Map) params);
|
|
|
+ sqlParameterSource = new MapSqlParameterSource((Map)params);
|
|
|
} else {
|
|
|
sqlParameterSource = new BeanPropertySqlParameterSource(params);
|
|
|
}
|
|
@@ -178,7 +185,6 @@ public class BasicBaseDao implements ApplicationContextAware,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
* 通用sql处理方式
|
|
|
*
|
|
@@ -244,7 +250,8 @@ public class BasicBaseDao implements ApplicationContextAware,
|
|
|
public <T> List<T> executeQueryList(String sql, Object o, Class<T> elementType) throws Exception {
|
|
|
JdbcParamsModel jdbcParamsModel = commonParseSql(sql, o);
|
|
|
long start = System.currentTimeMillis();
|
|
|
- List<T> ts = (List<T>) this.jdbcTemplate.query(jdbcParamsModel.getSql(), jdbcParamsModel.getObjects(), new TimeRowMapperResultSetExtractor(new ThirdRowMapper<T>(elementType)));
|
|
|
+ List<T> ts = (List<T>)this.jdbcTemplate.query(jdbcParamsModel.getSql(), jdbcParamsModel.getObjects(),
|
|
|
+ new TimeRowMapperResultSetExtractor(new ThirdRowMapper<T>(elementType)));
|
|
|
long time = System.currentTimeMillis() - start;
|
|
|
logger.debug(" SQL 执行耗时 : " + time);
|
|
|
return ts;
|
|
@@ -253,7 +260,8 @@ public class BasicBaseDao implements ApplicationContextAware,
|
|
|
public <T> List<T> executeQueryBasicTypeList(String sql, Object o, Class<T> elementType) throws Exception {
|
|
|
JdbcParamsModel jdbcParamsModel = commonParseSql(sql, o);
|
|
|
long start = System.currentTimeMillis();
|
|
|
- List<T> ts = (List<T>) this.jdbcTemplate.query(jdbcParamsModel.getSql(), jdbcParamsModel.getObjects(), new SingleColumnRowMapper<>(elementType));
|
|
|
+ List<T> ts = (List<T>)this.jdbcTemplate
|
|
|
+ .query(jdbcParamsModel.getSql(), jdbcParamsModel.getObjects(), new SingleColumnRowMapper<>(elementType));
|
|
|
long time = System.currentTimeMillis() - start;
|
|
|
logger.debug(" SQL 执行耗时 : " + time);
|
|
|
return ts;
|
|
@@ -270,8 +278,8 @@ public class BasicBaseDao implements ApplicationContextAware,
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
- public <T> List<T> executeQueryList(String sql, Object obj, ListDynamicSearch search, Class<T> elementType) throws
|
|
|
- Exception {
|
|
|
+ public <T> List<T> executeQueryList(String sql, Object obj, ListDynamicSearch search, Class<T> elementType)
|
|
|
+ throws Exception {
|
|
|
JdbcParamsModel jdbcParamsModel = commonParseSql(sql, obj, search);
|
|
|
List<T> ts = findForList(elementType, jdbcParamsModel);
|
|
|
return ts;
|
|
@@ -279,7 +287,8 @@ public class BasicBaseDao implements ApplicationContextAware,
|
|
|
|
|
|
private <T> List<T> findForList(Class<T> elementType, JdbcParamsModel jdbcParamsModel) {
|
|
|
long start = System.currentTimeMillis();
|
|
|
- List<T> ts = (List<T>) this.jdbcTemplate.query(jdbcParamsModel.getSql(), jdbcParamsModel.getObjects(), new TimeRowMapperResultSetExtractor(getRowMapper(elementType)));
|
|
|
+ List<T> ts = (List<T>)this.jdbcTemplate.query(jdbcParamsModel.getSql(), jdbcParamsModel.getObjects(),
|
|
|
+ new TimeRowMapperResultSetExtractor(getRowMapper(elementType)));
|
|
|
long time = System.currentTimeMillis() - start;
|
|
|
logger.debug(" SQL 执行耗时 : " + time);
|
|
|
return ts;
|
|
@@ -303,10 +312,12 @@ public class BasicBaseDao implements ApplicationContextAware,
|
|
|
* @param <T>
|
|
|
* @return
|
|
|
*/
|
|
|
- public <T> List<T> executeQueryPage(PageModel pageModel, String sql, Object o, Class<T> elementType) throws Exception {
|
|
|
+ public <T> List<T> executeQueryPage(PageModel pageModel, String sql, Object o, Class<T> elementType)
|
|
|
+ throws Exception {
|
|
|
JdbcParamsModel jdbcParamsModel = commonParseSql(sql, o);
|
|
|
long start = System.currentTimeMillis();
|
|
|
- List<T> ts = this.jdbcTemplate.query(jdbcParamsModel.getSql(), jdbcParamsModel.getObjects(), new ThirdRowMapper<T>(elementType));
|
|
|
+ List<T> ts = this.jdbcTemplate
|
|
|
+ .query(jdbcParamsModel.getSql(), jdbcParamsModel.getObjects(), new ThirdRowMapper<T>(elementType));
|
|
|
long time = System.currentTimeMillis() - start;
|
|
|
logger.debug(" SQL 执行耗时 : " + time);
|
|
|
return ts;
|
|
@@ -336,7 +347,8 @@ public class BasicBaseDao implements ApplicationContextAware,
|
|
|
public int executeQueryCount(String sql, Object arg) throws Exception {
|
|
|
JdbcParamsModel jdbcParamsModel = commonParseSql(sql, arg);
|
|
|
long start = System.currentTimeMillis();
|
|
|
- Integer count = this.jdbcTemplate.queryForObject(jdbcParamsModel.getSql(), jdbcParamsModel.getObjects(), Integer.class);
|
|
|
+ Integer count =
|
|
|
+ this.jdbcTemplate.queryForObject(jdbcParamsModel.getSql(), jdbcParamsModel.getObjects(), Integer.class);
|
|
|
long time = System.currentTimeMillis() - start;
|
|
|
logger.debug(" SQL 执行耗时 : " + time);
|
|
|
logger.debug(" 获取到的总数 : " + count);
|
|
@@ -350,10 +362,12 @@ public class BasicBaseDao implements ApplicationContextAware,
|
|
|
* @param o 参数
|
|
|
* @return
|
|
|
*/
|
|
|
- public List<Map<String, Object>> executeQueryForListMap(String sql, Object o, ListDynamicSearch dynamicSearch) throws Exception {
|
|
|
+ public List<Map<String, Object>> executeQueryForListMap(String sql, Object o, ListDynamicSearch dynamicSearch)
|
|
|
+ throws Exception {
|
|
|
JdbcParamsModel jdbcParamsModel = commonParseSql(sql, o, dynamicSearch);
|
|
|
long start = System.currentTimeMillis();
|
|
|
- List<Map<String, Object>> maps = this.jdbcTemplate.queryForList(jdbcParamsModel.getSql(), jdbcParamsModel.getObjects());
|
|
|
+ List<Map<String, Object>> maps =
|
|
|
+ this.jdbcTemplate.queryForList(jdbcParamsModel.getSql(), jdbcParamsModel.getObjects());
|
|
|
long time = System.currentTimeMillis() - start;
|
|
|
logger.debug(" SQL 执行耗时 : " + time);
|
|
|
return maps;
|
|
@@ -386,7 +400,8 @@ public class BasicBaseDao implements ApplicationContextAware,
|
|
|
*/
|
|
|
public <T> List<T> queryBigDataForList(String sql, Object[] args, Class<T> elementType) throws DataAccessException {
|
|
|
long start = System.currentTimeMillis();
|
|
|
- List<T> query = (List<T>) this.jdbcTemplate.query(sql, args, new TimeRowMapperResultSetExtractor(new ThirdRowMapper<T>(elementType)));
|
|
|
+ List<T> query = (List<T>)this.jdbcTemplate
|
|
|
+ .query(sql, args, new TimeRowMapperResultSetExtractor(new ThirdRowMapper<T>(elementType)));
|
|
|
long time = System.currentTimeMillis() - start;
|
|
|
logger.debug(" SQL 执行耗时 : " + time);
|
|
|
return query;
|
|
@@ -421,7 +436,8 @@ public class BasicBaseDao implements ApplicationContextAware,
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
- public <T> T executeQueryCount(String sql, Object o, ListDynamicSearch search, Class<T> elementType) throws Exception {
|
|
|
+ public <T> T executeQueryCount(String sql, Object o, ListDynamicSearch search, Class<T> elementType)
|
|
|
+ throws Exception {
|
|
|
JdbcParamsModel jdbcParamsModel = commonParseSql(sql, o, search);
|
|
|
long start = System.currentTimeMillis();
|
|
|
T ts = executorQuerySingleObject(elementType, jdbcParamsModel);
|
|
@@ -440,7 +456,8 @@ public class BasicBaseDao implements ApplicationContextAware,
|
|
|
*/
|
|
|
private <T> T executorQuerySingleObject(Class<T> elementType, JdbcParamsModel jdbcParamsModel) {
|
|
|
try {
|
|
|
- return this.jdbcTemplate.queryForObject(jdbcParamsModel.getSql(), jdbcParamsModel.getObjects(), elementType);
|
|
|
+ return this.jdbcTemplate
|
|
|
+ .queryForObject(jdbcParamsModel.getSql(), jdbcParamsModel.getObjects(), elementType);
|
|
|
} catch (EmptyResultDataAccessException e) {
|
|
|
return null;
|
|
|
} catch (IncorrectResultSizeDataAccessException e) {
|
|
@@ -449,7 +466,6 @@ public class BasicBaseDao implements ApplicationContextAware,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
* 查询一个数据集合对象,根据自定义的数据转换器返回
|
|
|
*
|
|
@@ -528,30 +544,38 @@ public class BasicBaseDao implements ApplicationContextAware,
|
|
|
/**
|
|
|
* 添加数据操作
|
|
|
*
|
|
|
- * @param sql SQL语句
|
|
|
- * @param o 参数对象
|
|
|
+ * @param sql SQL语句
|
|
|
+ * @param request 参数对象
|
|
|
* @return
|
|
|
*/
|
|
|
- public int insert(String sqlId, String table, String sql, Object o) throws Exception {
|
|
|
+ public int insert(String sqlId, String table, String sql, Object request) throws Exception {
|
|
|
int result = 0;
|
|
|
- SqlParameterSource sqlParameterSource = getSqlParameterSource(o);
|
|
|
- GeneratedKeyHolder keyHolder = new GeneratedKeyHolder();
|
|
|
- sqlInvokeBefore(SqlCommandType.INSERT, sqlId, table, sql, o);
|
|
|
- long start = System.currentTimeMillis();
|
|
|
- logger.debug("sql : " + sql + "\r\n" + JSON.toJSONString(o));
|
|
|
- this.getNamedParameterJdbcTemplate().update(sql, sqlParameterSource, keyHolder);
|
|
|
- // 如果存在自动生成的主键,不存在那么表示参数中已经涵盖了,不需要返回。
|
|
|
- if (keyHolder.getKey() != null) {
|
|
|
- result = keyHolder.getKey().intValue();
|
|
|
+ boolean isOk = false;
|
|
|
+ try {
|
|
|
+ SqlParameterSource sqlParameterSource = getSqlParameterSource(request);
|
|
|
+ GeneratedKeyHolder keyHolder = new GeneratedKeyHolder();
|
|
|
+ sqlInvokeBefore(SqlCommandType.INSERT, sqlId, table, sql, request);
|
|
|
+ long start = System.currentTimeMillis();
|
|
|
+ logger.debug("sql : " + sql + "\r\n" + JSON.toJSONString(request));
|
|
|
+ this.getNamedParameterJdbcTemplate().update(sql, sqlParameterSource, keyHolder);
|
|
|
+ // 如果存在自动生成的主键,不存在那么表示参数中已经涵盖了,不需要返回。
|
|
|
+ if (keyHolder.getKey() != null) {
|
|
|
+ result = keyHolder.getKey().intValue();
|
|
|
+ }
|
|
|
+ logger.debug(" 得到主键 : " + result);
|
|
|
+ long time = System.currentTimeMillis() - start;
|
|
|
+ logger.debug(" SQL 执行耗时 : " + time);
|
|
|
+ sqlInvokeAfter(SqlCommandType.INSERT, sqlId, table, sql, request, result);
|
|
|
+ sqlPostCallBack(SqlCommandType.INSERT, sqlId, table, sql, request, result, time, 1);
|
|
|
+ isOk = true;
|
|
|
+ } finally {
|
|
|
+ if (!isOk) {
|
|
|
+ sqlPostCallBack(SqlCommandType.INSERT, sqlId, table, sql, request, result, -1L, -1);
|
|
|
+ }
|
|
|
}
|
|
|
- logger.debug(" 得到主键 : " + result);
|
|
|
- long time = System.currentTimeMillis() - start;
|
|
|
- logger.debug(" SQL 执行耗时 : " + time);
|
|
|
- sqlInvokeAfter(SqlCommandType.INSERT, sqlId, table, sql, o, result);
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
* 将内部参数转换成对应的属性器
|
|
|
*
|
|
@@ -562,16 +586,15 @@ public class BasicBaseDao implements ApplicationContextAware,
|
|
|
private SqlParameterSource getConvertSqlParameterSource(Object bean, Map<String, String> nameMap) {
|
|
|
SqlParameterSource sqlParameterSource = null;
|
|
|
if (bean instanceof Map) {
|
|
|
- sqlParameterSource = new MapDataSqlParameterSource((Map<String, ?>) bean);
|
|
|
+ sqlParameterSource = new MapDataSqlParameterSource((Map<String, ?>)bean);
|
|
|
} else {
|
|
|
sqlParameterSource = new BeanSqlParameterSource(bean);
|
|
|
- ((BeanSqlParameterSource) sqlParameterSource).setPropertyMap(nameMap);
|
|
|
+ ((BeanSqlParameterSource)sqlParameterSource).setPropertyMap(nameMap);
|
|
|
}
|
|
|
return sqlParameterSource;
|
|
|
}
|
|
|
|
|
|
- public int[] batchOperation(SqlCommandType sqlCommandType, String sql, List list)
|
|
|
- throws Exception {
|
|
|
+ public int[] batchOperation(SqlCommandType sqlCommandType, String sql, List list) throws Exception {
|
|
|
return batchOperation(sqlCommandType, "", "", sql, list);
|
|
|
}
|
|
|
|
|
@@ -583,8 +606,8 @@ public class BasicBaseDao implements ApplicationContextAware,
|
|
|
* @param list 数据集合
|
|
|
* @return
|
|
|
*/
|
|
|
- public int[] batchOperation(SqlCommandType sqlCommandType, String sqlId, String table, String sql, List list) throws
|
|
|
- Exception {
|
|
|
+ public int[] batchOperation(SqlCommandType sqlCommandType, String sqlId, String table, String sql, List list)
|
|
|
+ throws Exception {
|
|
|
if (list != null && list.size() > 0) {
|
|
|
long start = System.currentTimeMillis();
|
|
|
logger.debug("sql : " + sql);
|
|
@@ -616,6 +639,7 @@ public class BasicBaseDao implements ApplicationContextAware,
|
|
|
logger.debug(" params size : " + list.size());
|
|
|
long time = System.currentTimeMillis() - start;
|
|
|
logger.debug(" SQL 执行耗时 : " + time);
|
|
|
+ sqlPostCallBack(SqlCommandType.UPDATE, sqlId, table, sql, list, results, time, 1);
|
|
|
return results;
|
|
|
} else {
|
|
|
throw new CoreException("批量操作数据不能为空 ...");
|
|
@@ -633,7 +657,8 @@ public class BasicBaseDao implements ApplicationContextAware,
|
|
|
private int[] batchInsert(String sql, SqlParameterSource[] parameterSource) throws SQLException {
|
|
|
ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql);
|
|
|
String sqlToUse = NamedParameterUtils.substituteNamedParameters(parsedSql, parameterSource[0]);
|
|
|
- List<SqlParameter> declaredParameters = NamedParameterUtils.buildSqlParameterList(parsedSql, parameterSource[0]);
|
|
|
+ List<SqlParameter> declaredParameters =
|
|
|
+ NamedParameterUtils.buildSqlParameterList(parsedSql, parameterSource[0]);
|
|
|
PreparedStatementCreatorFactory pscf = new PreparedStatementCreatorFactory(sqlToUse, declaredParameters);
|
|
|
/**
|
|
|
* 参数执行处理
|
|
@@ -656,14 +681,14 @@ public class BasicBaseDao implements ApplicationContextAware,
|
|
|
* 创建Statement对象的时候,加上Statement.RETURN_GENERATED_KEYS参数
|
|
|
*/
|
|
|
BatchPreparedStatementCreator batchPreparedStatementCreator = new BatchPreparedStatementCreator(sqlToUse);
|
|
|
- return jdbcTemplate.execute(batchPreparedStatementCreator, (PreparedStatementCallback<int[]>) ps -> {
|
|
|
+ return jdbcTemplate.execute(batchPreparedStatementCreator, (PreparedStatementCallback<int[]>)ps -> {
|
|
|
try {
|
|
|
int batchSize = pss.getBatchSize();
|
|
|
int[] keys = new int[batchSize];
|
|
|
|
|
|
InterruptibleBatchPreparedStatementSetter ipss =
|
|
|
- (pss instanceof InterruptibleBatchPreparedStatementSetter ?
|
|
|
- (InterruptibleBatchPreparedStatementSetter) pss : null);
|
|
|
+ (pss instanceof InterruptibleBatchPreparedStatementSetter ?
|
|
|
+ (InterruptibleBatchPreparedStatementSetter)pss : null);
|
|
|
if (JdbcUtils.supportsBatchUpdates(ps.getConnection())) {
|
|
|
for (int i = 0; i < batchSize; i++) {
|
|
|
pss.setValues(ps, i);
|
|
@@ -698,14 +723,14 @@ public class BasicBaseDao implements ApplicationContextAware,
|
|
|
}
|
|
|
} finally {
|
|
|
if (pss instanceof ParameterDisposer) {
|
|
|
- ((ParameterDisposer) pss).cleanupParameters();
|
|
|
+ ((ParameterDisposer)pss).cleanupParameters();
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- protected PreparedStatementCreatorFactory getPreparedStatementCreatorFactory(
|
|
|
- ParsedSql parsedSql, SqlParameterSource paramSource) {
|
|
|
+ protected PreparedStatementCreatorFactory getPreparedStatementCreatorFactory(ParsedSql parsedSql,
|
|
|
+ SqlParameterSource paramSource) {
|
|
|
String sqlToUse = NamedParameterUtils.substituteNamedParameters(parsedSql, paramSource);
|
|
|
List<SqlParameter> declaredParameters = NamedParameterUtils.buildSqlParameterList(parsedSql, paramSource);
|
|
|
return new PreparedStatementCreatorFactory(sqlToUse, declaredParameters);
|
|
@@ -720,8 +745,8 @@ public class BasicBaseDao implements ApplicationContextAware,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private void sqlInvokeAfter(SqlCommandType event, String sqlId, String table, String sql, Object request, Object
|
|
|
- result) {
|
|
|
+ private void sqlInvokeAfter(SqlCommandType event, String sqlId, String table, 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);
|
|
@@ -730,6 +755,37 @@ public class BasicBaseDao implements ApplicationContextAware,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private void sqlPostCallBack(SqlCommandType event, String sqlId, String table, String sql, Object request,
|
|
|
+ Object result, Long time, Integer status) {
|
|
|
+ if (this.sqlPostProcessorList != null && this.sqlPostProcessorList.size() > 0) {
|
|
|
+ for (int i = 0; i < sqlPostProcessorList.size(); i++) {
|
|
|
+ SqlResult sqlResult = new SqlResult();
|
|
|
+ sqlResult.setCommandType(event.name());
|
|
|
+ sqlResult.setSqlId(sqlId);
|
|
|
+ sqlResult.setTable(table);
|
|
|
+ sqlResult.setSql(sql);
|
|
|
+ sqlResult.setRequestParams(request);
|
|
|
+ sqlResult.setResult(result);
|
|
|
+ sqlResult.setTime(time);
|
|
|
+ if (result != null) {
|
|
|
+ if (result instanceof int[]) {
|
|
|
+ sqlResult.setAffectRows(((int[])result).length);
|
|
|
+ sqlResult.setCommandType("batch_" + event.name());
|
|
|
+ } else if (result instanceof Number) {
|
|
|
+ if (event == SqlCommandType.INSERT) {
|
|
|
+ sqlResult.setAffectRows(1);
|
|
|
+ } else {
|
|
|
+ sqlResult.setAffectRows((Integer)result);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ sqlResult.setStatus(status);
|
|
|
+ SQLPostProcess sqlPostProcess = sqlPostProcessorList.get(i);
|
|
|
+ sqlPostProcess.callback(sqlResult);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 执行修改
|
|
|
*
|
|
@@ -745,20 +801,28 @@ public class BasicBaseDao implements ApplicationContextAware,
|
|
|
/**
|
|
|
* 修改数据操作
|
|
|
*
|
|
|
- * @param sql SQL语句
|
|
|
- * @param o 对象
|
|
|
+ * @param sql SQL语句
|
|
|
+ * @param request 对象
|
|
|
* @return
|
|
|
*/
|
|
|
- public int update(String sqlId, String table, String sql, Object o) throws Exception {
|
|
|
- JdbcParamsModel jdbcParamsModel = commonParseSql(sql, o);
|
|
|
- sqlInvokeBefore(SqlCommandType.UPDATE, sqlId, table, sql, o);
|
|
|
- long start = System.currentTimeMillis();
|
|
|
- int update = this.jdbcTemplate.update(jdbcParamsModel.getSql(), jdbcParamsModel.getObjects());
|
|
|
- long time = System.currentTimeMillis() - start;
|
|
|
- sqlInvokeAfter(SqlCommandType.UPDATE, sqlId, table, sql, o, update);
|
|
|
- logger.debug(" SQL 执行耗时 : " + time);
|
|
|
- return update;
|
|
|
+ public int update(String sqlId, String table, String sql, Object request) throws Exception {
|
|
|
+ boolean isOk = false;
|
|
|
+ try {
|
|
|
+ JdbcParamsModel jdbcParamsModel = commonParseSql(sql, request);
|
|
|
+ sqlInvokeBefore(SqlCommandType.UPDATE, sqlId, table, sql, request);
|
|
|
+ long start = System.currentTimeMillis();
|
|
|
+ int update = this.jdbcTemplate.update(jdbcParamsModel.getSql(), jdbcParamsModel.getObjects());
|
|
|
+ long time = System.currentTimeMillis() - start;
|
|
|
+ sqlInvokeAfter(SqlCommandType.UPDATE, sqlId, table, sql, request, update);
|
|
|
+ logger.debug(" SQL 执行耗时 : " + time);
|
|
|
+ sqlPostCallBack(SqlCommandType.UPDATE, sqlId, table, sql, request, update, time, 1);
|
|
|
+ isOk = true;
|
|
|
+ return update;
|
|
|
+ } finally {
|
|
|
+ if (!isOk) {
|
|
|
+ sqlPostCallBack(SqlCommandType.UPDATE, sqlId, table, sql, request, null, -1L, -1);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
-
|
|
|
}
|