| « | November 2025 | » | | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | | | | | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | | | | | | | |
| 公告 |
好好学习 天天向上 |
| Blog信息 |
|
blog名称:小鱼的空间 日志总数:18 评论数量:20 留言数量:0 访问次数:84648 建立时间:2006年7月6日 |

| |
|
[”学习“]hibernate的crud 软件技术
taotao6086 发表于 2007/8/14 10:28:00 |
| 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;} |
|
|