JFinal 源碼--類映射解析

1.User類 java

import com.jfinal.plugin.activerecord.Model;
/**
 * user model.
 */
@SuppressWarnings("serial")
public class User extends Model<User> {
    public static final User dao = new User();
}

2. configPlugin(Plugins me)這裏實例化一個ActiveRecordPlugin類,將數據庫代表與實體類的映射保存到ActiveRecordPlugin類中 mysql

@Override
    public void configPlugin(Plugins me) {
        C3p0Plugin cp = new C3p0Plugin(Util.JDBC, Util.USER, Util.PASSWORD);
        me.add(cp);
        ActiveRecordPlugin arp = new ActiveRecordPlugin(cp);
        me.add(arp);
        arp.addMapping("user", User.class);//將user(數據庫表名)與User(模型類)映射關係保存到arp中
    }

3.ActiveRecordPlugin類 sql

將表-類映射封裝到TableInfo中,並 用一個 List<TableInfo>  tableMappings 保存 數據庫

public ActiveRecordPlugin  addMapping(String tableName, Class < ?  extends Model < ? >> modelClass) { 
        tableMappings.add( new TableInfo(tableName, modelClass)); 
         return  this; 
    }

系統啓動時用TableInfoBuilder類來實現表中具體元素和類的映射 設計模式

public  boolean start() { 
         if (isStarted) 
             return true; 
         
         if (dataSourceProvider  != null) 
            dataSource  = dataSourceProvider.getDataSource(); 
         
         if (dataSource  == null) 
             throw  new RuntimeException( "ActiveRecord start error: ActiveRecordPlugin need DataSource or DataSourceProvider"); 
         
        DbKit.setDataSource(dataSource); //設置數據庫的具體實現(這裏用的是mysql)使用了工廠設計模式 
         
        isStarted  = true; 
         return  TableInfoBuilder.buildTableInfo(tableMappings); //去實現具體的映射 
    }
4.TableInfoBuilder類
把數據庫表的每一個字段以及類型 映射到類
class TableInfoBuilder { 
     
     static  boolean  buildTableInfo(List <TableInfo > tableMappings) { 
         boolean succeed  = true; 
        Connection conn  = null; 
         try { 
            conn  = DbKit.getDataSource().getConnection(); 
        }  catch (SQLException e) { 
             throw  new ActiveRecordException(e); 
        } 
         
        TableInfoMapping tim  = TableInfoMapping.me(); //最終保存表與類的映射,單例 
         for (TableInfo mapping  : tableMappings) { //遍歷以前保存映射的List 
             try { 
                TableInfo tableInfo  = doBuildTableInfo(mapping, conn); //從數據庫中讀取到表的全部字段及類型,封裝到TableInfo類中 
                tim.putTableInfo(mapping.getModelClass(), tableInfo);// 最終保存表與類的映射 
            }  catch (Exception e) { 
                succeed  = false; 
                System.err.println( "Can not build TableInfo, maybe the table "  + mapping.getTableName()  +  " is not exists."); 
                 throw  new ActiveRecordException(e); 
            } 
        } 
        DbKit.close(conn); 
         return succeed; 
    } 
    /**
      * 從數據庫中讀取到表的全部字段及類型,封裝到TableInfo類中
      *
      **/
     private   static  TableInfo doBuildTableInfo(TableInfo tableInfo, Connection conn)  throws  SQLException {
        TableInfo result  = tableInfo; //保存類的全部屬性及類型 
         
        String sql  = DbKit.getDialect().forTableInfoBuilderDoBuildTableInfo(tableInfo.getTableName()); 
        Statement stm  = conn.createStatement(); 
        ResultSet rs  = stm.executeQuery(sql); 
        ResultSetMetaData rsmd  = rs.getMetaData(); 
         
         for ( int i = 1; i < =rsmd.getColumnCount(); i ++) { 
            String colName  = rsmd.getColumnName(i); //取得列表標題(類屬性) 
            String colClassName  = rsmd.getColumnClassName(i); //屬性的type 
             if ( "java.lang.String".equals(colClassName)) { 
                 // varchar, char, enum, set, text, tinytext, mediumtext, longtext 
                result.addInfo(colName, java.lang.String. class); 
            } 
             else  if ( "java.lang.Integer".equals(colClassName)) { 
                 // int, integer, tinyint, smallint, mediumint 
                result.addInfo(colName, java.lang.Integer. class); 
            } 
             else  if ( "java.lang.Long".equals(colClassName)) { 
                 // bigint 
                result.addInfo(colName, java.lang.Long. class); 
            } 
             // else if ("java.util.Date".equals(colClassName)) {        // java.util.Data can not be returned 
                 // java.sql.Date, java.sql.Time, java.sql.Timestamp all extends java.util.Data so getDate can return the three types data 
                 // result.addInfo(colName, java.util.Date.class); 
             // } 
             else  if ( "java.sql.Date".equals(colClassName)) { 
                 // date, year 
                result.addInfo(colName, java.sql.Date. class); 
            } 
             else  if ( "java.lang.Double".equals(colClassName)) { 
                 // real, double 
                result.addInfo(colName, java.lang.Double. class); 
            } 
             else  if ( "java.lang.Float".equals(colClassName)) { 
                 // float 
                result.addInfo(colName, java.lang.Float. class); 
            } 
             else  if ( "java.lang.Boolean".equals(colClassName)) { 
                 // bit 
                result.addInfo(colName, java.lang.Boolean. class); 
            } 
             else  if ( "java.sql.Time".equals(colClassName)) { 
                 // time 
                result.addInfo(colName, java.sql.Time. class); 
            } 
             else  if ( "java.sql.Timestamp".equals(colClassName)) { 
                 // timestamp, datetime 
                result.addInfo(colName, java.sql.Timestamp. class); 
            } 
             else  if ( "java.math.BigDecimal".equals(colClassName)) { 
                 // decimal, numeric 
                result.addInfo(colName, java.math.BigDecimal. class); 
            } 
             else  if ( "[B".equals(colClassName)) { 
                 // binary, varbinary, tinyblob, blob, mediumblob, longblob 
                 // qjd project: print_info.content varbinary(61800); 
                result.addInfo(colName,  byte[]. class); 
            } 
             else { 
                 int type  = rsmd.getColumnType(i); 
                 if (type  == Types.BLOB) { 
                    result.addInfo(colName,  byte[]. class); 
                } 
                 else  if (type  == Types.CLOB  || type  == Types.NCLOB) { 
                    result.addInfo(colName, String. class); 
                } 
                 else { 
                    result.addInfo(colName, String. class); 
                } 
                 // core.TypeConverter 
                 // throw new RuntimeException("You've got new type to mapping. Please add code in " + TableInfoBuilder.class.getName() + ". The ColumnClassName can't be mapped: " + colClassName); 
            } 
        } 
         
        rs.close(); 
        stm.close(); 
         return result; 
    } 
}
5.TableInfo類
保存着類的全部屬性
columnTypeMap //map保存着類的屬性以及屬性的數據類型
modelClass     //類的具體class類(User.class)
private String tableName;//表名 
     private String primaryKey;//主鍵 
     private String secondaryKey  = null; 
     
    @SuppressWarnings( "unchecked") 
     private Map <String, Class < ? >> columnTypeMap  = DbKit.containerFactory.getAttrsMap();     //    new HashMap<String, Class<?>>(); 
     private Class < ?  extends Model < ? >> modelClass;
相關文章
相關標籤/搜索