平时开发中我们每天都会跟数据库打交道,页面上显示的数字,图片,语音,等等都存在某个地方,而我们就是要从那个地方拿到我们想要的。现在存储数据的方式越来越多,多种多样,但用的最多的还是关系数据库。Spring 中有多种配置数据源方式,各有优缺点。
(1)DBCP
<dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version>
</dependency>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url"
value="jdbc:mysql://1.1.0.9:3306/dis?useSSL=false"></property>
<property name="username" value="root"></property>
<property name="password" value="123"></property>
<property name="initialSize" value="5"></property>
<property name="maxActive" value="10"></property>
</bean>
(2)DriverManagerDataSource 和 SingleConnectionDataSource
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.0.0.RELEASE</version>
</dependency>
<bean id="dataSource2"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://1.1.0.9:3306/dis"></property>
<property name="username" value="root"></property>
<property name="password" value="123"></property>
</bean>
<bean id="dataSource3"
class="org.springframework.jdbc.datasource.SingleConnectionDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://1.1.0.9:3306/dis"></property>
<property name="username" value="root"></property>
<property name="password" value="123"></property>
</bean>
(3)C3P0
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<bean id="dataSource5" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl"
value="jdbc:mysql://1.1.0.9:3306/dis?useSSL=false"></property>
<property name="user" value="root"></property>
<property name="password" value="123"></property>
<property name="minPoolSize" value="5" />
<property name="maxPoolSize" value="20" />
<property name="initialPoolSize" value="10" />
<property name="maxIdleTime" value="60" />
<property name="idleConnectionTestPeriod" value="60" />
<property name="acquireIncrement" value="5" />
<property name="acquireRetryAttempts" value="30" />
</bean>
(4)DRUID
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.19</version>
</dependency>
<bean id="dataSource1" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url"
value="jdbc:mysql://1.1.0.9:3306/dis?useSSL=false"></property>
<property name="username" value="root"></property>
<property name="password" value="abcd123"></property>
<property name="filters" value="stat" />
<property name="maxActive" value="20" />
<property name="initialSize" value="2" />
<property name="maxWait" value="60000" />
<property name="minIdle" value="10" />
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<property name="minEvictableIdleTimeMillis" value="300000" />
<property name="validationQuery" value="SELECT '1'" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<property name="maxOpenPreparedStatements" value="100" />
</bean>