jeesite.properties配置多數資料源位址,這裡以mysql5.7和sqlserver2008版本為例子 #mysql database settingjdbc.type=mysql#jdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/nkydsj?useUnicode=true&characterEncoding=utf-8jdbc.username=rootjdbc.password=111111
#mssql database settings#jdbc.type2=mssqljdbc.url2=jdbc:sqlserver://localhost:1433;DatabaseName=NXQiXiangjdbc.username2=sajdbc.password2=111111
#pool settingsjdbc.pool.init=1jdbc.pool.minIdle=3jdbc.pool.maxActive=20
#jdbc.testSql=SELECT 'x'jdbc.testSql=SELECT 'x' FROM DUALjdbc.testSql2= SELECT getdate()
建立動态資料源類 package com.thinkgem.jeesite.common.db;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class DynamicDataSource extends AbstractRoutingDataSource {
private static final ThreadLocal contextHolder = new ThreadLocal();
public static String getCurrentLookupKey() {
return (String) contextHolder.get();
}
public static void setCurrentLookupKey(String currentLookupKey) {
contextHolder.set(currentLookupKey);
}
@Override
protected Object determineCurrentLookupKey() {
return getCurrentLookupKey();
}
}
修改spring-context.xml
建立方言動态切換類 com.thinkgem.jeesite.common.db.DbDialectFoactory,暫時隻用sqlserver2008和mysql,是以隻寫兩個 package com.thinkgem.jeesite.common.db;
import com.thinkgem.jeesite.common.persistence.dialect.Dialect;
import com.thinkgem.jeesite.common.persistence.dialect.db.MySQLDialect;
import com.thinkgem.jeesite.common.persistence.dialect.db.SQLServer2005Dialect;
public class DbDialectFoactory {
public static Dialect createDbDialect(String type) {
if ("sqlserver".equals(type)) {
return new SQLServer2005Dialect();
}
else{
return new MySQLDialect();
}
}
}
修改架構自帶類 com.thinkgem.jeesite.common.persistence.interceptor.PaginationInterceptor
67行添加如下兩行代碼,并修改原來的圓圈地方:
pom.xml 添加sqlserver2008驅動和依賴
com.microsoft.sqlserver
sqljdbc4
4.0
com.github.jsqlparser
jsqlparser
1.2
mybatis sqlserver 分頁查詢sql,findlist方法
動态調用方式: @RequestMapping(value = {"api/list", ""})
@ResponseBody
public ResponseEntity> list(WeatherCondition weatherCondition, HttpServletRequest request, HttpServletResponse response, Model model) {
//切換資料源sqlserver,預設資料源mysql
DynamicDataSource.setCurrentLookupKey("sqlserver");
Page page = weatherConditionService.findPage(new Page(request, response), weatherCondition);
DynamicDataSource.setCurrentLookupKey("mysql");
return new ResponseEntity(page, HttpStatus.OK);
}