6.1 Spring Configuration Management Database Connection Pool Object (key)
Import the jar package
Druid – 1.1.9. Jar mysql connector – Java – 5.1.37 – bin. The jar
Configuration information:
<! - configure the database connection pool - > < bean id = "dataSource" class = "com. Alibaba. Druid. Pool. DruidDataSource" > <! --> <property name="username" value="root"/> <! - password - > < property name = "password" value = "root" / > <! - the connection address - > < property name = "url" value = "JDBC: mysql: / / localhost: 3306 / test" / > <! <property name="driverClassName" value=" com.mysql.jdbc.driver "/> <! --> <property name="initialSize" value="5" /> <! <property name="maxActive" value="10" /> </bean>Copy the code
6.2. Spring introduces a separate jdbc.properties configuration file (emphasis)
Jdbc.properties property configuration file:
url=jdbc:mysql://localhost:3306/book
username=root
password=root
driverClassName=com.mysql.jdbc.Driver
initialSize=5
maxActive=10
Configuration information:
<! - this class is dedicated to the load property configuration file - > < bean class = "org. Springframework. Beans. Factory. Config. Accomplished" > <! -- location property sets the location of the jdbc.properties property configuration file to be loaded: Said classpath load - > < property name = "location" value = "classpath: JDBC. Properties" / > < / bean > <! - configure the database connection pool - > < bean id = "dataSource" class = "com. Alibaba. Druid. Pool. DruidDataSource" > <! - username - > < property name = "username" value = "${username}" / > <! - password - > < property name = "password" value = "${password}" / > <! - the connection address - > < property name = "url" value = "${url}" / > <! Property name="driverClassName" value="${driverClassName}"/> <! Property name="initialSize" value="${initialSize}" /> <! Value ="${maxActive}" /> </bean>Copy the code
6.3 loading the jdbc.properties configuration file using the context namespace (key)
Configure the context namespace:
The configuration information is as follows:
<! - loading classpath classpath of the JDBC. The properties attribute configuration file - > < context: the property - placeholder location = "classpath: JDBC. Properties" / >Copy the code