From d6f4e1c1c8de8a370c224ea4857aef5f35f4b98a Mon Sep 17 00:00:00 2001 From: 许鹏程 <1821349743@qq.com> Date: 星期二, 28 五月 2024 16:09:02 +0800 Subject: [PATCH] commit --- src/main/java/com/product/datasource/dao/ConnectionInterface.java | 10 src/main/java/com/product/datasource/dao/impl/MysqlDaoImpl.java | 1110 +++++++++++++++++++++++++++++----------------------------- 2 files changed, 569 insertions(+), 551 deletions(-) diff --git a/src/main/java/com/product/datasource/dao/ConnectionInterface.java b/src/main/java/com/product/datasource/dao/ConnectionInterface.java new file mode 100644 index 0000000..badeb7b --- /dev/null +++ b/src/main/java/com/product/datasource/dao/ConnectionInterface.java @@ -0,0 +1,10 @@ +package com.product.datasource.dao; + +import java.sql.Connection; +import java.sql.SQLException; + +public interface ConnectionInterface { + + Connection getConnection() throws SQLException; + +} diff --git a/src/main/java/com/product/datasource/dao/impl/MysqlDaoImpl.java b/src/main/java/com/product/datasource/dao/impl/MysqlDaoImpl.java index fead93d..b025479 100644 --- a/src/main/java/com/product/datasource/dao/impl/MysqlDaoImpl.java +++ b/src/main/java/com/product/datasource/dao/impl/MysqlDaoImpl.java @@ -11,6 +11,7 @@ import com.product.datasource.config.DataBaseType; import com.product.datasource.config.ErrorCode; import com.product.datasource.connection.ConnectionManager; +import com.product.datasource.dao.ConnectionInterface; import com.product.datasource.dao.Dao; import com.product.datasource.entity.BatchResultEntity; import com.product.datasource.entity.DataBaseEntity; @@ -18,6 +19,7 @@ import com.product.datasource.utils.BatchUtil; import org.apache.commons.lang3.StringUtils; +import javax.swing.*; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -30,599 +32,605 @@ */ public class MysqlDaoImpl implements Dao { - private DataBaseEntity dataBaseEntity; - private Connection connection; + private Connection connection; - private boolean outLog; + private boolean outLog; - public MysqlDaoImpl(DataBaseEntity dataBaseEntity) { - if (!DataBaseType.MYSQL.equals(dataBaseEntity.getDataBaseType())) { - throw new BaseException(ErrorCode.INIT_DAO_IMPL_TYPE_ERROR); - } - this.dataBaseEntity = dataBaseEntity; - this.outLog = "dev".equals(Global.getSystemConfig("spring.profiles.active", "prod")); - } + private ConnectionInterface connectionInterface; - private void info(String message) { - if (this.outLog) { - SpringMVCContextHolder.getSystemLogger().info(message); - } - } + public MysqlDaoImpl(DataBaseEntity dataBaseEntity) { + if (!DataBaseType.MYSQL.equals(dataBaseEntity.getDataBaseType())) { + throw new BaseException(ErrorCode.INIT_DAO_IMPL_TYPE_ERROR); + } + this.outLog = "dev".equals(Global.getSystemConfig("spring.profiles.active", "prod")); + connectionInterface = () -> ConnectionManager.getConnection(dataBaseEntity); + } + + public MysqlDaoImpl(ConnectionInterface connectionInterface) { + this.connectionInterface = connectionInterface; + } + + private void info(String message) { + if (this.outLog) { + SpringMVCContextHolder.getSystemLogger().info(message); + } + } - /** - * 鑾峰彇鏁版嵁搴撶被鍨� - * - * @return - * @throws BaseException - */ - @Override - public DataBaseType getDataBaseType() throws com.product.core.exception.BaseException { - return DataBaseType.MYSQL; - } + /** + * 鑾峰彇鏁版嵁搴撶被鍨� + * + * @return + * @throws BaseException + */ + @Override + public DataBaseType getDataBaseType() throws com.product.core.exception.BaseException { + return DataBaseType.MYSQL; + } - public Connection getConnection() throws com.product.core.exception.BaseException { - try { - if (this.connection == null || this.connection.isClosed()) { - //鍒濆鍖栬繛鎺� - this.connection = ConnectionManager.getConnection(dataBaseEntity); - } - return this.connection; - } catch (Exception e) { - e.printStackTrace(); - throw new BaseException(ErrorCode.GET_CONNECTION_FAIL, e); - } - } + public Connection getConnection() throws com.product.core.exception.BaseException { + try { + if (this.connection == null || this.connection.isClosed()) { + //鍒濆鍖栬繛鎺� + this.connection = this.connectionInterface.getConnection(); + } + return this.connection; + } catch (Exception e) { + e.printStackTrace(); + throw new BaseException(ErrorCode.GET_CONNECTION_FAIL, e); + } + } - /** - * 鏂板鍗曟潯璁板綍 - * - * @param fse - * @throws OracleDaoImpl.BaseException - */ - @Override - public void add(FieldSetEntity fse) throws com.product.core.exception.BaseException { - StringBuilder insertSql = BatchUtil.getInsertSql(fse.getMeta()); - try (PreparedStatement pst = getConnection().prepareStatement(insertSql.toString())) { - Object[] fields = fse.getFields(); - for (int i = 0; i < fields.length; i++) { - pst.setObject(i + 1, fse.getObject((String) fields[i])); - } - pst.execute(); - } catch (Exception e) { - e.printStackTrace(); - throw new BaseException(ErrorCode.ADD_RECORD_FAIL, e); - } - } + /** + * 鏂板鍗曟潯璁板綍 + * + * @param fse + * @throws OracleDaoImpl.BaseException + */ + @Override + public void add(FieldSetEntity fse) throws com.product.core.exception.BaseException { + StringBuilder insertSql = BatchUtil.getInsertSql(fse.getMeta()); + try (PreparedStatement pst = getConnection().prepareStatement(insertSql.toString())) { + Object[] fields = fse.getFields(); + for (int i = 0; i < fields.length; i++) { + pst.setObject(i + 1, fse.getObject((String) fields[i])); + } + pst.execute(); + } catch (Exception e) { + e.printStackTrace(); + throw new BaseException(ErrorCode.ADD_RECORD_FAIL, e); + } + } - /** - * 鏌ヨ鍒楄〃鏁版嵁 - * - * @param sql - * @return - */ - @Override - public DataTableEntity getList(String sql) throws com.product.core.exception.BaseException { - return this.getList(sql, new Object[]{}); - } + /** + * 鏌ヨ鍒楄〃鏁版嵁 + * + * @param sql + * @return + */ + @Override + public DataTableEntity getList(String sql) throws com.product.core.exception.BaseException { + return this.getList(sql, new Object[]{}); + } - /** - * 鏌ヨ鍒楄〃鏁版嵁 - * - * @param sql sql - * @param params 鍙傛暟 - * @return - */ - @Override - public DataTableEntity getList(String sql, Object[] params) throws com.product.core.exception.BaseException { - try (PreparedStatement pst = getConnection().prepareStatement(sql)) { - info("鎵цSQL锛歕n" + sql); - if (params != null && params.length > 0) { - for (int i = 0; i < params.length; i++) { - info("param" + (i + 1) + ": " + params[i]); - pst.setObject(i + 1, params[i]); - } - } - ResultSet resultSet = pst.executeQuery(); - return loaddingDataList(resultSet); - } catch (Exception e) { - SpringMVCContextHolder.getSystemLogger().error("EXECUTE_ERROR_SQL:\n" + sql); - throw new BaseException(ErrorCode.GET_LIST_FAIL, e); - } - } + /** + * 鏌ヨ鍒楄〃鏁版嵁 + * + * @param sql sql + * @param params 鍙傛暟 + * @return + */ + @Override + public DataTableEntity getList(String sql, Object[] params) throws com.product.core.exception.BaseException { + try (PreparedStatement pst = getConnection().prepareStatement(sql)) { + info("鎵цSQL锛歕n" + sql); + if (params != null && params.length > 0) { + for (int i = 0; i < params.length; i++) { + info("param" + (i + 1) + ": " + params[i]); + pst.setObject(i + 1, params[i]); + } + } + ResultSet resultSet = pst.executeQuery(); + return loaddingDataList(resultSet); + } catch (Exception e) { + SpringMVCContextHolder.getSystemLogger().error("EXECUTE_ERROR_SQL:\n" + sql); + throw new BaseException(ErrorCode.GET_LIST_FAIL, e); + } + } - /** - * 鏌ヨ鍒楄〃鏁版嵁 - * - * @param tableName - * @param filter - * @return - */ - @Override - public DataTableEntity getList(String tableName, String filter) throws com.product.core.exception.BaseException { - return getList(tableName, filter, new Object[]{}); - } + /** + * 鏌ヨ鍒楄〃鏁版嵁 + * + * @param tableName + * @param filter + * @return + */ + @Override + public DataTableEntity getList(String tableName, String filter) throws com.product.core.exception.BaseException { + return getList(tableName, filter, new Object[]{}); + } - /** - * 鏌ヨ鍒楄〃鏁版嵁 - * - * @param tableName 琛ㄥ悕 - * @param filter 鏉′欢 - * @param params 鍙傛暟 - * @return - */ - @Override - public DataTableEntity getList(String tableName, String filter, Object[] params) throws com.product.core.exception.BaseException { - return this.getList(tableName, filter, null, params); - } + /** + * 鏌ヨ鍒楄〃鏁版嵁 + * + * @param tableName 琛ㄥ悕 + * @param filter 鏉′欢 + * @param params 鍙傛暟 + * @return + */ + @Override + public DataTableEntity getList(String tableName, String filter, Object[] params) throws com.product.core.exception.BaseException { + return this.getList(tableName, filter, null, params); + } - /** - * 鏌ヨ鍒楄〃鏁版嵁 - * - * @param tableName 琛ㄥ悕 - * @param filter 鏉′欢 - * @param fields - * @param params 鍙傛暟 - * @return - */ - @Override - public DataTableEntity getList(String tableName, String filter, String[] fields, Object[] params) throws com.product.core.exception.BaseException { - StringBuilder sql = new StringBuilder("SELECT "); - if (ArrayUtil.isEmpty(fields)) { - sql.append(" * "); - } else { - for (int i = 0; i < fields.length; i++) { - sql.append(fields[i]); - if (i + 1 < fields.length) { - sql.append(","); - } - } - } - sql.append(" FROM "); - sql.append(tableName); - if (!StringUtils.isEmpty(filter)) { - sql.append(" WHERE (").append(filter).append(" ) "); - } - return this.getList(sql.toString(), params); - } + /** + * 鏌ヨ鍒楄〃鏁版嵁 + * + * @param tableName 琛ㄥ悕 + * @param filter 鏉′欢 + * @param fields + * @param params 鍙傛暟 + * @return + */ + @Override + public DataTableEntity getList(String tableName, String filter, String[] fields, Object[] params) throws com.product.core.exception.BaseException { + StringBuilder sql = new StringBuilder("SELECT "); + if (ArrayUtil.isEmpty(fields)) { + sql.append(" * "); + } else { + for (int i = 0; i < fields.length; i++) { + sql.append(fields[i]); + if (i + 1 < fields.length) { + sql.append(","); + } + } + } + sql.append(" FROM "); + sql.append(tableName); + if (!StringUtils.isEmpty(filter)) { + sql.append(" WHERE (").append(filter).append(" ) "); + } + return this.getList(sql.toString(), params); + } - /** - * 鏌ヨ鍒楄〃鏁版嵁 - * - * @param tableName 琛ㄥ悕 - * @param filter 鏉′欢 - * @param params 鍙傛暟 - * @param pageIndex 椤垫暟 - * @param pageSize 鏉℃暟 - * @return - */ - @Override - public DataTableEntity getList(String tableName, String filter, Object[] params, int pageIndex, int pageSize) throws com.product.core.exception.BaseException { - StringBuilder sql = new StringBuilder(); - sql.append(" SELECT * FROM ").append(tableName); - if (!StringUtils.isEmpty(filter)) { - sql.append(" WHERE (").append(filter).append(" )"); - } - return getList(sql.toString(), params, pageIndex, pageSize); - } + /** + * 鏌ヨ鍒楄〃鏁版嵁 + * + * @param tableName 琛ㄥ悕 + * @param filter 鏉′欢 + * @param params 鍙傛暟 + * @param pageIndex 椤垫暟 + * @param pageSize 鏉℃暟 + * @return + */ + @Override + public DataTableEntity getList(String tableName, String filter, Object[] params, int pageIndex, int pageSize) throws com.product.core.exception.BaseException { + StringBuilder sql = new StringBuilder(); + sql.append(" SELECT * FROM ").append(tableName); + if (!StringUtils.isEmpty(filter)) { + sql.append(" WHERE (").append(filter).append(" )"); + } + return getList(sql.toString(), params, pageIndex, pageSize); + } - @Override - public DataTableEntity getList(String tableName, String filter, Object[] params, String orderBy, int pageIndex, int pageSize) throws com.product.core.exception.BaseException { - StringBuilder sql = new StringBuilder(); - sql.append(" SELECT * FROM ").append(tableName); - if (!StringUtils.isEmpty(filter)) { - sql.append(" WHERE (").append(filter).append(" )"); - } - if (!StringUtils.isEmpty(orderBy)) { - sql.append(" ORDER BY ").append(orderBy); - } - return getList(sql.toString(), params, pageIndex, pageSize); - } + @Override + public DataTableEntity getList(String tableName, String filter, Object[] params, String orderBy, int pageIndex, int pageSize) throws com.product.core.exception.BaseException { + StringBuilder sql = new StringBuilder(); + sql.append(" SELECT * FROM ").append(tableName); + if (!StringUtils.isEmpty(filter)) { + sql.append(" WHERE (").append(filter).append(" )"); + } + if (!StringUtils.isEmpty(orderBy)) { + sql.append(" ORDER BY ").append(orderBy); + } + return getList(sql.toString(), params, pageIndex, pageSize); + } - @Override - public DataTableEntity getList(String tableName, String[] fields, String filter, Object[] params, String orderBy, int pageIndex, int pageSize) throws com.product.core.exception.BaseException { - StringBuilder sql = new StringBuilder(); - sql.append(" SELECT "); - if (ArrayUtil.isEmpty(fields)) { - sql.append("*"); - } else { - for (int i = 0; i < fields.length; i++) { - sql.append(fields[i]); - if (i + 1 < fields.length) { - sql.append(","); - } - } - } - sql.append(" FROM ").append(tableName); - if (!StringUtils.isEmpty(filter)) { - sql.append(" WHERE (").append(filter).append(" )"); - } - if (!StringUtils.isEmpty(orderBy)) { - sql.append(" ORDER BY ").append(orderBy); - } - return null; - } + @Override + public DataTableEntity getList(String tableName, String[] fields, String filter, Object[] params, String orderBy, int pageIndex, int pageSize) throws com.product.core.exception.BaseException { + StringBuilder sql = new StringBuilder(); + sql.append(" SELECT "); + if (ArrayUtil.isEmpty(fields)) { + sql.append("*"); + } else { + for (int i = 0; i < fields.length; i++) { + sql.append(fields[i]); + if (i + 1 < fields.length) { + sql.append(","); + } + } + } + sql.append(" FROM ").append(tableName); + if (!StringUtils.isEmpty(filter)) { + sql.append(" WHERE (").append(filter).append(" )"); + } + if (!StringUtils.isEmpty(orderBy)) { + sql.append(" ORDER BY ").append(orderBy); + } + return null; + } - public DataTableEntity getList(String sql, Object[] params, int pageIndex, int pageSize) throws com.product.core.exception.BaseException { - int number = getPageParam(pageIndex, pageSize); - StringBuilder sb = new StringBuilder(sql); - sb.append(" LIMIT ?,? "); - if (params == null) { - params = new Object[]{number, pageSize}; - } else { - params = ArrayUtil.append(params, number, pageSize); - } - return this.getList(sb.toString(), params); - } + public DataTableEntity getList(String sql, Object[] params, int pageIndex, int pageSize) throws com.product.core.exception.BaseException { + int number = getPageParam(pageIndex, pageSize); + StringBuilder sb = new StringBuilder(sql); + sb.append(" LIMIT ?,? "); + if (params == null) { + params = new Object[]{number, pageSize}; + } else { + params = ArrayUtil.append(params, number, pageSize); + } + return this.getList(sb.toString(), params); + } - private int getPageParam(int pageIndex, int pageSize) { - return pageIndex <= 1 ? 0 : (pageIndex - 1) * pageSize; - } + private int getPageParam(int pageIndex, int pageSize) { + return pageIndex <= 1 ? 0 : (pageIndex - 1) * pageSize; + } - /** - * 鏌ヨ涓�鏉℃暟鎹� - * - * @param sql - * @return - */ - @Override - public FieldSetEntity getOne(String sql) throws com.product.core.exception.BaseException { - return this.getOne(sql, new Object[]{}); - } + /** + * 鏌ヨ涓�鏉℃暟鎹� + * + * @param sql + * @return + */ + @Override + public FieldSetEntity getOne(String sql) throws com.product.core.exception.BaseException { + return this.getOne(sql, new Object[]{}); + } - /** - * 鏌ヨ涓�鏉℃暟鎹� - * - * @param sql sql - * @param params 鍙傛暟 - * @return - */ - @Override - public FieldSetEntity getOne(String sql, Object[] params) throws com.product.core.exception.BaseException { - DataTableEntity list = this.getList(sql, params); - if (DataTableEntity.isEmpty(list)) { - return null; - } - return list.getFieldSetEntity(0); - } + /** + * 鏌ヨ涓�鏉℃暟鎹� + * + * @param sql sql + * @param params 鍙傛暟 + * @return + */ + @Override + public FieldSetEntity getOne(String sql, Object[] params) throws com.product.core.exception.BaseException { + DataTableEntity list = this.getList(sql, params); + if (DataTableEntity.isEmpty(list)) { + return null; + } + return list.getFieldSetEntity(0); + } - /** - * 鏌ヨ涓�鏉℃暟鎹� - * - * @param tableName - * @param filter - * @return - */ - @Override - public FieldSetEntity getOne(String tableName, String filter) throws com.product.core.exception.BaseException { - return this.getOne(tableName, filter, null); - } + /** + * 鏌ヨ涓�鏉℃暟鎹� + * + * @param tableName + * @param filter + * @return + */ + @Override + public FieldSetEntity getOne(String tableName, String filter) throws com.product.core.exception.BaseException { + return this.getOne(tableName, filter, null); + } - /** - * 鏌ヨ涓�鏉℃暟鎹� - * - * @param tableName 琛ㄥ悕 - * @param filter 鏉′欢 - * @param params 鍙傛暟 - * @return - */ - @Override - public FieldSetEntity getOne(String tableName, String filter, Object[] params) throws com.product.core.exception.BaseException { - return this.getOne(tableName, filter, null, params); - } + /** + * 鏌ヨ涓�鏉℃暟鎹� + * + * @param tableName 琛ㄥ悕 + * @param filter 鏉′欢 + * @param params 鍙傛暟 + * @return + */ + @Override + public FieldSetEntity getOne(String tableName, String filter, Object[] params) throws com.product.core.exception.BaseException { + return this.getOne(tableName, filter, null, params); + } - /** - * 鏌ヨ涓�鏉℃暟鎹� - * - * @param tableName 琛ㄥ悕 - * @param filter 鏉′欢 - * @param fields - * @param params 鍙傛暟 - * @return - */ - @Override - public FieldSetEntity getOne(String tableName, String filter, String[] fields, Object[] params) throws com.product.core.exception.BaseException { - StringBuilder sb = new StringBuilder(); - sb.append(" SELECT "); - if (ArrayUtil.isEmpty(fields)) { - sb.append(" * "); - } else { - for (int i = 0; i < fields.length; i++) { - sb.append(fields[i]); - if (i + 1 < fields.length) { - sb.append(","); - } - } - } - sb.append(" FROM ").append(tableName); - sb.append(" WHERE "); - if (!StringUtils.isEmpty(filter)) { - sb.append(" (").append(filter).append(") "); - } - sb.append(" limit 1"); - DataTableEntity list = this.getList(sb.toString(), params); - return DataTableEntity.isEmpty(list) ? null : list.getFieldSetEntity(0); - } + /** + * 鏌ヨ涓�鏉℃暟鎹� + * + * @param tableName 琛ㄥ悕 + * @param filter 鏉′欢 + * @param fields + * @param params 鍙傛暟 + * @return + */ + @Override + public FieldSetEntity getOne(String tableName, String filter, String[] fields, Object[] params) throws com.product.core.exception.BaseException { + StringBuilder sb = new StringBuilder(); + sb.append(" SELECT "); + if (ArrayUtil.isEmpty(fields)) { + sb.append(" * "); + } else { + for (int i = 0; i < fields.length; i++) { + sb.append(fields[i]); + if (i + 1 < fields.length) { + sb.append(","); + } + } + } + sb.append(" FROM ").append(tableName); + sb.append(" WHERE "); + if (!StringUtils.isEmpty(filter)) { + sb.append(" (").append(filter).append(") "); + } + sb.append(" limit 1"); + DataTableEntity list = this.getList(sb.toString(), params); + return DataTableEntity.isEmpty(list) ? null : list.getFieldSetEntity(0); + } - /** - * 鎵瑰鐞嗘坊鍔犳暟鎹� - * - * @param data 鏁版嵁闆� - * @return - */ - @Override - public BatchResultEntity addBatch(DataTableEntity data) throws com.product.core.exception.BaseException { - info("寮�濮嬫壒閲忔柊澧炴暟鎹�"); - if (DataTableEntity.isEmpty(data)) { - info("鎵归噺鏂板鏁版嵁鏉℃暟涓�0"); - return new BatchResultEntity(0); - } - TimeInterval timer = DateUtil.timer(); - info("鎵归噺鏂板鏁版嵁鏉℃暟涓猴細" + data.getRows()); - BatchResultEntity batchResultEntity = new BatchResultEntity(data.getRows()); -// Connection connection = ConnectionManager.getConnection(dataBaseEntity); - try { - StringBuilder insertSql = BatchUtil.getInsertSql(data.getMeta()); - info("SQL:" + insertSql); - Connection connection = getConnection(); - connection.setAutoCommit(false); + /** + * 鎵瑰鐞嗘坊鍔犳暟鎹� + * + * @param data 鏁版嵁闆� + * @return + */ + @Override + public BatchResultEntity addBatch(DataTableEntity data) throws com.product.core.exception.BaseException { + info("寮�濮嬫壒閲忔柊澧炴暟鎹�"); + if (DataTableEntity.isEmpty(data)) { + info("鎵归噺鏂板鏁版嵁鏉℃暟涓�0"); + return new BatchResultEntity(0); + } + TimeInterval timer = DateUtil.timer(); + info("鎵归噺鏂板鏁版嵁鏉℃暟涓猴細" + data.getRows()); + BatchResultEntity batchResultEntity = new BatchResultEntity(data.getRows()); +// ConnectionInterface connection = ConnectionManager.getConnection(dataBaseEntity); + try { + StringBuilder insertSql = BatchUtil.getInsertSql(data.getMeta()); + info("SQL:" + insertSql); + Connection connection = getConnection(); + connection.setAutoCommit(false); - try (PreparedStatement pst = connection.prepareStatement(insertSql.toString())) { - Object[] fields = data.getMeta().getFields(); - for (int i = 0; i < data.getRows(); i++) { - FieldSetEntity fse = data.getFieldSetEntity(i); - for (int j = 1; j <= fields.length; j++) { - if (fse.getObject(fields[j - 1].toString()) instanceof oracle.sql.TIMESTAMP) { - pst.setObject(j, fse.getObject(fields[j - 1].toString()).toString()); - } else { - pst.setObject(j, fse.getObject(fields[j - 1].toString())); - } - } - pst.addBatch(); - } - info("寮�濮嬫墽琛屾壒閲忔彁浜ゆ暟鎹�"); - int[] ints = pst.executeBatch(); - connection.commit(); - info("鎵ц鎵归噺鎻愪氦鏁版嵁瀹屾垚锛屽叡鎻愪氦 " + data.getRows() + " 鏉�,鑰楁椂锛�" + timer.intervalSecond() + " 绉� 锛�"); - pst.clearBatch(); - } catch (Exception e) { - throw e; - } finally { - connection.setAutoCommit(true); - } - } catch (Exception e) { - SpringMVCContextHolder.getSystemLogger().error(e); - throw new BaseException(ErrorCode.ADD_BATCH_ERROR, e); - } - return batchResultEntity; - } + try (PreparedStatement pst = connection.prepareStatement(insertSql.toString())) { + Object[] fields = data.getMeta().getFields(); + for (int i = 0; i < data.getRows(); i++) { + FieldSetEntity fse = data.getFieldSetEntity(i); + for (int j = 1; j <= fields.length; j++) { + if (fse.getObject(fields[j - 1].toString()) instanceof oracle.sql.TIMESTAMP) { + pst.setObject(j, fse.getObject(fields[j - 1].toString()).toString()); + } else { + pst.setObject(j, fse.getObject(fields[j - 1].toString())); + } + } + pst.addBatch(); + } + info("寮�濮嬫墽琛屾壒閲忔彁浜ゆ暟鎹�"); + int[] ints = pst.executeBatch(); + connection.commit(); + info("鎵ц鎵归噺鎻愪氦鏁版嵁瀹屾垚锛屽叡鎻愪氦 " + data.getRows() + " 鏉�,鑰楁椂锛�" + timer.intervalSecond() + " 绉� 锛�"); + pst.clearBatch(); + } catch (Exception e) { + SpringMVCContextHolder.getSystemLogger().error(e); + throw e; + } finally { + connection.setAutoCommit(true); + } + } catch (Exception e) { + SpringMVCContextHolder.getSystemLogger().error(e); + throw new BaseException(ErrorCode.ADD_BATCH_ERROR, e); + } + return batchResultEntity; + } - /** - * 鎵瑰鐞嗘坊鍔犳暟鎹� - * - * @param data 鏁版嵁闆� - * @param AutomaticallyPrimaryField - * @return - */ - @Override - @Deprecated - public BatchResultEntity addBatch(DataTableEntity data, String AutomaticallyPrimaryField) throws com.product.core.exception.BaseException { - return addBatch(data); - } + /** + * 鎵瑰鐞嗘坊鍔犳暟鎹� + * + * @param data 鏁版嵁闆� + * @param AutomaticallyPrimaryField + * @return + */ + @Override + @Deprecated + public BatchResultEntity addBatch(DataTableEntity data, String AutomaticallyPrimaryField) throws com.product.core.exception.BaseException { + return addBatch(data); + } - @Override - public int update(FieldSetEntity fse, UpdateFilterEntity updateFilter) throws com.product.core.exception.BaseException { - //todo 寰呭疄鐜� - return -1; - } + @Override + public int update(FieldSetEntity fse, UpdateFilterEntity updateFilter) throws com.product.core.exception.BaseException { + //todo 寰呭疄鐜� + return -1; + } - /** - * 鎵瑰鐞嗘洿鏂版暟鎹� - * - * @param data - * @param updateFilter 杩囨护鏉′欢 - * @param isCommit 鑷姩鎻愪氦 - * @return - */ - @Override - public BatchResultEntity updateBatch(DataTableEntity data, UpdateFilterEntity updateFilter, boolean isCommit) throws com.product.core.exception.BaseException { - info("寮�濮嬫壒閲忔洿鏂版暟鎹�"); - if (DataTableEntity.isEmpty(data)) { - info("鎵归噺鏇存柊鏁版嵁鏉℃暟涓�0"); - return new BatchResultEntity(0); - } - TimeInterval timer = DateUtil.timer(); - info("鎵归噺鏇存柊鏁版嵁鏉℃暟涓猴細" + data.getRows()); - BatchResultEntity batchResultEntity = new BatchResultEntity(data.getRows()); -// Connection connection = ConnectionManager.getConnection(dataBaseEntity); - try { - StringBuilder updateSql = BatchUtil.getUpdateSql(data.getMeta(), updateFilter.getFilter()); - info("SQL: " + updateSql); - Connection connection = getConnection(); - connection.setAutoCommit(false); - try (PreparedStatement pst = connection.prepareStatement(updateSql.toString())) { - Object[] fields = data.getMeta().getFields(); - for (int i = 0; i < data.getRows(); i++) { - FieldSetEntity fse = data.getFieldSetEntity(i); - int j = 1; - for (; j <= fields.length; j++) { - Object value = fse.getObject(fields[j - 1].toString()); - pst.setObject(j,value ); - info("鍙傛暟锛� " + (fields[j - 1]) + " = " + value); - } - j--; - Object[] valueFields = updateFilter.getValueFields(); - for (int i1 = 1; i1 <= valueFields.length; i1++) { - Object value = fse.getObject(valueFields[i1 - 1].toString()); - pst.setObject(j + i1, value); - info("鍙傛暟锛� " + (valueFields[i1 - 1]) + " = " + value); - } - pst.addBatch(); - } - info("寮�濮嬫墽琛屾壒閲忔彁浜ゆ暟鎹�"); - pst.executeBatch(); - info("鎵ц鎵归噺鎻愪氦鏁版嵁瀹屾垚锛屽叡鎻愪氦 " + data.getRows() + " 鏉�,鑰楁椂锛�" + timer.intervalSecond() + " 绉� 锛�"); - pst.clearBatch(); - if (isCommit) { - connection.commit(); - } - batchResultEntity.setConnection(connection); - return batchResultEntity; - } catch (Exception e) { - throw e; - } finally { - if (isCommit) { - connection.setAutoCommit(true); - } + /** + * 鎵瑰鐞嗘洿鏂版暟鎹� + * + * @param data + * @param updateFilter 杩囨护鏉′欢 + * @param isCommit 鑷姩鎻愪氦 + * @return + */ + @Override + public BatchResultEntity updateBatch(DataTableEntity data, UpdateFilterEntity updateFilter, boolean isCommit) throws com.product.core.exception.BaseException { + info("寮�濮嬫壒閲忔洿鏂版暟鎹�"); + if (DataTableEntity.isEmpty(data)) { + info("鎵归噺鏇存柊鏁版嵁鏉℃暟涓�0"); + return new BatchResultEntity(0); + } + TimeInterval timer = DateUtil.timer(); + info("鎵归噺鏇存柊鏁版嵁鏉℃暟涓猴細" + data.getRows()); + BatchResultEntity batchResultEntity = new BatchResultEntity(data.getRows()); +// ConnectionInterface connection = ConnectionManager.getConnection(dataBaseEntity); + try { + StringBuilder updateSql = BatchUtil.getUpdateSql(data.getMeta(), updateFilter.getFilter()); + info("SQL: " + updateSql); + Connection connection = getConnection(); + connection.setAutoCommit(false); + try (PreparedStatement pst = connection.prepareStatement(updateSql.toString())) { + Object[] fields = data.getMeta().getFields(); + for (int i = 0; i < data.getRows(); i++) { + FieldSetEntity fse = data.getFieldSetEntity(i); + int j = 1; + for (; j <= fields.length; j++) { + Object value = fse.getObject(fields[j - 1].toString()); + pst.setObject(j, value); + info("鍙傛暟锛� " + (fields[j - 1]) + " = " + value); + } + j--; + Object[] valueFields = updateFilter.getValueFields(); + for (int i1 = 1; i1 <= valueFields.length; i1++) { + Object value = fse.getObject(valueFields[i1 - 1].toString()); + pst.setObject(j + i1, value); + info("鍙傛暟锛� " + (valueFields[i1 - 1]) + " = " + value); + } + pst.addBatch(); + } + info("寮�濮嬫墽琛屾壒閲忔彁浜ゆ暟鎹�"); + pst.executeBatch(); + info("鎵ц鎵归噺鎻愪氦鏁版嵁瀹屾垚锛屽叡鎻愪氦 " + data.getRows() + " 鏉�,鑰楁椂锛�" + timer.intervalSecond() + " 绉� 锛�"); + pst.clearBatch(); + if (isCommit) { + connection.commit(); + } + batchResultEntity.setConnection(connection); + return batchResultEntity; + } catch (Exception e) { + throw e; + } finally { + if (isCommit) { + connection.setAutoCommit(true); + } // this.closeConnection(); - } - } catch (Exception e) { - e.printStackTrace(); - SpringMVCContextHolder.getSystemLogger().error(e); - throw new BaseException(ErrorCode.UPDATE_BATCH_ERROR, e); - } - } + } + } catch (Exception e) { + e.printStackTrace(); + SpringMVCContextHolder.getSystemLogger().error(e); + throw new BaseException(ErrorCode.UPDATE_BATCH_ERROR, e); + } + } - /** - * 鍒犻櫎鏁版嵁 - * - * @param sql sql璇彞 - * @param params 鍙傛暟 - * @return - */ - @Override - public Boolean delete(String sql, Object[] params) throws com.product.core.exception.BaseException { - return executeSql(sql, params); - } + /** + * 鍒犻櫎鏁版嵁 + * + * @param sql sql璇彞 + * @param params 鍙傛暟 + * @return + */ + @Override + public Boolean delete(String sql, Object[] params) throws com.product.core.exception.BaseException { + return executeSql(sql, params); + } - /** - * 鍒犻櫎鏁版嵁 - * - * @param tableName 琛ㄥ悕 - * @param filter 鏉′欢 - * @return - */ - @Override - public Boolean delete(String tableName, String filter) throws com.product.core.exception.BaseException { - return delete(tableName, filter, null); - } + /** + * 鍒犻櫎鏁版嵁 + * + * @param tableName 琛ㄥ悕 + * @param filter 鏉′欢 + * @return + */ + @Override + public Boolean delete(String tableName, String filter) throws com.product.core.exception.BaseException { + return delete(tableName, filter, null); + } - /** - * 鍒犻櫎鏁版嵁 - * - * @param tableName 琛ㄥ悕 - * @param filter 鏉′欢 - * @param params 鍙傛暟 - * @return - */ - @Override - public Boolean delete(String tableName, String filter, Object[] params) throws com.product.core.exception.BaseException { - StringBuilder sql = new StringBuilder(); - sql.append(" DELETE FROM ").append(tableName); - if (!StringUtils.isEmpty(filter)) { - sql.append(" WHERE (").append(filter).append(" ) "); - } - return executeSql(sql.toString(), params); - } + /** + * 鍒犻櫎鏁版嵁 + * + * @param tableName 琛ㄥ悕 + * @param filter 鏉′欢 + * @param params 鍙傛暟 + * @return + */ + @Override + public Boolean delete(String tableName, String filter, Object[] params) throws com.product.core.exception.BaseException { + StringBuilder sql = new StringBuilder(); + sql.append(" DELETE FROM ").append(tableName); + if (!StringUtils.isEmpty(filter)) { + sql.append(" WHERE (").append(filter).append(" ) "); + } + return executeSql(sql.toString(), params); + } - public int deleteRInt(String tableName, String filter, Object[] params) throws com.product.core.exception.BaseException { - StringBuilder sql = new StringBuilder(); - sql.append(" DELETE FROM ").append(tableName); - if (!StringUtils.isEmpty(filter)) { - sql.append(" WHERE (").append(filter).append(" ) "); - } - return executeSqlResult(sql.toString(), params); - } + public int deleteRInt(String tableName, String filter, Object[] params) throws com.product.core.exception.BaseException { + StringBuilder sql = new StringBuilder(); + sql.append(" DELETE FROM ").append(tableName); + if (!StringUtils.isEmpty(filter)) { + sql.append(" WHERE (").append(filter).append(" ) "); + } + return executeSqlResult(sql.toString(), params); + } - /** - * 鎵цsql - * - * @param sql - * @return - */ - @Override - public boolean executeSql(String sql) throws com.product.core.exception.BaseException { - return executeSql(sql, null); - } + /** + * 鎵цsql + * + * @param sql + * @return + */ + @Override + public boolean executeSql(String sql) throws com.product.core.exception.BaseException { + return executeSql(sql, null); + } - /** - * 鎵цsql - * - * @param sql - * @param params - * @return - */ - @Override - public boolean executeSql(String sql, Object[] params) throws com.product.core.exception.BaseException { - info("鎵цSQL锛歕n" + sql); - try (PreparedStatement pst = getConnection().prepareStatement(sql)) { - if (params != null && params.length > 0) { - for (int i = 0; i < params.length; i++) { - info("param" + (i + 1) + ": " + params[i]); - pst.setObject(i + 1, params[i].toString()); - } - } - return pst.execute(); - } catch (Exception e) { - e.printStackTrace(); - info("ERROR_SQL锛歕n" + sql); - throw new BaseException(ErrorCode.EXECUTE_SQL_FAIL, e); - } - } + /** + * 鎵цsql + * + * @param sql + * @param params + * @return + */ + @Override + public boolean executeSql(String sql, Object[] params) throws com.product.core.exception.BaseException { + info("鎵цSQL锛歕n" + sql); + try (PreparedStatement pst = getConnection().prepareStatement(sql)) { + if (params != null && params.length > 0) { + for (int i = 0; i < params.length; i++) { + info("param" + (i + 1) + ": " + params[i]); + pst.setObject(i + 1, params[i].toString()); + } + } + return pst.execute(); + } catch (Exception e) { + e.printStackTrace(); + info("ERROR_SQL锛歕n" + sql); + throw new BaseException(ErrorCode.EXECUTE_SQL_FAIL, e); + } + } - /** - * 鎵цsql - * - * @param sql - * @param params - * @return - */ - public int executeSqlResult(String sql, Object[] params) throws com.product.core.exception.BaseException { - try (PreparedStatement pst = getConnection().prepareStatement(sql)) { - info("鎵цSQL锛歕n" + sql); - if (params != null && params.length > 0) { - for (int i = 0; i < params.length; i++) { - info("param" + (i + 1) + ": " + params[i]); - pst.setObject(i + 1, params[i].toString()); - } - } - return pst.executeUpdate(); - } catch (Exception e) { - throw new BaseException(ErrorCode.EXECUTE_SQL_FAIL, e); - } - } + /** + * 鎵цsql + * + * @param sql + * @param params + * @return + */ + public int executeSqlResult(String sql, Object[] params) throws com.product.core.exception.BaseException { + try (PreparedStatement pst = getConnection().prepareStatement(sql)) { + info("鎵цSQL锛歕n" + sql); + if (params != null && params.length > 0) { + for (int i = 0; i < params.length; i++) { + info("param" + (i + 1) + ": " + params[i]); + pst.setObject(i + 1, params[i].toString()); + } + } + return pst.executeUpdate(); + } catch (Exception e) { + throw new BaseException(ErrorCode.EXECUTE_SQL_FAIL, e); + } + } - @Override - public void closeConnection() { - try { - if (this.connection != null && !this.connection.isClosed()) { - this.connection.close(); - } - } catch (SQLException e) { - e.printStackTrace(); - } finally { - this.connection = null; - } - } + @Override + public void closeConnection() { + try { + if (this.connection != null && !this.connection.isClosed()) { + this.connection.close(); + } + } catch (SQLException e) { + e.printStackTrace(); + } finally { + this.connection = null; + } + } - class BaseException extends com.product.core.exception.BaseException { - public BaseException(IEnum a) { - super(a); - } + class BaseException extends com.product.core.exception.BaseException { + public BaseException(IEnum a) { + super(a); + } - public BaseException(IEnum iEnum, Throwable throwable) { - super(iEnum.getValue(), iEnum.getText() + (throwable.getMessage() != null ? "," + throwable.getMessage() : "")); - } - } + public BaseException(IEnum iEnum, Throwable throwable) { + super(iEnum.getValue(), iEnum.getText() + (throwable.getMessage() != null ? "," + throwable.getMessage() : "")); + } + } - @Override - protected void finalize() throws Throwable { - this.closeConnection(); - super.finalize(); - } + @Override + protected void finalize() throws Throwable { + this.closeConnection(); + super.finalize(); + } } -- Gitblit v1.9.2