hibernate 实现模糊查询两种传参方式,其实各个方法的实质都是一样的,只不过传递参数的方法稍微有点区别 public ListUser getUsers(String id){ List list=new ArrayListUser(); String hql="from User as user
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; }