版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/joman5/article/details/53673107
有时根据项目需要,我们要连接多个数据,如db1 ,或db2,分别对应不同的ip地址及数据库名称,
jfinal 对于这样的要求,有自己的一套方案,需要进行如下配置。
public void configPlugin(Plugins me) {
PropKit.use("config.txt");
C3p0Plugin cp = new C3p0Plugin(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password"),
PropKit.get("jdbcDriver"));
cp.setInitialPoolSize(3);
cp.setMaxIdleTime(10);
cp.setMinPoolSize(3);
cp.setMaxIdleTime(6);
me.add(cp);
ActiveRecordPlugin arp = new ActiveRecordPlugin(cp);
arp.setDialect(new AnsiSqlDialect());
me.add(arp);
C3p0Plugin cp2 = new C3p0Plugin(PropKit.get("jdbcUrl2"), PropKit.get("user2"), PropKit.get("password2"),
PropKit.get("jdbcDriver2"));
cp2.setInitialPoolSize(3);
cp2.setMaxIdleTime(10);
cp2.setMinPoolSize(3);
cp2.setMaxIdleTime(6);
me.add(cp2);
ActiveRecordPlugin arp2 = new ActiveRecordPlugin("db2",cp2);
arp2.setDialect(new AnsiSqlDialect());
me.add(arp2);
_MappingKit.mapping(arp);
}
注意这句 ActiveRecordPlugin arp2 = new ActiveRecordPlugin("db2",cp2); db2 是我为第2个数据库连接起的别名,根据需要可以增加多个
具体可以增加多少个数据库连接没有实验过,有兴趣的可以试试 。
对于使用
Db.use("db2").update(sql); 这样就可以使用 db2 数据库了,使用 use 函数。
Db.update(sql); 当不使用 ues 函数使用的是 前面 配置里面默认的数据库。
以上事例为 连接 sql server 2008 数据库,
arp2.setDialect(new AnsiSqlDialect()); 注意这句话 连接 sql2008 特有的解决字符 问题。