1. 程式人生 > >ssm整合最全配置檔案詳解版(在idea下整合)

ssm整合最全配置檔案詳解版(在idea下整合)

先放目錄結構,便於清晰瞭解整個專案


由簡單的到複雜的進行排列:

SqlMapConfig.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPEconfigurationPUBLIC"-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!-- 配置別名 -->
<typeAliases>
        <!-- 批量掃描 別名-->
<package 
name="com.lnp.model"/> </typeAliases> </configuration>
jdbc.properties:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/data?characterEncoding=utf-8
jdbc.username=root
jdbc.password=123
log4j.properties:
# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# MyBatis logging configuration...
log4j.logger.com.how2java=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

springMvc.xml:

<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-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> <!--這裡讓掃描controller,指定controller的包--> <context:component-scan base-package="com.lnp.controller"/> <!-- 註解的對映器 --> <!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> --> <!-- 註解的介面卡 --> <!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/> --> <!-- 使用 mvc:annotation-driven可以代替上面註解對映器和註解介面卡配置 mvc:annotation-driven預設載入了很多的引數繫結方法,比如json轉換解析器就預設載入了 實際開發中使用mvc:annotation-driven --> <!-- 註解驅動:配置處理器對映器和介面卡 --> <mvc:annotation-driven/> <!-- 檢視解析器 解析jsp解析,預設使用jstl標籤,classpath下得有jstl的包 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 配置jsp路徑的字首 --> <property name="prefix" value="/WEB-INF/jsp"/> <!-- 配置jsp路徑的字尾 --> <property name="suffix" value=".jsp"/> </bean> <!--靜態資源對映--> <mvc:resources mapping="/css/**" location="/WEB-INF/css/"/> <mvc:resources mapping="/js/**" location="/WEB-INF/js"/> <!--&lt;!&ndash; 轉換器 &ndash;&gt;--> <!--<property name="converters">--> <!--<list>--> <!--&lt;!&ndash; 日期型別轉換 &ndash;&gt;--> <!--<bean class="com.lnp.controller.converter.CustomDateConverter"/>--> <!--</list>--> <!--</property>--> <!--</bean>--> <!--&lt;!&ndash; 全域性攔截器 &ndash;&gt;--> <!--<mvc:interceptors>--> <!--&lt;!&ndash; 順序執行 &ndash;&gt;--> <!--<mvc:interceptor>--> <!--<mvc:mapping path="/**"/>--> <!--<bean class="com.lnp.interceptor.HandlerInterceptor1"></bean>--> <!--</mvc:interceptor>--> <!--<mvc:interceptor>--> <!--<mvc:mapping path="/**"/>--> <!--<bean class="com.lnp.interceptor.HandlerInterceptor2"></bean>--> <!--</mvc:interceptor>--> <!--<mvc:interceptor>--> <!--<mvc:mapping path="/**"/>--> <!--<bean class="com.lnp.interceptor.LoginInterceptor"></bean>--> <!--</mvc:interceptor>--> <!--</mvc:interceptors>--> </beans>

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" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!--開啟註解模式-->
<context:annotation-config />

    <!-- 資料庫連線池 -->
    <!-- 載入配置檔案 -->
<context:property-placeholder location="classpath:properties/jdbc.properties"/>
    <!-- 資料庫連線池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
destroy-method="close">
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="maxActive" value="10"/>
        <property name="minIdle" value="5"/>
    </bean>

    <!-- spring管理sqlsessionfactory 使用mybatisspring整合包中的 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 資料庫連線池 -->
<property name="dataSource" ref="dataSource"/>
        <!-- 載入mybatis的全域性配置檔案 -->
<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"/>
    </bean>

    <!--配置mapper掃描包-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.lnp.mapper"/>
    </bean>

    <!--==========================以上內容為applicationContext-dao配置===================================-->
    <!--自動掃描service-->
<context:component-scan base-package="com.lnp.service" />

    <!-- 事務管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- 資料來源 -->
<property name="dataSource" ref="dataSource"/>
    </bean>
    <!-- 通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!-- 傳播行為 -->
<tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="create*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="select*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
        </tx:attributes>
    </tx:advice>
    <!-- 切面 -->
<aop:config>
        <aop:advisor advice-ref="txAdvice"
pointcut="execution(* com.lnp.service.*.*(..))" />
    </aop:config>

    <!--==========================以上內容為applicationContext-service配置============================-->
</beans>

web.xml:

<!DOCTYPEweb-appPUBLIC"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN""http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name>Archetype Created Web Application</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <!-- log4j配置檔案地址 -->
<context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>classpath:properties/log4j.properties</param-value>
  </context-param>
  <!-- Log4j的監聽器要放在spring監聽器前面 -->
<listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
  </listener>

  <!-- 載入spring容器 -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/applicationContext.xml</param-value>
  </context-param>

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

  <!--前端控制器-->
<servlet>
    <servlet-name>taotao-manager</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- contextConfigLocation配置springmvc記載的配置檔案(配置處理器對映器、介面卡等等)
    如果不配置contextConfigLocation,預設載入的是/WEB-INF/servlet名稱-servlet.xml
     -->
<init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring/springMvc.xml</param-value>
    </init-param>
    <!-- 配置伺服器啟動後立即載入Spring MVC配置檔案 -->
<load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>taotao-manager</servlet-name>
    <!--
第一種:*.action,訪問以action結尾由DispatcherServlet進行解析
    第二種:/,所有的訪問的地址都由DispatcherServlet進行解析,對於靜態的檔案我們希望不用DispatcherServlet解析,
需要在springMvc.xml中新增靜態資源對映!!。
    使用此種方法可以實現RESTful風格的url
第三種:/*,這種配置不對,使用這種配置,最終要轉發到一個jsp頁面時,仍然會由DispatcherServlet解析jsp,不能根據jsp頁面找到handler,會報錯
-->
<url-pattern>/</url-pattern>
  </servlet-mapping>

  <!-- post亂碼過濾器 -->
<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>
    <