1. 程式人生 > 其它 >配置一個簡單的傳統SSM專案

配置一個簡單的傳統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