1. 程式人生 > >SSM (Spring + Spring MVC +Mybatis)搭建

SSM (Spring + Spring MVC +Mybatis)搭建

 maven環境 

 pom.xml↓

 <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

        <!-- spring核心包 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.3.13.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.3.13.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-oxm</artifactId>
            <version>4.3.13.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>4.3.13.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.3.13.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.13.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.3.13.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>4.3.13.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.3.13.RELEASE</version>
        </dependency>
        <!-- mybatis核心包 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.5</version>
        </dependency>

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

        <!--加入servlet依賴-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.0</version>
        </dependency>

        <!-- 匯入Mysql資料庫連結jar包 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.46</version>
        </dependency>
        <!-- 匯入dbcp的jar包,用來在applicationContext.xml中配置資料庫 -->
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>

        <!-- 日誌檔案管理包 -->
        <!-- log start -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.12</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.5.6</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.7</version>
            <scope>test</scope>
        </dependency>

        <!-- log end -->

        <!--加入jackson包-->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.2</version>
        </dependency>


        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.9.2</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.9.2</version>
        </dependency>

        <!-- c標籤庫 -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
    </dependencies>
//

pom.xml裡<build>裡面還有加一段


 <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>

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>

  <!--載入spring配置檔案-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring-mybatis.xml</param-value>
  </context-param>
 
  <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>
    <init-param>
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <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:spring-mvc.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.action</url-pattern>
  </servlet-mapping>

</web-app>

log4j.properties

# Global logging configuration
log4j.rootLogger=ERROR, stdout
# MyBatis logging configuration...
log4j.logger.com.zking.mapper=TRACE
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

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

    <!--1) 排除掃描@Component、@Repository、@Service 註解-->
    <context:component-scan base-package="com.zking.controller"></context:component-scan>

    <!--2) 此標籤預設註冊DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter -->
    <!--兩個bean,這兩個bean是spring MVC為@Controllers分發請求所必須的。並提供了資料繫結支援,-->
    <[email protected]支援,@DateTimeFormat支援,@Valid支援,讀寫XML的支援(JAXB),讀寫JSON的支援(Jackson)-->
    <mvc:annotation-driven></mvc:annotation-driven>

    <!--但是,從spring3.1開始DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter的使用已經過時-->
    <!--spring3.1開始我們應該用RequestMappingHandlerMapping 來替換DefaultAnnotationHandlerMapping,-->
    <!--spring3.1開始我們應該用用RequestMappingHandlerAdapter 來替換AnnotationMethodHandlerAdapter-->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="mappingJackson2HttpMessageConverter"/>
            </list>
        </property>
    </bean>
    <bean id="mappingJackson2HttpMessageConverter"
          class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
        <!--處理中文亂碼以及避免IE執行AJAX時,返回JSON出現下載檔案-->
        <property name="supportedMediaTypes">
            <list>
                <value>text/html;charset=UTF-8</value>
                <value>text/json;charset=UTF-8</value>
                <value>application/json;charset=UTF-8</value>
            </list>
        </property>
    </bean>


    <!--3) ViewResolver -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <!--4) 單獨處理圖片、樣式、js等資源 -->
    <mvc:resources location="/css/" mapping="/css/**"/>
    <mvc:resources location="/images/" mapping="/images/**"/>
    <mvc:resources location="/js/" mapping="/js/**"/>
   <!-- <mvc:resources location="/jquery-easyui/" mapping="/jquery-easyui/**"/>-->


    <!-- ********************* 國際化配置開始***************** -->
    <!--修改springmvc-servlet.xml-->
    <!--1) 配置國際化資原始檔 -->
    <!--<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">-->
    <!--<property name="basenames">-->
    <!--<list>-->
    <!--<value>i18n</value>-->
    <!--</list>-->
    <!--</property>-->
    <!--</bean>-->
    <!--&lt;!&ndash;2) 指定語言區域解析器,由它來確定使用哪個語言 &ndash;&gt;-->
    <!--<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">-->
    <!--</bean>-->
    <!--&lt;!&ndash;3) 配置國際化操作攔截器&ndash;&gt;-->
    <!--<mvc:interceptors>-->
    <!--<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/>-->
    <!--</mvc:interceptors>-->


    <!-- ********************* 檔案上傳配置開始***************** -->
    <!--<bean id="multipartResolver"-->
    <!--class="org.springframework.web.multipart.commons.CommonsMultipartResolver">-->
    <!--&lt;!&ndash; 必須和使用者JSP 的pageEncoding屬性一致,以便正確解析表單的內容 &ndash;&gt;-->
    <!--<property name="defaultEncoding" value="UTF-8"></property>-->
    <!--&lt;!&ndash; 檔案最大大小(位元組) 1024*1024*100=50M&ndash;&gt;-->
    <!--<property name="maxUploadSize" value="52428800"></property>-->
    <!--&lt;!&ndash;resolveLazily屬性啟用是為了推遲檔案解析,以便捕獲檔案大小異常&ndash;&gt;-->
    <!--<property name="resolveLazily" value="true"/>-->
    <!--</bean>-->
</beans>

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

    <!--配置註解掃描-->
    <context:component-scan base-package="com.zking"/>

    <!--配置dbcp的資料庫連線池-->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <!--連線物件-->
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <!--連線地址-->
        <property name="url" value="jdbc:mysql://localhost:3306/smbms?characterEncoding=UTF-8"/>
        <!--使用者名稱-->
        <property name="username" value="root"/>
        <property name="password" value="sasa"/>
        <!-- 初始化連線大小 -->
        <property name="initialSize" value="20"></property>
        <!-- 連線池最大數量 -->
        <property name="maxActive" value="100"></property>
        <!-- 連線池最大空閒 -->
        <property name="maxIdle" value="20"></property>
        <!-- 連線池最小空閒 -->
        <property name="minIdle" value="1"></property>
        <!-- 獲取連線最大等待時間 -->
        <property name="maxWait" value="6000"></property>
    </bean>

    <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
 <!-- 路徑 比如:com.ssm.mapper -->
        <property name="typeAliasesPackage" value="Dao層路徑"></property>
        <property name="mapperLocations" value="classpath*:com/zking/mapper/*.xml"></property>
    </bean>

    <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 路徑 比如:com.ssm.mapper -->
        <property name="basePackage" value=" Dao層路徑 "></property>
        <property name="sqlSessionFactoryBeanName" value="sessionFactory"></property>
    </bean>

    <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>

</beans>

以上就是SSM框架的搭建整合   如有不足歡迎指出!!!!