搭建一個SSM專案

13678398293發表於2019-01-19

一 新建一個Maven工程

1.1 為什麼要用Maven搭建專案?
    Maven可以對專案依賴的jar包進行管理,需要的jar包只需要到Maven倉庫裡面去拿到版本資訊複製到pom.xml檔案即可。同時,它也能對專案進行編譯、測試、打包等功能。
1.2 新建一個Maven工程(這裡用的是Eclipse):
    1.2.1 點選File -> New Project -> Maven Project,點選Next;
    1.2.2 彈出一個介面,這裡直接點選Next,然後選擇webapp那一欄,點選Next;
    1.2.3 此時,將進入另一個介面,會叫你輸入一些有關專案的資訊。主要的分別是:
        Group Id: 這個可以看做專案的座標,一般為域和公司名組成
        Artifact Id: 這個可以看做成專案名
        Version: 這個是專案版本

點選Finish後,一個Maven專案就建立完成。此時,需要注意的是這個Maven專案可能用的是JDK1.5,這可能導致專案的目錄不正確,解決辦法就是:右鍵專案 -> 選擇Properties -> 選擇Java Build Path -> 選擇Libraries那一欄 -> 選擇JRE Library那一欄 -> Edit -> 把預設的JDK版本換成自己下載的版本即可。

二 進行Spring和SpringMVC還有Mybatis的整合

1.1 首先需要依賴整合SSM相關的jar包
 <properties>
     <spring.version>4.3.7.RELEASE</spring.version>
 </properties>
 <dependencies>
     <!-- 測試所需要的包 -->
     <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>4.12</version>
         <scope>test</scope>
     </dependency>
     <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-test</artifactId>
         <version>${spring.version}</version>
     </dependency>
     <!-- 日誌所需要的包 -->
     <dependency>
         <groupId>ch.qos.logback</groupId>
         <artifactId>logback-classic</artifactId>
         <version>1.2.3</version>
     </dependency>
     <!-- 資料庫所需要的包  -->
     <dependency>
         <groupId>mysql</groupId>
         <artifactId>mysql-connector-java</artifactId>
         <version>5.1.37</version>
         <scope>runtime</scope>
     </dependency>
     <dependency>
         <groupId>c3p0</groupId>
         <artifactId>c3p0</artifactId>
         <version>0.9.1.2</version>
     </dependency>

     <!-- Mybatis的包 -->
     <dependency>
         <groupId>org.mybatis</groupId>
         <artifactId>mybatis</artifactId>
         <version>3.4.2</version>
     </dependency>
     <dependency>
         <groupId>org.mybatis</groupId>
         <artifactId>mybatis-spring</artifactId>
         <version>1.3.1</version>
     </dependency>

     <!-- JavaWeb的包 -->
     <dependency>
         <groupId>taglibs</groupId>
         <artifactId>standard</artifactId>
         <version>1.1.2</version>
     </dependency>
     <dependency>
         <groupId>jstl</groupId>
         <artifactId>jstl</artifactId>
         <version>1.2</version>
     </dependency>
     
     <dependency>
         <groupId>com.fasterxml.jackson.core</groupId>
         <artifactId>jackson-databind</artifactId>
         <version>2.8.7</version>
     </dependency>
     <dependency>
         <groupId>javax.servlet</groupId>
         <artifactId>javax.servlet-api</artifactId>
         <version>3.1.0</version>
     </dependency>

     <!-- 4.Spring的包 -->
     <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-core</artifactId>
         <version>${spring.version}</version>
     </dependency>
     <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-beans</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-jdbc</artifactId>
         <version>${spring.version}</version>
     </dependency>
     <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-tx</artifactId>
         <version>${spring.version}</version>
     </dependency>
     <!-- SpringMVC的包 -->
     <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>
     <!-- AOP配置 -->
     <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-aop</artifactId>
         <version>4.3.10.RELEASE</version>
     </dependency>
     <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-aspects</artifactId>
         <version>4.3.10.RELEASE</version>
     </dependency>
     <dependency>
         <groupId>aopalliance</groupId>
         <artifactId>aopalliance</artifactId>
         <version>1.0</version>
     </dependency>
     <dependency>
         <groupId>org.aspectj</groupId>
         <artifactId>aspectjrt</artifactId>
         <version>1.8.10</version>
     </dependency>
     <dependency>
         <groupId>org.aspectj</groupId>
         <artifactId>aspectjweaver</artifactId>
         <version>1.8.10</version>
     </dependency>
     <dependency>
         <groupId>cglib</groupId>
         <artifactId>cglib-nodep</artifactId>
         <version>3.2.5</version>
     </dependency>
     <!-- Map工具類 -->
     <dependency>
         <groupId>commons-collections</groupId>
         <artifactId>commons-collections</artifactId>
         <version>3.2</version>
     </dependency>
     <!-- 圖片縮放的包 -->
     <dependency>
         <groupId>net.coobird</groupId>
         <artifactId>thumbnailator</artifactId>
         <version>0.4.8</version>
     </dependency>
     <!-- 檔案上傳的包 -->
     <dependency>
         <groupId>commons-fileupload</groupId>
         <artifactId>commons-fileupload</artifactId>
         <version>1.3.2</version>
     </dependency>
 </dependencies>

