数据库代码:Hibernate编写的通用数据库操作代码来源: 发布时间:星期四, 2009年2月12日 浏览:194次 评论:0
今天来看个例子下面是段Hibernate编写通用数据库操作代码大家研究下对以后开发会有帮助中国自学编程网www.zxbc.cn ! 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.,id); (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; (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 .gif' /> = ArrayList; while(rs.next){ ht = Hashtable; for( i=0;i<RSMD.GETCOLUMNCOUNT;I){ ht.put(rsmd.getColumnName(i+1),rs.getObject(i+1)); } .gif' />.add(ht); } HibernateSessionFactory.clossSession; .gif' />; } 0
相关文章读者评论发表评论 |