JFinal 源码导读第二天(3) tableMappings,TableInfoMapping

1.接上面的代码 setPrimaryKey(primaryKey.trim());    // this.primaryKey = primaryKey.trim();代码看看应该就会懂啦
setPrimaryKey(primaryKey.trim());	// this.primaryKey = primaryKey.trim();
	private void setPrimaryKey(String primaryKey) {
		String[] keyArr = primaryKey.split(",");
		if (keyArr.length > 1) {
			if (StringKit.isBlank(keyArr[0]) || StringKit.isBlank(keyArr[1]))
				throw new IllegalArgumentException("The composite primary key can not be blank.");
			this.primaryKey = keyArr[0].trim();
			this.secondaryKey = keyArr[1].trim();
		}
		else {
			this.primaryKey = primaryKey;
		}
	}

2.startPlugins();    // very important!!!
这个代码为什么说重要,等我慢慢道来
我上面代码有C3p0Plugin,ActiveRecordPlugin两个插件

private static void startPlugins() {
		List<IPlugin> pluginList = plugins.getPluginList();
		if (pluginList != null) {
			for (IPlugin plugin : pluginList) {
				try {
					boolean success = plugin.start();
					if (!success) {
						String message = "Plugin start error: " + plugin.getClass().getName();
						log.error(message);
						throw new RuntimeException(message);
					}
				}
				catch (Exception e) {
					String message = "Plugin start error: " + plugin.getClass().getName() + ". \n" + e.getMessage();
					log.error(message, e);
					throw new RuntimeException(message, e);
				}
			}
		}
	}
3.C3p0Plugin初始化代码大家好好看看应该会明白,就不细说啦
public boolean start() {
		dataSource = new ComboPooledDataSource();
		dataSource.setJdbcUrl(jdbcUrl);
		dataSource.setUser(user);
		dataSource.setPassword(password);
		try {dataSource.setDriverClass(driverClass);}
		catch (PropertyVetoException e) {dataSource = null; System.err.println("C3p0Plugin start error"); throw new RuntimeException(e);} 
		dataSource.setMaxPoolSize(maxPoolSize);
		dataSource.setMinPoolSize(minPoolSize);
		dataSource.setInitialPoolSize(initialPoolSize);
		dataSource.setMaxIdleTime(maxIdleTime);
		dataSource.setAcquireIncrement(acquireIncrement);
		
		return true;
	}
4.ActiveRecordPlugin的初始化
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);
		
		isStarted = true;
		return TableInfoBuilder.buildTableInfo(tableMappings);
	}
5.TableInfoBuilder.buildTableInfo(tableMappings);这段代码我会好好详细的讲一下
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) {
			try {
				TableInfo tableInfo = doBuildTableInfo(mapping, conn);
				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;
	}
5.上面代码的核心如下显示
for (TableInfo mapping : tableMappings) {
			try {
				TableInfo tableInfo = doBuildTableInfo(mapping, conn);
				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);
			}
		}

6.tableMappings如果忘记是什么意思啦,可以看看我上面的代码,
arp.addMapping("blog", Blog.class);    // 映射blog 表到 Blog模型
tableMappings.add(new TableInfo(tableName, modelClass));
7.上面代码的核心就是这句代码doBuildTableInfo(mapping, conn);

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);
			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;
	}
8.String sql = DbKit.getDialect().forTableInfoBuilderDoBuildTableInfo(tableInfo.getTableName());
static Dialect dialect = new MysqlDialect();
	public String forTableInfoBuilderDoBuildTableInfo(String tableName) {
		return "select * from `" + tableName + "` where 1 = 2";
	}
	
上面是执行默认的sql方言的
	@SuppressWarnings("unchecked")
	private Map<String, Class<?>> columnTypeMap = DbKit.containerFactory.getAttrsMap();
	public void addInfo(String columnLabel, Class<?> columnType) {
		columnTypeMap.put(columnLabel, columnType);
	}
	
上面的就是把blog表里面的字段和类型放到columnTypeMap中
TableInfoMapping tim = TableInfoMapping.me();
tim.putTableInfo(mapping.getModelClass(), tableInfo);

总结上面语句的意思就是将表的类名blog.class,blgg相关的字段和类型放在tableInfo中

private static final Map<Class<? extends Model<?>>, TableInfo> tableInfoMap = new HashMap<Class<? extends Model<?>>, TableInfo>();
public void putTableInfo(Class<? extends Model<?>> modelClass, TableInfo tableInfo) {
		tableInfoMap.put(modelClass, tableInfo);
	}

睡觉啦,接下来我会详细介绍Interceptor初始化等信息,我会把整个代码的详细介绍都写出来,假如有时间,我也是最近几天才开始读的,感觉还可以,就准备共享出来

你可能感兴趣的:(jFinal,tableMappings)