整合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_3_1.xsd"
         version="3.1">

    <!--配置spring容器启动-->
    <context-param>
        <param-name>contextLoaderListener</param-name>
        <param-value>classpath:spring/applicationContext.xml</param-value>
    </context-param>

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

    <!--配置SpringMVC的前端控制器-->
    <servlet>
        <servlet-name>springDispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/applicationContext-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>springDispatcherServlet</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>
        <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>

    <!--支持REST风格的过滤器-->
    <filter>
        <filter-name>hiddenHttpMethodFilter</filter-name>
        <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>hiddenHttpMethodFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
</web-app>

二、SpringMVC配置

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

    <!--SpringMVC只扫描控制器,禁用默认的规则-->
    <context:component-scan base-package="ssm" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:include-filter>
    </context:component-scan>

    <!--配置视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!--配置文件上传解析器-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="utf-8"></property>
        <property name="maxUploadSize" value="#{1024*1024*20}"></property>
    </bean>

    <!--扫描静态资源-->
    <mvc:default-servlet-handler></mvc:default-servlet-handler>

    <!--扫描动态资源-->
    <mvc:annotation-driven></mvc:annotation-driven>
</beans>

三、Spring配置

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



    <!--Spring除了控制器不需要配置,其它的业务逻辑组件都需要配置,包括dao,service-->
    <context:component-scan base-package="ssm">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:exclude-filter>
    </context:component-scan>

    <!--1、导入外部的配置文件-->
    <context:property-placeholder location="classpath:dbconfig.properties"></context:property-placeholder>

    <!--2、配数据源-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="#{jdbc.user}"></property>
        <property name="password" value="#{jdbc.password}"></property>
        <property name="jdbcUrl" value="#{jdbc.jdbcUrl}"></property>
        <property name="driverClass" value="#{jdbc.driverClass}"></property>

        <property name="maxPoolSize" value="#{jdbc.maxPoolSize}"></property>
        <property name="minPoolSize" value="#{jdbc.minPoolSize}"></property>
    </bean>

    <!--3、配置mybatis操作数据库,可以根据配置文件得到SqlSessionFactory-->
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--指定mybatis全局配置文件的位置-->
        <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property>
        <!--指定数据源-->
        <property name="dataSource" ref="dataSource"></property>
        <!--指定xml映射文件的位置-->
        <property name="mapperLocations" value="classpath:mybatis/mapper/*.xml"></property>
    </bean>
    <!--把dao接口的每一个实现注入到ioc容器中-->
    <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--指定dao接口所在的包-->
        <property name="basePackage" value="ssm.dao"></property>
    </bean>

    <!--4、配置事务控制;配置事务管理器,让管理器管理数据源里面的连接的关闭和提交-->
    <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--5、基于xml配置,配置事务,哪些方法切入事务还要写切入表达式-->
    <aop:config>
        <!--配置切入点表达式-->
        <aop:pointcut id="txPoint" expression="execution(* ssm.service.*.*(..))"></aop:pointcut>
        <aop:advisor advice-ref="myTx" pointcut-ref="txPoint"></aop:advisor>
    </aop:config>

    <!--6、配置事务增强、事务属性、事务建议-->
    <tx:advice id="myTx" transaction-manager="dataSourceTransactionManager">
        <tx:attributes>
            <!--配置所有方法出现异常就回滚-->
            <tx:method name="*" rollback-for="java.lang.Exception"/>
            <!--配置以get开头的方法,即查询的方法,设置为只读-->
            <tx:method name="get*" read-only="true"></tx:method>
        </tx:attributes>
    </tx:advice>

</beans>

四、MyBatis整合的关键配置

需要导入mybatis-spring-1.3.0.jar整合的jar包,在Spring配置文件中整合MyBatis

    <!--3、配置mybatis操作数据库,可以根据配置文件得到SqlSessionFactory-->
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--指定mybatis全局配置文件的位置-->
        <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property>
        <!--指定数据源-->
        <property name="dataSource" ref="dataSource"></property>
        <!--指定xml映射文件的位置-->
        <property name="mapperLocations" value="classpath:mybatis/mapper/*.xml"></property>
    </bean>
    <!--把dao接口的每一个实现注入到ioc容器中-->
    <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--指定dao接口所在的包-->
        <property name="basePackage" value="ssm.dao"></property>
    </bean>
上一篇:Spring 相关


下一篇:Spring 监听器listener原理-手写监听器(二)