如题 我现在打算使用dbcp作为数据源 在配置完成之后 该怎么在程序中具体的使用呢?
这是配置xml<?xml version="1.0" encoding="UTF-8"?>
<beans>
<bean id="propertyConfigurer"          
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">           
    <property name="location" value="classpath:conf/jdbc.peoperties"/>           
</bean>           
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">           
    <property name="driverClassName" value="${jdbc.driverClassName}" />           
    <property name="url" value="${jdbc.url}" />           
    <property name="username" value="${jdbc.username}" />           
    <property name="password" value="${jdbc.password}" />  
    <property name="maxActive" value="100"/> 
    <property name="maxIdle" value="20"/> 
    <property name="maxWait" value="1000"/> 
</bean>
<bean id= "transactionManager" 
class= "org.springframework.jdbc.datasource.DataSourceTransactionManager" > 
        <property name= "dataSource" > 
            <ref bean= "dataSource" /> 
        </property> 
</bean> 
       
</beans><?xml version="1.0" encoding="UTF-8"?>
<beans>
<bean id="propertyConfigurer"          
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">           
    <property name="location" value="classpath:conf/jdbc.peoperties"/>           
</bean>           
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">           
    <property name="driverClassName" value="${jdbc.driverClassName}" />           
    <property name="url" value="${jdbc.url}" />           
    <property name="username" value="${jdbc.username}" />           
    <property name="password" value="${jdbc.password}" />  
    <property name="maxActive" value="100"/> 
    <property name="maxIdle" value="20"/> 
    <property name="maxWait" value="1000"/> 
</bean>
<bean id= "transactionManager" 
class= "org.springframework.jdbc.datasource.DataSourceTransactionManager" > 
        <property name= "dataSource" > 
            <ref bean= "dataSource" /> 
        </property> 
</bean> 
       
</beans>
之后要怎么使用呢?

解决方案 »

  1.   

    配下jdbcTemplate 跟hibernateTemplate用法一样
      

  2.   

    配置的时候直接可以生成dao的非常好用祝楼主好运
      

  3.   

    给你个查询集合的例子吧
    @SuppressWarnings("unchecked")
    public List<ProMoney> findProMoner(String month) throws Exception {
    String sql = "select pro_id,sum(AMOUNTSMALL) money from tbl_fms_bill_details  where checkmonth = '"+month+"' group by pro_id";
    List<ProMoney> pms =this.getJdbcTemplate().query(sql,new RowMapper(){
    public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
    ProMoney pm = new ProMoney();
    pm.setProid(rs.getInt("PRO_ID"));
    pm.setMoney(rs.getString("money"));
    return pm;
    }
    });
    if(pms.isEmpty()){
    return null;
    }
    return pms;
    }
      

  4.   

    给Hibernate 都差不多  JdbcTemplate 这样用起来方便
      

  5.   

    我只想要dbcp的连接池功能 jdbcTemplate部分的代码要自己组织 就我上面的配置 怎么样拿到connection?我现在的方法总是报空指针错误   private BasicDataSource dataSource;    
        private static final Logger logger = Logger.getLogger(Db.class.getClass());
    public BasicDataSource getDataSource() {
    return dataSource;
    }

    public void setDataSource(BasicDataSource dataSource) {
    this.dataSource = dataSource;
    }

    public Connection getConnection() throws Exception {
    return dataSource.getConnection();

    }这是代码的一部分 取不到连接