第一步:引入jdbc的包(IDEA常用的会已经给你引入)
第二步:配置数据源
第一种方式:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" > <!--数据源配置--> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/spring10"></property> <property name="username" value="root"/> <property name="password" value="root"/> <!-- 初始化的连接数--> <property name="initialSize" value="1"/> <!-- 连接池的最大连接数--> <property name="maxActive" value="5"/> <!-- 最大空闲值的连接数--> <property name="maxIdle" value="2"/> <!-- 最小的空闲连接数--> <property name="minIdle" value="1"/> </bean> </beans>
spring属性文件读取
第二种方式:
1.创建properties文件
driverClassName=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/spring10 uname=root pword=123 initialSize=1 maxActive=5 maxIdle=2 minIdle=12.配置文件
<!-- Spring读取属性文件配置--> <context:property-placeholder location="classpath:jdbc.prpperties"/> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${driverClassName}"></property> <property name="url" value="${url}"></property> <property name="username" value="${uname}"/> <property name="password" value="${pword}"/> <!-- 初始化的连接数--> <property name="initialSize" value="${initialSize}"/> <!-- 连接池的最大连接数--> <property name="maxActive" value="${maxActive}"/> <!-- 最大空闲值的连接数--> <property name="maxIdle" value="${maxIdle}"/> <!-- 最小的空闲连接数--> <property name="minIdle" value="${minIdle}"/> </bean>
