2017-08-17 SSM 環境搭建

2017-08-17 SSM 環境搭建

前言

SSM(Spring、Spring MVC 和 Mybatis)的環境搭建,在網上看過不少教程,有些明明將代碼一行行復制下來,卻出錯了。終究覺得需要自己碼一篇來當作筆記以及教程。

概念性的東西並不是這裏的重點,這個需要自己去查閱相關的知識點,建議先去了解 Spring、Spring MVC 和 Mybatis 的基礎知識再來跟着操作。

本教程的項目可以在此鏈接中下載 點擊我

默認環境

eclipse 4.6

JDK 1.8

1. 導入相關 Jar 包

  • Spring 4.3.9

  • MyBatis 3.4.4

  • commons-logging-1.2.jar

  • mybatis-spring-1.3.1.jar

  • mysql-connector-java-5.1.43-bin.jar

  • 其它的需要的時候再導入即可

2. 配置 web.xml 文件

在 WEB-INF 下創建一個 web.xml,按以下進行配置

主要配置的是 spring 的核心監聽器 以及 Spring MVC 的前端控制器

Spring MVC 前端控制器配置默認會在 WEB-INF 文件夾查找:[servlet-name]-servlet.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                             http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         id="WebApp_ID" version="3.1">

    <display-name>test_SSM</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <!-- 配置 spring 核心監聽器,默認會以 /WEB-INF/applicationContext.xml 作爲配置文件 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- contextConfigLocation 參數用來指定 Spring 的配置文件 -->
<!--
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
-->
    <!-- 如果將配置文件存放到 src/config 文件夾下,則配置如下 -->
<!-- 
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:config/applicationContext.xml</param-value>
    </context-param>
 -->

    <!-- 定義 Spring MVC 前端控制器 -->
    <servlet>
        <servlet-name>spring-mvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring-mvc-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- 讓 Spring MVC 的前端控制器攔截所有請求 -->
    <servlet-mapping>
        <servlet-name>spring-mvc</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>
</web-app>

3. 配置 applicationContext.xml 文件

在 WEB-INF 文件夾下創建 applicationContext.xml 文件

在之後的文章裏,將會將數據源更改爲常用的 C3P0 連接池

<?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:p="http://www.springframework.org/schema/p"
    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-4.3.xsd
                        http://www.springframework.org/schema/tx
                        http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

    <!-- 配置數據源 -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName">
            <value>com.mysql.jdbc.Driver</value>
        </property>
        <property name="url">
            <value>jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8</value>
        </property>
        <property name="username">
            <value>root</value>
        </property>
        <property name="password">
            <value>123456</value>
        </property>
    </bean>

    <!-- 配置 SqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--dataSource 屬性指定要用到的連接池 -->
        <property name="dataSource" ref="dataSource" />
        <!-- configLocation 屬性指定 mybatis 的核心配置文件 -->
        <property name="typeAliasesPackage" value="com.demo.bean" />
        <!-- 所有配置的 mapper 文件 -->
        <property name="mapperLocations" value="classpath*:/com/demo/mapper/*.xml" />
    </bean>

    <!-- JDBC事務管理器 -->
    <bean id="transactionManage-r"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- 啓用支持 annotation 註解方式事務管理 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>

    <!-- MapperScannerConfigurer 自動掃描,將 Mapper 接口生成代理注入到 Spring -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.demo.mapper" />
    </bean>
</beans>

4. 配置 Spring MVC 前端控制器配置文件

在 WEB-INF 文件夾中創建 [servlet-name]-servlet.xml 文件,這裏爲 spring-mvc-servlet.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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    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-4.3.xsd
                        http://www.springframework.org/schema/tx
                        http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
                        http://www.springframework.org/schema/mvc
                        http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">

    <!-- 自動掃描該包,SpringMVC 會將包下用了註解的類註冊爲 Spring 的 bean -->
    <context:component-scan base-package="com.demo.*" />

    <!-- 設置默認配置方案 -->
    <mvc:annotation-driven />

    <!-- 使用默認的 Servlet 來響應靜態文件 -->
    <mvc:default-servlet-handler />

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

創建對應的包與文件夾

在 src 下創建以下包名(根據自己的情況對包名進行修改,修改後記得改 SSM 配置文件)

* com.demo.bean
* com.demo.mapper
* com.demo.dao
* com.demo.service
* com.demo.controller

在 WEB-INF 文件夾中創建 pages 文件夾,將 jsp 文件存放在此處。關於爲何將 jsp 文件存放在 WEB-INF 下可以查看這篇文章瞭解一下http://blog.csdn.net/saygoodbyetoyou/article/details/9944773

後言

SSM 的環境就先到這裏,下一篇以登錄功能來示範一下 SSM 的使用

本文基於個人的學習
分享出來純當給大家參考一下,也當作自己的筆記

編輯:HochenChong
時間:2017-08-17
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章