调用HibernateTemplate的find方法时,报空指针
public Employee findByUsernameAndPassword(Employee employee) {
String hql = "form Employee where username = ? and password = ?";
/* List<Employee> list = (List<Employee>) this.getHibernateTemplate().find(hql, employee.getUsername(),employee.getPassword());
*/
List<Employee> list = (List<Employee>) this.getHibernateTemplate().find(hql, new String[]{employee.getUsername(),employee.getPassword()});
if(list.size()>0) {
return list.get(0);
}
return null;
}
如何解决:
改成下面这样就好了:
list = (List<Employee>) this.getHibernateTemplate().find(hql, "employee.getUsername()","employee.getPassword()");
find:
Spring中常用的hql查询方法(getHibernateTemplate())
一、find(String queryString);
示例:this.getHibernateTemplate().find("frombean.User");
返回所有User对象
二、find(String queryString , Object value);
示例:this.getHibernateTemplate().find("frombean.User u where u.name=?","test");
或模糊查询:this.getHibernateTemplate().find("frombean.User u where u.name like ?", "%test%");
返回name属性值为test的对象(模糊查询,返回name属性值包含test的对象)
三、find(String queryString, Object[] values);
示例:String hql= "from bean.Useru where u.name=? and u.password=?"
this.getHibernateTemplate().find(hql, new String[]{"test","123"});
返回用户名为test并且密码为123的所有User对象