SSM框架及項目搭建總結


SSM框架及項目搭建總結

1、Mybatis

  • MyBatis是一款優秀的持久層框架,Dao層,Service層,Controller層……
  • MyBatis避免了幾乎所有的JDBC代碼和手動設置參數以及獲取結果集。
  • MyBatis 可以使用簡單的XML或注解來配置和映射原生類型、接口和 Java 的 POJO。
  • sql和代碼的分離,提高了可維護性。
  • 提供xml標簽,支持編寫動態sql
  • ……
  • 詳情見:https://www.cnblogs.com/mc-blog/p/15145744.html

2、Spring

  • Rod Jahnson,Spring Framework創始人,著名作者,悉尼大學的博士,專業是音樂學。
  • spring理念:使現有的技術更加容易使用,本身是一個大雜燴,整合了現有的技術框架!
  • 一個輕量級的,開源的,免費的框架(容器)。
  • 支持事務的處理,對框架整合的支持!
  • ***總結一句話:Spring就是一個輕量級的控制反轉(IOC)和面向切面編程(AOP)的框架!
  • 詳情見:https://www.cnblogs.com/mc-blog/p/15139093.html

3、SpringMVC

  • MVC是模型(Model)、視圖(View)、控制器(Controller)的簡寫,是一種軟件設計規范。
  • 是將業務邏輯、數據、顯示分離的方法來組織代碼。
  • MVC主要作用是降低了視圖與業務邏輯間的雙向偶合
  • MVC不是一種設計模式,MVC是一種架構模式。當然不同的MVC存在差異。
  • Spring MVC是Spring Framework的一部分,是基於Java實現MVC的輕量級Web框架。
  • 詳情見:https://www.cnblogs.com/mc-blog/p/15150530.html

4、SSM項目搭建所需的jar包依賴

