传递查询参数的值列表

时间:2016-04-22 01:24:45

标签: java mysql sql hql

String hql = "select * from myTable where isActive IN (:isActive)";
        Query query = session.createQuery(hql);
        query.setString("school","");
        query.setString("isActive", "Y");//working
        query.setString("isActive", "N");//working
        query.setString("isActive", "Y","N"); // not working
        query.setString("isActive", "'Y','N'"); // not working
        return query.list();

我不知道下面的代码是否可行,我想知道我是否可以将值列表传递给我的搜索字符串参数,因此我无需创建查询;一个用于选择所有数据,无论状态如何,另一个用于仅选择活动数据。

1 个答案:

答案 0 :(得分:2)

使用Query.setParameterList()传递List作为参数:

String hql = "select * from myTable where isActive IN (:isActive)";
Query query = session.createQuery(hql);
List<String> isActiveList = new ArrayList<>();
isActiveList.add("Y");
isActiveList.add("N");
query.setParameterList("isActive", isActiveList);
return query.list();