手写SORM(simple object relation mapping)框架3—DBManager和TableContext的设计

代码如下:

package com.yf.sorm.core;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import com.yf.sorm.bean.Configuration;

/**
 * 根据配置信息,维持连接对象的管理(增加连接池功能)
 * @author yangf
 *
 */
public class DBManager {
    private static Configuration conf;
    
    // 静态代码块
    static {
        Properties props = new Properties();
        try {
            props.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        conf = new Configuration();
        conf.setDriver(props.getProperty("driver"));
        conf.setMysql(props.getProperty("mysql"));
        conf.setPassword(props.getProperty("password"));
        conf.setPoPackage(props.getProperty("poPackage"));
        conf.setSrcPath(props.getProperty("srcPath"));
        conf.setUrl(props.getProperty("url"));
        conf.setUser(props.getProperty("user"));
    }
    
    /**
     * 返回连接
     * @return 连接(先不写连接池后面加)
     */
    public static Connection getConnection () {
        try {
            Class.forName(conf.getDriver());
            return DriverManager.getConnection(conf.getUrl(), conf.getUser(), conf.getPassword());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    
    /**
     * 关闭
     * @param rs ResultSet
     * @param ps PreparedStatement
     * @param con Connection
     */
    public static void close (ResultSet rs, Statement ps, Connection con) {
        try {
            if (rs != null) {
                rs.close();
            }
        } catch(SQLException e) {
            e.printStackTrace();
        }
        
        try {
            if (ps != null) {
                ps.close();
            }
        } catch(SQLException e) {
            e.printStackTrace();
        }
        
        try {
            if (con != null) {
                con.close();
            }
        } catch(SQLException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 关闭
     * @param ps PreparedStatement
     * @param con Connection
     */
    public static void close (Statement ps, Connection con) {
        try {
            if (ps != null) {
                ps.close();
            }
        } catch(SQLException e) {
            e.printStackTrace();
        }
        
        try {
            if (con != null) {
                con.close();
            }
        } catch(SQLException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 关闭
     * @param con Connection
     */
    public static void close (Connection con) {
        
        try {
            if (con != null) {
                con.close();
            }
        } catch(SQLException e) {
            e.printStackTrace();
        }
    }
}
package com.yf.sorm.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.yf.sorm.bean.ColumnInfo;
import com.yf.sorm.bean.TableInfo;

/**
 * 负责获取管理数据库所有表结构和类结构的关系,并可以根据表结构生成类结构
 * @author yangf
 *
 */
public class TableContext {
    
    /**
     * 表名为key,表信息为value
     */
    public static Map<String, TableInfo> tables = new HashMap<String, TableInfo>();
    
    /**
     * 将po的class对象和表信息对象关联起来,便于重用
     */
    public static Map<Class<?>, TableInfo> poClassTableMap = new HashMap<Class<?>, TableInfo>();
    
    private TableContext(){}
    
    static {
        
        try {
            Connection con = DBManager.getConnection();
            DatabaseMetaData dbmd = con.getMetaData();
            
            ResultSet tableSet = dbmd.getTables(null, "%", "%", new String[]{"TABLE"});
            
            while(tableSet.next()) {
                String tableName = (String) tableSet.getObject("TABLE_NAME");
                TableInfo ti = new TableInfo(tableName, new ArrayList<ColumnInfo>(), new HashMap<String, 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.getColums().put(set.getString("COLUMN_NAME"), ci);
                }
                // 查询表中主键
                ResultSet set2 = dbmd.getPrimaryKeys(null, "%", tableName);
                while(set2.next()) {
                    ColumnInfo ci2 = ti.getColums().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();
        }
    }
}

 

posted @ 2017-05-18 20:22  noooooob  阅读(266)  评论(0编辑  收藏  举报