最近研究項目查詢底層使用的dao時發現了一個比較好用的封裝類,在查詢數據庫時,可以將查詢出來的字段直接封裝成key-value模式,即,key是字段名,value是字段對應的值,固然是一個map<String,String>表明一行數據/對象,多個對象在一個list中,代碼以下:java
import java.lang.reflect.Method; import java.sql.CallableStatement; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Statement; import java.sql.Timestamp; import java.sql.Types; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.List; import org.apache.log4j.Logger; import org.hibernate.HibernateException; import org.hibernate.Session; import org.springframework.orm.hibernate3.HibernateCallback; import org.springframework.orm.hibernate3.SessionFactoryUtils; import org.springframework.stereotype.Repository; import com.minxinloan.common.utils.DateTimeUtils; import com.minxinloan.common.utils.SystemConstant; import com.minxinloan.dao.JdbcDao; /** * Created with IntelliJ IDEA. * User: Administrator * Date: 13-10-21 * Time: 下午1:24 * To change this template use File | Settings | File Templates. */ public class JdbcDaoImpl extends BaseDaoImpl implements JdbcDao{ private final static Logger log = Logger.getLogger(JdbcDaoImpl.class); /** * get data from the ResultSet which is save in the given colnum and type * * @param rs * @param colNum * @param type * @return * @throws SQLException */ private Object getValue(final ResultSet rs, int colNum, int type) throws SQLException { switch (type) { case Types.ARRAY: case Types.BLOB: // case Types.CLOB: case Types.DISTINCT: case Types.LONGVARBINARY: case Types.VARBINARY: case Types.BINARY: case Types.REF: case Types.STRUCT: return null; default: { Object value = rs.getObject(colNum); if (rs.wasNull() || (value == null)) return null; else return value; } } } /** * change a ResultSet to ArrayList if the data in ResultSet is null then * save it as a default string * * @param resultSet * @param strDefalut * @param transType "MAP"-every record translate to HashMap,"LIST"-every record translate to ArrayList * @return * @throws SQLException */ public ArrayList FromResultToArrayList(ResultSet resultSet,String strDefalut,String transType) { ArrayList dataList = new ArrayList(); if (resultSet == null) return dataList; try { // get the strut of the ResultSet ResultSetMetaData resultmd = resultSet.getMetaData(); if (resultmd == null) return dataList; // get the total count of column int colCount = resultmd.getColumnCount(); // deal with all data one by one while (resultSet.next()) { Object row = null; if ("MAP".equalsIgnoreCase(transType)){ row = new HashMap(); }else{ row = new ArrayList(colCount); } for (int i = 1; i <= colCount; i++) { // get type of column int type = resultmd.getColumnType(i); String colName = resultmd.getColumnName(i); // get data if ("MAP".equalsIgnoreCase(transType)){ ((HashMap)row).put(colName.toUpperCase(), ObjectToString(getValue(resultSet, i, type), strDefalut)); }else{ ((ArrayList)row).add(ObjectToString(getValue(resultSet, i, type), strDefalut)); } } dataList.add(row); } } catch (SQLException e) { log.error(e); } return dataList; } static public Object convertClobColumn(Object value) throws Exception { if (value != null) { if (value.getClass().getName().toLowerCase().contains("clob")) { StringBuilder sb = new StringBuilder(); long clenl = (Long) new Object() { Method getMethod(Object obj) throws Exception { Method method = null; method = obj.getClass().getMethod("length", null); return method; } }.getMethod(value).invoke(value, null); int bufl = 4096; bufl = bufl < clenl ? bufl : (int) clenl; int rcount = 0; for (long l = 1; l <= clenl; l += bufl) { rcount = (int) (clenl - l + 1); rcount = rcount < bufl ? rcount : bufl; sb.append(new Object() { Method getMethod(Object obj) throws Exception { Method method = null; method = obj.getClass().getMethod("getSubString", long.class, int.class); return method; } }.getMethod(value).invoke(value, l, rcount)); } value = sb.toString(); } } return value; } /** * change the object to String type * * @param obj * @param strDefalut * @return */ private String ObjectToString(Object obj, String strDefalut) { if (obj == null) return strDefalut; String strResult = strDefalut; // date type java.sql.Date if (obj instanceof java.sql.Date) { strResult = DateTimeUtils.DateToString((java.sql.Date) obj, SystemConstant.CURR_JAVA_DATE_FORMATTER, strDefalut); // date type java.sql.T } else if (obj instanceof Timestamp) { strResult = DateTimeUtils.TimestampToString( (java.sql.Timestamp) obj, SystemConstant.CURR_JAVA_DATETIME_FORMATTER, strDefalut); // numeral type } else if (obj instanceof java.math.BigDecimal) { strResult = ((java.math.BigDecimal) obj).toString(); }else if(obj.getClass().getName().toLowerCase().contains("clob")){ try{ obj=convertClobColumn(obj); }catch(Exception e){e.printStackTrace();} strResult=obj==null?"":obj.toString(); } else { strResult = obj.toString(); } if (strResult != null) strResult = strResult.trim(); return strResult; } private String ObjectToString(Object obj) { return ObjectToString(obj, ""); } /** * 從數據庫獲取查詢結果 * @param sql * @param encapType * @return * @see queryData(final String sql,final String encapType,final List params) */ public List queryData(final String sql,final String encapType){ return queryData(sql,encapType,null); } /** * 從數據庫獲取查詢結果 * @param sql * @param encapType: 查詢結果封裝方式MAP/LIST * @param params: 參數列表 * @return */ public List queryData(final String sql,final String encapType,final List params){ return (List) getHibernateTemplate().execute(new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException, SQLException { PreparedStatement pst = null; ResultSet rs = null; ArrayList resultList = null; Connection conn = null; try { conn = session.connection(); pst = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); if(params!=null){ for (int i = 0; i<params.size(); i++){ Object obj = params.get(i); if (obj instanceof Integer){ pst.setInt(i+1, ((Integer)obj).intValue()); }else if (obj instanceof Long){ pst.setLong(i+1, ((Long)obj).longValue()); }else if (obj instanceof Double){ pst.setDouble(i+1, ((Double)obj).doubleValue()); }else if (obj instanceof Float){ pst.setFloat(i+1, ((Float)obj).floatValue()); }else if (obj instanceof java.util.Date){ pst.setDate(i+1, (java.sql.Date)obj); }else if (obj instanceof java.sql.Date){ pst.setDate(i+1, (java.sql.Date)obj); }else if (obj instanceof String){ pst.setString(i+1, (String)obj); }else{ pst.setObject(i+1, obj); } } } rs = pst.executeQuery(); resultList = FromResultToArrayList(rs,SystemConstant.DEFAULTNNULLVAL,encapType); } catch (SQLException e) { log.error(e.getMessage(),e); log.error("sql:"+sql+"————————encapType:"+encapType+"————————params:"+params); } finally { closeResultSet(rs); closeStatement(pst); } return resultList; } }); } public List queryData(String sql){ return queryData(sql,"LIST"); } public List queryDataOfMapEncap(String sql){ return queryData(sql,"MAP"); } /** * 得到查詢的結果總數 * @param conn * @param originalSql * @return */ public int getTotalCount(final String sql){ Integer iResult = (Integer) getHibernateTemplate().execute(new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException, SQLException { Connection conn = null; int iRes = 0; ResultSet rs = null; PreparedStatement pst = null; String newSQL = "SELECT COUNT(*) FROM (" + sql + ") a"; try { conn = session.connection(); pst = conn.prepareStatement(newSQL, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); rs = pst.executeQuery(); if (rs.next()) iRes = rs.getInt(1); } catch (SQLException e) { log.error(e); } finally { closeResultSet(rs); closeStatement(pst); } return new Integer(iRes); } }); return iResult.intValue(); } protected void closeResultSet(ResultSet result) { if (result != null) { try { result.close(); } catch (Exception e) { } } result = null; } protected void closeStatement(Statement st) { if (st != null) { try { st.close(); } catch (Exception e) { } } st = null; } protected void closeConnection(Connection conn) { if (conn != null) { try { conn.close(); } catch (Exception e) { } } conn = null; } public int execute(final String sql){ Integer iResult = (Integer) getHibernateTemplate().execute(new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException, SQLException { int returnVal = 0; PreparedStatement statement = null; try{ Connection conn = session.connection(); statement = conn.prepareStatement(sql); returnVal = statement.executeUpdate(); return new Integer(returnVal); }finally{ closeStatement(statement); } } }); return iResult.intValue(); } public int updateAndCommit(final String sql){ Integer iResult = (Integer) getHibernateTemplate().execute(new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException, SQLException { int returnVal = 0; PreparedStatement statement = null; try{ Connection conn = session.connection(); statement = conn.prepareStatement(sql); returnVal = statement.executeUpdate(); conn.commit(); return new Integer(returnVal); }finally{ closeStatement(statement); } } }); return iResult.intValue(); } public String executeProc(final String procName){ String iResult = (String) getHibernateTemplate().execute(new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException, SQLException { String returnVal = ""; CallableStatement statement = null; try{ Connection conn = session.connection(); statement = conn.prepareCall(procName); statement.registerOutParameter(1, Types.VARCHAR); statement.execute(); returnVal = statement.getString(1); return returnVal; }finally{ closeStatement(statement); } } }); return iResult; } /** * * @return * @throws Exception */ public Connection getConnection() throws Exception{ return SessionFactoryUtils.getDataSource(getSessionFactory()).getConnection(); } /** * */ public String getCommaSQL(String sql){ String result=null; List list=queryData(sql); if(list.size()>0){ List tmp=(List)list.get(0); result=tmp.get(0).toString(); } return result; } @Override public void saveOrUpdateAll(List pos) { // TODO Auto-generated method stub getHibernateTemplate().saveOrUpdateAll(pos); } /** * 批量插入的方法 * @param sql 執行的sql * @param argss 參數 * @return * @throws Exception */ @Override public int[] batchExecuteSql(final String sql, final Collection<Object[]> argss) throws Exception { return (int[]) getHibernateTemplate().execute(new HibernateCallback() { public Object doInHibernate(Session session)throws HibernateException, SQLException { int[] rvl = null; PreparedStatement stmt = null; try { Connection conn = session.connection(); stmt = conn.prepareStatement(sql); stmt.clearBatch(); for (Object[] args : argss) { if (args != null && args.length > 0) { for(int i = 0; i < args.length; ++i){ stmt.setObject(i + 1, args[i]); } stmt.addBatch(); } } rvl = stmt.executeBatch(); } catch (SQLException e) { throw e; } finally { if (stmt != null) stmt.close(); } return rvl; } }); }
這裏使用的hibernate+jdbc一塊兒的。spring