三 整理XML配置檔案

這裡我分為三個XML配置檔案,一個是spring-dao(資料庫的配置以及DAO層的掃描),第二個是spring-service(掃描Service層以及事務的配置),第三個是spring-web(掃描controller層和配置檔案檢視等)

  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">
    
    <!-- 配置相關的properties過程 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!-- 配置資料庫連線池 -->
    <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}" />

        <!-- c3p0連線池的私有屬性 -->
        <property name="maxPoolSize" value="30" />
        <property name="minPoolSize" value="10" />
        <!-- 關閉連線後不自動commit -->
        <property name="autoCommitOnClose" value="false" />
        <!-- 獲取連線超時時間 -->
        <property name="checkoutTimeout" value="10000" />
        <!-- 當獲取連線失敗重試次數 -->
        <property name="acquireRetryAttempts" value="2" />
    </bean>
    <!-- 配置連sqlSessionFactory物件 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 注入資料庫連線池 -->
        <property name="dataSource" ref="dataSource" />
        <!-- 配置MyBaties全域性配置檔案:mybatis-config.xml -->
        <property name="configLocation" value="classpath:mybatis-config.xml" />
        <!-- 掃描entity包 使用別名 -->
        <property name="typeAliasesPackage" value="com.yangxiang.pojo" />
        <!-- 掃描sql配置檔案:mapper需要的xml檔案 -->
        <property name="mapperLocations" value="classpath:mapper/*.xml" />
    </bean>
    <!-- 配置DAO介面包 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 注入sqlSessionFactory -->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
        <!-- 給出需要掃描Dao介面包 -->
        <property name="basePackage" value="com.yangxiang.dao" />
    </bean>        
</beans>

其中spring-dao引用了定義的jdbc.properties和mybatis-config.xml
jdbc.properties內容如下:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/course?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=password

mybatis-config.xml內容如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC   
    "-//mybatis.org//DTD Mapper 3.0//EN"  
    "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>
    <!-- 配置全域性屬性 -->
    <settings>
        <!-- 是否使用主鍵生成 -->
        <setting name="useGeneratedKeys" value="true" />
        <!-- 別名比列標籤有限 -->
        <setting name="useColumnLabel" value="true"/>
        <!-- 是否使用下劃線與駝峰命名轉換 -->
        <setting name="mapUnderscoreToCamelCase" value="true" />
    </settings>
</configuration>
  1. 配置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:tx="http://www.springframework.org/schema/tx"
    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/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd">
    
    <!-- 掃描service包下所有使用註解的型別 -->
    <context:component-scan base-package="com.yangxiang.service" />
    
     <!-- 配置事務管理器 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- 注入資料庫連線池 -->
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- 配置基於註解的宣告式事務 -->
    <tx:annotation-driven transaction-manager="transactionManager" />
  
</beans>
  1. 配置spring-web.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"
    xmlns:aop="http://www.springframework.org/schema/aop"
    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-3.2.xsd   
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    <!-- 配置SpringMVC -->

    <!-- 1.開啟SpringMVC註解模式 -->
    <mvc:annotation-driven />
    <context:component-scan
        base-package="com.yangxiang.web" />

    <!-- 定義靜態資源的處理 -->
    <!-- 2.靜態資源預設servlet配置 (1)加入對靜態資源的處理:js,gif,png (2)允許使用"/"做整體對映 -->
    <mvc:resources mapping="/resources/**"
        location="/resources/" />
    <mvc:default-servlet-handler />
    <!-- 定義檢視解析器 -->
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/html/"></property>
        <property name="suffix" value=".html"></property>
    </bean>
    <!-- 檔案上傳解析器 -->
    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="utf-8"></property>
        <property name="maxUploadSize" value="10485760000"></property><!-- 最大上傳檔案大小 -->
        <property name="maxInMemorySize" value="10960"></property>
    </bean>
    <!-- 在spring-mvc.xml檔案中加入這段配置後,spring返回給頁面的都是utf-8編碼了 -->
    <bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <bean
                    class="org.springframework.http.converter.StringHttpMessageConverter">
                    <property name="supportedMediaTypes">
                        <list>
                            <value>text/html;charset=UTF-8</value>
                        </list>
                    </property>
                </bean>
            </list>
        </property>
    </bean>
</beans>

在上面把SSM的配置寫完後,需要在web.xml引入

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <display-name>Archetype Created Web Application</display-name>
    
    <!-- 設定UTF-8格式 -->
    <filter>
        <filter-name>characterEncoding</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>characterEncoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- 配置SpringMVC核心攔截器 -->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 初始化spring-dao,spring-service,spring-web配置檔案 -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/spring-*.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

當完成以上配置後,就可以開始寫程式碼了!(小弟新手,以上所寫或許有錯,請包涵)

相關文章