1. 程式人生 > 遊戲 >《喋血復仇》8月有兩輪測試 首先面向預購玩家

《喋血復仇》8月有兩輪測試 首先面向預購玩家

SSM框架整合

學完SSM之後,感覺記憶並不深刻,因此留筆記,以待後用。

1.開發環境搭建

自行百度

2.Java web專案建立

自行百度

3.SSM框架整合

這次整合分成了6個配置檔案,分別是applicationContext.xml,jdbc.properties,spring-dao.xml,spring-service.xml,spring-mvc.xml,mybatis-config.xml。

3.1 Maven引入需要的JAR包

pom.xml檔案

<!--?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>Easy_Integration</artifactid>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>Easy_Integration Maven Webapp</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceencoding>UTF-8</project.build.sourceencoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>

    <dependencies>
        <!--Junit-->
        <dependency>
            <groupid>junit</groupid>
            <artifactid>junit</artifactid>
            <version>4.13</version>
            <scope>test</scope>
        </dependency>

        <!--資料庫驅動-->
        <dependency>
            <groupid>mysql</groupid>
            <artifactid>mysql-connector-java</artifactid>
            <version>5.1.49</version>
        </dependency>
        <!--資料庫連線池-->
        <dependency>
            <groupid>com.mchange</groupid>
            <artifactid>c3p0</artifactid>
            <version>0.9.5.4</version>
        </dependency>
        <!--dbcp-->
        <!--<dependency>
          <groupId>commons-dbcp</groupId>
          <artifactId>commons-dbcp</artifactId>
          <version>1.4</version>
        </dependency>-->

        <!--Servlet-JSP-->
        <dependency>
            <groupid>javax.servlet</groupid>
            <artifactid>servlet-api</artifactid>
            <version>2.5</version>
        </dependency>
        <dependency>
            <groupid>javax.servlet.jsp</groupid>
            <artifactid>jsp-api</artifactid>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupid>javax.servlet</groupid>
            <artifactid>jstl</artifactid>
            <version>1.2</version>
        </dependency>
        <!--Mybatis-->
        <dependency>
            <groupid>org.mybatis</groupid>
            <artifactid>mybatis</artifactid>
            <version>3.5.4</version>
        </dependency>
        <dependency>
            <groupid>org.mybatis</groupid>
            <artifactid>mybatis-spring</artifactid>
            <version>2.0.4</version>
        </dependency>
        <!--Spring-->
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-webmvc</artifactid>
            <version>5.2.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-jdbc</artifactid>
            <version>5.2.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-beans</artifactid>
            <version>5.2.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-web</artifactid>
            <version>5.2.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-core</artifactid>
            <version>5.2.8.RELEASE</version>
        </dependency>
        
        <!-- 匯入java ee jar 包 -->
        <dependency>
            <groupid>javax</groupid>
            <artifactid>javaee-api</artifactid>
            <version>7.0</version>
        </dependency>
        <!-- 日誌檔案管理包 -->
        <!-- log start -->
        <dependency>
            <groupid>log4j</groupid>
            <artifactid>log4j</artifactid>
            <version>1.2.12</version>
        </dependency>


        <!-- 格式化物件,方便輸出日誌 -->
        <dependency>
            <groupid>com.alibaba</groupid>
            <artifactid>fastjson</artifactid>
            <version>1.1.41</version>
        </dependency>
        <dependency>
            <groupid>org.slf4j</groupid>
            <artifactid>slf4j-api</artifactid>
            <version>1.7.7</version>
        </dependency>

        <dependency>
            <groupid>org.slf4j</groupid>
            <artifactid>slf4j-log4j12</artifactid>
            <version>1.7.7</version>
        </dependency>
        <!-- log end -->
        <!-- 映入JSON -->
        <dependency>
            <groupid>org.codehaus.jackson</groupid>
            <artifactid>jackson-mapper-asl</artifactid>
            <version>1.9.13</version>
        </dependency>
        <!-- 上傳元件包 -->
        <dependency>
            <groupid>commons-fileupload</groupid>
            <artifactid>commons-fileupload</artifactid>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupid>commons-io</groupid>
            <artifactid>commons-io</artifactid>
            <version>2.4</version>
        </dependency>
        <dependency>
            <groupid>commons-codec</groupid>
            <artifactid>commons-codec</artifactid>
            <version>1.9</version>
        </dependency>

    </dependencies>

    <build>

        <finalname>Easy_Integration</finalname>
        <pluginmanagement>
            <!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <plugin>
                    <artifactid>maven-clean-plugin</artifactid>
                    <version>3.1.0</version>
                </plugin>
                <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
                <plugin>
                    <artifactid>maven-resources-plugin</artifactid>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactid>maven-compiler-plugin</artifactid>
                    <version>3.8.0</version>
                </plugin>
                <plugin>
                    <artifactid>maven-surefire-plugin</artifactid>
                    <version>2.22.1</version>
                </plugin>
                <plugin>
                    <artifactid>maven-war-plugin</artifactid>
                    <version>3.2.2</version>
                </plugin>
                <plugin>
                    <artifactid>maven-install-plugin</artifactid>
                    <version>2.5.2</version>
                </plugin>
                <plugin>
                    <artifactid>maven-deploy-plugin</artifactid>
                    <version>2.8.2</version>
                </plugin>
            </plugins>
        </pluginmanagement>
        <!--靜態資源匯出問題-->
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resource</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>

        <plugins>
            <!-- tomcat7外掛 -->
            <plugin>
                <groupid>org.apache.tomcat.maven</groupid>
                <artifactid>tomcat7-maven-plugin</artifactid>
                <version>2.2</version>
                <configuration>
                    <port>8080</port>
                    <path>/</path>
                    <uriencoding>UTF-8</uriencoding>
                    <!-- tomcat熱部署 -->
                    <username>shang</username>
                    <password>123456</password>
                    <url>http://localhost:8080/manager/text</url>
                </configuration>
            </plugin>
            <!-- jetty外掛 -->
            <plugin>
                <groupid>org.eclipse.jetty</groupid>
                <artifactid>jetty-maven-plugin</artifactid>
                <version>11.0.1</version>
                <configuration>
                    <webapp>
                        <contextpath>/</contextpath>
                    </webapp>
                    <httpconnector>
                        <port>8080</port>
                    </httpconnector>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