<?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>org.example</groupId>
    <artifactId>SSM-Vue框架開發</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>OnlineSSM</module>
    </modules>

    <!-- 集中定義依賴版本號 -->
    <properties>
        <spring.version>5.2.5.RELEASE</spring.version>
        <mybatis.version>3.5.7</mybatis.version>
        <mybatis.spring.version>2.0.6</mybatis.spring.version>
        <servlet-api.version>3.0.1</servlet-api.version>
        <jsp-api.version>2.0</jsp-api.version>
        <mysql.version>8.0.21</mysql.version>
        <c3p0.version>0.9.5.5</c3p0.version>
        <jstl.version>1.2</jstl.version>
        <junit.version>4.12</junit.version>
        <lombok.version>1.18.20</lombok.version>
    </properties>

    <dependencies>
        <!-- Mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>${mybatis.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>${mybatis.spring.version}</version>
        </dependency>

        <!--Spring-->
        <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>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>${servlet-api.version}</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jsp-api</artifactId>
            <version>${jsp-api.version}</version>
        </dependency>
        <!-- JSP相關 -->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>${jstl.version}</version>
        </dependency>
        <!-- MySql -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>

        <!--數據庫連接池:-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.10</version>
        </dependency>

        <!--json格式數據的處理-->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.5</version>
        </dependency>

        <!-- 用於測試 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.3.8.RELEASE</version>
        </dependency>

        <!--生成set,get,無參,有參等方法-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
        </dependency>

        <!--分頁插件-->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.1.10</version>
        </dependency>
    </dependencies>

    <!--靜態資源導出問題,識別所有的配置文件-->
    <!--  在build中配置resources,來防止我們資源導出失敗的問題  -->
    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>

</project>

5、SSM項目搭建所需的配置文件

1.1 spring-dao.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"
       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">

    <!-- 關聯數據庫配置文件 -->
    <context:property-placeholder ignore-unresolvable="true" location="classpath:db.properties"/>

    <!-- 數據源配置, 使用 Druid 數據庫連接池 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <!-- 數據源驅動類可不寫,Druid默認會自動根據URL識別DriverClass -->
        <property name="driverClassName" value="${jdbc.driverClass}"/>
        <!-- 基本屬性 url、user、password -->
        <property name="url" value="${jdbc.connectionURL}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>

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

        <!-- 配置獲取連接等待超時的時間 -->
        <property name="maxWait" value="60000"/>

        <!-- 配置間隔多久才進行一次檢測,檢測需要關閉的空閑連接,單位是毫秒 -->
        <property name="timeBetweenEvictionRunsMillis" value="60000"/>

        <!-- 配置一個連接在池中最小生存的時間,單位是毫秒 -->
        <property name="minEvictableIdleTimeMillis" value="300000"/>

        <property name="validationQuery" value="${jdbc.testSql}"/>
        <property name="testWhileIdle" value="true"/>
        <property name="testOnBorrow" value="false"/>
        <property name="testOnReturn" value="false"/>

        <!-- 配置監控統計攔截的filters -->
        <property name="filters" value="stat"/>

    </bean>

    <!-- 配置 SqlSession -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--配置數據源-->
        <property name="dataSource" ref="dataSource"/>
        <!-- 用於配置對象關系映射配置文件所在目錄 -->
        <property name="mapperLocations" value="classpath:/mapper/**/*.xml"/>
        <!--配置MyBatis的核心配置文件-->
        <property name="configLocation" value="classpath:/mybatis-config.xml"/>

        <!--        &lt;!&ndash; other configuration &ndash;&gt;-->
        <!--        <property name="plugins">-->
        <!--            <array>-->
        <!--                <bean class="com.github.pagehelper.PageInterceptor">-->
        <!--                    <property name="properties">-->
        <!--                        &lt;!&ndash; config params as the following &ndash;&gt;-->
        <!--                        <value>-->
        <!--                            helperDialect=mysql-->
        <!--                            reasonable=true-->
        <!--                        </value>-->
        <!--                    </property>-->
        <!--                </bean>-->
        <!--            </array>-->
        <!--        </property>-->
    </bean>

    <!--配置mapper/dao接口的掃描器,動態的實現了mapper/dao接口可以注入到Spring容器中!-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--注入sqlSessionFactory -->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <!--配置mapper/dao文件的掃描器-->
        <property name="basePackage" value="com.mc.mapper"/>
    </bean>

</beans>

1.2 spring-service.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
        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
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">

<!--開啟注解掃描-->
    <context:annotation-config />
    <!--設置業務邏輯層的包掃描器,目的是在指定的路徑下,使用@Service注解的類, spring負責創建對象,並添加依賴-->
    <!--不掃描Controller注解-->
    <context:component-scan base-package="com.mc">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
<!--    &lt;!&ndash;等價於如下設置&ndash;&gt;-->
<!--    <context:component-scan base-package="com.mc.service"/>-->

    <!-- 配置事務管理器,聲明式事務配置 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--注入數據源-->
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!-- 開啟事務注解驅動 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>

    <!--4.aop事務支持!-->
    <!--結合AOP實現事務的織入-->
    <!--配置事務通知-->
<!--    <tx:advice id="txAdvice" transaction-manager="transactionManager">-->
<!--        &lt;!&ndash;給哪些方法配置事務&ndash;&gt;-->
<!--        &lt;!&ndash;配置事務的傳播特性: propagation&ndash;&gt;-->
<!--        <tx:attributes>-->
<!--            <tx:method name="*select*" read-only="true"/>-->
<!--            <tx:method name="*find*" read-only="true"/>-->
<!--            <tx:method name="*search*" read-only="true"/>-->
<!--            <tx:method name="*get*" read-only="true"/>-->
<!--            <tx:method name="*insert*" propagation="REQUIRED"/>-->
<!--            <tx:method name="*add*" propagation="REQUIRED"/>-->
<!--            <tx:method name="*save*" propagation="REQUIRED"/>-->
<!--            <tx:method name="*delete*" propagation="REQUIRED"/>-->
<!--            <tx:method name="*remove*" propagation="REQUIRED"/>-->
<!--            <tx:method name="*clear*" propagation="REQUIRED"/>-->
<!--            <tx:method name="*update*" propagation="REQUIRED"/>-->
<!--            <tx:method name="*modify*" propagation="REQUIRED"/>-->
<!--            <tx:method name="*change*" propagation="REQUIRED"/>-->
<!--            <tx:method name="*" propagation="SUPPORTS"/>-->
<!--        </tx:attributes>-->
<!--    </tx:advice>-->

<!--    &lt;!&ndash;完成切面和切入點的織入&ndash;&gt;-->
<!--    <aop:config>-->
<!--        <aop:pointcut id="txPointCut" expression="execution(* com.mc.service.*.*(..))"/>-->
<!--        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>-->
<!--    </aop:config>-->

</beans>

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

    <description>Spring MVC Configuration</description>

    <!-- 默認的注解映射的支持 -->
    <mvc:annotation-driven/>

    <!-- 靜態資源映射 -->
    <mvc:resources mapping="/static/**" location="/static/" cache-period="31536000"/>
<!--    <mvc:default-servlet-handler/>-->

    <!-- 使用 Annotation 自動注冊 Bean,只掃描 @Controller -->
    <context:component-scan base-package="com.mc" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
<!--    等價於如下設置-->
<!--    <context:component-scan base-package="com.mc.controller"/>-->

    <!--設置視圖解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="viewResolver">
        <!--前綴-->
        <property name="prefix" value="/WEB-INF/views/"/>
        <!--后綴-->
        <property name="suffix" value=".jsp"/>
    </bean>

<!--    &lt;!&ndash;設置文件上傳的核心組件&ndash;&gt;-->
<!--    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">-->
<!--        &lt;!&ndash;請求的編碼格式,必須和jsp的pageEncoding屬性一致,以便正確讀取表單的內容,默認為ISO-8859-1 &ndash;&gt;-->
<!--        <property name="defaultEncoding" value="utf-8"/>-->
<!--        &lt;!&ndash; 上傳文件大小上限,單位為字節(10485760=10M ) &ndash;&gt;-->
<!--        <property name="maxUploadSize" value="10485760"/>-->
<!--        <property name="maxInMemorySize" value="40960"/>-->
<!--    </bean>-->

    <!--跨域問題-->
    <mvc:cors>
        <mvc:mapping path="/**"
                     allowed-origins="*"
                     allowed-methods="POST, GET, OPTIONS, DELETE, PUT,PATCH"
                     allowed-headers="Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With"
                     allow-credentials="true"/>
    </mvc:cors>

</beans>

1.4 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>
    <!-- 全局參數 -->
    <settings>
        <!-- 打印 SQL 語句 -->
        <setting name="logImpl" value="STDOUT_LOGGING" />

        <!-- 使全局的映射器啟用或禁用緩存。 -->
        <setting name="cacheEnabled" value="false"/>

        <!-- 全局啟用或禁用延遲加載。當禁用時,所有關聯對象都會即時加載。 -->
        <setting name="lazyLoadingEnabled" value="true"/>

        <!-- 當啟用時,有延遲加載屬性的對象在被調用時將會完全加載任意屬性。否則,每種屬性將會按需要加載。 -->
        <setting name="aggressiveLazyLoading" value="true"/>

        <!-- 是否允許單條 SQL 返回多個數據集 (取決於驅動的兼容性) default:true -->
        <setting name="multipleResultSetsEnabled" value="true"/>

        <!-- 是否可以使用列的別名 (取決於驅動的兼容性) default:true -->
        <setting name="useColumnLabel" value="true"/>

        <!-- 允許 JDBC 生成主鍵。需要驅動器支持。如果設為了 true,這個設置將強制使用被生成的主鍵,有一些驅動器不兼容不過仍然可以執行。 default:false  -->
        <setting name="useGeneratedKeys" value="false"/>

        <!-- 指定 MyBatis 如何自動映射 數據基表的列 NONE:不映射 PARTIAL:部分 FULL:全部  -->
        <setting name="autoMappingBehavior" value="PARTIAL"/>

        <!-- 這是默認的執行類型 (SIMPLE: 簡單; REUSE: 執行器可能重復使用prepared statements語句;BATCH: 執行器可以重復執行語句和批量更新) -->
        <setting name="defaultExecutorType" value="SIMPLE"/>

        <!-- 使用駝峰命名法轉換字段。 -->
        <setting name="mapUnderscoreToCamelCase" value="true"/>

        <!-- 設置本地緩存范圍 session:就會有數據的共享 statement:語句范圍 (這樣就不會有數據的共享 ) defalut:session -->
        <setting name="localCacheScope" value="SESSION"/>

        <!-- 設置 JDBC 類型為空時,某些驅動程序 要指定值, default:OTHER,插入空值時不需要指定類型 -->
        <setting name="jdbcTypeForNull" value="NULL"/>

    </settings>

    <!-- 用於配置對應實體類所在的包,多個 package 之間可以用 ',' 號分割 -->
    <!--  可以給實體類起別名  -->
    <typeAliases>
        <package name="com.mc.pojo"/>
    </typeAliases>

    <plugins>
        <!-- com.github.pagehelper為PageHelper類所在包名 -->
        <plugin interceptor="com.github.pagehelper.PageInterceptor">
            <!-- 使用下面的方式配置參數,后面會有所有的參數介紹 -->
            <property name="helperDialect" value="mysql"/>
            <property name="reasonable" value="true"/>
        </plugin>
    </plugins>

</configuration>

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

    <import resource="classpath:spring-dao.xml"/>
    <import resource="classpath:spring-service.xml"/>
    <import resource="classpath:spring-mvc.xml"/>

</beans>

1.6 db.properties

# JDBC
jdbc.driverClass=com.mysql.cj.jdbc.Driver
jdbc.connectionURL=jdbc:mysql://localhost:3306/online?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
jdbc.username=root
jdbc.password=mc123456
# JDBC Pool
jdbc.pool.init=1
jdbc.pool.minIdle=3
jdbc.pool.maxActive=20
# JDBC Test
jdbc.testSql=SELECT 'x' FROM DUAL

1.7 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">

    <!--添加字符編碼過濾器-->
    <filter>
        <filter-name>encodingFilter</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>
        <init-param>
            <param-name>forceRequestEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>forceResponseEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!--注冊SpringMVC框架,請求分發器,前端控制器-->
    <!--配置DispatchServlet:這個是SpringMVC的核心,請求分發器,前端控制器-->
    <servlet>
        <servlet-name>springServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--DispatchServlet要綁定(關聯)一個springmvc的配置文件:【servlet-name】-servlet.xml-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:/spring-mvc*.xml</param-value>
        </init-param>
        <!--啟動級別-1-->
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>springServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!--注冊Spring框架-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-*.xml</param-value>
    </context-param>

    <!--Session-->
    <session-config>
        <session-timeout>15</session-timeout>
    </session-config>

    <welcome-file-list>
        <welcome-file>static/index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

</web-app>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM