1.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
? ? ? ? xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
? ? ? ? xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
? ? ? ? version="3.1">
? <display-name>Archetype Created Web Application</display-name>
? <!-- 中文亂碼過(guò)濾 -->
? <filter>
? ? ? ? <filter-name>CharacterEncodingFilter</filter-name>
? ? ? ? <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
? ? ? ? <init-param>
? ? ? ? ? ? <param-name>encoding</param-name>
? ? ? ? ? ? <param-value>UTF-8</param-value>
? ? ? ? </init-param>
? ? </filter>
? ? <filter-mapping>
? ? ? ? <filter-name>CharacterEncodingFilter</filter-name>
? ? ? ? <url-pattern>/*</url-pattern>
? ? </filter-mapping>
? ? <!--配置SpringMVC的前端控制器(核心控制器)-->
? ? <servlet>
? ? ? ? <servlet-name>dispatcherServlet</servlet-name>
? ? ? ? <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
? ? ? ? <!--配置初始化參數(shù),用于讀取SpringMVC配置文件(指定配置文件的位置),使得dispatcherServlet被創(chuàng)建時(shí),就加載配置文件,初始化Spring容器-->
? ? ? ? <init-param>
? ? ? ? ? ? <param-name>contextConfigLocation</param-name>
? ? ? ? ? ? <param-value>classpath:spring-mvc.xml</param-value>
? ? ? ? </init-param>
? ? ? ? <!--設(shè)置DispatcherServlet控制器,在服務(wù)器啟動(dòng)(應(yīng)用加載)的時(shí)候創(chuàng)建對(duì)象,取值只能是非0正整數(shù),表示啟動(dòng)順序,數(shù)字越小優(yōu)先級(jí)越高-->
? ? ? ? <load-on-startup>1</load-on-startup>
? ? </servlet>
? ? <!--配置dispatcherServlet的映射路徑-->
? ? <servlet-mapping>
? ? ? ? <servlet-name>dispatcherServlet</servlet-name>
? ? ? ? <!--把dispatcherServlet設(shè)置成? 默認(rèn)的缺省處理器 (覆蓋Tomcat的默認(rèn)處理器)-->
? ? ? ? <url-pattern>*.do</url-pattern>
? ? </servlet-mapping>
? ? <!-- 配置加載Spring文件的監(jiān)聽(tīng)器-->
? ? <context-param>
? ? ? ? <param-name>contextConfigLocation</param-name>
? ? ? ? <param-value>classpath:applicationContext.xml</param-value>
? ? </context-param>
? ? <listener>
? ? ? ? <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
? ? </listener>
<!-- 配置log4j配置文件路徑 -->
? ? <context-param>
? ? ? ? <param-name>log4jConfigLocation</param-name>
? ? ? ? <param-value>classpath:log4j.properties</param-value>
? ? </context-param>
? ? <!-- 配置Log4j監(jiān)聽(tīng)器 -->
? ? <listener>
? ? ? ? <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
? ? </listener>
? ? <error-page>
? ? ? <error-code>404</error-code>
? ? ? <location>/WEB-INF/errors/404.jsp</location>
? ? </error-page>
? ? <error-page>
? ? ? <error-code>500</error-code>
? ? ? <location>/WEB-INF/errors/500.jsp</location>
? ? </error-page>
<welcome-file-list>
? ? ? ? <welcome-file>index.jsp</welcome-file>
? ? </welcome-file-list>
</web-app>
2.spring-mybatis.xml
<?xml version="1.0" encoding="UTF-8"?>
<xml-body>
<beans xmlns="http://www.springframework.org/schema/beans"? ?
? ? xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"? ?
? ? 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-4.1.xsd? ? ?
? ? ? ? ? ? ? ? ? ? ? ? http://www.springframework.org/schema/context? ? ?
? ? ? ? ? ? ? ? ? ? ? ? http://www.springframework.org/schema/context/spring-context-4.0.xsd? ? ?
? ? ? ? ? ? ? ? ? ? ? ? http://www.springframework.org/schema/mvc? ? ?
? ? ? ? ? ? ? ? ? ? ? ? http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">? ?
? ? <!-- 自動(dòng)掃描 放進(jìn)容器-->? ?
? ? <context:component-scan base-package="com.market" >
? ? ? <context:include-filter type="annotation" expression="org.springframework.steretype.Component"/>? ?
? ? ? <context:include-filter type="annotation" expression="org.springframework.steretype.Repository"/>
? ? ? <context:include-filter type="annotation" expression="org.springframework.steretype.Service"/>
? ? </context:component-scan>
? ? <!--? 加載配數(shù)據(jù)源配置文件jdbc.properties-->
? ? <context:property-placeholder ignore-unresolvable="true" location="classpath:jdbc.properties" />?
? ? <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init"?
? ? ? ? destroy-method="close">? ?
? ? ? ? <property name="driver" value="com.mysql.jdbc.Driver"/>
? ? ? ? <property name="url" value="jdbc:mysql://localhost:3306/dongfeng?serverTimezone=GMT"/>
? ? ? ? <property name="username" value="root"/>
? ? ? ? <property name="password" value="19980420"/>
? ? ? ? <!-- 初始化連接大小 -->? ?
? ? ? ? <property name="initialSize" value="${jdbc.initialSize}"></property>? ?
? ? ? ? <!-- 連接池最大數(shù)量 -->? ?
? ? ? ? <property name="maxActive" value="${jdbc.maxActive}"></property>? ?
? ? ? ? <!-- 連接池最大空閑 -->? ?
? ? ? ? <property name="maxIdle" value="${jdbc.maxIdle}"></property>? ?
? ? ? ? <!-- 連接池最小空閑 -->? ?
? ? ? ? <property name="minIdle" value="${jdbc.minIdle}"></property>? ?
? ? ? ? <!-- 獲取連接最大等待時(shí)間 -->? ?
? ? ? ? <property name="maxWait" value="${jdbc.maxWait}"></property>? ?
? ? </bean>? ?
? ? <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->? ?
? ? <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">? ?
? ? ? ? <property name="dataSource" ref="dataSource" />? ?
? ? ? ? <!-- 自動(dòng)掃描mapping.xml文件 -->? ?
? ? ? ? <property name="mapperLocations" value="classpath:com.market/mapping/*.xml"></property>? ?
? ? </bean>? ?
? ? <!-- DAO接口所在包名,Spring會(huì)自動(dòng)查找其下的類 -->? ?
? ? <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">? ?
? ? ? ? <property name="basePackage" value="com.cn.hnust.dao" />? ?
? ? ? ? <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>? ?
? ? </bean>? ?
? ? <!-- (事務(wù)管理)transaction manager, use JtaTransactionManager for global tx -->? ?
? ? <bean id="transactionManager"? ?
? ? ? ? class="org.springframework.jdbc.datasource.DataSourceTransactionManager">? ?
? ? ? ? <property name="dataSource" ref="dataSource" />? ?
? ? </bean>? ?
</beans>? ?
? ? </xml-body>
3.spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<xml-body>
<beans xmlns="http://www.springframework.org/schema/beans"
? ? ? xmlns:mvc="http://www.springframework.org/schema/mvc"
? ? ? xmlns:context="http://www.springframework.org/schema/context"
? ? ? 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.xsd
? ? ? ? http://www.springframework.org/schema/mvc
? ? ? ? http://www.springframework.org/schema/mvc/spring-mvc.xsd
? ? ? ? http://www.springframework.org/schema/context
? ? ? ? http://www.springframework.org/schema/context/spring-context.xsd">
? ? <!--開(kāi)啟注解掃描,只掃描Controller注解-->
? ? <context:component-scan base-package="com.market.controller">
? ? ? ? <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
? ? </context:component-scan>
? ? <!-- 啟動(dòng)mvc 注解驅(qū)動(dòng) -->
? ? <mvc:annotation-driven></mvc:annotation-driven>
? ? <!-- 靜態(tài)資源處理 -->
? ? <mvc:default-servlet-handler/>
? ? <!-- 視圖解釋器-->? ?
? ? <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">? ?
? ? ? ? <!-- 設(shè)置前綴 -->
? ? ? ? <property name="prefix" value="/WEB-INF/jsp/" />?
? ? ? ? <!-- 設(shè)置后綴 -->
? ? ? ? <property name="suffix" value=".jsp" />? ?
? ? </bean>?
? ? <!--避免IE執(zhí)行AJAX時(shí),返回JSON出現(xiàn)下載文件 -->? ?
? ? <bean id="mappingJacksonHttpMessageConverter"? ?
? ? ? ? class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">? ?
? ? ? ? <property name="supportedMediaTypes">? ?
? ? ? ? ? ? <list>? ?
? ? ? ? ? ? ? ? <value>text/html;charset=UTF-8</value>? ?
? ? ? ? ? ? </list>? ?
? ? ? ? </property>? ?
? ? </bean>? ?
? ? <!-- 啟動(dòng)SpringMVC的注解功能,完成請(qǐng)求和注解POJO的映射 -->? ?
? ? <bean? ?
? ? ? ? class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">? ?
? ? ? ? <property name="messageConverters">? ?
? ? ? ? ? ? <list>? ?
? ? ? ? ? ? ? ? <ref bean="mappingJacksonHttpMessageConverter" /> <!-- JSON轉(zhuǎn)換器 -->? ?
? ? ? ? ? ? </list>? ?
? ? ? ? </property>? ?
? ? </bean>? ?
? ? <!-- 配置文件上傳 -->? ?
? ? <bean id="multipartResolver"? ? ?
? ? ? ? class="org.springframework.web.multipart.commons.CommonsMultipartResolver">? ? ?
? ? ? ? <!-- 默認(rèn)編碼 -->? ?
? ? ? ? <property name="defaultEncoding" value="utf-8" />? ? ?
? ? ? ? <!-- 文件大小最大值 -->? ?
? ? ? ? <property name="maxUploadSize" value="10485760000" />? ? ?
? ? ? ? <!-- 內(nèi)存中的最大值 -->? ?
? ? ? ? <property name="maxInMemorySize" value="40960" />? ? ?
? ? </bean>? ?
</beans>?
</xml-body>
4.applicationcontext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
? ? ? xmlns:context="http://www.springframework.org/schema/context"
? ? ? xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
? ? ? xmlns:aop="http://www.springframework.org/schema/aop"
? ? ? xmlns:tx="http://www.springframework.org/schema/tx"
? ? ? xmlns:p="http://www.springframework.org/schema/p"
? ? ? xsi:schemaLocation="
? ? ? ? ? ? ? ? ? ? http://www.springframework.org/schema/context
? ? ? ? ? ? ? ? ? ? http://www.springframework.org/schema/context/spring-context.xsd
? ? ? ? ? ? ? ? ? ? http://www.springframework.org/schema/beans
? ? ? ? ? ? ? ? ? ? http://www.springframework.org/schema/beans/spring-beans.xsd
? ? ? ? ? ? ? ? ? ? http://www.springframework.org/schema/tx
? ? ? ? ? ? ? ? ? ? http://www.springframework.org/schema/tx/spring-tx.xsd
? ? ? ? ? ? ? ? ? ? http://www.springframework.org/schema/aop
? ? ? ? ? ? ? ? ? ? http://www.springframework.org/schema/aop/spring-aop.xsd">
? ? ? <!-- 自動(dòng)掃描 放進(jìn)容器-->? ?
? ? <context:component-scan base-package="com.market" >
? ? ? <context:include-filter type="annotation" expression="org.springframework.steretype.Component"/>? ?
? ? ? <context:include-filter type="annotation" expression="org.springframework.steretype.Repository"/>
? ? ? <context:include-filter type="annotation" expression="org.springframework.steretype.Service"/>
? ? </context:component-scan>
? ? <!-- 加載配置文件 -->
? ? <context:property-placeholder location="classpath:jdbc.properties"/>
? ? <!-- 自動(dòng)掃描web包 ,將帶有注解的類納入spring容器管理 -->
? ? <!--Spring 容器初始化的時(shí)候,會(huì)掃描 com.web 下標(biāo)有
? ? ? ? (@Component,@Service,@Controller,@Repository) 注解的類,納入spring容器管理-->
? ? <context:component-scan base-package="com.market"></context:component-scan>
? ? <!-- dataSource 配置 -->
? ? <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
? ? ? ? <!-- 基本屬性 url、user、password -->
? ? ? ? <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
? ? ? ? <property name="url" value="jdbc:mysql://localhost:3306/dongfeng?serverTimezone=GMT"/>
? ? ? ? <property name="username" value="root"/>
? ? ? ? <property name="password" value="19980420"/>
? ? ? ? <!-- 配置初始化大小 -->
? ? ? ? <property name="initialSize" value="${jdbc.initialSize}"/>
? ? ? ? <!-- 連接池最小空閑 -->
? ? ? ? <property name="minIdle" value="${jdbc.minIdle}"/>
? ? ? ? <!-- 連接池最大使用連接數(shù)量 -->
? ? ? ? <property name="maxActive" value="${jdbc.maxActive}"/>
? ? ? ? <!-- 配置獲取連接等待超時(shí)的時(shí)間 -->
? ? ? ? <property name="maxWait" value="${jdbc.maxWait}"/>
? ? ? ? <!-- 配置間隔多久才進(jìn)行一次檢測(cè),檢測(cè)需要關(guān)閉的空閑連接,單位是毫秒 -->
? ? ? ? <property name="timeBetweenEvictionRunsMillis" value="60000"/>
? ? ? ? <!-- 配置一個(gè)連接在池中最小生存的時(shí)間,單位是毫秒 -->
? ? ? ? <property name="minEvictableIdleTimeMillis" value="300000"/>
? ? </bean>
? ? <!--使用Spring+MyBatis的環(huán)境下,我們需要配值一個(gè)SqlSessionFactoryBean來(lái)充當(dāng)SqlSessionFactory
? ? ? ? 在基本的MyBatis中,SqlSessionFactory可以使用SqlSessionFactoryBuilder來(lái)創(chuàng)建,
? ? ? ? 而在mybatis-spring中,則使用SqlSessionFactoryBean來(lái)創(chuàng)建。-->
? ? <!-- mybatis文件配置,掃描所有mapper文件 -->
? ? <bean id="sqlSessionFactory"
? ? ? ? ? class="org.mybatis.spring.SqlSessionFactoryBean"
? ? ? ? ? p:dataSource-ref="dataSource"
? ? ? ? ? p:typeAliasesPackage="com.market.entity"
? ? ? ? ? p:mapperLocations="classpath:mapper/*.xml"/>
? ? ? ? ? ? <!-- 如果 MyBatis 映射器 XML 文件在和映射器類相同的路徑下不存在,那么另外一個(gè)需要配置文件的原因就是它了。 -->
? ? <!-- spring與mybatis整合配置,自動(dòng)掃描所有dao ,將dao接口生成代理注入到Spring-->
? ? <!-- MapperScannerConfigurer 的作用是取代手動(dòng)添加 Mapper ,自動(dòng)掃描完成接口代理。
? ? ? ? ? 而不需要再在mybatis-config.xml里面去逐一配置mappers。 -->
? ? ? ? ? <!-- 配置掃描dao包 動(dòng)態(tài)實(shí)現(xiàn)dao接口 注入到spring容器中 -->
? ? <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"
? ? ? ? ? p:basePackage="com.market.dao"
? ? ? ? ? p:sqlSessionFactoryBeanName="sqlSessionFactory"/>
? ? <!-- 事務(wù)管理器(JDBC) -->
? ? <bean id="transactionManager"
? ? ? ? ? class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
? ? ? ? ? p:dataSource-ref="dataSource"/>
? ? <!-- 啟動(dòng)聲明式事務(wù)驅(qū)動(dòng) -->
? ? <tx:annotation-driven transaction-manager="transactionManager"/>
? ? <!-- 配置AOP通知 -->
? ? <tx:advice id="txAdvice" transaction-manager="transactionManager">
? ? ? ? <!-- 配置事務(wù)屬性 -->
? ? ? ? <tx:attributes>
? ? ? ? ? ? <!-- 添加事務(wù)管理的方法 -->
? ? ? ? ? ? <tx:method name="save*" propagation="REQUIRED"/>
? ? ? ? ? ? <tx:method name="delete*" propagation="REQUIRED"/>
? ? ? ? ? ? <tx:method name="update*" propagation="REQUIRED"/>
? ? ? ? ? ? <tx:method name="select*" propagation="REQUIRED" read-only="true"/>
? ? ? ? </tx:attributes>
? ? </tx:advice>
? ? <!-- 配置一個(gè)切面AOP -->
? ? <aop:config>
? ? ? ? <aop:aspect id="helloWorldAspect" ref="txAdvice">
? ? ? ? ? ? <!-- 配置切點(diǎn) -->
? ? ? ? ? ? <aop:pointcut id="pointcut" expression="execution(* com.aop.*.*(..))"/>
? ? ? ? ? ? <!-- 配置前置通知 -->
? ? ? ? ? ? <aop:before pointcut-ref="pointcut" method="beforeAdvice"/>
? ? ? ? ? ? <!-- 配置前置通知 -->
? ? ? ? ? ? <aop:after pointcut-ref="pointcut" method="afterAdvice"/>
? ? ? ? ? ? <!-- 配置后置返回通知 -->
? ? ? ? ? ? <aop:after-returning pointcut-ref="pointcut" method="afterReturnAdvice" returning="result"/>
? ? ? ? ? ? <!-- 配置環(huán)繞通知 -->
? ? ? ? ? ? <aop:around pointcut-ref="pointcut" method="aroundAdvice"/>
? ? ? ? ? ? <!-- 異常通知 -->
? ? ? ? ? ? <aop:after-throwing pointcut-ref="pointcut" method="throwingAdvice" throwing="e"/>
? ? ? ? </aop:aspect>
? ? </aop:config>
? ? <!-- 配置使Spring采用CGLIB代理 -->
? ? <aop:aspectj-autoproxy proxy-target-class="true"/>
</beans>