jeesite如何已生成数据的数据源_jeesite1.X 集成多数据源

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

/**

* @return the currentLookupKey

* @author sa

* @date 2012-5-18 下午4:06:44

*/

public static String getCurrentLookupKey() {

return (String) contextHolder.get();

}

/**

* @param currentLookupKey the currentLookupKey to set

* @author sa

* @date 2012-5-18 下午4:06:44

*/

public static void setCurrentLookupKey(String currentLookupKey) {

contextHolder.set(currentLookupKey);

}

/*

* (non-Javadoc)

*

* @see

* org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource#

* determineCurrentLookupKey()

*/

@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行添加如下两行代码,并修改原来的圆圈地方:

0ec961169010e345c52d1589829026fc.png

pom.xml 添加sqlserver2008驱动和依赖

com.microsoft.sqlserver

sqljdbc4

4.0

com.github.jsqlparser

jsqlparser

1.2

mybatis sqlserver 分页查询sql,findlist方法

d1a2c3c00866cd95d750c29137bec593.png

动态调用方式: @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);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值