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