maven与springMVC之数据库dbcp、c3p0、bonecp

前面几篇文章已经基本把maven与springMVC组合的框架说清楚了,现在欠缺的就是和数据库链接方面的介绍了。
首先需要引入dbcp、c3p0、bonecp以及mysql的依赖,如下:
 <span style="white-space:pre">		</span><dependency>
			<groupId>commons-dbcp</groupId>
			<artifactId>commons-dbcp</artifactId>
			<version>1.4</version>
		</dependency>


		<dependency>
			<groupId>c3p0</groupId>
			<artifactId>c3p0</artifactId>
			<version>0.9.1.2</version>
		</dependency>


		<dependency>
			<groupId>com.jolbox</groupId>
			<artifactId>bonecp</artifactId>
			<version>0.8.0.RELEASE</version>
		</dependency>


		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.25</version>
		</dependency>
然后准备jdbc的配置文件jdbc.properties,内容如下:
#sqlserver 
#jdbc.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver  
#jdbc.url=jdbc:sqlserver://192.168.1.60:1408;DatabaseName=test  
#jdbc.username=sa  
#jdbc.password=1111  
  
#mysql 
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=root
  
#Oracle 
#jdbc.driverClassName=oracle.jdbc.driver.OracleDriver  
#jdbc.url=jdbc:oracle:thin:@10.60.17.9:1521:orcl  
#jdbc.username=admin  
#jdbc.password=admin  

jdbc.properties文件位置如下:


现在可以在spring-mvc.xml中配置数据库了:

<!-- 配置jdbc.properties的路径 -->
	<bean id="propertyConfigurer"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>classpath:jdbc.properties</value>
			</list>
		</property>
	</bean>


	<!-- 配置dbcp -->
	<!-- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
		destroy-method="close"> 
		<property name="driverClassName" value="${jdbc.driverClassName}" /> 
		<property name="url" value="${jdbc.url}" /> 
		<property name="username" value="${jdbc.username}" /> 
		<property name="password" value="${jdbc.password}"/> 
		<property name="maxActive" value="30" /> 
		<property name="maxIdle" value="10" /> 
		<property name="maxWait" value="1000" /> 
		<property name="defaultAutoCommit" value="true" /> </bean> -->


	<!-- 配置c3p0 -->
	<!-- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
		destroy-method="close">
		<property name="driverClass" value="${jdbc.driverClassName}" />
		<property name="jdbcUrl" value="${jdbc.url}" />
		<property name="user" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		
		<property name="minPoolSize" value="10" />
		<property name="maxPoolSize" value="100" />
		<property name="maxIdleTime" value="1800" />
		<property name="acquireIncrement" value="3" />
		<property name="maxStatements" value="1000" />
		<property name="initialPoolSize" value="10" />
		<property name="idleConnectionTestPeriod" value="60" />
		<property name="acquireRetryAttempts" value="30" />
		<property name="breakAfterAcquireFailure" value="true" />
		<property name="testConnectionOnCheckout" value="false" />
	</bean>-->


	<!-- 配置bonecp -->
	<bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource"
		destroy-method="close">
		<property name="driverClass" value="${jdbc.driverClassName}" />
		<property name="jdbcUrl" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		
		<property name="idleConnectionTestPeriod" value="60"/>
	    	<property name="idleMaxAge" value="240"/>
	    	<property name="maxConnectionsPerPartition" value="30"/>
	    	<property name="minConnectionsPerPartition" value="10"/>
	    	<property name="partitionCount" value="3"/>
	    	<property name="acquireIncrement" value="5"/>
	    	<property name="statementsCacheSize" value="100"/>
	    	<property name="releaseHelperThreads" value="3"/>
	</bean>


	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> 
		<property name="dataSource" ref="dataSource" />
	</bean>


	<bean id="userDao" class="com.wangpeng.testmodule.dao.UserDao">
		<property name="jdbcTemplate" ref="jdbcTemplate" />
	</bean>


	<bean id="springController" class="com.wangpeng.testmodule.controller.SpringController">
		<property name="userDao" ref="userDao" />
	</bean>
看一下UserDao类,上面的配置就相当于调用setDataSource方法,并且把DataSource dataSource参数传入。
public class UserDao {
	private JdbcTemplate jdbcTemplate;


	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		System.out.println("setJdbcTemplate--->");
		this.jdbcTemplate = jdbcTemplate;
	}
	
	public Map getUserInfo(){
		Map map = jdbcTemplate.queryForMap("select * from user");  
		return map;
	}


}
再看一下SpringController类添加了如下代码
private UserDao userDao;
	public UserDao getUserDao() {
		return userDao;
	}


	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}


	@RequestMapping("testA")
	public ModelAndView testA() {
		System.out.println("testControl/testA-----new bonecp------->");
		Map<String, String> map = new HashMap<String, String>();


		//请求数据并传到视图中展示,mapUser.get("name")就是获取数据库name字段的数据
		Map mapUser =getUserDao().getUserInfo();
		map.put("message", "testA:"+mapUser.get("name"));


		ModelAndView modelView = new ModelAndView("md_index");
		modelView.addAllObjects(map);
		
		return modelView;
	}

然后部署测试就可以看到页面上成功获取到数据库中数据并展示。



你可能感兴趣的:(spring,maven,mvc,c3p0,DBCP,bonecp)