SpringMVC是一个MVC的流程框架,在流程处理方面更加灵活,可以很容易的进行扩展,可以和Spring框架进行无缝集成。但SpringMVC有侵入性,action需要继承框架指定的类,或者实现指定的接口。
mybatis是三层架构中数据持久层的框架,对sql进行了封装和外部化,使sql从应用程序代码中分离出来。
SSM 优点
- spring3开发效率高于struts。
- spring3 mvc可以认为是零配置。
- spring3mvc的方法之间基本上独立的,独享requestresponse数据,方法之间不共享变量。 刚开始接触SSM框架,发现网上资料比较少,有什么不对的地方欢迎指正。
首先在web.xml里,配置mybatis和Spring MVC servlet
---------------------------------web.xml----------------------------------
<?xmlversion="1.0"encoding="UTF-8"?>
<web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>Archetype Created Web Application</display-name>
<!-- Spring和mybatis的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mybatis.xml</param-value>
</context-param>
<!-- 编码过滤器 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<async-supported>true</async-supported>
<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>
<!-- Spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 防止Spring内存溢出监听器 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<!-- Spring MVC servlet -->
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<!-- 此处可以可以配置成*.do,对应struts的后缀习惯 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list>
<!-- 配置SESSION超时,单位是分钟 -->
<session-config>
<session-timeout>15</session-timeout>
</session-config>
</web-app>
spring-mybatis.xml 和spring-mvc.xml 需要建在SourceFolder 下,要不然路径找不到
----------------------------------------------------spring-mybatis.xml----------------------------------------------------
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="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-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- 自动扫描 -->
<context:component-scanbase-package="com.testSSM.test" />
<!-- 引入配置文件 -->
<beanid="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<propertyname="location" value="classpath:jdbc.properties"/>
</bean>
<beanid="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<propertyname="driverClassName" value="${driver}" />
<propertyname="url" value="${url}"/>
<propertyname="username" value="${username}"/>
<propertyname="password" value="${password}"/>
<!-- 初始化连接大小 -->
<propertyname="initialSize" value="${initialSize}"></property>
<!-- 连接池最大数量 -->
<propertyname="maxActive" value="${maxActive}"></property>
<!-- 连接池最大空闲 -->
<propertyname="maxIdle" value="${maxIdle}"></property>
<!-- 连接池最小空闲 -->
<propertyname="minIdle"value="${minIdle}"></property>
<!-- 获取连接最大等待时间 -->
<propertyname="maxWait" value="${maxWait}"></property>
</bean>
<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
<beanid="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<propertyname="dataSource" ref="dataSource" />
<!-- 自动扫描mapping.xml文件 -->
<propertyname="mapperLocations" value="classpath:com/testSSM/test/mapping/*.xml"></property>
</bean>
<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<beanclass="org.mybatis.spring.mapper.MapperScannerConfigurer">
<propertyname="basePackage" value="com.testSSM.test.dao" />
<!-- <propertyname="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> -->
</bean>
<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx-->
<beanid="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<propertyname="dataSource" ref="dataSource" />
</bean>
</beans>
---------------------------spring-mvc.xml---------------------------
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="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-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->
<context:component-scanbase-package="com.testSSM.test.controller" />
<!--避免IE执行AJAX时,返回JSON出现下载文件 -->
<beanid="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<propertyname="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
<!-- 启动SpringMVC的注解功能,完成请求和注解POJO的映射 -->
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<propertyname="messageConverters">
<list>
<refbean="mappingJacksonHttpMessageConverter"/> <!--JSON转换器 -->
</list>
</property>
</bean>
<!-- 定义跳转的文件的前后缀 ,视图模式配置-->
<beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
<propertyname="prefix" value="/WEB-INF/jsp/"/>
<propertyname="suffix" value=".jsp"/>
</bean>
<!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 -->
<beanid="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 默认编码 -->
<propertyname="defaultEncoding"value="utf-8" />
<!-- 文件大小最大值 -->
<propertyname="maxUploadSize"value="10485760000" />
<!-- 内存中的最大值 -->
<propertyname="maxInMemorySize"value="40960" />
</bean>
</beans>
---------------------jdbc.properties-----------------------
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
username=test
password=test
#定义初始连接数
initialSize=0
#定义最大连接数
maxActive=20
#定义最大空闲
maxIdle=20
#定义最小空闲
minIdle=1
#定义最长等待时间
maxWait=60000
- 层级结构
•
• Controller
• Dao
• Mapping
• Model
• service
Controller
Service
Dao
Mapping
Model