/** * 判断并返回表的sql id * * @param conn * @param detailInfoDto * @return */ private static String getSEQID( Connection conn, GDAODetailInfoDto detailInfoDto, String tableSeq) { if (!CopyOfGDAOToolsUtil.isEmpty(tableSeq)) { try { executeSql(conn, "select PURCHASE." + tableSeq + ".NEXTVAL from DUAL"); return tableSeq; } catch (SQLException e) { // e.printStackTrace(); } } String seqID = "SEQ_" + detailInfoDto.getTableName() + "_ID"; try { executeSql(conn, "select PURCHASE." + seqID + ".NEXTVAL from DUAL"); return seqID; } catch (SQLException e) { // e.printStackTrace(); } seqID = "SEQ_" + detailInfoDto.getClassName().toUpperCase() + "_ID"; try { executeSql(conn, "select PURCHASE." + seqID + ".NEXTVAL from DUAL"); return seqID; } catch (SQLException e1) { // e1.printStackTrace(); } return null; }
/** * 获取生成DAO的详细信息 (字段,属性,类型) * * @param conn * @param tableName * @param sqlTypeProperties * @return * @throws Exception */ public static GDAODetailInfoDto getGDAODetailInfo( Connection conn, String tableName, String tableSeq, Properties sqlTypeProperties) throws Exception { GDAODetailInfoDto detailInfoDto = new GDAODetailInfoDto(); detailInfoDto.setTableName(tableName); detailInfoDto.setClassName(getClassName(tableName)); String seqIdName = getSEQID(conn, detailInfoDto, tableSeq); if (seqIdName == null) { throw new Exception("表" + tableName + "的序列号无法获取"); } detailInfoDto.setSeqIdName(seqIdName); final String executeSql = "select * from PURCHASE." + tableName; Statement statement = conn.createStatement(); ResultSet resultSet = statement.executeQuery(executeSql); ResultSetMetaData metaData = resultSet.getMetaData(); boolean pk_id = false; for (int i = 1; i <= metaData.getColumnCount(); i++) { ColumnInfoDto columnIDto = new ColumnInfoDto(); columnIDto.setColumnName(metaData.getColumnName(i)); columnIDto.setPropertyName(getPropertyName(metaData.getColumnName(i))); if (metaData.getColumnType(i) == 2) { // System.out.println(tableName + " : " + columnIDto.getColumnName() + " : " + // metaData.getScale(i) + " : " + metaData.getPrecision(i)); if (metaData.getScale(i) > 0) { columnIDto.setSqlType(7); columnIDto.setJavaType(getColumnMethod(sqlTypeProperties, 7)); } else if (metaData.getPrecision(i) > 10) { columnIDto.setSqlType(-5); columnIDto.setJavaType(getColumnMethod(sqlTypeProperties, -5)); } else { columnIDto.setSqlType(4); columnIDto.setJavaType(getColumnMethod(sqlTypeProperties, 4)); } } else { columnIDto.setSqlType(metaData.getColumnType(i)); columnIDto.setJavaType(getColumnMethod(sqlTypeProperties, metaData.getColumnType(i))); } detailInfoDto.getColumnList().add(columnIDto); // System.out.println(tableName + " : " + columnIDto.getColumnName() + " : " + // metaData.getScale(i) + " : " + columnIDto.getSqlType()); String className = getColumnClassName(sqlTypeProperties, columnIDto.getSqlType()); if (!detailInfoDto.getPackageNameList().contains(className) && !className.contains("lang")) { detailInfoDto.getPackageNameList().add(className); } // 判断表是否存在ID主键 if (columnIDto.getPropertyName().equals("id")) { pk_id = true; } } // 主键不为ID的话,不能生成代码 if (!pk_id) { throw new Exception("表" + tableName + "的主键不为ID,无法自动生成代码"); } return detailInfoDto; }