欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

如何用Jfinal連接多個(gè)數(shù)據(jù)庫(kù)

 更新時(shí)間:2021年03月15日 11:55:55   作者:可愛的黑精靈  
這篇文章主要介紹了如何用Jfinal連接多個(gè)數(shù)據(jù)庫(kù),幫助大家更好的理解和學(xué)習(xí)使用Jfinal,感興趣的朋友可以了解下

ActiveRecordPlugin可以支持多個(gè)數(shù)據(jù)庫(kù),多個(gè)語言,我們只需要添加多個(gè)ActiveRecordPlugin,分別配置即可。

Jfinal連接多個(gè)數(shù)據(jù)庫(kù)

1. 添加mssql-jdbc-7.4.1.jar和mysql-connector-java-8.0.12.jar分別用于連接sqlserver及mysql數(shù)據(jù)庫(kù)。

<!-- https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc -->
<dependency>
 <groupId>com.microsoft.sqlserver</groupId>
 <artifactId>mssql-jdbc</artifactId>
 <version>7.4.1.jre8</version>
 <scope>test</scope>
</dependency>


<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
 <groupId>mysql</groupId>
 <artifactId>mysql-connector-java</artifactId>
 <version>8.0.12</version>
</dependency>

2. 添加數(shù)據(jù)庫(kù)配置文件

datasource.properties:

driver = com.microsoft.sqlserver.jdbc.SQLServerDriver
jdbcUrl = jdbc:sqlserver://localhost;Database=test
user = sa
password = 123456
devMode = true  

jdbcUrl2 = jdbc:mysql://localhost/test?characterEncoding=utf-8&useSSL=false&serverTimezone=GMT
user2 = root
password2 =123456
devMode2 =true

3. 配置configPlugin

 public void configPlugin(Plugins me) {
 	
 	// 連接sqlserver數(shù)據(jù)庫(kù)
 	C3p0Plugin c3p0Plugin = new C3p0Plugin(getProperty("jdbcUrl"), getProperty("user"), getProperty("password"), getProperty("driver"));
  me.add(c3p0Plugin);
  // 給數(shù)據(jù)源添加別名sqlserver
  ActiveRecordPlugin arp = new ActiveRecordPlugin("sqlserver",c3p0Plugin);
  // 添加sqlserver方言 SqlServer方言在jfinal2.0以上才引入
  arp.setDialect(new SqlServerDialect());
  me.add(arp);
 	
  // 連接mysql數(shù)據(jù)庫(kù)
 	C3p0Plugin c3p0Plugin2 = new C3p0Plugin(getProperty("jdbcUrl2"), getProperty("user2"), getProperty("password2"));
  me.add(c3p0Plugin2);
  // 給數(shù)據(jù)源添加別名mysql
  ActiveRecordPlugin arp2 = new ActiveRecordPlugin("mysql",c3p0Plugin2);
  // 添加mysql方言
  arp2.setDialect(new MysqlDialect());
  me.add(arp2);
 	
 	
 }

JFinal 的方言僅用于 Model 的 save()、update()、delete()等等由框架生成 sql 的方法,而開發(fā)者自由傳入的 sql 框架并不會(huì)干預(yù),這通常是在使用 find()、query()、paginate() 之類需要傳入 sql 的方法。
JFinal 默認(rèn)方言為mysql的,如果不配置方言用Model中的方法會(huì)出莫名其妙的問題。

ActiveRecordPlugin提供了MysqlDialect、SqlServerDialect、OracleDialect、Sqlite3Dialect、AnsiSqlDialect實(shí)現(xiàn)類,來支持mysql、sqlserver、oracle、sqlite3、ANSI等數(shù)據(jù)庫(kù)的支持。

4. 使用

// 查詢sqlserver的test表
List<Record> rs = Db.use("sqlserver").find("select * from test");

// 查詢mysql的test表		
List<Record> rs1 = Db.use("mysql").find("select * from test");

以上就是如何用Jfinal連接多個(gè)數(shù)據(jù)庫(kù)的詳細(xì)內(nèi)容,更多關(guān)于Jfinal連接多個(gè)數(shù)據(jù)庫(kù)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論