📄 hibernatedaoimpl.java
字号:
package com.util;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class HibernateDaoImpl extends HibernateDaoSupport implements HibernateDao {
public List query(String hql) throws Exception {
return this.getHibernateTemplate().find(hql);
}
public void save(Object po) throws Exception {
this.getHibernateTemplate().save(po);
}
public Object get(Class clas, Serializable id) throws Exception {
return this.getHibernateTemplate().get(clas, id);
}
public void update(Object po) throws Exception {
this.getHibernateTemplate().update(po);
}
public void delete(Object po) throws Exception {
this.getHibernateTemplate().delete(po);
}
public void executeSql(String sql) throws Exception {
Session session = null;
session = this.getSession();
Transaction t=session.beginTransaction();
Connection con = session.connection();
con.createStatement().execute(sql);
con.close();
t.commit();
}
public List getTopN(String hql, int n) throws Exception {
Session session = null;
session = this.getSession();
Query query = session.createQuery(hql);
query.setMaxResults(n);
List list = query.list();
this.closeSession();
return list;
}
public List getListByExecuteSql(String sql, String columnName)
throws Exception {
Session session = null;
session = this.getSession();
Connection con = session.connection();
Statement stmt = con.createStatement(ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_READ_ONLY);
ResultSet rs = stmt.executeQuery(sql);
List<String> list = new ArrayList<String>();
while (rs.next()) {
list.add(rs.getString(columnName));
}
this.closeSession();
return list;
}
public List<Object[]> getListByExecuteSql(String sql, int fetchNubmer)
throws Exception {
Session session = null;
session = this.getSession();
Connection con = session.connection();
Statement stmt = con.createStatement(ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_READ_ONLY);
ResultSet rs = stmt.executeQuery(sql);
List<Object[]> list = new ArrayList<Object[]>();
Object[] objs = null;
while (rs.next()) {
objs = new Object[fetchNubmer];
for (int i = 0; i < fetchNubmer; i++) {
objs[i] = rs.getObject(i + 1);
}
list.add(objs);
}
this.closeSession();
return list;
}
public void closeSession() throws Exception {
this.getSession().close();
}
public Session openSession() throws Exception {
return this.getSession();
}
/* (non-Javadoc)
* @see cn.pisoft.dataplant.common.hibernate.HibernateDao#getCountByExecuteSql(java.lang.String)
*/
public int getCountByExecuteSql(String sql) throws Exception {
Session session = null;
session = this.getSession();
Connection con= session.connection();
Statement stmt = con.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
ResultSet rs = stmt.executeQuery(sql);
int returnNumber =0;
while (rs.next()){
returnNumber = rs.getInt(1);
break;
}
con.commit();
this.closeSession();
return returnNumber;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -