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 }
24 回答
慕少森
TA贡献2019条经验 获得超9个赞
@laugher_ccc:
即使不设参数这样:
hql="from Student where studentName like '%"+serachText+"%'";
执行时候,就成这样了:
from Student where studentName like '%'3'%'
添加回答
举报
0/150
提交
取消