第一步:创建jdbc.properties配置文件
内容:
jdbc.driver = com.mysql.jdbc.Driver jdbc.url = jdbc:mysql://localhost:3306/test jdbc.username = root jdbc.password = 123456
第二步:SpringMVC配置文件内声明context命名空间
xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
第三步:引入
<!-- 加载properties配置文件 需要context声明-->
<context:property-placeholder location="jdbc.properties"/>
第四步:在xml配置文件中,使用.properties内的键值对数据
即采用el表达式,${key}引用即可
<!-- 配置数据源对象-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- jdbc模板对象-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>