zoukankan      html  css  js  c++  java
  • 数据库表结构上下文

    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  Map<String,tableInfo>  tables = new HashMap<String,tableInfo>();
        
        /**
         * 
         */
        public static  Map<Class,tableInfo>  poClassTableMap = new HashMap<Class,tableInfo>();
        
        
        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<String, columnInfo>() ,new ArrayList<columnInfo>());
                    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<String,tableInfo>  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);
            }
            }
        
        
        
    
    }
  • 相关阅读:
    EasyUI基础searchbox&amp;progressbar(搜索框,进度条)
    git 仓库
    “农民代码”讨论
    多线程和多进程之间的区别
    move_uploaded_file
    在form里面,放了四个UEditor,怎么在后台分别获取它们值
    ThinkPHP模板IF标签用法详解
    Tp框架查询分页显示与全部查询出来显示运行时间快慢有区别吗?
    百度UEditor基本使用
    织梦DedeCms获取当前页面URL地址的调用方法
  • 原文地址:https://www.cnblogs.com/helloMyworld0001/p/5972966.html
Copyright © 2011-2022 走看看