用like实现根据部分字段搜索数据
%替代一个或多个字符_???? (下划线)仅替代一个字符[charlist]字符列中的任何单一字符
[^charlist]? 或者?? [!charlist]
不在字符列中的任何单一字符?
??
LIKE和通配符结合使用即可模糊查询,也就是常见的根据字段搜索数据
SQL原始语句可以是:
select title from t_question where title like '%关键字%'
?查找表t_question中,title的任意位置包含”关键字“3个字的title,其中”关键字“可以任意调换
?
通过HQL使用则如下:
public List<Question> searchByKeyword(String keyword) throws Exception {List<Question> list = null;String hql = "from Question as q where q.title like ?";Query query = sessionFactory.getCurrentSession().createQuery(hql);query.setString(0, '%'+keyword+'%');list = query.list();return list;}
?
唯一需要注意的是
query.setString(0, '%'+keyword+'%');
?需要自行组装通配符才能正确查询。