Hibernate中配置公用的存储过程方法

public List<Map<String, Object>> executeProc(final String pkgName, final String procName, final List paraList)
throws Exception {
if (pkgName == null || procName == null || paraList == null)
throw new IllegalArgumentException("有空参数,无法执行!");

final StringBuilder query = new StringBuilder();
Object queryResult;

// 拼装存储过程
query.append("{call ");
query.append(pkgName);
query.append(".");
query.append(procName);
query.append("(?,?,?");
for (int i = 0; i < paraList.size(); i++) {
query.append(",?");
}
query.append(")}");

queryResult = (Object) HibernateUtil.getJdbcTemplate().execute(query.toString(), new CallableStatementCallback() {

public Object doInCallableStatement(CallableStatement cs) throws SQLException, DataAccessException {
cs.registerOutParameter(1, Types.INTEGER);
cs.registerOutParameter(2, Types.VARCHAR);
cs.registerOutParameter(3, oracle.jdbc.OracleTypes.CURSOR);

for (int i = 0; i < paraList.size(); i++) {
Object object = paraList.get(i);
if (object == null)
cs.setObject(3 + i + 1, null);
else if (object instanceof Integer)
cs.setInt(3 + i + 1, ((Integer) object).intValue());
else if (object instanceof String)
cs.setString(3 + i + 1, object.toString());
else if (object instanceof Date)
cs.setDate(3 + i + 1, new java.sql.Date(((Date) object).getTime()));
else
cs.setObject(3 + i + 1, object);
}
cs.execute();

int retcode = cs.getInt(1);
if (retcode != 0) {
return cs.getString(2);
}

ResultSet rs = (ResultSet) cs.getObject(3);
ResultSetMetaData metaHeader = rs.getMetaData();
int columnCount = metaHeader.getColumnCount();

List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
while (rs.next()) {
HashMap<String, Object> entry = new HashMap<String, Object>();
for (int i = 1; i <= columnCount; i++) {
Object rsc = rs.getObject(i);
if (rsc instanceof TIMESTAMP) {
entry.put(metaHeader.getColumnName(i), new Date(rs.getTimestamp(i).getTime()));
} else {
entry.put(metaHeader.getColumnName(i), rsc);
}
}
list.add(entry);
}

return list;
}
});

if (queryResult instanceof String) {
// 抛出异常信息
throw BankServiceExceptionConverter.convert("存储过程" + query.toString() + "执行发生错误,原因:" + (String) queryResult);
} else if (queryResult instanceof List) {
return (List<Map<String, Object>>) queryResult;
} else {
throw BankServiceExceptionConverter.convert("存储过程" + query.toString() + "执行发生异常,结果集无法获取");
}
}

详细;存储过程中包含3个参数,包名,存储过程名,参数集合。默认返回3个参数,一个number; 一个varchar,一个cursor.

在第三个参数中,可以自定义需要输入的参数个数,存储过程会自动添加输入参数:

create or replace package body XXX is


   procedure GET_XXXX_QUERY(RETCODE   OUT NUMBER,
                          RETSTR    OUT NVARCHAR2,
                          P_CUR     OUT REF_CURSOR,
                          P_Par in varchar2) as
  begin
    RETCODE := 0;
    RETSTR  := '';
   
    open p_cur for
        select b.字段 as aa from table b where b.字段2=P_Par ;
  end;

end XXX;

调用例子:

List<String> paraList = new ArrayList<String>();
  paraList.add(1);
  paraList.add(2);
 

  List<Map<String, Object>> results = null;

       results = commonDao.executeProc("XXX", "GET_XXXX_QUERY", paraList);

   if (results != null && results.size() > 0) {
    Map<String, Object> result = results.get(0);

    aa= Integer.parseInt(result.get("aa") + "");

   }

可以根据实际情况自行调整存储过程

相关推荐