Spring JDBC 模板查询

Spring JDBC template query

我在将值列表传递给放置在属性文件中的 SQL 查询时遇到问题。无论如何,我可以动态生成占位符吗?根据收到的值。如果是这样,静态查询会起作用吗?请指教。

DAO 代码:

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
)

需要查询format:example

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
)

为什么不尝试使用子选择而不是在 IN 子句中发送参数?

我知道我的回答不能帮助是另一种方法。

正在考虑您的额外评论。

试试这样的东西:

在属性文件中:

myquery=SELECT * from somehting where in (%IN_CLAUSE%);

在代码中你可以获得字符串并且:

String q = originalQuery.replace("%IN_CLAUSE%", str);

然后检查 q 值。