Spring整合SSM的配置文件詳解


在整合三大框架SSM , 即 Spring 和 SpingMVC和Mybatis的時候,搭建項目最初需要先配置好配置文件.

有人在剛開始學習框架的時候會糾結項目搭建的順序,因為頻繁的報錯提示是會很影響強迫症和編程心情的,這里分享我在構建項目時候的心得和配置文件的編寫

首先你需要知道你的項目需要哪些結構,我們這里舉一個基於Java的簡單Web項目,實現最簡單的功能,我需要Mybatis來進行持久層的控制,那么Mybatis就需要配置文件,我需要Dao層提供數據,那就需要Dao層的配置文件,我還需要服務層,那就需要Service的配置文件,再其次,我在進行一些選項的時候需要開啟事務操作,那就需要事務的配置文件,最后是整合的SpringMVC的配置文件.

那么綜上而言 我們把文件列表可以羅列出來:

Mybatis的配置文件: sqlMapConfig.xml

Spring的配置文件:  applicationContext-dao.xml

          applicationContext-service.xml

          applicationContext-tx.xml

          springmvc.xml

外部的Properties配置文件: jabc.properties 和 log4j.properties

學會配置xml文件是學習spring的基礎,需要長時間的練習來達到熟練的效果.

我們先編寫jabc.properties的配置文件:

這里使用的是阿里雲的連接池Druid:

druid.url=jdbc:mysql://localhost:3306/egoubuy?characterEncoding=utf-8
druid.password=root
druid.username=root
druid.driverClassName=com.mysql.jdbc.Driver
druid.initialSize=5
druid.minIdle=3
druid.maxActive=20

再編寫log4j.properties:

# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# 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

現在,我們開始編寫SSM的xml

首先配置Mybatis的配置文件

<?xml version="1.0" encoding="UTF-8" ?>
        <!DOCTYPE configuration
                PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
                "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<plugins>
    <!-- com.github.pagehelper為PageHelper類所在包名 -->
    <plugin interceptor="com.github.pagehelper.PageInterceptor">

    </plugin>
</plugins>
</configuration>

然后配置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
       http://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"></context:property-placeholder>
    <!--數據源-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="url" value="${druid.url}"></property>
        <property name="username" value="${druid.username}"></property>
        <property name="password" value="${druid.password}"></property>
        <property name="driverClassName" value="${druid.driverClassName}"></property>
        <property name="initialSize" value="${druid.initialSize}"></property>
        <property name="minIdle" value="${druid.minIdle}"></property>
        <property name="maxActive" value="${druid.maxActive}"></property>
    </bean>

    <!--配置sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--配置mybatis配置文件,這里的名稱一定要對應-->
        <property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml"></property>
        <!--配置數據源對象-->
        <property name="dataSource" ref="dataSource"></property>
    </bean>


    <!--批量配置mapper接口:
    通過mapper掃描器,掃描某個包下的所有mapper接口,批量加載配置
    -->

    <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--設置需要掃描的包-->
        <property name="basePackage" value="這里是你需要的被掃描的包"></property>
        <!--配置sqlSessionFactory對象-->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>



</beans>

再編寫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
       http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">
    <!--掃描service包,將service包下添加@Servcie注解的類添加到容器中-->
    <context:component-scan base-package="這里是你編寫的service的包"></context:component-scan>
</beans>

由於需要開啟事務,再編寫事務的配置文件

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
 http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd
 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
">

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

    <!--配置通知-->
    <tx:advice id="tx_advice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="insert*" propagation="REQUIRED" isolation="DEFAULT"/>
            <tx:method name="add*"></tx:method>
            <tx:method name="update*"></tx:method>
            <tx:method name="modify*"></tx:method>
            <tx:method name="delete*"></tx:method>
            <tx:method name="remove*"></tx:method>
            <tx:method name="get*" read-only="true"></tx:method>
            <tx:method name="select*" read-only="true"></tx:method>
            <tx:method name="query*" read-only="true"></tx:method>
        </tx:attributes>
    </tx:advice>

    <!--配置切面-->
    <aop:config>
        <aop:advisor advice-ref="tx_advice" pointcut="execution(* 這里一般寫service的類所在包.*.*(..))"></aop:advisor>
    </aop:config>


</beans>

最后是編寫springmvc的配置文件

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

    <!--配置掃描controller包下的所有以@Controller注解修飾的類-->
    <context:component-scan base-package="這里是控制器的所在類,你可以放一起寫,也可以分開寫"></context:component-scan>

    <!--配置注解驅動-->
    <mvc:annotation-driven></mvc:annotation-driven>

    <!--放行靜態資源文件-->
    <mvc:default-servlet-handler></mvc:default-servlet-handler>

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

    <!--配置文件上傳解析器 吐過需要,你就打開注釋commons-io commons-fileupload-->
    <!--<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="10000000"></property>
    </bean>-->

</beans>

Java代碼部分不做展示沒有意義,當你在寫項目的時候也要注意web.xml的配置,前提是web的項目

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

  <!--配置post提交中文亂碼的過濾器-->
  <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>

  <!--給監聽器指定初始化參數,用於加載spring配置文件-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <!--classpath:spring/applicationContext-dao.xml,classpath:spring/applicationContext-tx.xml,classpath:spring/applicationContext-servcie.xml-->
    <param-value>classpath:spring/applicationContext-*.xml</param-value>
  </context-param>

  <!--配置監聽器 加載spring配置文件-->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!--配置前端控制器-->
  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring/springmvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>


</web-app>

關於涉及到spring相關的項目的xml文件bean頭文件導包的問題,在idea中一般是自動的,你可以在setting中配置自動加載,同時,每一個需要的包都是對應好的,所以不用擔心,編程使我們快樂,歡迎入坑框架.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM