SSM框架整合步骤:
1.导入相关jar包
aopalliance.jar
asm-3.3.1.jar
aspectjweaver.jar
cglib-2.2.2.jar
commons-fileupload-1.3.2.jar
commons-io-2.2.jar
commons-logging-1.1.3.jar
jackson-annotations-2.5.4.jar
jackson-core-2.5.4.jar
jackson-databind-2.5.4.jar
javassist-3.17.1-GA.jar
log4j-1.2.17.jar
log4j-api-2.0-rc1.jar
log4j-core-2.0-rc1.jar
mybatis-3.2.7.jar
mybatis-spring-1.2.3.jar
mysql-connector-java-5.1.20-bin.jar
slf4j-api-1.7.5.jar
slf4j-log4j12-1.7.5.jar
spring-aop-4.1.6.RELEASE.jar
spring-aspects-4.1.6.RELEASE.jar
spring-beans-4.1.6.RELEASE.jar
spring-context-4.1.6.RELEASE.jar
spring-core-4.1.6.RELEASE.jar
spring-expression-4.1.6.RELEASE.jar
spring-jdbc-4.1.6.RELEASE.jar
spring-tx-4.1.6.RELEASE.jar
spring-web-4.1.6.RELEASE.jar
spring-webmvc-4.1.6.RELEASE.jar
2.编写配置文件
1)web.xml ---->项目全局配置文件
2)applicationContext.xml ---->Spring配置文件
3)mvc.xml ---->SpringMVC配置文件
4)mybatis.cfg.xml ---->mabatis配置文件
值得注意的是mybatis.cfg.xm可以没有,可以把它配置到Spring的配置文件中,而mvc.xml必须有,因为它和applicationContext.xml是分别读取的,所以个人建议还是三个配置文件比较好,相对比较清晰。
3.编写代码
接下来就开始编写controller层,Service层,以及dao层的代码。
最后贴一下配置文件的代码:
web.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_2_5.xsd" version="2.5">
<display-name></display-name>
<!-- 配置spring -->
<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>
<!-- spring mvc的配置
配置DispatcherServlet(前控制器/分发器) 这个类接管所有请求。 必须配置在web.xml中
-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--设置一下参数后 改变mvc.xml在src路径下 否则默认在WEB-INF下 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 截获请求的 -->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!-- 对于所有符合*.do的请求都将交给DispatcherServlet去处理 -->
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
mybatis.cfg.xml(Mybatis配置文件):
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<package name="com.syd.vo"/>
</typeAliases>
<mappers>
<mapper resource="com/syd/vo/user.mapper.xml"/>
</mappers>
</configuration>
applicationContext.xml(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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置视图渲染器 -->
<!-- <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
将视图名 渲染后视图的前缀
<property name="prefix" value="/WEB-INF/jsp/"/>
渲染后视图的后缀
<property name="suffix" value=".jsp"/>
例:视图名为:hello 渲染后:/WEB-INF/jsp/hello.jsp 该页面
</bean> -->
<context:component-scan base-package="com.syd.controller"/>
</beans>