3.2 jdbc屬性檔案

jdbc.properties

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

3.3 建立mybatis-config的檔案

mybatis-config

<!--?xml version="1.0" encoding="UTF-8" ?-->

<configuration>
    <!--配置日誌-->
<!--
   <settings>
        <setting name="logImpl" value="LOG4J"/>
        <setting name="cacheEnabled" value="true"/>
        <setting name="autoMappingBehavior" value="FULL"/>
    </settings>
-->

    <!--別名-->
    <typealiases>
        <package name="com.bean">
    </package></typealiases>
    <!--<mappers>
        <mapper class="com.dao.CommodityMapper"/>
    </mappers>-->
</configuration>

3.4 建立spring的檔案

spring-dao

<!--?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
        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">

    <!--連線資料來源
    dbcp:半自動
    c3p0:自動化com.mchange.v2.c3p0.ComboPooledDataSource
    druid
    hikari
    -->
    <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="30">
        <!--設定最小連線池大小-->
        <property name="minPoolSize" value="10">
        <!--關閉連線後不自動commit-->
        <property name="autoCommitOnClose" value="false">
        <!--獲取連線超時時間-->
        <property name="checkoutTimeout" value="10000">
        <!--當獲取連線失敗重試次數-->
        <property name="acquireRetryAttempts" value="2">
    </property></property></property></property></property></property></property></property></property></bean>


    <!--sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource">
        <!--繫結配置檔案-->
        <property name="configLocation" value="classpath:mybatis-config.xml">
    </property></property></bean>

    <!--配置dao介面掃描包,動態的實現了dao介面注入到spring容器中-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--注入sqlSessionFactory-->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory">
        <!--value填寫的是dao的檔案位置-->
        <property name="basePackage" value="com.dao">
    </property></property></bean>
</context:property-placeholder></beans>

spring-service

<!--?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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">
    <!--掃描service下的包-->
    <context:component-scan base-package="com.service">
    <!--宣告事務配置-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--注入資料來源-->
        <property name="dataSource" ref="dataSource">
    </property></bean>
    <!--aop事務支援-->
</context:component-scan></beans>

spring-mvc

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

        <!--註解驅動-->
        <mvc:annotation-driven>
        <!--掃描包-->
        <context:component-scan base-package="com.controller">
        <!--配置檢視解析器 /WEB-INF/jsp/query.jsp  -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                <property name="prefix" value="/WEB-INF/jsp/">
                <property name="suffix" value=".jsp">
        </property></property></bean>
         <!--靜態資源-->
        <mvc:default-servlet-handler>
        <!--<mvc:resources mapping="/statics/**" location="/statics/"/>-->

<!--        全域性異常處理,僅限於Controller-->
<!--        <com.bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">-->
<!--                異常的對映-->
               <!-- <property name="exceptionMappings">
                        <props>
                                <prop key="java.lang.RuntimeException">error</prop>
                        </props>
                </property>
        </com.bean>-->
        


<!--        用於檔案上傳-->
       <!-- <com.bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
                <property name="maxInMemorySize" value="5000000"/>
                <property name="defaultEncoding" value="UTF-8"/>
        </com.bean>-->
</mvc:default-servlet-handler></context:component-scan></mvc:annotation-driven></beans>

3.5 建立applicationContext.xml檔案

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="spring-dao.xml">
    <import resource="spring-service.xml">
    <import resource="spring-mvc.xml">
</import></import></import></beans>

3.6 配置web.xml檔案

這裡面對spring-mybatis.xml的引入以及配置的spring-mvc的Servlet就是為了完成SSM整合。

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">
    <!--配置核心控制器DispatcherServlet-->
    <servlet>
        <servlet-name>easy_integration</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--初始化springmvc配置檔案-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
<!--    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet-mapping>
        <servlet-name>easy_integration</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <!--亂碼過濾-->
    <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>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!--Session-->
    <session-config>
        <session-timeout>15</session-timeout>
    </session-config>
</web-app>