JdbcTemplate事务操作:
<bean id="dataSource"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="数据源JNDI名字" />//weblogic
</bean>
<bean id="sysCommon" class="..SysCommon"
factory-method="getInstance">
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
或者:
<bean id="xxdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
JdbcDaoSupport:已有属性org.springframework.jdbc.core.JdbcTemplate jdbcTemplate;
public final class SysCommon extends JdbcDaoSupport {
private static SysCommon sysDb = new SysCommon();
public static SysCommon getInstance() {
if (SysCommon == null) {
SysCommon = new SysCommon();
}
return SysCommon;
}
}
获得:
JdbcTemplate template = SysCommon.getInstance().getJdbcTemplate();
final Long id = (Long) list.get(0);//传入参数值
Object obj = template.execute(new ConnectionCallback(){
//执行存储过程
public Object doInConnection(Connection conn) throws SQLException, DataAccessException {
CallableStatement cstmt = conn.prepareCall("{ call 存储过程所在包.存储过程名(?,?,?,?) }");
cstmt.setLong(1, id.longValue());
cstmt.registerOutParameter(2, Types.VARCHAR);
cstmt.registerOutParameter(3, Types.VARCHAR);
cstmt.registerOutParameter(4, Types.VARCHAR);
cstmt.execute();
String id = cstmt.getString(2);// id
String name = cstmt.getString(3);
List rtn = new ArrayList();
if(id != null && id.length()>0){
rtn.add(id);
rtn.add(name);
}
cstmt.close();
return rtn;
}
});