Java Spring JDBC模板查询
在将值列表传递给放置在属性文件中的SQL查询时,我遇到了一个问题。我是否可以动态生成占位符?根据收到的数值。如果是这样,静态查询会起作用吗?请告知 DAO代码:Java Spring JDBC模板查询,java,db2,spring-jdbc,Java,Db2,Spring Jdbc,在将值列表传递给放置在属性文件中的SQL查询时,我遇到了一个问题。我是否可以动态生成占位符?根据收到的数值。如果是这样,静态查询会起作用吗?请告知 DAO代码: public List<DestinationDTO> fetchEmsStatistics(DestinationDTO destinationDTO) //throws Exception { LOG.info("start of -- MonitorDAOImpl.fetchEmsStatis
public List<DestinationDTO> fetchEmsStatistics(DestinationDTO destinationDTO)
//throws Exception
{
LOG.info("start of -- MonitorDAOImpl.fetchEmsStatistics()");
List<DestinationDTO> destinationDTOList = new ArrayList<DestinationDTO>();
String str = destinationDTO.getDestinationNames();
String dest[] = str.split(",");
/* "example 1" , "example2" are hardcoded,however i want the values of dest array and according to count the placeholders should be placed in the query */
try{
destinationDTOList = this.jdbcTemplate.query(
this.fetchEmsStatisticsQuery,
new Object[] {
"%" + destinationDTO.getDestinationTypeId() + "%",
destinationDTO.getDestinationSourceId(),
"example1","example2",
destinationDTO.getStartTime() + ":00",
destinationDTO.getEndTime() + ":00" },new DestinationDTORowMapper());
System.out.println("finally"+ destinationDTOList.size());
}
catch(Exception e)
{
e.printStackTrace();
}
LOG.info("end of -- MonitorDAOImpl.fetchEmsStatistics()");
return destinationDTOList;
}
查询:
Select
DESTINATION
,MIN_IC
, MAX_IC
,MAX_OC
,MAX_IC-MIN_IC as PROCESSEDMSGS
from (
select
DESTINATION
,min(IN_MSG_COUNT) MIN_IC
,max(IN_MSG_COUNT) MAX_IC
, max(OUT_MSG_COUNT) MAX_OC
from
EMS_MONITOR C
where
C.DESTINATION_TYPE_ID like ? and
C.SOURCE_ID=? and
C.DESTINATION IN (?,?) and
C.RCVD_DATE >=? and C.RCVD_DATE <=?
group by
DESTINATION
)
所需的查询格式:示例
Select
DESTINATION
,MIN_IC
, MAX_IC
,MAX_OC
,MAX_IC-MIN_IC as PROCESSEDMSGS
from (
select
DESTINATION
,min(IN_MSG_COUNT) MIN_IC
,max(IN_MSG_COUNT) MAX_IC
, max(OUT_MSG_COUNT) MAX_OC
from
EMS_MONITOR C
where
C.DESTINATION_TYPE_ID like '%1%' and
C.SOURCE_ID=1 and
C.DESTINATION in ('M.COM.CAT.AVAIL.STORE.Q.FCC','M.COM.CAT.ELIG.BOPS.STORE.Q.FCC') and
C.RCVD_DATE >='2014-09-16 17:00:00' and
C.RCVD_DATE <='2014-09-16 20:01:00'
group by
DESTINATION
)
为什么不尝试在子句中使用subselect而不是send参数
我知道我的答案是另一种方法,这对我没有帮助。考虑到你的额外评论 尝试以下方法: 在属性文件中:
myquery=SELECT * from somehting where in (%IN_CLAUSE%);
在代码中,您可以获取字符串和:
String q = originalQuery.replace("%IN_CLAUSE%", str);
然后检查q值。我有一个值列表,所以我使用IN运算符。我只想知道如何使用IN操作符。非常感谢。一个参数标记-一个值。@mustaccio我现在更改了代码,它可以很好地处理硬编码的值,但是,我希望放置动态值,而不是硬编码的值。如何通过在DAO或查询中更改代码来改进代码?我希望查询只放在属性文件中。请提供您有价值的建议。如果您的IN列表具有合理的最大值数,您可以使用该数量的参数标记保存语句文本:…目的地在?、?、?、?、?、?,?,?,?。。。,?。。。并在没有足够的值时为参数指定空值。或者,使用动态SQL,如下面一个答案中所建议的。当然。。我会试试看,结果会出来的。。非常感谢。谢谢..动态sql非常有效:谢谢你的建议。。如果这有效,我会尝试并让你知道!这是我的荣幸!!: