JdbcTemplate中的exectue和queryForList方法的性能对比

@Autowired
JdbcTemplate jdbcParam;

pstm =
                jdbcParam.getDataSource()
                    .getConnection()
                    .prepareStatement(" SELECT T.ID, T.EPARCHY_CODE FROM  TD_B_SPECIALID_HOME T WHERE T.ID_TYPE = 2 ");
            result = pstm.executeQuery();
            long acctId;
            String eparchyCode;
            while (result.next())
            {
                acctId = result.getLong("ID");
                eparchyCode = result.getString("EPARCHY_CODE");
                specialAcctIdWithEparchyCodeMap.put(acctId, eparchyCode);
            }
        }
        catch (Exception e)
        {
            logger.error("run initSpecialAcctIdWithEparchyCodeMap error.", e);
        }
        finally
        {
            try
            {
                if (!result.isClosed())
                {
                    result.close();
                }
            }
            catch (SQLException e)
            {
                logger.error("Can not close resultset conn.", e);
            }
            finally
            {
                try
                {
                    if (!pstm.isClosed())
                    {
                        pstm.close();
                    }
                }
                catch (SQLException e)
                {
                    logger.error("Can not close preparedstatment conn.", e);
                }
            }
        }

以上代码:需要打开preparedStatement和ResultSet连接,影响性能

而用jdbcParam.queryForList(sql,new Object[]{},Integer.class);

不需要做打开连接关闭连接动作,直接由spring容器管理,性能提升不少。

原文地址:https://www.cnblogs.com/nizuimeiabc1/p/4254147.html