以下是用 hibernate的HQL(面向对象的查询语言)实现模糊查询的3种方式,其中方式一是这三中方式中最理想的方式,至少方式一可以有效的防止由于查询条 件中需要参数的增多导致的代码长度太长出现代码折行的情况(代码太长会给后期的维护和测试带来很大的不便)。但是还有比他更理想的方式,因为方式一在定义 strSQL时使用String ,这就势必会造成当变量过多时strSQL自身太长的问题。一个比较有效的改进办法就是将String 改进为StringBuffer来处理。
方法一:
java 代码
public List listUncertainClasses(Object OId) throws Exception{
Session session=HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
String strSQL="from Classes as a where a.classno like :name";
Query query = session.createQuery(strSQL);
query.setString("name", "%"+OId+"%");
List result=query.list();
for(int i=0;i
Classes classes=(Classes)result.get(i);
String classname=classes.getClassname();
String classno=classes.getClassno();
String specName=classes.getSpeciality().getName();
String departName=classes.getSpeciality().getDepartment().getName();
System.out.println(departName+"\t"+specName+"\t"+classname+"\t"+classno);
}
session.getTransaction().commit();
return result;
}
方法二:
Session session=HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
String strSQL="from Classes as a where a.classno like :name";
Query query = session.createQuery(strSQL);
query.setString("name", "%"+OId+"%");
List result=query.list();
for(int i=0;i
Classes classes=(Classes)result.get(i);
String classname=classes.getClassname();
String classno=classes.getClassno();
String specName=classes.getSpeciality().getName();
String departName=classes.getSpeciality().getDepartment().getName();
System.out.println(departName+"\t"+specName+"\t"+classname+"\t"+classno);
}
session.getTransaction().commit();
return result;
}
方法二:
java 代码
public List listUncertainClasses_01(Object OId) throws Exception{
Session session=HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
List result=session.createQuery("from Classes as a where a.classno like '%"+OId+"%'").list();
for(int i=0;i
Classes classes=(Classes)result.get(i);
String classname=classes.getClassname();
String classno=classes.getClassno();
String specName=classes.getSpeciality().getName();
String departName=classes.getSpeciality().getDepartment().getName();
System.out.println(departName+"\t"+specName+"\t"+classname+"\t"+classno);
}
session.getTransaction().commit();
return result;
}
方法三:
Session session=HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
List result=session.createQuery("from Classes as a where a.classno like '%"+OId+"%'").list();
for(int i=0;i
Classes classes=(Classes)result.get(i);
String classname=classes.getClassname();
String classno=classes.getClassno();
String specName=classes.getSpeciality().getName();
String departName=classes.getSpeciality().getDepartment().getName();
System.out.println(departName+"\t"+specName+"\t"+classname+"\t"+classno);
}
session.getTransaction().commit();
return result;
}
方法三:
java 代码
public List listUncertainClasses_02(Object OId) throws Exception{
Session session=HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
List result=session.createQuery("from Classes as a where a.classno like :name").setParameter("pid",OId).list();
for(int i=0;i
Classes classes=(Classes)result.get(i);
String classname=classes.getClassname();
String classno=classes.getClassno();
String specName=classes.getSpeciality().getName();
String departName=classes.getSpeciality().getDepartment().getName();
System.out.println(departName+"\t"+specName+"\t"+classname+"\t"+classno);
}
session.getTransaction().commit();
return result;
}
Session session=HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
List result=session.createQuery("from Classes as a where a.classno like :name").setParameter("pid",OId).list();
for(int i=0;i
Classes classes=(Classes)result.get(i);
String classname=classes.getClassname();
String classno=classes.getClassno();
String specName=classes.getSpeciality().getName();
String departName=classes.getSpeciality().getDepartment().getName();
System.out.println(departName+"\t"+specName+"\t"+classname+"\t"+classno);
}
session.getTransaction().commit();
return result;
}
文章出处:http://www.diybl.com/course/7_databases/database_other/20071019/78248.html