SPRING-JDBC多个数据源



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

}

展开阅读全文

没有更多推荐了,返回首页