JFinal 源码导读第二天(2)configPlugin,configRoute


1.接上面的代码
if (map.containsKey(controllerKey))
			throw new IllegalArgumentException("The controllerKey already exists");
		
		if (!controllerKey.startsWith("/"))
			controllerKey = "/" + controllerKey;
		map.put(controllerKey, controllerClass);
2.贴出上面代码,主要是想让大家知道报The controllerKey already exists的问题
if (baseViewPath != null)						// support baseViewPath
			viewPath = baseViewPath + viewPath;
3.这段代码中的baseViewPath默认是null,是不会执行
private static String baseViewPath;
	
	/**
	 * Set the base path for all views
	 */
	static void setBaseViewPath(String baseViewPath) {
		if (baseViewPath == null)
			throw new IllegalArgumentException("The baseViewPath can not be null");
		baseViewPath = baseViewPath.trim();
		if ("".equals(baseViewPath))
			throw new IllegalArgumentException("The baseViewPath can not be blank");
		
		if (! baseViewPath.startsWith("/"))			// add prefix "/"
			baseViewPath = "/" + baseViewPath;
		
		if (baseViewPath.endsWith("/"))				// remove "/" in the end of baseViewPath
			baseViewPath = baseViewPath.substring(0, baseViewPath.length() - 1);
		
		Routes.baseViewPath = baseViewPath;
	}
4.上面代码的核心就是放入到
viewPathMap.put(controllerKey, viewPath);
在我们的代码中,就是变成下面那样的
me.add("/", CommonController.class);
me.add("/blog", BlogController.class);
viewPathMap.put("/", "/"); 
viewPathMap.put("/blog","/blog/")
5.jfinalConfig.configPlugin(plugins);下面的代码会执行下面的代码
	/**
	 * 配置插件
	 */
	public void configPlugin(Plugins me) {
		// 配置C3p0数据库连接池插件
		C3p0Plugin c3p0Plugin = new C3p0Plugin(getProperty("jdbcUrl"), getProperty("user"), getProperty("password").trim());
		me.add(c3p0Plugin);
		
		// 配置ActiveRecord插件
		ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin);
		me.add(arp);
		arp.addMapping("blog", Blog.class);	// 映射blog 表到 Blog模型
	}
6.getProperty("jdbcUrl")调用相关如下方法,这个properties就是上面初始化载入a_little_config.txt文件时候执行的
	private void checkPropertyLoading() {
		if (properties == null)
			throw new RuntimeException("You must load properties file by invoking loadPropertyFile(String) method in configConstant(Constants) method before.");
	}

public String getProperty(String key) {
		checkPropertyLoading();
		return properties.getProperty(key);
	}
7.me.add方法如下,很简单,看看就明白啦
/**
 * Plugins.
 */
final public class Plugins {
	
	private final List<IPlugin> pluginList = new ArrayList<IPlugin>();
	
	public Plugins add(IPlugin plugin) {
		if (plugin != null)
			this.pluginList.add(plugin);
		return this;
	}
	
	public List<IPlugin> getPluginList() {
		return pluginList;
	}
}

8.  配置ActiveRecord插件 ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin);

	public ActiveRecordPlugin(IDataSourceProvider dataSourceProvider) {
		ActiveRecordPlugin.dataSourceProvider = dataSourceProvider;
	}
9.me.add(arp);就是执行上面的代码就不说啦
arp.addMapping("blog", Blog.class);    // 映射blog 表到 Blog模型
private static final List<TableInfo> tableMappings = new ArrayList<TableInfo>();	
public ActiveRecordPlugin addMapping(String tableName, Class<? extends Model<?>> modelClass) {
		tableMappings.add(new TableInfo(tableName, modelClass));
		return this;
	}

10.TableInfo这个类我现在就不先贴出来,下面我会详细介绍
new TableInfo(table,modelClass)

static Dialect dialect = new MysqlDialect();
public TableInfo(String tableName, Class<? extends Model<?>> modelClass) {
		this(tableName, DbKit.dialect.getDefaultPrimaryKey(), modelClass);
	}

    public TableInfo(String tableName, String primaryKey, Class<? extends Model<?>> modelClass) {
        if (StringKit.isBlank(tableName))
            throw new IllegalArgumentException("Table name can not be blank.");
        if (StringKit.isBlank(primaryKey))
            throw new IllegalArgumentException("Primary key can not be blank.");
        if (modelClass == null)
            throw new IllegalArgumentException("Model class can not be null.");
        
        this.tableName = tableName.trim();
        setPrimaryKey(primaryKey.trim());    // this.primaryKey = primaryKey.trim();
        this.modelClass = modelClass;
    }
    

11.DbKit.dialect.getDefaultPrimaryKey()这个方法调用代码如下,主键默认为id

public String getDefaultPrimaryKey() {
		return "id";
	}

你可能感兴趣的:(ActiveRecord,jFinal,configplugin,TableInfo)