美文网首页程序员
SSM框架搭建总结(重点)

SSM框架搭建总结(重点)

作者: 凡哥爱丽姐 | 来源:发表于2021-01-20 08:59 被阅读0次

    1、添加jar包

    <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.11</version>
          <scope>test</scope>
        </dependency>
    
        <!-- 加入ServletAPI -->
        <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>javax.servlet-api</artifactId>
          <version>3.1.0</version>
          <scope>provided</scope>
        </dependency>
    
        <!-- MySQL依赖 start -->
        <dependency>
          <groupId>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
          <version>5.1.38</version>
        </dependency>
    
        <!-- 加入MyBatis 依赖 start -->
        <dependency>
          <groupId>org.mybatis</groupId>
          <artifactId>mybatis</artifactId>
          <version>3.2.8</version>
        </dependency>
    
        <!-- 引入Spring(包含SpringMVC) 依赖 start -->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-core</artifactId>
          <version>5.0.8.RELEASE</version>
        </dependency>
    
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-web</artifactId>
          <version>5.0.8.RELEASE</version>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-oxm</artifactId>
          <version>5.0.8.RELEASE</version>
        </dependency>
       <!--事务依赖包-->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-tx</artifactId>
          <version>5.0.8.RELEASE</version>
        </dependency>
    
    
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-jdbc</artifactId>
          <version>5.0.8.RELEASE</version>
        </dependency>
    
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-webmvc</artifactId>
          <version>5.0.8.RELEASE</version>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-aop</artifactId>
          <version>5.0.8.RELEASE</version>
        </dependency>
    
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context-support</artifactId>
          <version>5.0.8.RELEASE</version>
        </dependency>
    
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-test</artifactId>
          <version>5.0.8.RELEASE</version>
        </dependency>
    
        <!-- 引用c3p0 依赖 start
          <dependency>
              <groupId>com.mchange</groupId>
              <artifactId>c3p0</artifactId>
              <version>0.9.2.1</version>
          </dependency>
           -->
        <!-- 引用插件依赖:MyBatis整合Spring,如果mybatis版本在3.4及以上版本
           mybatis-spring的版本要在1.3以上   -->
        <dependency>
          <groupId>org.mybatis</groupId>
          <artifactId>mybatis-spring</artifactId>
          <version>1.2.2</version>
        </dependency>
    
        <!-- JSTL -->
        <dependency>
          <groupId>jstl</groupId>
          <artifactId>jstl</artifactId>
          <version>1.2</version>
        </dependency>
    
        <!-- 德鲁伊数据连接池 -->
        <dependency>
          <groupId>com.alibaba</groupId>
          <artifactId>druid</artifactId>
          <version>1.0.9</version>
        </dependency>
    
        <!-- pagehelper -->
        <dependency>
          <groupId>com.github.pagehelper</groupId>
          <artifactId>pagehelper</artifactId>
          <version>4.1.6</version>
        </dependency>
        <!--处理json-->
        <dependency>
          <groupId>com.fasterxml.jackson.core</groupId>
          <artifactId>jackson-databind</artifactId>
          <version>2.9.5</version>
        </dependency>
        <!--导出excel-->
        <dependency>
          <groupId>org.apache.poi</groupId>
          <artifactId>poi</artifactId>
          <version>3.6</version>
        </dependency>
        <!--javaee-->
        <dependency>
          <groupId>javax</groupId>
          <artifactId>javaee-api</artifactId>
          <version>7.0</version>
          <scope>provided</scope>
        </dependency>
        <!--文件上传下载-->
        <dependency>
          <groupId>commons-fileupload</groupId>
          <artifactId>commons-fileupload</artifactId>
          <version>1.3.1</version>
        </dependency>
    
        <dependency>
          <groupId>commons-io</groupId>
          <artifactId>commons-io</artifactId>
          <version>2.4</version>
        </dependency>
    
    
        <!--- Activiti依赖导入 -->
        <dependency>
          <groupId>org.activiti</groupId>
          <artifactId>activiti-spring</artifactId>
          <version>5.18.0</version>
        </dependency>
        <dependency>
          <groupId>org.activiti</groupId>
          <artifactId>activiti-engine</artifactId>
          <version>5.18.0</version>
          <exclusions>
            <exclusion>
              <artifactId>slf4j-api</artifactId>
              <groupId>org.slf4j</groupId>
            </exclusion>
            <exclusion>
              <artifactId>spring-beans</artifactId>
              <groupId>org.springframework</groupId>
            </exclusion>
            <exclusion>
              <artifactId>jackson-core-asl</artifactId>
              <groupId>org.codehaus.jackson</groupId>
            </exclusion>
            <exclusion>
              <artifactId>commons-lang3</artifactId>
              <groupId>org.apache.commons</groupId>
            </exclusion>
          </exclusions>
        </dependency>
      </dependencies>
    

    2、配置web.xml文件

    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns="http://java.sun.com/xml/ns/javaee"
             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>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>
      </servlet>
      <servlet-mapping>
        <servlet-name>springMvc</servlet-name>
        <url-pattern>/</url-pattern>
      </servlet-mapping>
    
      <filter>
        <filter-name>aa</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>aa</filter-name>
        <url-pattern>/*</url-pattern>
      </filter-mapping>
    </web-app>
    

    3、配置mybatis-config文件

    <?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>
        <plugins>
            <!-- 分页插件配置 PageHelper4.1.6 -->
            <plugin interceptor="com.github.pagehelper.PageHelper">
                <property name="dialect" value="mysql"/>
            </plugin>
        </plugins>
    </configuration>
    

    4、配置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"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xmlns:tx="http://www.springframework.org/schema/tx"
           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/mvc
                               http://www.springframework.org/schema/mvc/spring-mvc.xsd
                               http://www.springframework.org/schema/tx
                               http://www.springframework.org/schema/tx/spring-tx.xsd">
    
        <!--1、创建数据源,使用spring连接数据库-->
        <bean id="db" class="com.alibaba.druid.pool.DruidDataSource">
            <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
            <property name="url" value="jdbc:mysql://localhost:3306/test"></property>
            <property name="username" value="root"></property>
            <property name="password" value="root"></property>
        </bean>
    
        <!--2、扫描注解包-->
        <context:component-scan base-package="com"></context:component-scan>
    
        <!--3、创建视图解析器-->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/"></property>
            <property name="suffix" value=".jsp"></property>
        </bean>
    
        <!--4、加载注解驱动,会自动注册RequestMappingHandlerMapping与RequestMappingHandlerAdapter两个Bean,这是Spring MVC为@Controller分发请求所必需的-->
        <mvc:annotation-driven></mvc:annotation-driven>
    
        <!--5、创建sqlSession工厂-->
        <bean id="fac" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="db"></property>
            <property name="mapperLocations" value="classpath:mapper/*.xml"></property>
            <property name="configLocation" value="classpath:mybatis-config.xml"></property>
        </bean>
    
    <!--    &lt;!&ndash;6、使用dao层实现类时,需要得到sqlSessionTemplate&ndash;&gt;-->
    <!--    <bean id="temp" class="org.mybatis.spring.SqlSessionTemplate">-->
    <!--        <constructor-arg index="0" ref="fac"></constructor-arg>-->
    <!--    </bean>-->
    <!--    -->
    
        <!--6、省略实现类  省略dao层实现类(使用MapperScannerConfigurer替代SqlSessionTemplate):-->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <!--扫描com.fan.dao这个dao层-->
            <property name="basePackage" value="com.fan.dao"></property>
            <property name="sqlSessionFactoryBeanName" value="fac"></property>
        </bean>
    
        <!--7、配置事务-->
        <bean id="tx" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="db"></property>
        </bean>
        <tx:advice id="ad" transaction-manager="tx"></tx:advice>
    
        <!--8、配置静态资源-->
        <mvc:default-servlet-handler></mvc:default-servlet-handler>
    </beans>
    

    5、系统的整体架构图:

    整体架构

    相关文章

      网友评论

        本文标题:SSM框架搭建总结(重点)

        本文链接:https://www.haomeiwen.com/subject/jueutktx.html