jeesite如何已生成数据的数据源_jeesite添加多数据源

1.jeesite.properties 添加数据源信息,(url2,username2,pawwword2)

#mysql database setting

jdbc.type=mysql

jdbc.driver=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://localhost:3306/website?useUnicode=true&characterEncoding=utf-8

jdbc.username=root

jdbc.password=root

#mysql2 database setting

#jdbc.type=mysql

#jdbc.driver=com.mysql.jdbc.Driver

jdbc.url2=jdbc:mysql://218.28.123.82:3306/stkcentervideosys?useUnicode=true&characterEncoding=utf-8

jdbc.username2=root

jdbc.password2=root

2.修改spring-context.xml(src/main/resources/),3处需要修改/添加

第一处(spring-context.xml):

第二处(spring-context.xml):修改为dynamicDataSource

第三处(spring-context.xml):修改为dynamicDataSource

3.添加DynamicDataSource.java (com.thinkgem.jeesite.common.db.DynamicDataSource.java)

package com.thinkgem.jeesite.common.db;

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

/**

* Mysql 多数据源切换

*

* @author sa

* @version V1.0

* @Description:

* @date 2015/10/09

*/

public class DynamicDataSource extends AbstractRoutingDataSource {

private static final ThreadLocal contextHolder = new ThreadLocal();

/**

*

* @author sa

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

* @return the currentLookupKey

*/

public static String getCurrentLookupKey() {

return (String) contextHolder.get();

}

/**

*

* @author sa

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

* @param currentLookupKey

* the currentLookupKey to set

*/

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

}

}

4.在Controller中切换(在service层切换不管用,还没查原因)

//切换数据源dataSource2,默认数据源dataSource

DynamicDataSource.setCurrentLookupKey("dataSource2");

List list = stkScenerySpotService.findAll();

model.addAttribute("list",list);

DynamicDataSource.setCurrentLookupKey("dataSource");

注:

要对切换的数据源dataSource2 中的表手动写映射和三层

实体:com.thinkgem.jeesite.modules.cms.entity.StkScenerySpot.java

service:com.thinkgem.jeesite.modules.cms.service.StkScenerySpotService.java

mapper:StkScenerySpotDao.xml (src/main/resources/modules/cms)

我只查所有,所以sql很简单

a.Spot_Id AS "id",

a.CamId AS "camId",

a.Name AS "name",

a.Comment AS "commnet"

SELECT

FROM stk_scenery_spot a

1 = 1

ORDER BY a.Spot_Id DESC

效果:

jeesite数据库表

214e84cbc9d069d8c924e77b54d1133d.png

datasource2数据表

49488be79e0501d15f6ef73bec6d3487.png

获取数据

ace1db6bdbe17b9112a16a5a8b7fc5fd.png

前端展示

2d9c0e5bf3fb915063f1c31330f434a3.png

datasource2数据表中的内容

950d332b81e42dbd6150209e167248aa.png

参考资料:http://www.hifreud.com/2015/02/25/07-spring-datasources/

http://www.cnblogs.com/digdeep/p/4512368.html

b949ac046c48c7b1abbe67dd73ecdf7d.png

大小: 104.3 KB

d72d3509ac35d89f2e7b47d0913f7ef8.png

大小: 124.3 KB

5ac7a1476b6c8ee9560b64c1b66b3bff.png

大小: 37.5 KB

39157fb80ca77b5d992a7d991ea974a1.png

大小: 9.2 KB

4408ba10436bf282ee22725a24b7fff5.png

大小: 28 KB

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2015-10-12 10:00

浏览 8354

评论

1 楼

wqlin_007

2017-03-28

我也在service进行了配置不成功,是不是在进入service时sqlSessionFactory和mapperScannerConfigurer中已经通过getCurrentLookupKey()获取了数据库连接?能不能在DAO层进行切换数据源呢?查了一些资料,也有配置在DAO上的,不过我现在还没有测试成功

0956f0a970daab536ce4be0ee96b00ac.gif 

0956f0a970daab536ce4be0ee96b00ac.gif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值