zouxuan 2 gadi atpakaļ
vecāks
revīzija
4417af0046

+ 64 - 24
mec-common/common-core/src/main/java/com/ym/mec/common/service/impl/BaseServiceImpl.java

@@ -25,6 +25,7 @@ import java.math.BigDecimal;
 import java.sql.Connection;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
+import java.sql.SQLException;
 import java.util.*;
 
 /**
@@ -131,15 +132,19 @@ public abstract class BaseServiceImpl<PK extends Serializable, T> implements Bas
 		}
 		StringBuffer sql=new StringBuffer();
 		Map<Y,Z> result=new HashMap();
+		SqlSession sqlSession = null;
+		Connection connection = null;
+		PreparedStatement ps = null;
+		ResultSet resultSet = null;
 		try {
-			SqlSession sqlSession = sqlSessionFactory.openSession();
-			Connection connection = sqlSession.getConnection();
+			sqlSession = sqlSessionFactory.openSession();
+			connection = sqlSession.getConnection();
 			sql.append("SELECT ").append(columnKey).append(",").append(columnValue).append(" FROM ").append(tableName).append(" WHERE ").append(columnKey).append(" IN (").append(StringUtils.join(ids, ",")).append(")");
 			if(tenantId != null){
 				sql.append(" AND tenant_id_ = ").append(tenantId);
 			}
-			PreparedStatement ps = connection.prepareStatement(sql.toString());
-			ResultSet resultSet = ps.executeQuery();
+			ps = connection.prepareStatement(sql.toString());
+			resultSet = ps.executeQuery();
 			while (resultSet.next()){
 				Y key;
 				Z value;
@@ -159,17 +164,26 @@ public abstract class BaseServiceImpl<PK extends Serializable, T> implements Bas
 				}
 				result.put(key, value);
 			}
+		} catch (Exception e) {
+			e.printStackTrace();
+		}finally {
 			if(resultSet!=null){
-				resultSet.close();
+				try {
+					resultSet.close();
+				} catch (SQLException e) {
+					e.printStackTrace();
+				}
 			}
 			if(ps!=null){
-				ps.close();
+				try {
+					ps.close();
+				} catch (SQLException e) {
+					e.printStackTrace();
+				}
 			}
 			if(sqlSession!=null){
 				sqlSession.close();
 			}
-		} catch (Exception e) {
-			e.printStackTrace();
 		}
 
 		return result;
@@ -182,12 +196,16 @@ public abstract class BaseServiceImpl<PK extends Serializable, T> implements Bas
 		}
 		StringBuffer sql=new StringBuffer();
 		Map<Y,Z> result=new HashMap();
+		SqlSession sqlSession = null;
+		Connection connection = null;
+		PreparedStatement ps = null;
+		ResultSet resultSet = null;
 		try {
-			SqlSession sqlSession = sqlSessionFactory.openSession();
-			Connection connection = sqlSession.getConnection();
+			sqlSession = sqlSessionFactory.openSession();
+			connection = sqlSession.getConnection();
 			sql.append("SELECT ").append(columnKey).append(",").append(columnValue).append(" FROM ").append(tableName).append(" WHERE ").append(columnKey).append(" IN (").append(ids).append(")");
-			PreparedStatement ps = connection.prepareStatement(sql.toString());
-			ResultSet resultSet = ps.executeQuery();
+			ps = connection.prepareStatement(sql.toString());
+			resultSet = ps.executeQuery();
 			while (resultSet.next()){
 				Y key;
 				Z value;
@@ -207,17 +225,26 @@ public abstract class BaseServiceImpl<PK extends Serializable, T> implements Bas
 				}
 				result.put(key, value);
 			}
+		} catch (Exception e) {
+			e.printStackTrace();
+		}finally {
 			if(resultSet!=null){
-				resultSet.close();
+				try {
+					resultSet.close();
+				} catch (SQLException e) {
+					e.printStackTrace();
+				}
 			}
 			if(ps!=null){
-				ps.close();
+				try {
+					ps.close();
+				} catch (SQLException e) {
+					e.printStackTrace();
+				}
 			}
 			if(sqlSession!=null){
 				sqlSession.close();
 			}
-		} catch (Exception e) {
-			e.printStackTrace();
 		}
 
 		return result;
@@ -228,9 +255,13 @@ public abstract class BaseServiceImpl<PK extends Serializable, T> implements Bas
 	public <K extends List, Y, Z> Map<Y,Z> getMap(String tableName, String columnKey, String columnValue,Boolean hasDelFlag,Integer tenantId, Class<Y> keyType, Class<Z> valueType){
 		StringBuffer sql=new StringBuffer();
 		Map<Y,Z> result=new HashMap();
+		SqlSession sqlSession = null;
+		Connection connection = null;
+		PreparedStatement ps = null;
+		ResultSet resultSet = null;
 		try {
-			SqlSession sqlSession = sqlSessionFactory.openSession();
-			Connection connection = sqlSession.getConnection();
+			sqlSession = sqlSessionFactory.openSession();
+			connection = sqlSession.getConnection();
 			sql.append("SELECT ").append(columnKey).append(",").append(columnValue).append(" FROM ").append(tableName).append(" WHERE 1 = 1");
 			if(hasDelFlag){
 				sql.append(" AND del_flag_ = 0");
@@ -238,8 +269,8 @@ public abstract class BaseServiceImpl<PK extends Serializable, T> implements Bas
 			if(tenantId != null){
 				sql.append(" AND tenant_id_ = ").append(tenantId);
 			}
-			PreparedStatement ps = connection.prepareStatement(sql.toString());
-			ResultSet resultSet = ps.executeQuery();
+			ps = connection.prepareStatement(sql.toString());
+			resultSet = ps.executeQuery();
 			while (resultSet.next()){
 				Y key;
 				Z value;
@@ -259,17 +290,26 @@ public abstract class BaseServiceImpl<PK extends Serializable, T> implements Bas
 				}
 				result.put(key, value);
 			}
+		} catch (Exception e) {
+			e.printStackTrace();
+		}finally {
 			if(resultSet!=null){
-				resultSet.close();
+				try {
+					resultSet.close();
+				} catch (SQLException e) {
+					e.printStackTrace();
+				}
 			}
 			if(ps!=null){
-				ps.close();
+				try {
+					ps.close();
+				} catch (SQLException e) {
+					e.printStackTrace();
+				}
 			}
 			if(sqlSession!=null){
 				sqlSession.close();
 			}
-		} catch (Exception e) {
-			e.printStackTrace();
 		}
 
 		return result;