我想搜索以特定字母开头的特定字符串。
因此,例如,如果起始字母是'A'
那么它应该产生一个结果,其中将包含所有带有字母的字符串'A'
。
我该如何做到这一点?
我的查询如下所示:
Query qry = session.createQuery("From RegistrationBean as rb where rb."+searchCriteria+" like %?%");
qry.setString(0,searchField);
将您的查询更改为:
Query qry = session.createQuery("From RegistrationBean as rb where rb."+searchCriteria+" like ?");
qry.setString(0, "%"+searchField+"%");
您可以使用标准来使用 like
session = sessionFactory.openSession();
Criteria query = session.createCriteria(Pojo.class);
query.add(Restrictions.like("column", "a", MatchMode.START));
它将为您提供以 alpha-bate 'a' 开头的字符串列表。
改成
Query qry = session.createQuery("From RegistrationBean as rb where rb ";
if (searchField != null)
{
qry = qry + " where rb.searchCriteria like :searchField ";
}
if ( searchField!= null)
{
query.setString("searchField","%"+searchField+"%");
}
这边走
createQuery("from RegistrationBean as rb where rb.:searchCriteria like '%:searchField%'");
Query qry = session.createQuery("From RegistrationBean as rb where rb."+searchCriteria+" like :sf");
qry.setString("sf",'%'+searchField+'%');
上面的东西对我有用:)