insert方法
復(fù)制代碼 代碼如下:
public void insert(Object o){Session session = HibernateSessionFactory.currentSession();Transaction t = session.beginTransaction();session.save(o);t.commit();HibernateSessionFactory.clossSession();}
delete方法
復(fù)制代碼 代碼如下:
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方法
復(fù)制代碼 代碼如下:
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方法
復(fù)制代碼 代碼如下:
public ArrayList select(String sql){Session session = HibernateSessionFactory.currentSession();Query query = createQuery(sql);List list = query.list();HibernateSessionFactory.clossSession();return (ArrayList)list;}
基于SQL的通用select方法
復(fù)制代碼 代碼如下:
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;irsmd.getColumnCount();i++){ht.put(rsmd.getColumnName(i+1),rs.getObject(i+1));} array.add(ht);}HibernateSessionFactory.clossSession();return array;}
您可能感興趣的文章:- Java Hibernate中使用HQL語句進(jìn)行數(shù)據(jù)庫查詢的要點(diǎn)解析
- 擴(kuò)展Hibernate使用自定義數(shù)據(jù)庫連接池的方法
- Java的Hibernate框架數(shù)據(jù)庫操作中鎖的使用和查詢類型
- Java的Hibernate框架中用于操作數(shù)據(jù)庫的HQL語句講解
- 在Java的Hibernate框架中對(duì)數(shù)據(jù)庫數(shù)據(jù)進(jìn)行查詢操作
- 解決hibernate+mysql寫入數(shù)據(jù)庫亂碼
- Hibernate對(duì)數(shù)據(jù)庫刪除、查找、更新操作實(shí)例代碼