#
18516761980
2021-09-24 0df8f305406412a1229bf412a574cb026ac802cf
src/main/java/com/zy/common/service/erp/ErpSqlServer.java
@@ -2,9 +2,14 @@
import com.core.common.Cools;
import com.zy.common.properties.ErpDbProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
@@ -15,6 +20,7 @@
 * 简单持久层框架
 * Created by vincent on 2020/11/26
 */
@Slf4j
@Service
public class ErpSqlServer {
@@ -61,11 +67,13 @@
    /*********************************   核心层   *********************************/
    /*****************************************************************************/
    private List<Map<String, Object>> executeQuery(String sql) {
        try {
            Connection conn = getConn();
            pstm = conn.prepareStatement(sql);
            rs = pstm.executeQuery();
//            List<Map<String, Object>> maps = convertList(rs);
            return convertList(rs);
        } catch (Exception e) {
            e.printStackTrace();
@@ -82,6 +90,8 @@
            return pstm.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("更新ERP中间表失败===>>" + sql);
            log.error("更新ERP中间表失败===>>" + e);
            return 0;
        } finally {
            release();
@@ -102,12 +112,13 @@
        return list;
    }
    private Connection getConn() {
        if (null == this.conn) {
    public synchronized Connection getConn() throws SQLException {
        if (null == this.conn || this.conn.isClosed()) {
            try {
                Class.forName(erpDbProperties.getDriver_class_name()).newInstance();
                this.conn = DriverManager.getConnection(erpDbProperties.getUr(), erpDbProperties.getUsername(), erpDbProperties.getPassword());
            } catch (Exception e) {
                log.error("获取ERP数据库连接失败");
                e.printStackTrace();
                throw new RuntimeException("获取ERP数据库连接失败");
            }
@@ -130,13 +141,31 @@
                e.printStackTrace();
            }
        }
//        if (conn != null) {
//            try {
//                conn.close();
//            } catch (SQLException e) {
//                e.printStackTrace();
//            }
//        }
        if (conn != null) {
            try {
                conn.close();
                conn = null;
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    /**
     * 深拷贝
     */
    public ArrayList deepClone(List list) throws Exception {
        // 序列化
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(list);
        // 反序列化
        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bis);
        return (ArrayList) ois.readObject();
    }
}