spring之jdbc的DataSource的设置
2017-08-04 11:33
316 查看
(一)DataSource的常规设置
(二)通过属性文件连接使用PropertyPlaceholderConfigurer 类
创建一个属性文件(database.properties),包括数据库的详细信息,把它放到你的项目类路径。
<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>
相关文章推荐
- java.lang.ClassNotFoundException: org.springframework.jdbc.datasource.DriverManagerDataSource
- SPRING IN ACTION 第4版笔记-第十章Hitting the database with spring and jdbc-003-四种方式获取DataSource
- Spring配置DataSource时数据库用户名设置名称要注意
- 在spring配置文件中设置读取jdbc.properties配置文件的时候报错
- 再淡spring jdbc 连接池断开重连设置
- day03-spring与数据库操作的框架解析即dataSource的获取(JDBC)、使用spring中已经有的事务进行JDBC操作
- Spring配置jdbc.properties时数据库用户名设置名称要注意
- Error creating bean with name 'org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfigurati
- Spring代码实例系列-08:Spring JDBC + DataSource +JdbcTemplate+JdbcDaoSupport的基本实例
- Already value org.springframework.jdbc.datasource.ConnectionHolder报错
- Spring JDBC环境设置
- Spring配置错误java.lang.NoClassDefFoundError: org/springframework/jdbc/datasource/TransactionAwareDataS
- Error:NoSuchMethodError: org.springframework.jdbc.datasource.DataSourceUtils.isConnectionTransaction
- spring — jdbc 配置文件的设置
- java.lang.NoClassDefFoundError: org/springframework/jdbc/datasource/TransactionAwareDataSourceProxy
- 关于mybatis集成spring时,dataSource设置propertyPlaceholder的问题
- Spring学习-25:Spring中的JDBC Template(JDBC模板):设置参数到属性文件
- java.lang.ClassNotFoundException: org.springframework.jdbc.datasource.DriverManagerDataSource
- mybatis与spring整合报错NoClassDefFoundError: org/springframework/jdbc/datasource/TransactionAwareDataSour
- 再淡spring jdbc 连接池断开重连设置