配置一个简单的传统SSM项目
阅读原文时间:2021年10月03日阅读:1

我们知道,从2002年开始,Spring一直在飞速的发展,如今已经成为了在Java EE开发中的标准,早期的项目都是传统的Spring-SpringMVC-Mybatis项目,打成一个war包丢入tomcat容器运行。但是随着技术的发展,这种传统的项目逐渐笨重,大量的xml配置文件,存在项目之中,繁琐的配置整合第三方框架的配置问题,导致了开发和部署效率的降低。所以才有了后来真香的SpringBoot项目。

尽管传统SSM项目开发很笨重,但是仍有企业在继续使用,而且SpringBoot项目只是简化了它,SSM对于java后端开发来说,仍是要必须学习的。这有助于更好的过度到SpringBoot与后面的SpringCloud之中。

下面就编写SSM项目基本的配置文件(确实比较庞大与繁琐的~~)

我们知道 所有的bean都要交于Spring IOC 去托管,Spring的配置文件一般命名为applicationContext.xml,如果把所有的bean都配置到这个文件中,将会显得异常臃肿与杂乱……于是借鉴MVC分层架构将applicatinContext.xml一分为三,各司其职。

资源文件结构

## resources资源目录

- mapper文件夹                            ----mapper.xml文件放置处
- applicationContext.xml        ----Spring配置文件
- jdbc.properties                        ----数据库配置文件
- mybatis-config.xml                ----Mybatis配置文件
- spring-dao.xml                        ----dao层配置文件
- spring-mvc.xml                        ----web-mvc配置文件
- spring-service.xml                ----service层配置文件

applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
            https://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--  扫描所有的包  -->
    <!--    <context:component-scan base-package="com.qd"/>-->

    <!-- 引入各层的配置文件   -->
    <import resource="classpath:spring-dao.xml"/>
    <import resource="classpath:spring-service.xml"/>
    <import resource="classpath:spring-mvc.xml"/>

</beans>

jdbc.properties

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url= jdbc:mysql://localhost:3306/ssmdb?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
jdbc.username=root
jdbc.password=root

mybatis-config.xml

<?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>
    <!-- 配置数据源,交于spring   -->
    <!-- 打印sql日志   -->
    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>

    <typeAliases>
        <package name="com.qd.pojo"/>
    </typeAliases>

    <!--     扫描mapper.xml文件 交于spring去做  spring-dao已经配置 -->
    <!--    <mappers>-->
    <!--        <mapper resource="com/qd/mapper/mapper/userMapper.xml"/>-->
    <!--    </mappers>-->
</configuration>

spring-dao.xml

<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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
            https://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            https://www.springframework.org/schema/context/spring-context.xsd">

    <!--  关联数据库配置文件  -->
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <!-- 使用jdbcTemplate用的   -->
    <!--    &lt;!&ndash; 扫描包   &ndash;&gt;-->
    <!--    <context:component-scan base-package="com.qd.dao"/>-->
    <!--    <bean id="dataSource1" class="org.springframework.jdbc.datasource.DriverManagerDataSource">-->
    <!--        <property name="driverClassName" value="${jdbc.driver}"/>-->
    <!--        <property name="url" value="${jdbc.url}"/>-->
    <!--        <property name="username" value="${jdbc.username}"/>-->
    <!--        <property name="password" value="${jdbc.password}"/>-->
    <!--    </bean>-->
    <!--    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">-->
    <!--        <property name="dataSource" ref="dataSource1"/>-->
    <!--    </bean>-->

    <!--  数据库连接池  -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <!--配置数据库连接池的最小连接接数、最大连接数、初始连接数、失败重连次数、连接超时时间-->
        <property name="maxPoolSize" value="15"></property>
        <property name="minPoolSize" value="5"></property>
        <property name="initialPoolSize" value="5"></property>
        <property name="acquireIncrement" value="2"></property>
        <property name="checkoutTimeout" value="10000"></property>
    </bean>

    <!--  sqlSessionFactory  -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--  绑定mybatis配置文件      -->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <!--   扫描pojo包     -->
        <property name="typeAliasesPackage" value="com.qd.pojo"/>
        <!--   扫描mapper xml文件     -->
        <property name="mapperLocations" value="classpath:mapper/*.xml"/>
    </bean>

    <!-- 配置dao接口扫描,动态注入到spring中   -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <property name="basePackage" value="com.qd.mapper"/>
    </bean>

