Idea使用Maven搭建SSM框架Web项目

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/weixin_43611145/article/details/102702913

相关文章

1.SpringMVC搭建一个Web项目

链接地址: SpringMVC搭建一个Web项目

2.log4j.xml配置详解

链接地址: log4j.xml配置详解

3.logback.xml配置详解

链接地址:logback.xml配置详解

4.SSM框架搭建Web项目

链接地址:SSM框架搭建Web项目

本文可在SpringMVC搭建一个Web项目基础上配置,这里我所使用的环境是jdk1.8和Tomcat9.0.12,Idea版本为ultimate 2019.1。

1.基础概念

参照文章:SSM框架搭建Web项目

2.项目搭建

2.1 项目目录结构

在这里插入图片描述
在这里插入图片描述

2.2 新建项目

参考文章:Eclipse与Idea创建一个Maven的Java Web项目 第2.2.2节内容

2.3 添加配置文件

编辑pom.xml导入所需jar包,添加配置文件web.xml,applicationContext.xml,spring-mvc.xml,jdbc.properties,log4j.xml。jdbc.properties为连接数据库相关数据,log4j.xml为日志配置。

各配置文件中各标签解释可参考:SSM框架搭建Web项目 第2.2节内容

2.3.1 编辑pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>MavenMvc</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <spring.version>4.3.18.RELEASE</spring.version>
        <slf4j.version>1.7.28</slf4j.version>
        <log4j.version>1.2.17</log4j.version>
    </properties>

    <dependencies>
        <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-web</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</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-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-oxm</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${slf4j.version}</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.20</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.3</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.3</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.48</version>
        </dependency>

        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.4</version>
        </dependency>

    </dependencies>

    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>com/example/mapper/*.xml</include>
                </includes>
            </resource>
        </resources>
    </build>
    
</project>

2.3.2 添加web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
  <display-name>MavenMvc</display-name>

  <!-- 读取log4j配置文件 -->
  <context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>classpath:/config/log4j.xml</param-value>
  </context-param>
  <!-- 添加日志监听器 -->
  <listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
  </listener>

  <!-- 读取spring配置文件 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:/spring/applicationContext.xml</param-value>
  </context-param>
  <!-- 添加spring监听器 -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <!-- 配置DispatcherServlet -->
  <servlet>
    <servlet-name>springServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- servlet配置文件的位置 -->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:/spring/spring-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <!-- 拦截设置 -->
  <servlet-mapping>
    <servlet-name>springServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <!-- Spring字符集过滤器 -->
  <filter>
    <filter-name>encoding</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>encoding</filter-name>
    <!--"/*"表示拦截所有的请求 -->
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

2.3.3 添加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-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

        <description>spring配置</description>

        <!-- 使用annotation 自动注册bean, 并保证@Required、@Autowired的属性被注入 -->
        <context:component-scan base-package="com.example">
            <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        </context:component-scan>

        <!-- 数据库连接池 -->
        <!-- 加载jdbc.properties配置文件中的数据库连接信息 -->
        <context:property-placeholder location="/WEB-INF/config/jdbc.properties"/>
        <!-- 配置数据源 -->
        <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
            <property name="driverClassName" value="${jdbc.driver}"/>
            <property name="url" value="${jdbc.url}"/>
            <property name="username" value="${jdbc.username}"/>
            <property name="password" value="${jdbc.password}"/>

            <!-- 配置初始化大小、最小、最大 -->
            <property name="initialSize" value="${jdbc.pool.initialPoolSize}"/>
            <property name="minIdle" value="${jdbc.pool.minPoolSize}"/>
            <property name="maxActive" value="${jdbc.pool.maxPoolSize}"/>

            <!-- 配置获取连接等待超时的时间 -->
            <property name="maxWait" value="${jdbc.pool.maxIdleTime}"/>

            <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
            <property name="timeBetweenEvictionRunsMillis" value="${jdbc.pool.checkoutTimeout}"/>

            <!-- 配置监控统计拦截的filters:stat(监控)、wall(sql防火墙)、log4j(监控日志输出) -->
            <property name="filters" value="wall,stat,log4j"/>
        </bean>

        <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource"/>
            <!-- 自动扫描mapping.xml文件 -->
            <property name="mapperLocations" value="classpath:com/example/mapper/*.xml"/>
        </bean>

        <!-- DAO接口所在包名,Spring会自动查找其下的类 ,自动扫描了所有的XxxxMapper.xml对应的mapper接口文件,只要Mapper接口类和Mapper映射文件对应起来就可以了-->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="com.example.dao"/>
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        </bean>

        <!-- 配置事务管理器,对dataSource 数据源进行事务管理 -->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"/>
        </bean>

        <!-- 启用对事务注解的支持 -->
        <!-- 配置 Annotation 驱动,扫描@Transactional注解的类定义事务  -->
        <tx:annotation-driven transaction-manager="transactionManager"/>

</beans>

2.3.4 添加spring-mvc.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: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/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

    <!-- 自动扫描且只扫描@Controller -->
    <context:component-scan base-package="com.example" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <!-- 配置视图解析器 如何把handler 方法返回值解析为实际的物理视图 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- jsp路径前缀 -->
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <!-- jsp路径后缀 -->
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!-- 注解驱动 -->
    <mvc:annotation-driven/>

    <!-- 拦截器配置,根据自身业务配置,若无需求可不配置 -->
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <bean class="com.example.interceptor.RequestMappingInterceptor"></bean>
        </mvc:interceptor>
    </mvc:interceptors>

    <!-- 容器默认的DefaultServletHandler处理 所有静态内容与无RequestMapping处理的URL-->
    <mvc:default-servlet-handler/>

    <!-- SpringMVC上传文件时,需要配置MultipartResolver处理器 -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="UTF-8" />
        <!-- 指定所上传文件的总大小不能超过10485760000B。注意maxUploadSize属性的限制不是针对单个文件,而是所有文件的容量之和 -->
        <property name="maxUploadSize" value="10485760000"></property>
        <property name="maxInMemorySize" value="40960"></property>
    </bean>

    <!-- 静态资源映射 -->
    <mvc:resources mapping="/static/**" location="/WEB-INF/static/"/>
</beans>

2.3.5 添加jdbc.properties文件

具体配置详解可参考:SSM框架搭建Web项目 第2.2.4节内容进行修改。

2.3.6 添加log4j.xml文件

具体配置详解可参考:SSM框架搭建Web项目 第2.2.5节内容进行修改。

2.4 配置拦截器

具体配置详解可参考:SSM框架搭建Web项目 第2.3节内容进行修改。

2.5 添加Controller,Service,Dao,Mapper,Model,Jsp等文件

具体配置详解可参考:SSM框架搭建Web项目 第2.4节内容。

2.6 运行及结果

在这里插入图片描述
控制台日志信息
在这里插入图片描述

3.可能出现的问题及解决方法

部分未列出问题可参考:SSM框架搭建Web项目 第3节内容。

具体问题请根据实际原因解决,以下问题为我部署项目时的问题及解决方法,仅供参考。

3.1 问题:Establishing SSL connection without server’s identity……

若出现以下错误
在这里插入图片描述

Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.

翻译如下:

不建议在没有服务器身份验证的情况下建立SSL连接。 根据MySQL 5.5.45 +,5.6.26 +和5.7.6+的要求,如果未设置显式选项,则默认情况下必须建立SSL连接。 为了与不使用SSL的现有应用程序兼容,将verifyServerCertificate属性设置为’false’。 您需要通过设置useSSL = false来显式禁用SSL,或者设置useSSL = true并为服务器证书验证提供信任库。

查看了下我最开始的jdbc.properties部分配置如下

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=root

解决方法为在jdbc.url配置useSSL=false即可,修改后如下所示

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?useSSL=false
jdbc.username=root
jdbc.password=root

3.2 问题:org.springframework.beans.factory.UnsatisfiedDependencyException:……[classpath:com/example/mapper/*.xml]: ……cannot be resolved to URL because it does not exist

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'loginService': Unsatisfied dependency expressed through field 'loginDao'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loginDao' defined in file [E:\IdeaProjects\MavenMvc\out\artifacts\MavenMvc_war_exploded\WEB-INF\classes\com\example\dao\LoginDao.class]: Cannot resolve reference to bean 'sqlSessionFactory' while setting bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [spring/applicationContext.xml]: Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type 'java.lang.String' to required type 'org.springframework.core.io.Resource[]' for property 'mapperLocations'; nested exception is java.lang.IllegalArgumentException: Could not resolve resource location pattern [classpath:com/example/mapper/*.xml]: class path resource [com/example/mapper/] cannot be resolved to URL because it does not exist

查看了一下项目的映射文件夹,并没有生成mapper文件夹
在这里插入图片描述
解决方法是在pom.xml中project节点下添加以下配置

<build>
    <resources>
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>com/example/mapper/*.xml</include>
        </includes>
      </resource>
    </resources>
  </build>

重新编译即可
在这里插入图片描述

3.3 问题:org.apache.catalina.core.StandardContext.listenerStart Error configuring application listener of class [org.springframework.web.util.Log4jConfigListener]

org.apache.catalina.core.StandardContext.listenerStart Error configuring application listener of class [org.springframework.web.util.Log4jConfigListener]

在这里插入图片描述
查看了一下项目的映射文件夹,并没有生成依赖的lib文件夹

在这里插入图片描述
右上角在这里插入图片描述打开Project Structrue

在下图位置右键->Put into Output Root
在这里插入图片描述
在这里插入图片描述
重新编译即可
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_43611145/article/details/102702913