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

spring之jdbc的DataSource的设置

2017-08-04 11:33 316 查看
(一)DataSource的常规设置

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> 
<bean id="customerDAO" class="com.yiibai.customer.dao.impl.JdbcCustomerDAO">

<property name="dataSource" ref="dataSource" />
</bean>

<bean id="customerSimpleDAO" class="com.yiibai.customer.dao.impl.SimpleJdbcCustomerDAO">

<property name="dataSource" ref="dataSource" />
</bean>

<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">

<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/yiibaijava" />
<property name="username" value="root" />
<property name="password" value="password" />
</bean>

</beans>

(二)通过属性文件连接使用PropertyPlaceholderConfigurer 类

创建一个属性文件(database.properties),包括数据库的详细信息,把它放到你的项目类路径。
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/yiibai_db
jdbc.username=root
jdbc.password=123456

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> 
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

<property name="location">
<value>database.properties</value>
</property>
</bean>

<bean id="customerDAO" class="com.yiibai.customer.dao.impl.JdbcCustomerDAO">

<property name="dataSource" ref="dataSource" />
</bean>

<bean id="customerSimpleDAO"
class="com.yiibai.customer.dao.impl.SimpleJdbcCustomerDAO">

<property name="dataSource" ref="dataSource" />
</bean>

<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">

<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>

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