web.xml配置
<!--配置spring的核心監聽器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--手動指定spring 主配置檔案的位置和名稱-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
<!--springMVC前端控制器-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--解決亂碼問題-->
<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>
spring-dao配置
<!--1.關聯資料庫配置檔案-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--2.連接配接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--3.sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--綁定連接配接池-->
<property name="dataSource" ref="dataSource"/>
<!--綁定Mybatis的配置檔案-->
<property name="configLocation" value="classpath:mybatis_config.xml"/>
</bean>
<!--4.配置dao接口掃描包,動态的實作了Dao接口可以注入到Spring容器中-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--注入sqlSessionFactory-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!--要掃描的包-->
<property name="basePackage" value="com.javasm.goods.mapper"/>
</bean>
spring-service配置
<!--1.掃描service下面的包-->
<context:component-scan base-package="com.javasm.goods.service" />
<!--2.将我們所有的業務類,注入到Spring,可以通過配置,或者注解實作-->
<bean id="BookServiceImpl" class="com.javasm.service.BookServiceImpl">
<property name="bookMapper" ref="bookMapper"/>
</bean>
<!--3.聲明式事務配置-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--注入資料源-->
<property name="dataSource" ref="dataSource"/>
</bean>
<!--4.aop橫切-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
spring-mvc配置
<!--1.注解驅動-->
<mvc:annotation-driven/>
<!--2.靜态資源過濾-->
<mvc:default-servlet-handler/>
<!--3.掃描包:controller-->
<context:component-scan base-package="com.javasm.goods.handler"/>
<!--視圖解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--springmvc注解識别-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--檔案上傳-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"></property>
</bean>
application-context配置
<import resource="springmvc.xml"/>
<import resource="dao.xml"/>
<import resource="spring.xml"/>