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

被折叠的 条评论
为什么被折叠?



