您的位置:首页 > 编程语言 > Java开发

为Spring配置JDBC

2012-12-20 11:34 176 查看

首先在配置文件中定义

<bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>
                    file:${user.dir}/conf/config.properties
                </value>
            </list>
        </property>
    </bean>

配置文件


<bean id="xmsDataSource"
        class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close">
        <property name="driverClass" value="oracle.jdbc.OracleDriver" />
        <property name="jdbcUrl" value="${xmsDataSource.url}" />
        <property name="user" value="${xmsDataSource.username}" />
        <property name="password" value="${xmsDataSource.password}" />

Spring 的JDBC对象

<bean id="appJdbcTemplate"
        class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource">
            <ref bean="appDataSource" />
        </property>
    </bean>


在需要JDBC的对象上配置

<bean id="heartbeatUtil"
        class="com.techown.smplatform.util.HeartbeatUtil">
        <property name="xmsJdbcTemplate" ref="xmsJdbcTemplate"></property>
    </bean>


使用update或者insert  等方法时候,需要指定  已经实现 PreparedStatementSetter 的对象。,如果是集合则需要实现BatchPreparedStatementSetter和getBatchSize()方法,getBatchSize()方法中传参是传入集合的大小,返回是int类型。实现query查询的时候,需要实现RowMapper 类。返回的就是一个集合。




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: