package som.gu.core;import java.sql.Connection;import java.sql.DatabaseMetaData;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.HashMap;import java.util.Map;import com.gu.bean.columnInfo;import com.gu.bean.tableInfo;import com.gu.utils.StringUtils;import com.gu.utils.javaFieldUtils;/** * 负责获取管理数据库所有表结构和类结构的关系,并可以根据表结构生成类结构。 * @author * */public class tableContext { /** * 表名为key,表信息对象为value */ public static Maptables = new HashMap (); /** * */ public static Map poClassTableMap = new HashMap (); private tableContext(){} static { try { //初始化获得表的信息 Connection con = DBManager.getConnect(); DatabaseMetaData dbmd = con.getMetaData(); ResultSet tableRet = dbmd.getTables(null, "%","%",new String[]{"TABLE"}); while(tableRet.next()){ String tableName = (String) tableRet.getObject("TABLE_NAME"); tableInfo ti = new tableInfo(tableName,new HashMap () ,new ArrayList ()); tables.put(tableName, ti); ResultSet set = dbmd.getColumns(null, "%", tableName, "%"); while(set.next()){ columnInfo ci = new columnInfo(set.getString("COLUMN_NAME"), set.getString("TYPE_NAME"), 0); ti.getColumns().put(set.getString("COLUMN_NAME"), ci); } ResultSet set2 = dbmd.getPrimaryKeys(null, "%", tableName); while(set2.next()){ columnInfo ci2 = (columnInfo) ti.getColumns().get(set2.getObject("COLUMN_NAME")); ci2.setKeyType(1); ti.getPriKeys().add(ci2); } if(ti.getPriKeys().size()>0){ ti.setOnlyPriKey(ti.getPriKeys().get(0)); } } } catch (SQLException e) { e.printStackTrace(); } } /** * 根据数据库表结构生成类结构 */ public static void updateJavaPoFile(){ Map tables=tableContext.tables; for(tableInfo table:tables.values()){ javaFieldUtils.creatJavaPoFile(table,new MysqlTypeConvertor()); } } /** * 将po的class对象和表信息对象关联起来,便于重用! */ public static void LoadPoTables(){ /*Class c=Class.forName(""); poClassTableMap.put(key, value)*/ for(tableInfo table:tables.values()){ Class c=null; try { c = Class.forName(DBManager.getPoPackage()+"."+StringUtils.firstChar2uppercase(table.gettName())); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } poClassTableMap.put(c,table); } } }