我有以下DAO方法:
public String getSomeTable(final String param1) {
String sqlString = "select * from table where name ilike ?";
Query query = this.getEntityManager().createNativeQuery(sqlString);
query.setParameter(1, "%param1%");
}
如果param1
是null
或为空,那么我想从表中选择所有条目。这样做的正确方法是什么?我目前正在使用以下内容:
public String getSomeTable(final String param1) {
String sqlString = "select * from table where name = ?";
Query query = this.getEntityManager().createNativeQuery(sqlString);
if(param1 == null)
query.setParameter(1, "%%");
else
query.setParameter(1, "%param1%");
}
但这不可扩展。我有整数、日期等数据类型。我想知道是否有一种方法可以跳过对该参数的检查null
。
我打算使用 COALESCE(?, CASE WHEN ? = '' THEN '%%' ELSE '%?%') 但我认为?对于特定参数,只能使用一次。我写的下一个 > 链接到第二个参数。