天天看點

配置一個簡單的傳統SSM項目

早期的java後端項目都是傳統的Spring-SpringMVC-Mybatis項目,打成一個war包丢入tomcat容器運作。但是随着技術的發展,這種傳統的項目逐漸笨重,大量的xml配置檔案,存在項目之中,繁瑣的配置整合第三方架構的配置問題,導緻了開發和部署效率的降低。

背景

我們知道,從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

繼續閱讀