一:概述
SSM框架在项目开发中经常使用到,相比于SSH框架,它在仅几年的开发中运用的更加广泛。
- Spring作为一个轻量级的框架,有很多的拓展功能,最主要的我们一般项目使用的就是IOC和AOP。
- SpringMVC是Spring实现的一个Web层,相当于Struts的框架,但是比Struts更加灵活和强大!
- Mybatis是 一个持久层的框架,在使用上相比Hibernate更加灵活,可以控制sql的编写,使用 XML或注解进行相关的配置!
根据上面的描述,学习SSM框架就非常的重要了!
二:搭建一个SSM的过程
然后直接配置 pom.xml文件中的包依赖!
-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
-
<modelVersion>4.0.0</modelVersion>
-
<groupId>org.dufy</groupId>
-
<artifactId>ssm</artifactId>
-
<packaging>war</packaging>
-
<version>0.0.1-SNAPSHOT</version>
-
<name>ssmDemo</name>
-
<url>http://maven.apache.org</url>
-
<properties>
-
<spring.version>4.0.5.RELEASE</spring.version>
-
<mybatis.version>3.2.1</mybatis.version>
-
<slf4j.version>1.6.6</slf4j.version>
-
<log4j.version>1.2.12</log4j.version>
-
<mysql.version>5.1.35</mysql.version>
-
</properties>
-
<dependencies>
-
<!-- 添加Spring依赖 -->
-
<dependency>
-
<groupId>org.springframework</groupId>
-
<artifactId>spring-core</artifactId>
-
<version>${spring.version}</version>
-
</dependency>
-
<dependency>
-
<groupId>org.springframework</groupId>
-
<artifactId>spring-context</artifactId>
-
<version>${spring.version}</version>
-
</dependency>
-
<dependency>
-
<groupId>org.springframework</groupId>
-
<artifactId>spring-context-support</artifactId>
-
<version>${spring.version}</version>
-
</dependency>
-
<dependency>
-
<groupId>org.springframework</groupId>
-
<artifactId>spring-aop</artifactId>
-
<version>${spring.version}</version>
-
</dependency>
-
<dependency>
-
<groupId>org.springframework</groupId>
-
<artifactId>spring-aspects</artifactId>
-
<version>${spring.version}</version>
-
</dependency>
-
<dependency>
-
<groupId>org.springframework</groupId>
-
<artifactId>spring-tx</artifactId>
-
<version>${spring.version}</version>
-
</dependency>
-
<dependency>
-
<groupId>org.springframework</groupId>
-
<artifactId>spring-jdbc</artifactId>
-
<version>${spring.version}</version>
-
</dependency>
-
<dependency>
-
<groupId>org.springframework</groupId>
-
<artifactId>spring-web</artifactId>
-
<version>${spring.version}</version>
-
</dependency>
-
<!--spring单元测试依赖 -->
-
<dependency>
-
<groupId>org.springframework</groupId>
-
<artifactId>spring-test</artifactId>
-
<version>${spring.version}</version>
-
<scope>test</scope>
-
</dependency>
-
<!-- spring webmvc相关jar -->
-
<dependency>
-
<groupId>org.springframework</groupId>
-
<artifactId>spring-webmvc</artifactId>
-
<version>${spring.version}</version>
-
</dependency>
-
<dependency>
-
<groupId>org.springframework</groupId>
-
<artifactId>spring-web</artifactId>
-
<version>${spring.version}</version>
-
</dependency>
-
<!-- mysql驱动包 -->
-
<dependency>
-
<groupId>mysql</groupId>
-
<artifactId>mysql-connector-java</artifactId>
-
<version>${mysql.version}</version>
-
</dependency>
-
<!-- alibaba data source 相关jar包-->
-
<dependency>
-
<groupId>com.alibaba</groupId>
-
<artifactId>druid</artifactId>
-
<version>0.2.23</version>
-
</dependency>
-
<!-- alibaba fastjson 格式化对 -->
-
<dependency>
-
<groupId>com.alibaba</groupId>
-
<artifactId>fastjson</artifactId>
-
<version>1.1.41</version>
-
</dependency>
-
<!-- logback start -->
-
<dependency>
-
<groupId>log4j</groupId>
-
<artifactId>log4j</artifactId>
-
<version>${log4j.version}</version>
-
</dependency>
-
<dependency>
-
<groupId>org.slf4j</groupId>
-
<artifactId>slf4j-api</artifactId>
-
<version>${slf4j.version}</version>
-
</dependency>
-
<dependency>
-
<groupId>ch.qos.logback</groupId>
-
<artifactId>logback-classic</artifactId>
-
<version>1.1.2</version>
-
</dependency>
-
<dependency>
-
<groupId>ch.qos.logback</groupId>
-
<artifactId>logback-core</artifactId>
-
<version>1.1.2</version>
-
</dependency>
-
<dependency>
-
<groupId>org.logback-extensions</groupId>
-
<artifactId>logback-ext-spring</artifactId>
-
<version>0.1.1</version>
-
</dependency>
-
<!--mybatis依赖 -->
-
<dependency>
-
<groupId>org.mybatis</groupId>
-
<artifactId>mybatis</artifactId>
-
<version>${mybatis.version}</version>
-
</dependency>
-
<!-- mybatis/spring包 -->
-
<dependency>
-
<groupId>org.mybatis</groupId>
-
<artifactId>mybatis-spring</artifactId>
-
<version>1.2.0</version>
-
</dependency>
-
<!-- 添加servlet3.0核心包 -->
-
<dependency>
-
<groupId>javax.servlet</groupId>
-
<artifactId>javax.servlet-api</artifactId>
-
<version>3.0.1</version>
-
</dependency>
-
<dependency>
-
<groupId>javax.servlet.jsp</groupId>
-
<artifactId>javax.servlet.jsp-api</artifactId>
-
<version>2.3.2-b01</version>
-
</dependency>
-
<!-- jstl -->
-
<dependency>
-
<groupId>javax.servlet</groupId>
-
<artifactId>jstl</artifactId>
-
<version>1.2</version>
-
</dependency>
-
<!--单元测试依赖 -->
-
<dependency>
-
<groupId>junit</groupId>
-
<artifactId>junit</artifactId>
-
<version>3.8.1</version>
-
<scope>test</scope>
-
</dependency>
-
</dependencies>
-
<build>
-
<finalName>ssmDemo</finalName>
-
</build>
-
</project>
jdbc.properties
-
jdbc_driverClassName =com.mysql.jdbc.Driver
-
jdbc_url=jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=utf8
-
jdbc_username=root
-
jdbc_password=root
applicationContext.xml
-
<?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:context="http://www.springframework.org/schema/context"
-
xmlns:aop="http://www.springframework.org/schema/aop"
-
xmlns:tx="http://www.springframework.org/schema/tx"
-
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
-
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
-
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
-
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
-
">
-
<!-- 1.配置jdbc文件 -->
-
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
-
<property name="locations" value="classpath:jdbc.properties"/>
-
</bean>
-
<!-- 2.扫描的包路径,这里不扫描被@Controller注解的类 --><!--使用<context:component-scan/> 可以不在配置<context:annotation-config/> -->
-
<context:component-scan base-package="org.ssm.dufy">
-
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
-
</context:component-scan>
-
<import resource="classpath:spring-mybatis.xml" />
-
</beans>
spring-mybatis.xml
-
<?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:context="http://www.springframework.org/schema/context"
-
xmlns:aop="http://www.springframework.org/schema/aop"
-
xmlns:tx="http://www.springframework.org/schema/tx"
-
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
-
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
-
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
-
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
-
">
-
<!-- 1.配置jdbc文件 -->
-
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
-
<property name="locations" value="classpath:jdbc.properties"/>
-
</bean>
-
<!-- 2.扫描的包路径,这里不扫描被@Controller注解的类 --><!--使用<context:component-scan/> 可以不在配置<context:annotation-config/> -->
-
<context:component-scan base-package="org.ssm.dufy">
-
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
-
</context:component-scan>
-
<import resource="classpath:spring-mybatis.xml" />
-
</beans>
spring-mybatis.xml
-
<?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:context="http://www.springframework.org/schema/context"
-
xmlns:aop="http://www.springframework.org/schema/aop"
-
xmlns:tx="http://www.springframework.org/schema/tx"
-
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
-
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
-
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
-
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
-
">
-
<!-- 1.配置jdbc文件 -->
-
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
-
<property name="locations" value="classpath:jdbc.properties"/>
-
</bean>
-
<!-- 2.扫描的包路径,这里不扫描被@Controller注解的类 --><!--使用<context:component-scan/> 可以不在配置<context:annotation-config/> -->
-
<context:component-scan base-package="org.ssm.dufy">
-
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
-
</context:component-scan>
-
<import resource="classpath:spring-mybatis.xml" />
-
</beans>
spring-mybatis.xml
-
<?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:context="http://www.springframework.org/schema/context"
-
xmlns:aop="http://www.springframework.org/schema/aop"
-
xmlns:tx="http://www.springframework.org/schema/tx"
-
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
-
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
-
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
-
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
-
">
-
<!-- 1.配置jdbc文件 -->
-
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
-
<property name="locations" value="classpath:jdbc.properties"/>
-
</bean>
-
<!-- 2.扫描的包路径,这里不扫描被@Controller注解的类 --><!--使用<context:component-scan/> 可以不在配置<context:annotation-config/> -->
-
<context:component-scan base-package="org.ssm.dufy">
-
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
-
</context:component-scan>
-
<import resource="classpath:spring-mybatis.xml" />
- </beans>
spring-mybatis.xml
-
<?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:aop="http://www.springframework.org/schema/aop"
-
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
-
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
-
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
-
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
-
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
-
<!-- 3.配置数据源 ,使用的alibba的数据库-->
-
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
-
<!-- 基本属性 url、user、password -->
-
<property name="driverClassName" value="${jdbc_driverClassName}"/>
-
<property name="url" value="${jdbc_url}"/>
-
<property name="username" value="${jdbc_username}"/>
-
<property name="password" value="${jdbc_password}"/>
-
<!-- 配置初始化大小、最小、最大 -->
-
<property name="initialSize" value="10"/>
-
<property name="minIdle" value="10"/>
-
<property name="maxActive" value="50"/>
-
<!-- 配置获取连接等待超时的时间 -->
-
<property name="maxWait" value="60000"/>
-
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
-
<property name="timeBetweenEvictionRunsMillis" value="60000" />
-
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
-
<property name="minEvictableIdleTimeMillis" value="300000" />
-
<property name="validationQuery" value="SELECT 'x'" />
-
<property name="testWhileIdle" value="true" />
-
<property name="testOnBorrow" value="false" />
-
<property name="testOnReturn" value="false" />
-
<!-- 打开PSCache,并且指定每个连接上PSCache的大小 如果用Oracle,则把poolPreparedStatements配置为true,mysql可以配置为false。-->
-
<property name="poolPreparedStatements" value="false" />
-
<property name="maxPoolPreparedStatementPerConnectionSize" value="20" />
-
<!-- 配置监控统计拦截的filters -->
-
<property name="filters" value="wall,stat" />
-
</bean>
-
<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
-
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
-
<property name="dataSource" ref="dataSource" />
-
<!-- 自动扫描mapping.xml文件 -->
-
<property name="mapperLocations" value="classpath:org/ssm/dufy/mapper/*.xml"></property>
-
</bean>
-
<!-- DAO接口所在包名,Spring会自动查找其下的类 ,自动扫描了所有的XxxxMapper.xml对应的mapper接口文件,只要Mapper接口类和Mapper映射文件对应起来就可以了-->
-
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
-
<property name="basePackage" value="org.ssm.dufy.dao" />
-
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
-
</bean>
-
<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
-
<!-- 配置事务管理器 -->
-
<bean id="transactionManager"
-
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
-
<property name="dataSource" ref="dataSource" />
-
</bean>
-
<!--======= 事务配置 End =================== -->
-
<!-- 配置基于注解的声明式事务 -->
-
<!-- enables scanning for @Transactional annotations -->
-
<tx:annotation-driven transaction-manager="transactionManager" />
-
</beans>
-
spring-mvc.xml
-
<?xml version="1.0" encoding="UTF-8"?>
-
<beans xmlns="http://www.springframework.org/schema/beans"
-
xmlns:p="http://www.springframework.org/schema/p"
-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-
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.2.xsd
-
http://www.springframework.org/schema/context
-
http://www.springframework.org/schema/context/spring-context-3.2.xsd
-
http://www.springframework.org/schema/mvc
-
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
-
<!-- 扫描controller(controller层注入) -->
-
<context:component-scan base-package="org.ssm.dufy.web" use-default-filters="false">
-
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
-
</context:component-scan>
-
<mvc:annotation-driven />
-
<!-- 内容协商管理器 -->
-
<!--1、首先检查路径扩展名(如my.pdf);2、其次检查Parameter(如my?format=pdf);3、检查Accept Header-->
-
<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
-
<!-- 扩展名至mimeType的映射,即 /user.json => application/json -->
-
<property name="favorPathExtension" value="true"/>
-
<!-- 用于开启 /userinfo/123?format=json 的支持 -->
-
<property name="favorParameter" value="true"/>
-
<property name="parameterName" value="format"/>
-
<!-- 是否忽略Accept Header -->
-
<property name="ignoreAcceptHeader" value="false"/>
-
<property name="mediaTypes"> <!--扩展名到MIME的映射;favorPathExtension, favorParameter是true时起作用 -->
-
<value>
-
json=application/json
-
xml=application/xml
-
html=text/html
-
</value>
-
</property>
-
<!-- 默认的content type -->
-
<property name="defaultContentType" value="text/html"/>
-
</bean>
-
<!-- 当在web.xml 中 DispatcherServlet使用 <url-pattern>/</url-pattern> 映射时,能映射静态资源 -->
-
<mvc:default-servlet-handler />
-
<!-- 静态资源映射 -->
-
<mvc:resources mapping="/static/**" location="/WEB-INF/static/"/>
-
<!-- 对模型视图添加前后缀 -->
-
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
-
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"/>
-
</beans>
web.xml
-
<?xml version="1.0" encoding="UTF-8"?>
-
<web-app>
-
<display-name>SSM-DEMO</display-name>
-
<!-- 读取spring配置文件 -->
-
<context-param>
-
<param-name>contextConfigLocation</param-name>
-
<param-value>classpath:applicationContext.xml</param-value>
-
</context-param>
-
<!-- 设计路径变量值
-
<context-param>
-
<param-name>webAppRootKey</param-name>
-
<param-value>springmvc.root</param-value>
-
</context-param>
-
-->
-
<!-- Spring字符集过滤器 -->
-
<filter>
-
<filter-name>SpringEncodingFilter</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>
-
<init-param>
-
<param-name>forceEncoding</param-name>
-
<param-value>true</param-value>
-
</init-param>
-
</filter>
-
<filter-mapping>
-
<filter-name>SpringEncodingFilter</filter-name>
-
<url-pattern>/*</url-pattern>
-
</filter-mapping>
-
<!-- 添加日志监听器 -->
-
<context-param>
-
<param-name>logbackConfigLocation</param-name>
-
<param-value>classpath:logback.xml</param-value>
-
</context-param>
-
<listener>
-
<listener-class>ch.qos.logback.ext.spring.web.LogbackConfigListener</listener-class>
-
</listener>
-
<listener>
-
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
-
</listener>
-
<!-- springMVC核心配置 -->
-
<servlet>
-
<servlet-name>dispatcherServlet</servlet-name>
-
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
-
<init-param>
-
<param-name>contextConfigLocation</param-name>
-
<!--spingMVC的配置路径 -->
-
<param-value>classpath:spring-mvc.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>
-
<welcome-file-list>
-
<welcome-file>index.jsp</welcome-file>
-
</welcome-file-list>
-
</web-app>
7:编写Controller,和对应得业务界面
新增UserController ,通过参数传递uid获取用户,若用户存在,跳转到showName.jsp ,若用户不存在,则跳转到error.jsp,并返回提示信息!
-
package org.ssm.dufy.web;
-
import javax.servlet.http.HttpServletRequest;
-
import org.springframework.beans.factory.annotation.Autowired;
-
import org.springframework.stereotype.Controller;
-
import org.springframework.ui.Model;
-
import org.springframework.web.bind.annotation.RequestMapping;
-
import org.springframework.web.bind.annotation.RequestMethod;
-
import org.springframework.web.bind.annotation.RequestParam;
-
import org.springframework.web.servlet.ModelAndView;
-
import org.ssm.dufy.entity.User;
-
import org.ssm.dufy.service.IUserService;
-
@Controller
-
public class UserController {
-
@Autowired
-
private IUserService userService;
-
@RequestMapping(value="/showname",method=RequestMethod.GET)
-
public String showUserName(@RequestParam("uid") int uid,HttpServletRequest request,Model model){
-
System.out.println("showname");
-
User user = userService.getUserById(uid);
-
if(user != null){
-
request.setAttribute("name", user.getUserName());
-
model.addAttribute("mame", user.getUserName());
-
return "showName";
-
}
-
request.setAttribute("error", "没有找到该用户!");
-
return "error";
-
}
-
}