熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

Hibernate編寫通用數據庫操作代碼

2013-11-23 20:08:45  來源: Java開源技術 


insert方法
public void insert(Object o){
    Session session = HibernateSessionFactorycurrentSession();
    Transaction t = sessionbeginTransaction();
    sessionsave(o);
    mit();
    HibernateSessionFactoryclossSession();
}
 
delete方法
public void delete(Object oSerializable id){
    Session session = HibernateSessionFactorycurrentSession():
    Transaction t = sessionbeginTransaction();
    Object o = sessionget(oclassid);
    if(o!=null){
        sessiondelete(o);
    }
    mit();
    HibernateSessionFactoryclossSession();
}
 
update方法
public void update(Object oSerializable id){
    Session session = HibernateSessionFactorycurrentSession();
    Transaction t = sessionbeginTransaction();
    sessionupdate(oid);
    mit();
    HibernateSessionFactoryclossSession();
}
 
基於HQL的通用select方法
public ArrayList select(String sql){
    Session session = HibernateSessionFactorycurrentSession();
    Query query = createQuery(sql);
    List list = querylist();
    HibernateSessionFactoryclossSession();
    return (ArrayList)list;
}
 
基於SQL的通用select方法
public ArrayList select(String sql) throws Exception{
    Session session = HibernateSessionFactorycurrentSession();
    Connection con = nnection();
    PreparedStatement pstmt = conpreparedStatement(sql);
    ResultSet rs = pstmtexecuteQuery();
    ResultSetMetaData rsmd = rsgetMetaData();
    Hashtable ht = null;
    ArrayList array = new ArrayList();
    while(rsnext()){
        ht = new Hashtable();
        for(int i=;i<rsmdgetColumnCount();i++){
            htput(rsmdgetColumnName(i+)rsgetObject(i+));
        } 
        arrayadd(ht);
    }
    HibernateSessionFactoryclossSession();
    return array;
}
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28011.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.