日期:2014-05-18  浏览次数:20699 次

求hibernate查询数据和显示数据代码
求hibernate查询数据和显示数据代码

要代码,怎么实现的?

谢谢

------解决方案--------------------
网上和书上多得是!!!

------解决方案--------------------
//工具类
public class HibernateUtil
{
private static SessionFactory factory = null;

static
{
try
{
Configuration config = new Configuration();
factory = config.configure().buildSessionFactory();
} catch (Throwable e)
{
System.err.println( "Initial SessionFactory creation failed. " + e);
throw new ExceptionInInitializerError(e);
}
}

public static Session getSession()
{
return factory.openSession();
}

public static Session getCurrentSession()
{
return factory.getCurrentSession();
}
}

//查询
public Collection find()
{
Session session = null;
Transaction trans = null;
Collection users = null;

try
{
session = HibernateUtil.getSession();
trans = session.beginTransaction();
Query query = session.createQuery( "select u from User u ");
users = query.list();
trans.commit();
}catch(Exception e)
{
trans.rollback();
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}finally
{
session.close();
}

return users;
}

//显示的遍历返回的集合就可以了
------解决方案--------------------
用el表达式也可以
------解决方案--------------------
request.setAttribute( "name ",结果);
在JSP里,ITERATOR
------解决方案--------------------
看hibernate的帮助和例子
------解决方案--------------------
request.setAttribute( "list ",list结果);
取到这个list for循环就是了或用while循环ITERATOR也行
------解决方案--------------------
//EL表达式
<%@taglib prefix= "c " uri= "http://java.sun.com/jsp/jstl/core "%>
<%@page import= "com.tarena.qisj.biz.Cart "%>

<c:forEach items= "${cart.items} " var= "item ">
<TR align=middle>
<TD height=30> <INPUT type=checkbox value= "${item.value.product.id} " name= "id "> </TD>
<TD height=30> ${item.value.product.name} </TD>
<TD height=30> ${item.value.product.price} </TD>
<TD height=30> <INPUT maxLength=10 size=10 value=${item.value.amount} name=${item.value.product.id}> </TD>
<TD height=30> ${item.value.product.price * item.value.amount} </TD>
</TR>
</c:forEach>