1、使用?通配符
public User validate(String userName, String password) { String hql = "from User u where u.userName = ? and u.password = ?"; User user = null; List<User> list = ht.find(hql, new Object[]{userName, password}); if (list.size()!=0){ user = list.get(0); } return user; }
2、使用键值对应
public User validate(String userName, String password) { String hql = "from User where userName =:userName and password =:password"; User user = null; Object[] values = {userName, password}; String[] paramNames = {"userName", "password"}; List<User> list = ht.findByNamedParam(hql, paramNames, values); if (list.size()!=0){ user = list.get(0); } return user; }
Done