@SuppressWarnings({"rawtypes"}) public static List getResult(String keyName, String sqlAppend) throws Exception { String prefix = "jdbc.get"; String vsql = getProperty(prefix + "." + keyName + ".sql"); String vclass = getProperty(prefix + "." + keyName + ".class"); if (vsql == null || vclass == null) throw new IllegalArgumentException("请检查“" + prefix + "." + keyName + "”的相关配置!"); if (sqlAppend != null && !sqlAppend.trim().equals("")) vsql += sqlAppend; logger.info("sql:" + vsql); Connection con = null; Statement st = null; ResultSet rs = null; try { logger.info(" --------- getConnection: -----------"); con = getConnection(); logger.info(" --------- con: -----------" + con); st = con.createStatement(); rs = st.executeQuery(vsql); List retList = handlerResult(rs, Class.forName(vclass)); logger.info("SQL语句“" + vsql + "”。查询“" + keyName + "”成功!共查询出:" + retList.size() + "条!"); return retList; } finally { close(con, st, rs); } }
/* * 单条记录保存,并返回@id */ @SuppressWarnings({"rawtypes"}) public static Long save(Object obj, String keyName, String sqlAppend) throws Exception { Long id = 0L; if (obj == null) return id; String prefix = "jdbc.set"; String vsql = getProperty(prefix + "." + keyName + ".sql"); String vcolumn = getProperty(prefix + "." + keyName + ".column"); String vclass = getProperty(prefix + "." + keyName + ".class"); String tableName = getProperty(prefix + "." + keyName + ".table"); if (vsql == null || vclass == null || vcolumn == null) throw new IllegalArgumentException("please check“" + prefix + "." + keyName + "” config!"); if (sqlAppend != null && !sqlAppend.trim().equals("")) { // if(vsql.toLowerCase().contains(" where ")){ // vsql += sqlAppend.toLowerCase().replace(" where ", " and "); // }else if(sqlAppend.toLowerCase().contains(" where ")){ // vsql += sqlAppend.replace(" and ", " "); // }else{ // sqlAppend = " where " + sqlAppend; // // } vsql += sqlAppend; } logger.info("sql:" + vsql); Map map = handlerClass(Class.forName(vclass)); String[] vcolumnArr = vcolumn.split(","); Connection con = null; PreparedStatement ps = null; try { logger.info(" --------- getConnection: -----------"); con = getConnection(); logger.info(" --------- con: -----------" + con); con.setAutoCommit(false); ps = con.prepareStatement(vsql); for (int i = 0; i < vcolumnArr.length; i++) { boolean isKey = false; String col = vcolumnArr[i]; // 主键特殊处理 if (col.indexOf("@") == 0) { isKey = true; col = col.substring(1); } Method getMe = (Method) map.get(col + "_getMethod"); String returnType = (String) map.get(col + "_returnType"); if (getMe == null || returnType == null) continue; Object retObj = getMe.invoke(obj); logger.info(col + ":" + retObj); if (isKey) { retObj = handlerGenerator(tableName).generate(con); // id = Integer.parseInt(retObj.toString()); id = Long.parseLong(retObj.toString()); } else { retObj = getMe.invoke(obj); } if (returnType.equals("java.lang.String")) { if (retObj == null) ps.setString(i + 1, null); else ps.setString(i + 1, retObj.toString()); } else if (returnType.equals("java.lang.Long")) { if (retObj == null) ps.setLong(i + 1, 0L); else ps.setLong(i + 1, Long.parseLong(retObj.toString())); } else if (returnType.equals("java.lang.Integer")) { if (retObj == null) ps.setInt(i + 1, 0); else ps.setInt(i + 1, Integer.parseInt(retObj.toString())); } else if (returnType.equals("java.util.Date")) { if (retObj == null) { ps.setTimestamp(i + 1, null); } else { Date d = (Date) retObj; ps.setTimestamp(i + 1, new Timestamp(d.getTime())); } } else if (returnType.equals("java.lang.Short")) { if (retObj == null) ps.setInt(i + 1, 0); else ps.setInt(i + 1, Short.parseShort(retObj.toString())); } else { throw new IllegalAccessException(returnType); // TODO } } ps.executeUpdate(); con.commit(); logger.debug("SQL [" + vsql + "] save to [" + keyName + "] success!"); } catch (Exception e) { if (con != null) con.rollback(); throw e; } finally { close(con, ps, null); } return id; }
@SuppressWarnings({"rawtypes"}) public static void Batchsave(List list, String keyName) throws Exception { if (list == null || list.size() == 0) return; String prefix = "jdbc.set"; String vsql = getProperty(prefix + "." + keyName + ".sql"); String vcolumn = getProperty(prefix + "." + keyName + ".column"); String vclass = getProperty(prefix + "." + keyName + ".class"); String tableName = getProperty(prefix + "." + keyName + ".table"); if (vsql == null || vclass == null || vcolumn == null) throw new IllegalArgumentException("请检查“" + prefix + "." + keyName + "”的相关配置!"); logger.info("sql:" + vsql); Map map = handlerClass(Class.forName(vclass)); String[] vcolumnArr = vcolumn.split(","); Connection con = null; PreparedStatement st = null; try { con = getConnection(); logger.info(" --------- con: -----------" + con); con.setAutoCommit(false); logger.info(" ---getConnection---"); st = con.prepareStatement(vsql); int size = 0; for (Object obj : list) { for (int i = 0; i < vcolumnArr.length; i++) { boolean isKey = false; String col = vcolumnArr[i]; // 主键特殊处理 if (col.indexOf("@") == 0) { isKey = true; col = col.substring(1); } Method getMe = (Method) map.get(col + "_getMethod"); String returnType = (String) map.get(col + "_returnType"); if (getMe == null || returnType == null) continue; Object retObj = getMe.invoke(obj); if (isKey) { retObj = handlerGenerator(tableName).generate(con); } else { retObj = getMe.invoke(obj); } if (returnType.equals("java.lang.String")) { if (retObj == null) st.setString(i + 1, null); else st.setString(i + 1, retObj.toString()); } else if (returnType.equals("java.lang.Long")) { if (retObj == null) st.setLong(i + 1, 0L); else st.setLong(i + 1, Long.parseLong(retObj.toString())); } else if (returnType.equals("java.lang.Integer")) { if (retObj == null) st.setInt(i + 1, 0); else st.setInt(i + 1, Integer.parseInt(retObj.toString())); } else { throw new IllegalAccessException(returnType); // TODO } } st.addBatch(); if (++size == jdbcBatchSize) { st.executeBatch(); size = 0; } } if (size != 0) { st.executeBatch(); } con.commit(); logger.info("SQL语句“" + vsql + "”。保存“" + keyName + "”成功!共保存:" + list.size() + "条!"); } catch (Exception e) { if (con != null) con.rollback(); throw e; } finally { close(con, st, null); } }