一,前言
SSM框架即是将SpringMVC框架、Spring框架、MyBatis框架整合使用。以简化在web开发中繁琐、重复的操作,让开发人员的精力专注于业务处理的开发上。
二,SSM框架
2.1,SSM整合到底整合什么?
1、spring和mybaits整合(数据源》sessionFactory》session)
2、spirng和springMVC整合
2.2,为什么要整合到一起?
统一指挥、让两个框架中用到的类和对象,都让spring管理。好处:无缝衔接、用到spring提供的很多工具。
2.3,由谁来整合?
mybatis的主配置文件
spring的配置文件
springMVC的配置文件
web.xml
2.4,@ResponseBody注解的作用是什么
@ResponseBody注解的作用是将当前接口返回的数据直接写入HTTP Response Body中
2.5,JSON
JSON是FastJson提供的工具类,它提供了一些数据类型转换以及格式化数据的功能
三,各框架应用场景
3.1,SpringMVC框架
SpringMVC框架位于Controller层,主要为接收用户发起的请求,在接收请求后可进行一定处理(如:通过拦截器的信息验证处理)。在通过处理后SpringMVC会根据请求的路径将请求分发到对应的Controller类中的处理方法。处理方法再调用Service层的业务处理逻辑。
3.2,Spring框架
Spring框架在SSM中充当类似与粘合剂的作用,利用其对象托管的特性将SpringMVC、MyBatis两个独立的框架有机的结合起来。 Spring可将SpringMVC中的Controller类和MyBatis中的SqlSession类进行托管,简化了人工管理过程。 Spring除了能对SpringMVC和MyBatis的核心类进行管理外,还可对主要的业务处理的类进行管理。
3.3,MyBatis框架
MyBatis框架应用于对数据库的操作,其中主要功能类SqlSession可对数据库进行具体操作。
四,SSM框架中的容器管理
- SpringMVC容器:主要对Controller控制器对象,视图等用户请求和请求结果进行管理。
- Spring容器:主要对Service、Dao、工具类等对象进行管理。
- 两个容器的关系:SpringMVC容器为Spring容器的子容器,进而两容器中的对象进行间接管理。
五,SSM框架整合步骤
5.1,项目准备
数据库、数据表的建立。
5.2,新建项目,添加依赖
1.使用Maven新建一个webapp项目
2.在pom.xml文件中添加依赖和项目配置。
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> <spring.version>5.2.5.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <!-- 连接mysql5 的驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.29</version> </dependency> <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-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.2</version> </dependency> <!-- mybatis依赖 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.2</version> </dependency> <!-- 参考版本对应 http://www.mybatis.org/spring/ --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.2</version> </dependency> <!-- 数据源管理 使用了dbcp2数据 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-dbcp2</artifactId> <version>2.1.1</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> <version>2.4.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jsp-api</artifactId> <version>2.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <!-- commons 文件上传jar --> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <!-- jstl --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- 加入JSON转换工具 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.47</version> </dependency> </dependencies>
5.3,配置web.xml文件
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <display-name>Archetype Created Web Application</display-name> <display-name>Archetype Created Web Application</display-name> <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:applicationContext.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 页面中输入的中文,调到后端,出现乱码解决的问题 --> <filter> <filter-name>encodingFilter</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>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
5.4,创建目录结构,Bean,Dao,Service,controller结构图如下:
5.5,配置各个配置文件
1.jdbc.properties。数据库配置信息文件。
driver=com.mysql.cj.jdbc.Driver url=jdbc:mysql://localhost:3306/yonghu?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT-8 user= pwd=
2.mybatis.xml。MyBatis主配置文件,文件中数据源的配置交由Spring执行。
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "https://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 日志 --> <settings> <setting name="logImpl" value="LOG4J"/> </settings> </configuration>
3.applicationContext.xml。Spring整合MyBatis
<?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:p="http://www.springframework.org/schema/p" 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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 加载属性文件 --> <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder> <!-- 创建dbcp2数据源 此数据源可以替换为阿里的 德鲁伊 --> <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${driver}"></property> <property name="url" value="${url}"></property> <property name="username" value="${user}"></property> <property name="password" value="${pwd}"></property> </bean> <!-- 整合了sqlSessionFactory 包含了 数据源(dataSource)、配置文件(config)和映射文件(mapper) --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:mybatis.xml"></property> <property name="mapperLocations" value="classpath:mapper/*.xml"></property> </bean> <!-- 扫描mapper接口 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.xinxi2.dao"></property> </bean> <!-- 扫描业务逻辑层的注解 --> <context:component-scan base-package="com.xinxi2"></context:component-scan> <!-- 引入事务管理器 管理指定的数据源 --> <bean id="txMapper" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" > <property name="dataSource" ref="dataSource"></property> </bean> <!-- 把事务管理管理,变成增强(通知),同时指定了方法的事务传播机制 --> <tx:advice id="txAdvice" transaction-manager="txMapper"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="del*" propagation="REQUIRED"/> <tx:method name="get*" propagation="REQUIRED" read-only="true"/> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="servicePointcut" expression="execution(* com.xinxi2.service..*.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="servicePointcut"></aop:advisor> </aop:config> <import resource="springmvc.xml"></import> </beans>
4.springmvc.xml。Spring整合service。
<?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:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" 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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 扫描 --> <context:component-scan base-package="com.xinxi2.controller"></context:component-scan> <mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>application/json;charset=UTF-8</value> <value>text/html;charset=UTF-8</value> </list> </property> </bean> <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>application/json;charset=UTF-8</value> <value>text/html;charset=UTF-8</value> </list> </property> <property name="features"> <list> <!-- Date的日期转换器 --> <value>WriteDateUseDateFormat</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> <!-- 常用视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"></property> <property name="suffix" value=""></property> </bean> <!-- 解决了静态资源的加载问题 --> <mvc:resources mapping="/static/**" location="/static/"></mvc:resources> <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="exceptionMappings"> <props> <prop key="java.lang.RuntimeException">error.jsp</prop> </props> </property> </bean> <!-- 配置multipartResolver,用于上传文件,使用spring的CommonsMultipartResolver --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxInMemorySize" value="5000000"></property> <property name="defaultEncoding" value="UTF-8"></property> </bean> <!-- <mvc:interceptors>--> <!-- <mvc:interceptor>--> <!-- <mvc:mapping path="/hello/**"/>--> <!-- <mvc:exclude-mapping path="/hello/hello04"/>--> <!-- <bean class="com.Interceptor.LoginInterceptor"></bean>--> <!-- </mvc:interceptor>--> <!-- </mvc:interceptors>--> </beans>
5.6,编写业务实现代码,Bean实体类,dao层接口,mapper文件,service层业务处理类,controller层控制类等。
Bean实体类
package com.xinxi2.bean; import com.alibaba.fastjson.annotation.JSONField; import org.springframework.format.annotation.DateTimeFormat; import java.util.Date; public class Bookmanage { /** 图书编号 */ private Integer id ; /** 图书名称 */ private String name ; /** 图书作者 */ private String author ; /** 购买时间 */ @DateTimeFormat(pattern="yyyy-MM-dd") // String 转 Date 视图到控制层 @JSONField(format = "yyyy-MM_dd") private Date time ; /** 图书分类 */ private Integer type ; /** 图书编号 */ public Integer getId(){ return this.id; } /** 图书编号 */ public void setId(Integer id){ this.id=id; } /** 图书名称 */ public String getName(){ return this.name; } /** 图书名称 */ public void setName(String name){ this.name=name; } /** 图书作者 */ public String getAuthor(){ return this.author; } /** 图书作者 */ public void setAuthor(String author){ this.author=author; } /** 购买时间 */ public Date getTime(){ return this.time; } /** 购买时间 */ public void setTime(Date time){ this.time=time; } /** 图书分类 */ public Integer getType(){ return this.type; } /** 图书分类 */ public void setType(Integer type){ this.type=type; } }
Dao层
1.BookmanageMapper.java
package com.xinxi2.dao; import com.xinxi2.bean.Bookmanage; import java.util.List; public interface BookmanageMapper { List<Bookmanage> getlistBookmanage(Bookmanage bookmanage); int addBookmanage(Bookmanage bookmanage); Bookmanage updateByid(Integer id); int updateBookmanage(Bookmanage bookmanage); int deleteBookmanage(int id); }
2.BookmanageMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.xinxi2.dao.BookmanageMapper"> <resultMap id="Bookmanageinto" type="com.xinxi2.bean.Bookmanage"> <id property="id" column="id"></id> <result property="name" column="name"></result> <result property="author" column="author"></result> <result property="time" column="time"></result> <result property="type" column="type"></result> </resultMap> <select id="getlistBookmanage" resultType="com.xinxi2.bean.Bookmanage" resultMap="Bookmanageinto"> SELECT `id`,`name`,`author`,`time`,`type` FROM `bookmanage` <where> <if test="id!=null"> and id = #{id} </if> <if test="name!=null"> and `name` = #{name} </if> <if test="author!=null"> and author = #{author} </if> <if test="time!=null"> and `time` = #{time} </if> <if test="type!=null"> and `type` = #{type} </if> </where> </select> <insert id="addBookmanage"> insert into `bookmanage`(id,`name`,`author`,`time`,`type`) values(#{id},#{name},#{author},#{time},#{type}) </insert> <select id="updateByid" parameterType="integer" resultMap="Bookmanageinto"> SELECT `id`,`name`,`author`,`time`,`type` FROM `bookmanage` where id=#{id} </select> <update id="updateBookmanage"> update `bookmanage` <set> <if test="id!=null"> id = #{id}, </if> <if test="name!=null"> `name` = #{name}, </if> <if test="author!=null"> author = #{author}, </if> <if test="time!=null"> `time` = #{time}, </if> <if test="type!=null"> `type` = #{type}, </if> </set> where id=#{id} </update> <delete id="deleteBookmanage"> delete from `bookmanage` where id=#{id} </delete> </mapper>