Hibernate如何编写通用数据库操作代码?
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 ht。put(rsmd。getColumnName(i+1),rs。getObject(i+1)); } array。add(ht); } HibernateSessionFactory。
clossSession(); return array; }。
答:执行HQL查询,例如: String hql = "FROM User WHERE username = ? AND password = ?"; Listli...详情>>
问:同事ip:10.101.107.28 我的IP:10.101.104.23我们在...
答:你们公司是WINDOWS域环境吗,如果是的话,你在网上邻居里应该能看到对方的主机。让她把文件夹共享出来就可以。 如果不是WINDOWS域,那得看你们是不是在一个...详情>>
问:数据结构顺序表的建立,可定义顺序表 #define maxnum elemtyp...
答:打开VC ,然后点击新建,出来界面之后选择Win32 Console Application并输入工程的名称和选择项目的位置,然后出现“一个空工程”,在点击“...详情>>