我正在尝试从Spring JDBCTemplate执行一个非常简单的查询。我正在从主键标识的记录中检索一个属性。完整的代码如下所示。当我使用串联构造的查询(危险和丑陋,当前未注释)执行此操作时,它将在0.1秒内执行。当我更改我的注释并使用参数化查询时,它会在50秒内执行。我更倾向于获得参数化查询附带的保护,但50秒似乎需要支付高昂的代价。任何提示如何使这更合理。
public class JdbcEventDaoImpl {
private static JdbcTemplate jtemp;
private static PreparedStatement getJsonStatement;
private static final Logger logger = LoggerFactory.getLogger(JdbcEventDaoImpl.class);
@Autowired
public void setDataSource(DataSource dataSource) {
JdbcEventDaoImpl.jtemp = new JdbcTemplate(dataSource);
}
public String getJdbcForPosting(String aggregationId){
try {
return (String) JdbcEventDaoImpl.jtemp.queryForObject("select PostingJson from PostingCollection where AggregationId = '" + aggregationId + "'", String.class);
//return (String) JdbcEventDaoImpl.jtemp.queryForObject("select PostingJson from PostingCollection where AggregationId = ?", aggregationId, String.class);
} catch (EmptyResultDataAccessException e){
return "Not Available";
}
}
}