首页 > 其他 > 详细

SSM整合笔记

时间:2019-12-21 16:41:14      阅读:86      评论:0      收藏:0      [点我收藏+]

SSM一般是分开学的,Spring,SpringMVC,Mybatis。学完之后整合也是需要学习一段时间。下面是一次整合的实践...

SSM的整合顺序一般先整合Spring和Mybatis。再整合SpringMVC。

Spring整合Mybatis

首先把文件放到lib的文件夹下。

然后在src目录下新建Spring的配置文件(applicationContext.xml)。

然后开启自动扫描。

    <!-- 自动扫描组件 -->
    <!-- 扫描全部的包 -->
    <context:component-scan base-package="com.xxx" />

然后applicationContext.xml整合mybatis的全局配置文件。这个Bean的名字是固定的。org.springframework.beans.factory.config.PropertyPlaceholderConfigurer,通过这个bean,可以通过db.properties文件中的key使用value。

    <!-- 数据库相关配置
        引入数据库配置文件 db.properties以后,是的${}可以使用。
     -->
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
        <property name="location" value="classpath:db.properties" />
    </bean>

创建一个数据源的对象,类名固定org.apache.commons.dbcp.BasicDataSource。这里可以直接使用db.properties中的key。

    <!-- 创建一个数据源对象 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <!-- 设置数据源连接的参数 -->
        <property name="driverClassName" value="${driver}" />
        <property name="url" value="${url}" />
        <property name="username" value="${user}" />
        <property name="password" value="${pwd}" />
        <!-- 其他参数 -->
        <!-- <property name="initialSize" value="30"></property> -->
    </bean>

配置SqlSessionFactory的bean

    <!-- 配置sqlsessionFactory对象 -->
    <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactoryBean">
        <!-- 必须数据源 -->
        <property name="dataSource" ref="dataSource" />
        <!-- 配置映射文件对应的路径,Ant风格 -->
        <property name="mapperLocations" value="classpath:com/xxx/xxx/dao/*.xml" />
    </bean>

注入映射器:

? 有两种方法。

  1. 较第二种简单一些,扫描dao包下所有的配置。
    <!-- 使用扫描器,扫描某个包底下的所有的Mapper接口 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 要扫描的基本包 -->
        <property name="basePackage" value="com.ssm.dao" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean" />
    </bean>
  1. 一个个的单独配置
    <!-- 注入映射器 -->
    <!-- 注入BlogMapper -->
    <bean id="blogMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="sqlSessionFactory" ref="sqlSessionFactoryBean" />
        <!-- 指定接口类型 -->
        <property name="mapperInterface" value="com.ckwl.ssm.dao.BlogMapper" />
    </bean>

配置到这里,已经能在dao包中,使用Mapper.xml配置文件,也可以在Dao上使用注解,2者取其一。

这里不需要在dao上注解@Repository。在Service层上需要需要注解@Service,具体视情况而定。在@Service中注入Dao用@Resource通过名字注入。

整合SpringMVC

加入SpringMVC的配置文件到src目录下。一个小例子。

<!-- 自动扫描 -->
<context:component-scan base-package="com.ssm" /> 
     <!-- 配置处理器映射器 -->
    <!--  <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean> -->
    <!-- 配置处理器适配器 -->
     <!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean> -->
    <!-- 自动注解驱动,这个配置可以取代上面的两个配置(处理器映射器和处理器适配器) -->
    <mvc:annotation-driven />
    <!-- 视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 前缀 -->
        <property name="prefix" value="/" />
        <!-- 后缀 -->
        <property name="suffix" value=".jsp" />
    </bean>

编写自己的Controller和视图(JSP,Thymeleaf等)。

修改web.xml,加载spring,springmvc配置文件

  <!-- 加载spring的配置文件 -->
  <!-- needed for ContextLoaderListener -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <!-- Bootstraps the root web application context before servlet initialization -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
  
  <!-- 配置前端控制器 -->
  <!-- The front controller of this Spring Web application, responsible for handling all application requests -->
    <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:springmvc-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <!-- Map all requests to the DispatcherServlet for handling -->
    <servlet-mapping>
        <servlet-name>springDispatcherServlet</servlet-name>
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>

SSM整合笔记

原文:https://www.cnblogs.com/to-red/p/12076908.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!