SPRING-JDBC多个数据源
来源:互联网 发布:淘宝迅雷会员卡密 编辑:程序博客网 时间:2024/05/09 12:25
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
privateMap<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-JDBC多个数据源
- spring jdbc 使用多个数据源
- spring+ibatis 配置多个数据源,jdbc 和 jndi 实现
- Spring 配置多个数据源
- Spring配置多个数据源
- spring配置多个数据源
- Spring连接多个数据源
- Spring 配置多个数据源
- Spring数据源Spring数据源配置之JDBC
- Spring的数据源JDBC配置
- Spring 基于JDBC的数据源
- Spring 的 jdbc 数据源配置
- spring的profile定义多个数据源
- 使用spring配置多个数据源
- spring 配置多个数据源(基于hibernate)
- spring 配置多个数据源(基于JPA)
- spring + mybatis配置多个数据源
- spring 配置多个数据源(基于JPA)
- 微信小程序开发—(十三)可滚动视图区域 scroll-view
- java子父类初始化顺序
- mvvm设计模式之Data Binding库的使用
- 我的python学习手册1
- 通过usb线ssh连接iPhone
- SPRING-JDBC多个数据源
- MySQL中有关索引组织表的概念分析
- 以太坊开发资源
- 拖放操作之——dataTransfer对象
- jquery.param()实现数组或对象的序列化
- jsp页面没有刷新出来,提示我数据没匹配上
- linux实现crontab定时任务计划并删除之前的文件备份
- MySQL数据库学习日志5.16
- value &#