Hibernate通用数据库操作代码
程序员文章站
2023-12-04 11:24:28
insert方法 复制代码 代码如下:public void insert(object o){session session = hibernatesessionfact...
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;}
复制代码 代码如下:
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;}
下一篇: android的编译和运行过程深入分析