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 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 ht.put(rsmd.getColumnName(i+1),rs.getObject(i+1));
}
array.add(ht);
}
HibernateSessionFactory.clossSession();
return array;
}
本文提供了一组基于Hibernate框架的增删改查(CRUD)操作示例代码,包括插入、删除、更新及两种查询方法(HQL和SQL)。这些示例展示了如何使用Hibernate进行基本的数据持久化操作。
708

被折叠的 条评论
为什么被折叠?



