1.所需要jar包:
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- 配置DispatcherServlet -->
<servlet>
<servlet-name>commServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 手動配置加載的配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/spring/*.xml</param-value>
</init-param>
<!-- 項目啓動時加載 -->
<load-on-startup>1</load-on-startup>
</servlet>
<!-- servlet映射 -->
<servlet-mapping>
<!-- 所有.do結尾的請求都由commServlet管理 -->
<servlet-name>commServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- 配置編碼過濾器 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<!-- 初始化參數,編碼是UTF-8 -->
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<!-- 過濾器映射 -->
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 登錄過濾 -->
<filter>
<filter-name>loginFilter</filter-name>
<filter-class>com.cjh.filter.LoginFilter</filter-class>
</filter>
<!-- 登錄過濾映射 -->
<filter-mapping>
<?xml version="1.0" encoding="UTF-8" ?>
Spzzing MVC:
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<!-- ==================主鍵掃描,配置要掃描的Java類============================ -->
<context:component-scan base-package="com.cjh" />
<!-- ==================啓動Spring MVC的註解功能,完成請求和註解POJO的映射,配置一個基於註解的定製的WebBindingInitializer,
解決日期轉換問題,方法級別的處理器映射============================ -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="cacheSeconds" value="0" />
<!-- 配置一下對json數據的轉換 -->
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
</list>
</property>
</bean>
<!-- ==================定義一個視圖解析類,基於ResourceView的解析器 ,此解析器是在url解析器上,
加入了對jstl的支持============================ -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前綴 -->
<!-- <property name="prefix" value="/jsp/" /> -->
<!-- 後綴 -->
<property name="suffix" value=".jsp" />
</bean>
<!-- ================== 處理文件上傳 ============================ -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 默認編碼 (ISO-8859-1) -->
<property name="defaultEncoding" value="utf-8" />
<!-- 最大內存大小 (10240)-->
<property name="maxInMemorySize" value="10240" />
<!-- 上傳後的臨時目錄名 (WebUtils#TEMP_DIR_CONTEXT_ATTRIBUTE) -->
<property name="uploadTempDir" value="/upload_temp/" />
<!-- 最大文件大小,-1爲無限止(-1),注意maxUploadSize屬性的限制不是針對單個文件,而是所有文件的容量之和 -->
<property name="maxUploadSize" value="-1" />
</bean>
<!-- ================== SpringMVC在超出上傳文件限制時,會拋出org.springframework.web.multipart.MaxUploadSizeExceededException
該異常是SpringMVC在檢查上傳的文件信息時拋出來的,而且此時還沒有進入到Controller方法中 ============================ -->
<bean id="exceptionResolver"
class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<!-- 遇到MaxUploadSizeExceededException異常時,自動跳轉到manage/view/fileError.jsp頁面 -->
<prop
key="org.springframework.web.multipart.MaxUploadSizeExceededException">
manage/view/fileError
</prop>
</props>
</property>
</bean>
</beans>
Spring:
<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!-- ===================使用 annotation============================== -->
<context:annotation-config />
<!-- ===================hibernate屬性配置============================ -->
<context:property-placeholder location="classpath:hibernate/hibernate.properties" />
<!-- ===================C3P0 數據源配置=============================== -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 驅動 -->
<property name="driverClass" value="${c3p0.driver_class}" />
<!-- 數據庫連接url -->
<property name="jdbcUrl" value="${c3p0.url}" />
<!-- 用戶名 -->
<property name="user" value="${c3p0.username}"/>
<!-- 密碼 -->
<property name="password" value="${c3p0.password}"/>
<!-- 設置數據庫連接池的最大連接數 -->
<property name="maxPoolSize" value="${c3p0.maxPoolSize}"/>
<!-- 設置數據庫連接池的最小連接數 -->
<property name="minPoolSize" value="${c3p0.minPoolSize}"/>
<!-- 設置數據庫連接池的初始化連接數 -->
<property name="initialPoolSize" value="${c3p0.initialPoolSize}"/>
<!-- 設置數據庫連接池的連接的最大空閒時間,單位爲秒 -->
<property name="maxIdleTime" value="${c3p0.maxIdleTime}"/>
</bean>
<!-- ==================SessionFactory配置============================ -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<!-- 配置dataSource屬性 -->
<property name="dataSource" ref="dataSource" />
<!-- 掃描的包 -->
<property name="packagesToScan" value="com.cjh.model" />
<!-- 配置hibernate屬性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
${hibernate.dialect}
</prop>
<prop key="hibernate.show_sql">
${hibernate.show_sql}
</prop>
<prop key="hibernate.format_sql">
${hibernate.format_sql}
</prop>
<prop key="hibernate.hbm2ddl.auto">
${hibernate.hbm2ddl.auto}
</prop>
<prop key="hibernate.useUnicode">
${hibernate.useUnicode}
</prop>
<prop key="hibernate.characterEncoding">
${hibernate.characterEncoding}
</prop>
</props>
</property>
</bean>
<!-- ==================配置hibernateTemplate=========================
<bean id="hibernateTemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>-->
<!-- ==================配置事務管理==================================== -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- ==================配置註解實現管理事務(cglib:proxy-target-class="true")================= -->
<tx:annotation-driven transaction-manager="transactionManager"
proxy-target-class="true" />
</beans>
hibernate.priperties:
## C3P0配置
c3p0.driver_class=com.mysql.jdbc.Driver
c3p0.url=jdbc:mysql://127.0.0.1:3306/my
c3p0.username=root
c3p0.password=root
c3p0.maxPoolSize=100
c3p0.minPoolSize=3
c3p0.initialPoolSize=3
c3p0.maxIdleTime=100
## hibernate屬性配置
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hibernate.format_sql=true
hibernate.hbm2ddl.auto=update
hibernate.useUnicode=true
hibernate.characterEncoding=utf-8
## hibernate緩存配置
#hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
#hibernate.cache.use_query_cache=false
#hibernate.cache.use_second_level_cache=true
BaseDao代碼:
import javax.annotation.Resource;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
/**
* 公共類
*
* @author Only
*
*/
public class BaseDao {
@Resource
private SessionFactory sessionFactory;
/**
* 獲得session
* @return
*/
public Session getCurrentSession() {
return sessionFactory.openSession();
}
/**
* 關閉session
* @param session
*/
public void closeSession(Session session) {
try {
if (null != session) {
session.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}