SSM框架的整合

10 篇文章 0 订阅
10 篇文章 0 订阅

第一步:首先整合spring和mybatis

1.将spring与web项目整合

通过配置ContextLoaderListener来在项目刚启动(servletContext对象刚创建)的时候,初始化spring容器WebApplicationContext,详细的源代码如下:

public class ContextLoaderListener extends ContextLoader implements ServletContextListener {
    private ContextLoader contextLoader;
    public void contextInitialized(ServletContextEvent event) {
        this.contextLoader = createContextLoader();
        if (this.contextLoader == null) {
            this.contextLoader = this;
        }
        this.contextLoader.initWebApplicationContext(event.getServletContext());
    }
}

在ContextLoader中有如下方法
public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {
    ...
    if (this.context == null) {
            this.context = createWebApplicationContext(servletContext);
        }
    ...
    servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
    ...
}

可以看到ContextLoaderListener实现了ServletContextListener接口,并在其contextInitialized方法中通过contextLoader对象初始化了WebApplicationContext对象,并将其存放在了ServletContext对象中

    web.xml中:
      <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
      </context-param>
      <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>

2:配置mybatis的相关文件

1.spring管理mybatis的SqlSessionFactory

所以需要装载SqlSessionFactory,在装载这个对象的时候,要求配置dataSource(数据库连接池对象),此时我们就需要配置C3p0,以及采用placerHolderProperties的方式配置数据库连接时的相关属性(jdbcUrl,username,password,driver)

2.spring还需要扫描mybatis的mapper

3.spring还需要配置数据库中的事物

由于使用的是Mybatis,所以使用DataSourceTransactionManager这个事物平台管理器,一般情况下都是采用注解的方式配置事物

    <!-- mybatis.xml -->
    <!-- 配置sqlSessionfactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <!-- 扫描mapper所在的位置 -->
        <property name="mapperLocations" value="classpath:com/njust/core/dao/*.xml"></property>
        <!-- 配置po对象的别名 -->
        <property name="typeAliasesPackage" value="com.njust.core.bean,com.njust.core.query"></property>
    </bean>

    <!-- 扫包 扫描mapper的包-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.njust.core.dao"></property>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>

    <!--properties.xml-->
    <!-- 配置PropertyPlaceholderConfigure -->
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:springConfig/jdbc.properties</value>
            </list>
        </property>
    </bean>
    <!-- 实现同样的功能 -->
    <!-- <context:property-placeholder location="classpath:springConfig/jdbc.properties"/> -->

    <!-- jdbc.properties -->
    jdbc.driver = com.mysql.jdbc.Driver
    jdbc.url = jdbc:mysql:///babasport
    jdbc.username = root
    jdbc.password = yzgylq

    <!-- c3p0.xml -->
    <!-- 配置数据库的连接池c3p0.xml -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="user" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}"/>
    </bean>

    <!-- 配置TransactionManager transactionManager.xml-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 配置注解形式的事物管理  -->
    <tx:annotation-driven transaction-manager="transactionManager"/> 

3:spring扫描装载Service层中的对象

使用扫描的方式

<!-- 配置spring自动扫描注解,除了Controller注解,Controller由于是交给了SpringMvc框架,所以由SpringMvc框架来扫描 -->
    <context:component-scan base-package="com.njust">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

第二步:配置SpringMVC

1.配置DispatcherServlet

 <servlet>
    <servlet-name>back</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:springmvc/springmvc-back.xml</param-value>
    </init-param>
  </servlet>

  <servlet-mapping>
    <servlet-name>back</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>

2.配置post提交时的乱码问题characterEncodingFilter

  <!-- 配置post提交时的乱码问题,可以为一个filter设定多个拦截路径 -->
  <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>*.shtml</url-pattern>
  </filter-mapping>

  <filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>*.do</url-pattern>
  </filter-mapping>

3.配置处理器映射器

<!-- 配置处理器映射器 -->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
        <!-- 在处理器映射器中配置springMvc的拦截器 -->
        <property name="interceptors">
            <list>
                <bean class="com.njust.core.interceptor.LoginInterceptor">
                    <property name="isAdmin" value="1"></property>
                </bean>
            </list>
        </property>
    </bean>

4:配置处理器适配器

<!-- 配置处理器适配器 -->
    <bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    </bean>

5:配置视图解析器

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

6:配置Controller的扫描器

<!-- 配置扫描Controller的扫描器 -->
    <!-- 必须要设置use-default-filters="false",如果不设置这个,而只是设置include则表示肯定会扫描@Controller这个注解修饰的类,但是也会修饰@service 
        @component等其他的注解修饰的类 -->
    <context:component-scan base-package="com.njust"
        use-default-filters="false">
        <context:include-filter type="annotation"
            expression="org.springframework.stereotype.Controller" />
    </context:component-scan>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值