打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Hibernate编写通用数据库操作代码
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;
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Hibernate 数据的批量插入、更新和删除
HQL和SQL的区别
Hibernate HQL查询---投影查询的三种方式
Hibernate学习笔记
Hibernate HQL查询
Hibernate中对增删改查的小结
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服