| | |
| | | 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; |
| | |
| | | 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; |
| | |
| | | */ |
| | | public class MysqlDaoImpl implements Dao { |
| | | |
| | | private DataBaseEntity dataBaseEntity; |
| | | |
| | | private Connection connection; |
| | | |
| | | private boolean outLog; |
| | | |
| | | private ConnectionInterface connectionInterface; |
| | | |
| | | 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")); |
| | | connectionInterface = () -> ConnectionManager.getConnection(dataBaseEntity); |
| | | } |
| | | |
| | | public MysqlDaoImpl(ConnectionInterface connectionInterface) { |
| | | this.connectionInterface = connectionInterface; |
| | | } |
| | | |
| | | private void info(String message) { |
| | |
| | | try { |
| | | if (this.connection == null || this.connection.isClosed()) { |
| | | //初始化连接 |
| | | this.connection = ConnectionManager.getConnection(dataBaseEntity); |
| | | this.connection = this.connectionInterface.getConnection(); |
| | | } |
| | | return this.connection; |
| | | } catch (Exception e) { |
| | |
| | | TimeInterval timer = DateUtil.timer(); |
| | | info("批量新增数据条数为:" + data.getRows()); |
| | | BatchResultEntity batchResultEntity = new BatchResultEntity(data.getRows()); |
| | | // Connection connection = ConnectionManager.getConnection(dataBaseEntity); |
| | | // ConnectionInterface connection = ConnectionManager.getConnection(dataBaseEntity); |
| | | try { |
| | | StringBuilder insertSql = BatchUtil.getInsertSql(data.getMeta()); |
| | | info("SQL:" + insertSql); |
| | |
| | | info("执行批量提交数据完成,共提交 " + data.getRows() + " 条,耗时:" + timer.intervalSecond() + " 秒 !"); |
| | | pst.clearBatch(); |
| | | } catch (Exception e) { |
| | | SpringMVCContextHolder.getSystemLogger().error(e); |
| | | throw e; |
| | | } finally { |
| | | connection.setAutoCommit(true); |
| | |
| | | TimeInterval timer = DateUtil.timer(); |
| | | info("批量更新数据条数为:" + data.getRows()); |
| | | BatchResultEntity batchResultEntity = new BatchResultEntity(data.getRows()); |
| | | // Connection connection = ConnectionManager.getConnection(dataBaseEntity); |
| | | // ConnectionInterface connection = ConnectionManager.getConnection(dataBaseEntity); |
| | | try { |
| | | StringBuilder updateSql = BatchUtil.getUpdateSql(data.getMeta(), updateFilter.getFilter()); |
| | | info("SQL: " + updateSql); |