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";
	}

转载于:https://my.oschina.net/skyim/blog/137978

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值