无为清净楼资源网 Design By www.qnjia.com
insert方法
复制代码 代码如下:
public void insert(Object o){Session session = HibernateSessionFactory.currentSession();Transaction t = session.beginTransaction();session.save(o);t.commit();HibernateSessionFactory.clossSession();}

delete方法
复制代码 代码如下:
public void delete(Object o,Serializable id){Session session = HibernateSessionFactory.currentSession():Transaction t = session.beginTransaction();Object o = session.get(o.class,id);if(o!=null){session.delete(o);}t.commit();HibernateSessionFactory.clossSession();}


update方法
复制代码 代码如下:
public void update(Object o,Serializable id){Session session = HibernateSessionFactory.currentSession();Transaction t = session.beginTransaction();session.update(o,id);t.commit();HibernateSessionFactory.clossSession();}

基于HQL的通用select方法
复制代码 代码如下:
public ArrayList select(String sql){Session session = HibernateSessionFactory.currentSession();Query query = createQuery(sql);List list = query.list();HibernateSessionFactory.clossSession();return (ArrayList)list;}

基于SQL的通用select方法
复制代码 代码如下:
public ArrayList select(String sql) throws Exception{Session session = HibernateSessionFactory.currentSession();Connection con = session.connection();PreparedStatement pstmt = con.preparedStatement(sql);ResultSet rs = pstmt.executeQuery();ResultSetMetaData rsmd = rs.getMetaData();Hashtable ht = null;ArrayList array = new ArrayList();while(rs.next()){ht = new Hashtable();for(int i=0;i<rsmd.getColumnCount();i++){ht.put(rsmd.getColumnName(i+1),rs.getObject(i+1));} array.add(ht);}HibernateSessionFactory.clossSession();return array;}

标签:
Hibernate,数据库

无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com