|
|
@ -2,6 +2,8 @@ package com.spring.modules.report.dao.impl; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import com.spring.modules.report.dao.ProcedureDao; |
|
|
import com.spring.modules.report.dao.ProcedureDao; |
|
|
|
|
|
import org.slf4j.Logger; |
|
|
|
|
|
import org.slf4j.LoggerFactory; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.dao.DataAccessException; |
|
|
import org.springframework.dao.DataAccessException; |
|
|
import org.springframework.jdbc.core.CallableStatementCallback; |
|
|
import org.springframework.jdbc.core.CallableStatementCallback; |
|
|
@ -25,74 +27,101 @@ import java.util.*; |
|
|
@Repository |
|
|
@Repository |
|
|
public class ProcedureDaoImpl implements ProcedureDao { |
|
|
public class ProcedureDaoImpl implements ProcedureDao { |
|
|
|
|
|
|
|
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(ProcedureDaoImpl.class); |
|
|
|
|
|
|
|
|
@Autowired |
|
|
@Autowired |
|
|
private JdbcTemplate jdbcTemplate; |
|
|
private JdbcTemplate jdbcTemplate; |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public List<Map<String, Object>> getProcedureData(String procedureName, List<Object> params) { |
|
|
public List<Map<String, Object>> getProcedureData(String procedureName, List<Object> params) { |
|
|
return jdbcTemplate.execute(new CallableStatementCreator() { |
|
|
|
|
|
|
|
|
long startTime = System.currentTimeMillis(); |
|
|
|
|
|
log.info("Stored procedure call started. name={}, params={}", procedureName, params); |
|
|
|
|
|
try { |
|
|
|
|
|
List<Map<String, Object>> resultList = jdbcTemplate.execute(new CallableStatementCreator() { |
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
public CallableStatement createCallableStatement(Connection con) throws SQLException { |
|
|
|
|
|
StringBuilder sql = new StringBuilder("EXEC "+procedureName+" "); |
|
|
|
|
|
//拼好sql语句 |
|
|
|
|
|
for(int i = 0; i < params.size(); i++) { |
|
|
|
|
|
//判断是否是最后一个参数 |
|
|
|
|
|
if(i == params.size() - 1) { |
|
|
|
|
|
sql.append(" ?"); |
|
|
|
|
|
}else { |
|
|
|
|
|
sql.append(" ?,"); |
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
public CallableStatement createCallableStatement(Connection con) throws SQLException { |
|
|
|
|
|
StringBuilder sql = new StringBuilder("EXEC "+procedureName+" "); |
|
|
|
|
|
//拼好sql语句 |
|
|
|
|
|
for(int i = 0; i < params.size(); i++) { |
|
|
|
|
|
//判断是否是最后一个参数 |
|
|
|
|
|
if(i == params.size() - 1) { |
|
|
|
|
|
sql.append(" ?"); |
|
|
|
|
|
}else { |
|
|
|
|
|
sql.append(" ?,"); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
CallableStatement cs = con.prepareCall(sql.toString()); |
|
|
|
|
|
//循环设置参数 |
|
|
|
|
|
for(int i = 1; i <= params.size(); i++) { |
|
|
|
|
|
Object tempParam = params.get(i - 1); |
|
|
|
|
|
if(tempParam instanceof String) { |
|
|
|
|
|
cs.setString(i, String.valueOf(tempParam)); |
|
|
|
|
|
}else if(tempParam instanceof Integer) { |
|
|
|
|
|
cs.setInt(i, Integer.parseInt(String.valueOf(tempParam))); |
|
|
|
|
|
}else if(tempParam instanceof Float) { |
|
|
|
|
|
cs.setFloat(i, Float.parseFloat(String.valueOf(tempParam))); |
|
|
|
|
|
}else if(tempParam instanceof Double) { |
|
|
|
|
|
cs.setDouble(i, Double.parseDouble(String.valueOf(tempParam))); |
|
|
|
|
|
}else if(tempParam instanceof BigDecimal) { |
|
|
|
|
|
cs.setBigDecimal(i, (BigDecimal) tempParam); |
|
|
|
|
|
}else if(tempParam instanceof Date) { |
|
|
|
|
|
cs.setDate(i, (Date) tempParam, Calendar.getInstance()); |
|
|
|
|
|
|
|
|
CallableStatement cs = con.prepareCall(sql.toString()); |
|
|
|
|
|
//循环设置参数 |
|
|
|
|
|
for(int i = 1; i <= params.size(); i++) { |
|
|
|
|
|
Object tempParam = params.get(i - 1); |
|
|
|
|
|
if(tempParam instanceof String) { |
|
|
|
|
|
cs.setString(i, String.valueOf(tempParam)); |
|
|
|
|
|
}else if(tempParam instanceof Integer) { |
|
|
|
|
|
cs.setInt(i, Integer.parseInt(String.valueOf(tempParam))); |
|
|
|
|
|
}else if(tempParam instanceof Float) { |
|
|
|
|
|
cs.setFloat(i, Float.parseFloat(String.valueOf(tempParam))); |
|
|
|
|
|
}else if(tempParam instanceof Double) { |
|
|
|
|
|
cs.setDouble(i, Double.parseDouble(String.valueOf(tempParam))); |
|
|
|
|
|
}else if(tempParam instanceof BigDecimal) { |
|
|
|
|
|
cs.setBigDecimal(i, (BigDecimal) tempParam); |
|
|
|
|
|
}else if(tempParam instanceof Date) { |
|
|
|
|
|
cs.setDate(i, (Date) tempParam, Calendar.getInstance()); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
return cs; |
|
|
} |
|
|
} |
|
|
return cs; |
|
|
|
|
|
} |
|
|
|
|
|
}, new CallableStatementCallback<List<Map<String, Object>> >() { |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
public List<Map<String, Object>> doInCallableStatement(CallableStatement cs) throws SQLException, DataAccessException { |
|
|
|
|
|
List<Map<String, Object>> resultList = new ArrayList<>(); |
|
|
|
|
|
ResultSet rs = cs.executeQuery(); |
|
|
|
|
|
ResultSetMetaData rsmd = rs.getMetaData(); |
|
|
|
|
|
int columns = rsmd.getColumnCount(); |
|
|
|
|
|
//处理列名 |
|
|
|
|
|
Map<String, Object> columnMap = new HashMap<>(); |
|
|
|
|
|
for(int i = 1; i <= columns; i++){ |
|
|
|
|
|
columnMap.put(rsmd.getColumnName(i), null); |
|
|
|
|
|
} |
|
|
|
|
|
//循环处理数据 |
|
|
|
|
|
while(rs.next()){ |
|
|
|
|
|
Map<String, Object> resultMap = new HashMap<String, Object>(); |
|
|
|
|
|
//循环出当前行的所有数据 |
|
|
|
|
|
for(String str : columnMap.keySet()){ |
|
|
|
|
|
resultMap.put(str, rs.getObject(str)); |
|
|
|
|
|
|
|
|
}, new CallableStatementCallback<List<Map<String, Object>> >() { |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
public List<Map<String, Object>> doInCallableStatement(CallableStatement cs) throws SQLException, DataAccessException { |
|
|
|
|
|
List<Map<String, Object>> resultList = new ArrayList<>(); |
|
|
|
|
|
ResultSet rs = cs.executeQuery(); |
|
|
|
|
|
ResultSetMetaData rsmd = rs.getMetaData(); |
|
|
|
|
|
int columns = rsmd.getColumnCount(); |
|
|
|
|
|
//处理列名 |
|
|
|
|
|
Map<String, Object> columnMap = new HashMap<>(); |
|
|
|
|
|
for(int i = 1; i <= columns; i++){ |
|
|
|
|
|
columnMap.put(rsmd.getColumnName(i), null); |
|
|
} |
|
|
} |
|
|
resultList.add(resultMap); |
|
|
|
|
|
|
|
|
//循环处理数据 |
|
|
|
|
|
while(rs.next()){ |
|
|
|
|
|
Map<String, Object> resultMap = new HashMap<String, Object>(); |
|
|
|
|
|
//循环出当前行的所有数据 |
|
|
|
|
|
for(String str : columnMap.keySet()){ |
|
|
|
|
|
resultMap.put(str, rs.getObject(str)); |
|
|
|
|
|
} |
|
|
|
|
|
resultList.add(resultMap); |
|
|
|
|
|
} |
|
|
|
|
|
return resultList; |
|
|
} |
|
|
} |
|
|
return resultList; |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
}); |
|
|
|
|
|
log.info( |
|
|
|
|
|
"Stored procedure call finished. name={}, durationMs={}, resultCount={}, result={}", |
|
|
|
|
|
procedureName, |
|
|
|
|
|
System.currentTimeMillis() - startTime, |
|
|
|
|
|
resultList == null ? 0 : resultList.size(), |
|
|
|
|
|
resultList |
|
|
|
|
|
); |
|
|
|
|
|
return resultList; |
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
log.error( |
|
|
|
|
|
"Stored procedure call failed. name={}, params={}, durationMs={}, error={}", |
|
|
|
|
|
procedureName, |
|
|
|
|
|
params, |
|
|
|
|
|
System.currentTimeMillis() - startTime, |
|
|
|
|
|
e.getMessage(), |
|
|
|
|
|
e |
|
|
|
|
|
); |
|
|
|
|
|
throw e; |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public void execProduceData(String procedureName, List<Object> params) { |
|
|
public void execProduceData(String procedureName, List<Object> params) { |
|
|
|
|
|
long startTime = System.currentTimeMillis(); |
|
|
|
|
|
log.info("Stored procedure execute started. name={}, params={}", procedureName, params); |
|
|
StringBuilder sql = new StringBuilder("EXEC "+procedureName+" "); |
|
|
StringBuilder sql = new StringBuilder("EXEC "+procedureName+" "); |
|
|
//拼好sql语句 |
|
|
//拼好sql语句 |
|
|
for(int i = 0; i < params.size(); i++) { |
|
|
for(int i = 0; i < params.size(); i++) { |
|
|
@ -103,7 +132,25 @@ public class ProcedureDaoImpl implements ProcedureDao { |
|
|
sql.append("'" + params.get(i) + "', "); |
|
|
sql.append("'" + params.get(i) + "', "); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
jdbcTemplate.execute(sql.toString()); |
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
jdbcTemplate.execute(sql.toString()); |
|
|
|
|
|
log.info( |
|
|
|
|
|
"Stored procedure execute finished. name={}, durationMs={}, sql={}", |
|
|
|
|
|
procedureName, |
|
|
|
|
|
System.currentTimeMillis() - startTime, |
|
|
|
|
|
sql |
|
|
|
|
|
); |
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
log.error( |
|
|
|
|
|
"Stored procedure execute failed. name={}, params={}, durationMs={}, error={}", |
|
|
|
|
|
procedureName, |
|
|
|
|
|
params, |
|
|
|
|
|
System.currentTimeMillis() - startTime, |
|
|
|
|
|
e.getMessage(), |
|
|
|
|
|
e |
|
|
|
|
|
); |
|
|
|
|
|
throw e; |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |