SPRING-JDBC多个数据源

本文介绍了如何在Spring中配置和使用多个数据源。通过示例展示了如何为不同的数据源创建JdbcTemplate bean,并在服务层通过DbRunner实现动态选择数据源执行SQL操作。此外,还展示了如何在TransactionService中管理和调用这些数据源。


SPRINGJDBC:

 

1

<beanid="jdbcTemplate"class="org.springframework.jdbc.core.JdbcTemplate"lazy-init="true">

<propertyname="dataSource">

<refbean="dataSource" />

</property>

</bean>

 

2

@Service

@Lazy(true)

public classSpringJdbcDbRunner extends AbstractDbRunner {

 

@Autowired

privateJdbcTemplate jdbcTemplate;

 

@Override

publiclong count(String sql, Object... values) throws DbRuntimeException {

if(StringUtils.isEmpty(sql))

thrownew DbRuntimeException(DbErrorCode.ARGUMENT_ILLEGAL,

"SQL不能为空!!!");

returnjdbcTemplate.queryForObject(sql, values, Long.class);

}

 

 

 

JDBC

 

1

<beanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource"

       destroy-method="close"lazy-init="true">

       <propertyname="driverClassName"value="oracle.jdbc.driver.OracleDriver">

       </property>

       <property name="url"

          value="jdbc:oracle:thin:@(DESCRIPTION = (ADDRESS_LIST = (ADDRESS =(PROTOCOL = TCP)(HOST = )(PORT = 7)))(CONNECT_DATA = (SID =flu)))">

       </property>

       <property name="username"value="pdata"></property>

       <property name="password"value="c1234"></property>

       <property name="maxActive"value="50"></property>

       <property name="maxIdle"value="50"></property>

       <property name="minIdle"value="10"></property>

       <property name="maxWait"value="60000"></property>

       <propertyname="removeAbandoned" value="true"></property>

       <propertyname="removeAbandonedTimeout"value="180"></property>

    </bean>

 

2

@Service

@Lazy(true)

public classJdbcDbRunner extends AbstractDbRunner {

 

@Autowired

privateDataSource dataSource;

 

@Override

publicvoid execute(String sql, Object... values) throws DbRuntimeException {

if(StringUtils.isEmpty(sql))

thrownew DbRuntimeException(DbErrorCode.ARGUMENT_ILLEGAL,

"SQL不能为空!!!");

Connectionconnection = null;

PreparedStatementps = null;

try{

connection= dataSource.getConnection();

ps= getPreparedStatement(connection, sql, values);

ps.execute();

ps.close();

ps= null;

connection.close();

connection= null;

}catch (Exception e) {

logger.error(e.getMessage(),e);

}finally {

if(ps != null)

try{

ps.close();

}catch (SQLException e) {

logger.error(e.getMessage(),e);

}

if(connection != null)

try{

connection.close();

}catch (SQLException e) {

logger.error(e.getMessage(),e);

}

}

}

 

 

 

 

当动态的需要多个数据源时,

<beanid="jdbcTemplate_palife"class="org.springframework.jdbc.core.JdbcTemplate"lazy-init="true">

<propertyname="dataSource">

<refbean="dataSource_palife" />

</property>

</bean>

 

<beanid="jdbcTemplate_paf"class="org.springframework.jdbc.core.JdbcTemplate"lazy-init="true">

<propertyname="dataSource">

<refbean="dataSource_paf" />

</property>

</bean>        

 

<beanid="palifeSpringJdbcDbRunner"class="com.pingan.puf.sfap.oms.db.SpringJdbcDbRunner"  lazy-init="true">

<propertyname="jdbcTemplate"><refbean="jdbcTemplate_palife"></ref></property>

</bean>

<beanid="pafSpringJdbcDbRunner"class="com.pingan.puf.sfap.oms.db.SpringJdbcDbRunner"  lazy-init="true">

<propertyname="jdbcTemplate"><refbean="jdbcTemplate_paf"></ref></property>

</bean>

 

@Service

@Lazy(true)

publicclass TransactionService {

 

@Autowired

private Map<String, DbRunner> dbRunnerList;//将所有实现DbRunner的实现类加载到Map

 

privateDbRunner getDefaultDbRunner(){

returndbRunnerList.get("springJdbcDbRunner");

}

 

privateDbRunner getDbRunner(String type){

returndbRunnerList.get(type + "DbRunner");

}

 

publicPayPlatOrderInfo getPayOrderInfo(String tradePkgId) {

if(StringUtils.isEmpty(tradePkgId)) {

returnnull;

}

returngetDefaultDbRunner().findUnique(PayPlatOrderInfo.class,

"select* from T_PAY_ORDER where VC_TRADE_PKG_ID = ? ",

tradePkgId);

}

 

publicString getSweepTradeAccoPalife(String bankacco) {

returngetDbRunner("palifeSpringJdbc").findUnique(String.class,"select vc_tradeacco from tyebbankacco where vc_bankacco=? and c_type =1", bankacco);

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值