</beans>

spring-mvc.xml

<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
            https://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            https://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/mvc
            https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!--  扫描包 -->
    <context:component-scan base-package="com.qd.controller"/>
    <!--注解驱动-->
    <mvc:annotation-driven/>
    <!--静态资源过滤    -->
    <mvc:default-servlet-handler/>
    <!-- 视图解析器  -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--  <property name="prefix" value="/web/"/>-->
        <property name="suffix" value=".html"/>
    </bean>

    <!--配置拦截器-->
    <!--    <mvc:interceptors>-->
    <!--        <mvc:interceptor>-->
    <!--            &lt;!&ndash;对哪些资源执行拦截操作&ndash;&gt;-->
    <!--            <mvc:mapping path="/**"/>-->
    <!--            <bean class="自定义拦截器的包地址"/>-->
    <!--            &lt;!&ndash;配置哪些资源排除拦截操作&ndash;&gt;-->
    <!--            <mvc:exclude-mapping path="/"/>-->
    <!--        </mvc:interceptor>-->
    <!--    </mvc:interceptors>-->

    <!--  文件上传解析器  -->
    <!--    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">-->
    <!--        &lt;!&ndash;  单个文件上传的大小     &ndash;&gt;-->
    <!--        <property name="maxUploadSizePerFile" value="10240"/>-->
    <!--        &lt;!&ndash;默认编码&ndash;&gt;-->
    <!--        <property name="defaultEncoding" value="UTF-8"/>-->
    <!--    </bean>-->

</beans>

spring-service.xml

<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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
            https://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            https://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/aop
            https://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- 扫描service下的包   -->
    <context:component-scan base-package="com.qd.service"/>
    <!-- 将service层的业务类注入到spring 通过注解或者xml实现   -->

    <!-- 声明式事务配置   -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--  注入数据源      -->
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!-- aop事务支持   -->
    <!-- 日志切面类   -->
    <!--    <bean id="logAspect" class="com.qd.aspect.LogAspect"/>-->
    <!--    &lt;!&ndash;打印日志&ndash;&gt;-->
    <!--    <aop:config>-->
    <!--        &lt;!&ndash; 注入切面类   &ndash;&gt;-->
    <!--        <aop:aspect ref="logAspect">-->
    <!--            &lt;!&ndash; 切入点:service实现类中的任意类任意方法&ndash;&gt;-->
    <!--            <aop:pointcut id="pointCut" expression="execution(* com.qd.service.impl.*ServiceImpl.*(..))"/>-->
    <!--            &lt;!&ndash;  环绕通知  &ndash;&gt;-->
    <!--            <aop:around method="aroundAdvice" pointcut-ref="pointCut"/>-->
    <!--            &lt;!&ndash;  异常通知   &ndash;&gt;-->
    <!--            <aop:after-throwing method="afterThrowing" pointcut-ref="pointCut" throwing="throwable"/>-->
    <!--        </aop:aspect>-->
    <!--    </aop:config>-->

    <bean id="LogAspect" class="com.qd.aspect.LogAspect"/>
    <!--  Aop切面开启注解支持  -->
    <aop:aspectj-autoproxy/>

</beans>

WEB-INF下的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<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_4_0.xsd"
         version="4.0">

    <!--web服务默认打开index.jsp文件,修改为index.html-->
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>

    <!-- DispatcherServlet   -->
    <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>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>

    <!--  session 过期时间  -->
    <session-config>
        <session-timeout>20</session-timeout>
    </session-config>
</web-app>

ps: 这些配置完成创建后,一个简单的SSM项目就能启动运行了,这些只是基础,需要什么尽管配置就行,有些配置也可使用注解代替。这样分层之后,确实清爽了许多呢!

一个成功启动的项目示例:https://chenyu6666.lanzoui.com/iSfMru6jkqb