hql 查询返回某个实体对象的泛型方法

查询方法:

/**
 * 执行hQL查询
 * @param <T>
 * @param hql
 * @param cla 返回值实体
 * @return 查询结果实体列表第一个元素
 * @throws Exception
 */

//List<CommonEntity>
public static <T> T executeQuery(String hql,Class<T> cla)throws Exception{
    Session session=null;
    List<T> ret=null;
    try{
        session=getSession();
        Query query = session.createQuery(hql);
        ret =query.list();
    }catch (Exception e){
        throw e;
    }finally {
        closeSession(session);
    }
    return ret.get(0);
}
调用:

	public UserInfo getUserByActPwd1(String sql) throws Exception
	{ 
		UserInfo user=HibernateUtil.executeQuery(sql,UserInfo.class);
		return user;
	}



猜你喜欢

转载自blog.csdn.net/qq_27435059/article/details/78995477