zoukankan      html  css  js  c++  java
  • SSM整合

    SSM整合

    1.需要的依赖

    <dependencies>
        <!--springMVC-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.1.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>5.1.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>5.1.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>5.1.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.1.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.1.8.RELEASE</version>
        </dependency>
        <!--jackson-->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.9.8</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.9.8</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.8</version>
        </dependency>
        <!--mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.16</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.13</version>
        </dependency>
        <!--mybatis-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.1</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.1</version>
        </dependency>
        <!-- jstl -->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
    
    
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.2</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.12</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    

    2.SM整合

    通过让spring接管mybatis的核心对象sqlSessionFactory对象,进行创建

    1. 配置spring工厂监听器

      在web.xml的web-app标签内配置如下

      <!--配置spring工厂监听器-->
      <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
      <!--配置spring工厂配置文件位置-->
      <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:config/spring.xml</param-value>
      </context-param>
      
    2. 开启注解扫描

      在resources目录下创建config目录,再创建spring.xml配置文件。对应的web.xml中contextConfigLocation的值

      然后开启注解扫描,base-package指定要扫描的包。

      <!--开启注解扫描-->
      <context:component-scan base-package="cn.wsq.ssm"/>
      
    3. 创建数据源对象

      在resources目录下创建db.properties

      #db.properties
      jdbc.driverClass=com.mysql.cj.jdbc.Driver
      jdbc.jdbcUrl=jdbc:mysql://127.0.0.1:3306/jsp?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8
      jdbc.user=root
      jdbc.password=123456
      
      <!--加载配置文件-->
      <context:property-placeholder location="classpath:db.properties"/>
      <!--创建数据源对象-->
      <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
          <property name="driverClassName" value="${jdbc.driverClass}"/>
          <property name="url" value="${jdbc.jdbcUrl}"/>
          <property name="username" value="${jdbc.user}"/>
          <property name="password" value="${jdbc.password}"/>
      </bean>
      
    4. 创建sqlSessionFactory对象

      这里直接取消了mybatis的配置文件,mybatis的一切配置,可以由SqlSessionFactoryBean和Configuration共同完成。

      <!--创建sqlSessionFactory-->
      <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
          <property name="dataSource" ref="dataSource"/>
          <property name="mapperLocations" value="classpath:mapper/*Mapper.xml"/>
          <!--注入configuration,得到mybatis的其他配置项(-->
          <property name="configuration" ref="configuration"/>
      </bean>
      <bean id="configuration" class="org.apache.ibatis.session.Configuration">
          <property name="mapUnderscoreToCamelCase" value="true"/>
      </bean>
      
    5. 扫描dao/mapper对象

      需要为MapperScannerConfigurer注入sqlSessionFactory,需要指定扫描的包(mapper接口所在的包)

      <!--创建mapper-dao-->
      <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
          <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
          <property name="basePackage" value="cn.wsq.ssm.mapper"/>
      </bean>
      
    6. 创建事务管理器DataSourceTransactionManager

      <!--创建数据源事务管理器-->
      <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
          <property name="dataSource" ref="dataSource"/>
      </bean>
      
    7. 开启注解式事务

      这样开启注解式事务,就省去了切面配置事务,直接使用@Transactional注解即可指定事务

      <!--开启注解式事务生效-->
      <tx:annotation-driven transaction-manager="transactionManager"/>
      
    8. 配置aop

      <!--配置aop-->
        <!--注册通知类-->
         <!--前置通知-->
      <bean id="myBeforeAdvice" class="cn.wsq.ssm.advices.MyBeforeAdvice"/>
      <!--环绕通知-->
      <bean id="logAdvice" class="cn.wsq.ssm.advices.LogAdvice"/>
      <!--组装切面-->
      <aop:config>
          <!--配置切入点
                  id:切入点在工程中(spring.xml)的唯一标识
                  expression: 用来指定切入项目中的哪些组件中的哪些方法 精确到方法级别
                  within: 用来指定切入项目中的哪些组件中的哪些方法 精确到类级别 效率要高些
          -->
      <!-- 
      <aop:pointcut id="pc" expression="execution(* cn.wsq.ssm.service.impl.*ServiceImpl.(..))"/>
      -->
         <aop:pointcut id="pc" expression="within(cn.wsq.ssm.service.impl.*ServiceImpl)"/>
         <!--配置切面 advice-ref:工厂中通知类的id,pointcut-ref:工厂切入点的唯一标识-->
         <aop:advisor advice-ref="myBeforeAdvice" pointcut-ref="pc"/>
         <aop:advisor advice-ref="logAdvice" pointcut-ref="pc"/>
      </aop:config>
      

    SS整合

    因为spring和springmvc本来就是一加产品,所以可以达到无缝整合

    1. 配置springMVC核心servlet,DispatcherServlet

      在web.xml配置springmvc的核心servlet,并且指定contextConfigLocation,也就是指定配置文件的路径

      <!--配置springMVC核心servlet-->
      <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:config/springmvc.xml</param-value>
        </init-param>
        <load-on-startup>0</load-on-startup>
      </servlet>
      <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
      </servlet-mapping>
      
    2. 配置解决post请求乱码

      在web.xml配置过滤器解决post请求乱码,注意过滤路径是/*

      <!--解决post请求乱码-->
      <filter>
        <filter-name>characterEncodingFilter</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>
      </filter>
      <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
      </filter-mapping>
      
    3. 配置注解扫描控制器

      在config目录下新建springmvc.xml,加入如下配置

      <!--配置注解扫描控制器-->
      <context:component-scan base-package="cn.wsq.ssm.controller"/>
      
    4. 配置处理器映射器和处理器适配器

      <!--配置处理器映射器和处理器适配器-->
      <mvc:annotation-driven/>
      
    5. 放行静态资源文件

      <!-- 将图片,js,css等静态资源排除在外 -->
      <mvc:default-servlet-handler/>
      
    6. 配置视图解析器

      <!--配置视图解析器-->
      <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
          <property name="prefix" value="/"/>
          <property name="suffix" value=".jsp"/>
      </bean>
      
    7. 前后端分离模式下返回json

      只要加入Jackson相关依赖,并且在响应的方法或返回值上贴上@ResponseBody注解,spring就会使用Jackson自动将返回值解析为json格式返回给前端。

    4.项目目录结构

    image-20200706221906487
    1. spring.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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
             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
         http://www.springframework.org/schema/tx
         http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
          <!--开启注解扫描-->
          <context:component-scan base-package="cn.wsq.ssm"/>
          <!--加载配置文件-->
          <context:property-placeholder location="classpath:db.properties"/>
          <!--创建数据源对象-->
          <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
              <property name="driverClassName" value="${jdbc.driverClass}"/>
              <property name="url" value="${jdbc.jdbcUrl}"/>
              <property name="username" value="${jdbc.user}"/>
              <property name="password" value="${jdbc.password}"/>
          </bean>
          <!--创建sqlSessionFactory-->
          <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
              <property name="dataSource" ref="dataSource"/>
              <property name="mapperLocations" value="classpath:mapper/*Mapper.xml"/>
              <!--注入configuration,得到mybatis的其他配置项(-->
              <property name="configuration" ref="configuration"/>
          </bean>
          <bean id="configuration" class="org.apache.ibatis.session.Configuration">
              <property name="mapUnderscoreToCamelCase" value="true"/>
          </bean>
          <!--创建mapper-dao-->
          <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
              <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
              <property name="basePackage" value="cn.wsq.ssm.mapper"/>
          </bean>
          <!--开启注解式事务生效-->
          <tx:annotation-driven transaction-manager="transactionManager"/>
          <!--创建数据源事务管理器-->
          <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
              <property name="dataSource" ref="dataSource"/>
          </bean>
          <!--配置aop-->
              <!--注册通知类-->
                  <!--前置通知-->
          <bean id="myBeforeAdvice" class="cn.wsq.ssm.advices.MyBeforeAdvice"/>
                  <!--环绕通知-->
          <bean id="logAdvice" class="cn.wsq.ssm.advices.LogAdvice"/>
          <!--组装切面-->
          <aop:config>
              <!--配置切入点
                  id:切入点在工程中(spring.xml)的唯一标识
                  expression: 用来指定切入项目中的哪些组件中的哪些方法 精确到方法级别
                  within: 用来指定切入项目中的哪些组件中的哪些方法 精确到类级别 效率要高些
              -->
      <!--        <aop:pointcut id="pc" expression="execution(* cn.wsq.ssm.service.impl.*ServiceImpl.*(..))"/>-->
              <aop:pointcut id="pc" expression="within(cn.wsq.ssm.service.impl.*ServiceImpl)"/>
              <!--配置切面 advice-ref:工厂中通知类的id,pointcut-ref:工厂切入点的唯一标识-->
              <aop:advisor advice-ref="myBeforeAdvice" pointcut-ref="pc"/>
              <aop:advisor advice-ref="logAdvice" pointcut-ref="pc"/>
              <!--对于aop:advisor和aop:aspect的区别 https://www.jianshu.com/p/40f79da0cdef -->
      <!--        <aop:aspect ref="myBeforeAdvice">-->
      <!--            <aop:before method="before" pointcut-ref="pointcut"/>-->
      <!--        </aop:aspect>-->
          </aop:config>
      
          <!--通过factoryBean创建复杂对象-->
          <bean id="calendar" class="cn.wsq.ssm.factorybean.CalendarFactoryBean"/>
      </beans>
      
    2. springmvc.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:mvc="http://www.springframework.org/schema/mvc"
             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/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
          <!--配置注解扫描控制器-->
          <context:component-scan base-package="cn.wsq.ssm.controller"/>
          <!--配置处理器映射器和处理器适配器-->
          <mvc:annotation-driven/>
          <!-- 将图片,js,css等静态资源排除在外 -->
          <mvc:default-servlet-handler/>
          <!--配置视图解析器-->
          <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
              <property name="prefix" value="/"/>
              <property name="suffix" value=".jsp"/>
          </bean>
      </beans>
      
    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>
        <display-name>ssm整合</display-name>
        <!--配置spring工厂监听器-->
        <listener>
          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        <!--配置spring工厂配置文件-->
        <context-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:config/spring.xml</param-value>
        </context-param>
        <!--配置springMVC核心servlet-->
        <servlet>
          <servlet-name>dispatcher</servlet-name>
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:config/springmvc.xml</param-value>
          </init-param>
          <load-on-startup>0</load-on-startup>
        </servlet>
        <servlet-mapping>
          <servlet-name>dispatcher</servlet-name>
          <url-pattern>/</url-pattern>
        </servlet-mapping>
        <!--解决post请求乱码-->
        <filter>
          <filter-name>characterEncodingFilter</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>
        </filter>
        <filter-mapping>
          <filter-name>characterEncodingFilter</filter-name>
          <url-pattern>/*</url-pattern>
        </filter-mapping>
      </web-app>
      
  • 相关阅读:
    SQL Server 添加数据库没有权限等
    网站图片优化的重要性与技巧方案
    5年前端经验小伙伴教你纯css3实现饼状图
    css3 斜切角/斜边的实现方式来自BAT大神的出品
    Validate表单验证插件之常用参数介绍
    html实现邮箱发送邮件_js发送邮件至指定邮箱功能
    css重设样式_清除浏览器的默认样式
    大厂前端工程师教你如何使用css3绘制任意角度扇形+动画
    WordPress教程之如何批量删除未引用(无用)的TAG标签
    css引入的方式有哪些_四种css的引入方式与特点
  • 原文地址:https://www.cnblogs.com/seekknowledge/p/13282077.html
Copyright © 2011-2022 走看看