1 public List<Student> querySomeStudent(String serachText){ 2 Transaction tx = null; 3 String hql=""; 4 try{ 5 Session session = MyHibernateSessionFactory.getSessionFactory().getCurrentSession(); 6 tx = session.beginTransaction(); 7 hql="from Student where"; 8 StringBuffer hqlsb = new StringBuffer(); 9 hqlsb.append("studentName like ? or "); 10 hqlsb.append("studentNum like ? or "); 11 hqlsb.append("cellNum like ? or "); 12 hqlsb.append("className like ?"); 13 hql+=hqlsb; 14 Query query = session.createQuery(hql).setString(1, "'%serachText%'").setString(2, "'%serachText%'") 15 .setString(3, "'%serachText%'").setString(4, "'%serachText%'");//生成一个Query对象 16 List list= query.list(); 17 tx.commit(); 18 return list; 19 } 20 catch(Exception ex) 21 { 22 ex.printStackTrace(); 23 tx.commit(); 24 return null; 25 } 26 finally 27 { 28 if(tx!=null) 29 { 30 tx=null; 31 } 32 } 33 }
1 public List<Student> querySomeStudent(String serachText){ 2 2 Transaction tx = null; 3 3 String hql=""; 4 4 try{ 5 5 Session session = MyHibernateSessionFactory.getSessionFactory().getCurrentSession(); 6 6 tx = session.beginTransaction(); 7 7 hql="from Student where "; 8 8 StringBuffer hqlsb = new StringBuffer(); 9 9 hqlsb.append("studentName like ? or "); 10 10 hqlsb.append("studentNum like ? or "); 11 11 hqlsb.append("cellNum like ? or "); 12 12 hqlsb.append("className like ?"); 13 13 hql+=hqlsb; 14 14 Query query = session.createQuery(hql).setString(1, "'%"+serachText+"%'").setString(2,"'%"+serachText+"%'") .setString(3, "'%"+serachText+"%'").setString(4, "'%"+serachText+"%'");//生成一个Query对象 15 16 List list= query.list(); 16 17 tx.commit(); 17 18 return list; 18 19 } 19 20 catch(Exception ex) 20 21 { 21 22 ex.printStackTrace(); 22 23 tx.commit(); 23 24 return null; 24 25 } 25 26 finally 26 27 { 27 28 if(tx!=null) 28 29 { 29 30 tx=null; 30 31 } 31 32 } 32 33 }
这样还是不行
已解决
where后面加个空格
爱是不行
还是-_-!
from Student where studentName like '%'8'%' 在后台成这样了
@木子火日立: "'%'+"+serachText+"+'%'"
@长蘑菇星人: 报这个错 rg.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: like near line 1, column 56 [from entity.Student where studentName like studentName like ? or studentNum like ? or cellNum like ? or className like ?]
@木子火日立: 你写错了,仔细看一下where后面
@长蘑菇星人: 大神,说清楚点啊
@木子火日立:where studentName like studentName like
@长蘑菇星人: 改了那个,报这个错
六月 06, 2016 2:46:45 下午 org.hibernate.hql.internal.ast.HqlSqlWalker generatePositionalParameter
WARN: [DEPRECATION] Encountered positional parameter near line 1, column 44. Positional parameter are considered deprecated; use named parameters or JPA-style positional parameters instead.
@木子火日立: 你把 ? 改成 '%'+ ? + '%' 这样子呢。参数上就不加了。(逃
@长蘑菇星人: 还是不行,咋办啊?
@木子火日立: 去查文档 hibernate like(逃
@木子火日立: 将'%'8'%' 两个%号内的'去掉试下
@木子三金: 数据库里面去掉是可以的,但在java后台它就自动就拼接成' %'8'%' 了
直接"%"+serachText+"%"
试过了,不行
@木子火日立: %去掉试过吗
@laugher_ccc: %去掉就达不到模糊查询的效果了
@木子火日立: 那去看下hibernate预编译时候替换参数时候的源码怎么替换的,应该是某个符号的问题吧
@laugher_ccc: 估计也是那个问题,正在摆渡-_-!
@木子火日立: PreparedStatement的setObject
@laugher_ccc:
即使不设参数这样:
hql="from Student where studentName like '%"+serachText+"%'";
执行时候,就成这样了:
from Student where studentName like '%'3'%'