【spring mvc(二)】spring mvc使用屬性檔案配置c3p0和dbcp資料來源
spring mvc如果要用到資料庫,就要為專案配置資料來源,目前有兩個比較常見的資料來源:c3p0與dbcp。這兩個都是連線池技術,連線池的概念大概是,維護若干個資料庫的連線,程式需要使用的時候直接返回給它,這樣做的用意是降低資料庫連線,關閉的開銷,如果每次程式請求資料庫都進行連線操作,那麼代價會很大,取而代之的做法是先準備好若干個連線,用的時候返回即可。
1.web.xml:
web.xml的listener主要是配置spring 容器,通過<context-param>來指定spring配置檔案的位置,預設是web-inf下的applicatinContext.xml<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <context-param> <param-name>contextConfigLocation</param-name> <!-- 應用上下文配置檔案 --> <param-value>/WEB-INF/config/spring.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置spring核心servlet --> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/config/spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
2.屬性檔案jdbc.properties:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=
這個properties檔案本質上是一個map,等號左邊是key,右邊是value。
url項的格式是固定的,必須有jdbc:mysql才行,後面跟資料庫的url,後面加上引數是一個好的習慣,引數的意義在於如果專案編碼是utf-8而底層資料庫是gbk,那麼存資料的時候先把utf-8資料解碼,轉成gbk再存,取資料過程相反。
至於指定這個檔案的位置在後面。
3.spring配置檔案spring.xml:
這個是spring容器的配置檔案,裡面是spring要管理的bean,當然有關資料來源的資訊就可以配在這裡面,有的時候為了更好的維護或者安全考慮,會把資料庫的資訊另外存放在檔案中,比如上面的jdbc.properties。那麼需要配置propertyConfiger,指定配置檔案的位置資訊。
值得一說的是c3p0和dbcp資料來源的配置資訊是不同的,查表有兩點,要格外注意,否則資料來源連線不上。
(1),datasource的class不同,這個比較明顯,不說了。
(2),<property>名字不同,比如dbcp使用driverClassName而c3p0使用driverClass,dbcp用username而c3p0用user,這個property name屬性是定義死的,是連線池實現類的屬性,如果名字錯了,spring無法注入屬性,因此要格外注意。
下面是dbcp的配置資訊:
<?xml version="1.0" encoding="UTF-8"?>
<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-3.0.xsd">
<!--引入jdbc配置檔案 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>/WEB-INF/config/jdbc.properties</value>
</list>
</property>
<property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>
<!-- dataSource 配置 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName">
<value>${jdbc.driver}</value>
</property>
<property name="url">
<value>${jdbc.url}</value>
</property>
<property name="username">
<value>${jdbc.username}</value>
</property>
<property name="password">
<value>${jdbc.password}</value>
</property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" abstract="false"
lazy-init="false" autowire="default" >
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
</beans>
下面是c3p0的配置資訊:
<?xml version="1.0" encoding="UTF-8"?>
<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-3.0.xsd">
<!--引入jdbc配置檔案 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>/WEB-INF/config/jdbc.properties</value>
</list>
</property>
<property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>
<!-- dataSource 配置 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass">
<value>${jdbc.driver}</value>
</property>
<property name="jdbcUrl">
<value>${jdbc.url}</value>
</property>
<property name="user">
<value>${jdbc.username}</value>
</property>
<property name="password">
<value>${jdbc.password}</value>
</property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" abstract="false"
lazy-init="false" autowire="default" >
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
</beans>
配置好以後,再配置一個jdbctemplate即可。
4.spring mvc的配置檔案,spring-mvc.xml:
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<!-- 啟用spring mvc 註解 -->
<context:annotation-config />
<!-- 自動掃描的包名 -->
<context:component-scan base-package="com.test"/>
<!-- 預設的註解對映的支援,自動註冊DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter -->
<mvc:annotation-driven />
<!-- 檢視解釋類 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
</bean>
<!-- 對靜態資原始檔的訪問-->
<!-- <mvc:resources mapping="/images/**" location="/WEB-INF/images/" cache-period="31556926"/> -->
<!-- <mvc:resources mapping="/js/**" location="/WEB-INF/js/" cache-period="31556926"/> -->
<!-- <mvc:resources mapping="/css/**" location="/WEB-INF/css/" cache-period="31556926"/> -->
</beans>
主要是加了註解功能和檢視解析。
5.controller:
package com.test;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class TestController {
@Autowired
private JdbcTemplate jdbcTemplate;
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@RequestMapping(value="/index.htm")
public String test(){
String sql = "SELECT * FROM user";
List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
for(int i=0;i<list.size();i++){
System.out.println(list.get(i).get("first"));
}
return "index";
}
}
我的專案設定了web context root 為"/",直接http://localhost:8080/index.htm,就可以訪問。如果沒設定web context root ,可能訪問方式不一樣。原始碼下載:http://pan.baidu.com/s/1pKqZWvh