hibernateDao

package com.csair.gme.conf.dao.impl;


import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Session;
import org.hibernate.connection.ConnectionProvider;
import org.hibernate.connection.ConnectionProviderFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import com.csair.gme.conf.dao.BaseDao;
import com.csair.gme.infrastructure.common.dialect.OracleDialect;

@Repository
public  class BaseDaoImpl extends HibernateDaoSupport implements BaseDao{

/**
* 查询数据
*/
@Override
public List<Map<String,Object>> executeMapQuery(String sql) throws Exception{
// TODO Auto-generated method stub
List<Map<String,Object>> list = new ArrayList();
//ConnectionProvider cp  = ConnectionProviderFactory.newConnectionProvider();
Connection conn = null;
Session curSeesion=null;
Statement st = null;
try {
//conn = cp.getConnection();   //获取数据库连接

curSeesion = getHibernateTemplate().getSessionFactory().getCurrentSession();
  conn=curSeesion.connection();
st = conn.createStatement();
ResultSet  rs = st.executeQuery(sql);
list =convertList(rs);

} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw e;
}

finally{
st.close();
conn.close();
//curSeesion.close();
//cp.closeConnection(conn);
}

return list;
}

/**
* 分页查询
*/
@Override
public List<Map<String,Object>> executePageMapQuery(String sql, int start, int end)throws Exception {
// TODO Auto-generated method stub
OracleDialect dialect = new OracleDialect();
List<Map<String,Object>> list = new ArrayList();
try {
String newSql = dialect.getLimitString(sql, start, end); //处理分页SQL
list =executeMapQuery(newSql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw e;

}
return list;
}

/**
* ResultSet 转List
* @param rs
* @return
* @throws SQLException
*/
private static List<Map<String,Object>> convertList(ResultSet rs) throws SQLException {

List list = new ArrayList();

ResultSetMetaData md = rs.getMetaData();

int columnCount = md.getColumnCount(); //Map rowData;

while (rs.next()) { //rowData = new HashMap(columnCount);

Map<String, Object> rowData = new HashMap();

for (int i = 1; i <= columnCount; i++) {

rowData.put(md.getColumnName(i), rs.getObject(i));

}

list.add(rowData);

} return list;

}


@Override
public long getRecordCount(String sql) throws Exception{
// TODO Auto-generated method stub
long count =0;
try {
List list = executeMapQuery( sql);
count = list.size();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw e;
}

return count;
}

/**
* 获取连接
* @return
* @throws Exception
*/
public  Connection getConnection() throws Exception
{
/**
Connection con=null;
Session curSeesion=null;
try  {
   curSeesion = getHibernateTemplate().getSessionFactory().getCurrentSession();
   con=curSeesion.connection();
}
catch(Exception e)
{
curSeesion.close();
try {
con.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

}

return con;

**/


ConnectionProvider cp  = ConnectionProviderFactory.newConnectionProvider();
        Connection  conn    = null;
        try {
            conn = cp.getConnection();
        } catch (SQLException e) {
       
            e.printStackTrace();
            throw e;
        }
       
        return conn;
}



//public abstract Dialect getDialect();
}

猜你喜欢

转载自yaoyaoyue.iteye.com/blog/2306243