hibernate 實現模糊查詢兩種傳參方式,其實各個方法的實質都是一樣的,只不過傳遞參數的方法稍微有點區別
-
public List<User> getUsers(String id){
List list=new ArrayList<User>();
String hql="from User as user where user.id like '%"+id+"%'";
factory=DBHelper.getSessionFactory();
Session session=factory.openSession();
Transaction transaction=session.beginTransaction();
Query query=session.createQuery(hql);
list=query.list();
transaction.commit();
session.close();
return list;
}
-
public List<User> getUsers(String id){
List list=new ArrayList<User>();
String hql="from User as user where user.id like :id";
factory=DBHelper.getSessionFactory();
Session session=factory.openSession();
Transaction transaction=session.beginTransaction();
Query query=session.createQuery(hql);
query.setString("id", "%"+id+"%");
list=query.list();
transaction.commit();
session.close();
return list;
}