1 //action中的代码 2 public String execute() throws Exception { 3 EmployeeDaoImpl dao=new EmployeeDaoImpl(); 4 String str=dao.loginUser(username, password,status).toString(); 5 ServletActionContext.getResponse().setContentType("text/html;charset=utf-8"); 6 System.out.println(str); 7 ServletActionContext.getResponse().getWriter().println(str); 8 return null; 9 } 10 11 //EmployeeDaoImpl 类中的代码如下(测试无问题)
public List<TEmployee> loginUser(String username, String password, Integer role_id) { String hql="select a from TEmployee a inner join a.TRole b" + " where a.employeeName=? and a.password=? and b.roleId=?"; List<TEmployee> list=Template.select(hql, new Object[]{username,password,role_id}); return list; }
//Template类如下 @SuppressWarnings("unchecked") public static <T>List<T> select(String hql,Object[]params){ //debug时到此程序终止,JUnit测试成功 Session session=HibernateSessionFactory.getSession(); Query query=session.createQuery(hql); for (int i = 0; i < params.length; i++) { query.setParameter(i, params[i]); } List<T> list=query.list(); HibernateSessionFactory.closeSession(); return list; }