如何在jdbcTemplate中传递参数以用于具有相同值的多个参数标记?

huangapple go评论83阅读模式
英文:

How to pass parameters in jdbcTemplate for multiple parameter markers with same value?

问题

我在查询中有4个相同的参数标记,我正在通过“?”传递给getMyDateField()函数以获取数据,请问我需要4次传递相同的参数,如下所示?正确的方法是什么?请提供建议。

  final String[] args = new Date[]{
        getMyDateField(),
        getMyDateField(),
        getMyDateField(),
        getMyDateField()
};
int[] types = new int[]{Types.CHAR, Types.CHAR, Types.CHAR, Types.CHAR};
result = jdbcTemplate.query(query, args, types, new Mapper());
英文:

I am having 4 same parameter markers in my query where I am passing "?" to fetch the data returned by
getMyDateField() function, Do I need to pass same parameter 4 times as below ? What is the correct way. Please suggest

  final String[] args = new Date[]{
        getMyDateField(),
        getMyDateField(),
        getMyDateField(),
        getMyDateField()
};
int[] types = new int[]{Types.CHAR, Types.CHAR, Types.CHAR, Types.CHAR};
result = jdbcTemplate.query(query, args, types, new Mapper());

答案1

得分: 1

如果您需要让所有4个参数具有相同的日期值,并且 getMyDateField() 函数正在进行某些处理,您可以将 getMyDateField() 的结果存储在一个变量中,并将其传递4次到日期数组中,从而使您的代码更加高效。

如果可以修改 SQL 查询,即您不需要对4个参数设置4个约束,因为它们都共享相同的日期值,那么您可以将查询更改为 where date1 = date2 and date2 = date3 and date3 = date4 and date4 = ?dateparam,然后只将该参数传递一次。

英文:

If you need all 4 parameters to have the same date value and getMyDateField() is doing some processing, you can always store the result of getMyDateField() in a variable and pass it 4 times to the date array, making your code a bit more efficient.

If the sql query can be modified, i.e. you don't need 4 constraints on 4 parameters as they all share the same date value, then you could change the query to a where date1 = date2 and date2 = date3 and date3 = date4 and date4 = ?dateparam and pass that parameter only once.

huangapple
  • 本文由 发表于 2020年10月13日 00:26:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/64321670.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定