目录结构:
1.导包
2.配置Spring(applicationContext.xml)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" 4 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 5 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 7 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd 8 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd 9 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> 10 11 12 <context:property-placeholder location="classpath:db.properties"/> 13 14 <!-- 数据库连接池 --> 15 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 16 destroy-method="close"> 17 <property name="driverClassName" value="${jdbc.driver}" /> 18 <property name="url" value="${jdbc.url}" /> 19 <property name="username" value="${jdbc.username}" /> 20 <property name="password" value="${jdbc.password}" /> 21 <property name="maxActive" value="10" /> 22 <property name="maxIdle" value="5" /> 23 </bean> 24 25 <!-- Mybatis的工厂 --> 26 <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean"> 27 <property name="dataSource" ref="dataSource"/> 28 <!-- 核心配置文件的位置 --> 29 <property name="configLocation" value="classpath:sqlMapConfig.xml"/> 30 </bean> 31 32 <!-- Mapper动态代理开发 扫描 --> 33 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 34 <!-- 基本包 --> 35 <property name="basePackage" value="com.itheima.springmvc.dao"/> 36 </bean> 37 38 <!-- 注解事务 --> 39 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 40 <property name="dataSource" ref="dataSource"/> 41 </bean> 42 43 <!-- 开启注解 --> 44 <tx:annotation-driven transaction-manager="transactionManager"/> 45 46 47 </beans>
3.配置mybatis(sqlMapConfig.xml)
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 <!-- 设置别名 --> 7 <typeAliases> 8 <!-- 2. 指定扫描包,会把包内所有的类都设置别名,别名的名称就是类名,大小写不敏感 --> 9 <package name="com.itheima.springmvc.pojo" /> 10 </typeAliases> 11 12 </configuration>
4.配置SpringMVC
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:mvc="http://www.springframework.org/schema/mvc" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 7 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 8 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> 9 10 11 12 <!-- 扫描@Controler @Service --> 13 <context:component-scan base-package="com.itheima"/> 14 15 <!-- 处理器映射器 --> 16 <!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> --> 17 <!-- 处理器适配器 --> 18 <!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/> --> 19 <!-- 注解驱动 --> 20 <mvc:annotation-driven conversion-service="conversionServiceFactoryBean"/> 21 22 <!-- 配置Conveter转换器 转换工厂 (日期、去掉前后空格)。。 --> 23 <bean id="conversionServiceFactoryBean" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> 24 <!-- 配置 多个转换器--> 25 <property name="converters"> 26 <list> 27 <bean class="com.itheima.springmvc.conversion.DateConveter"/> 28 </list> 29 </property> 30 </bean> 31 32 <!-- 视图解释器 --> 33 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 34 <property name="prefix" value="/WEB-INF/jsp/"/> 35 <property name="suffix" value=".jsp"/> 36 </bean> 37 38 </beans>
5.web.xml配置spring监听器与springMVC前端控制器
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> 3 <display-name>springmvc-mybatis</display-name> 4 <welcome-file-list> 5 <welcome-file>index.html</welcome-file> 6 <welcome-file>index.htm</welcome-file> 7 <welcome-file>index.jsp</welcome-file> 8 <welcome-file>default.html</welcome-file> 9 <welcome-file>default.htm</welcome-file> 10 <welcome-file>default.jsp</welcome-file> 11 </welcome-file-list> 12 13 <context-param> 14 <param-name>contextConfigLocation</param-name> 15 <param-value>classpath:applicationContext.xml</param-value> 16 </context-param> 17 18 <!-- Spring监听器 --> 19 <listener> 20 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 21 </listener> 22 23 <!-- 处理POST提交乱码问题 --> 24 <filter> 25 <filter-name>encoding</filter-name> 26 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 27 <init-param> 28 <param-name>encoding</param-name> 29 <param-value>UTF-8</param-value> 30 </init-param> 31 </filter> 32 33 <filter-mapping> 34 <filter-name>encoding</filter-name> 35 <url-pattern>*.action</url-pattern> 36 </filter-mapping> 37 38 39 <!-- 前端控制器 --> 40 <servlet> 41 <servlet-name>springmvc</servlet-name> 42 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 43 <!-- 默认找 /WEB-INF/[servlet的名称]-servlet.xml --> 44 <init-param> 45 <param-name>contextConfigLocation</param-name> 46 <param-value>classpath:springmvc.xml</param-value> 47 </init-param> 48 </servlet> 49 50 <servlet-mapping> 51 <servlet-name>springmvc</servlet-name> 52 <!-- 53 1. /* 拦截所有 jsp js png .css 真的全拦截 建议不使用 54 2. *.action *.do 拦截以do action 结尾的请求 肯定能使用 ERP 55 3. / 拦截所有 (不包括jsp) (包含.js .png.css) 强烈建议使用 前台 面向消费者 www.jd.com/search /对静态资源放行 56 --> 57 <url-pattern>*.action</url-pattern> 58 </servlet-mapping> 59 60 61 </web-app>
6.总结
配置思路:
单独配置spring>单独配置mybatis>单独配置springMVC>mybatis与spring整合>web.xml配置