SSM框架整合的一般步骤

1.相关标签的一些含义

(1) mvc:annotation-driven/: 加载注解驱动。此标签默认创建了两个注解操作有关的bean,因此处理映射器可以根据@Controller、@RequestMapping注解去找到对应的处理器去处理请求。不加此标签,我们一般会显示创建一个BeanNameUrlHandlerMapping对象,此对象可以也可以帮我们去找到url对应的处理器,不过处理器需要以的方式创建。
(2)<context:property-placeholder location=“classpath:db.properties”></context:property-placeholder>可以读取配置文件的内容到当前配置文件中。

(3) <mvc:default-servlet-handler/ >过滤静态资源,如果是静态资源,dispatchServlet不拦截
(4)<context:component-scan base-package="…">开启注解扫描,并扫描目标包下的@Service、@Respository、@Controller等注解标注的类进入ioc容器。

2.整合的步骤

创建一个spring、springmvc、mybatis、web的配置文件。
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"
       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">
<import resource="springmvc.xml"></import>
</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
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!-- 通过注解的方式,找到处理器 -->
    <mvc:annotation-driven>
<!--        <mvc:message-converters>-->
<!--            <bean class="org.springframework.http.converter.StringHttpMessageConverter"></bean>-->
<!--            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean>-->
<!--        </mvc:message-converters>-->
    </mvc:annotation-driven>
<!--    读取数据库配置文件-->
    <context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
<!--静态资源过滤-->
    <mvc:default-servlet-handler/>
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="driverClass" value="${jdbc.driver}"></property>
    </bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
    </bean>
<!--    视图解析器-->
 <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
     <property name="prefix" value="/WEB-INF/jsp/"></property>
     <property name="suffix" value=".jsp"></property>
 </bean>
    <bean  class="org.mybatis.spring.mapper.MapperScannerConfigurer">
     <property name="basePackage" value="com.kuang.dao"></property>
    </bean>
    <!-- 自动扫描装配,同时默认开启了注解配置 -->
    <context:annotation-config></context:annotation-config>
    <context:component-scan base-package="com.kuang.controller"/>
    <context:component-scan base-package="com.kuang.service"/>
</beans>

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>
<!--    给包取别名-->
    <typeAliases>
        <package name="com.kuang.pojo"/>
    </typeAliases>
    <mappers>
        <mapper resource="mapper/BookMapper.xml"/>
    </mappers>

</configuration>

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_4_0.xsd"
         version="4.0">
    <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:applicationContext.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>

3.要点

1.首先是web.xml,配置一个前端控制器,并与spring的配置文件绑定。(注意:因为我将所有的配置都引入到spring的配置文件中了,用了import)
2.整合mybatis的时候,要定义一个数据源dataSource,根据数据源创建SqlSessionFactory,我们用这个可以去生成数据库会话,连接数据库。Sql用到的参数就有:dataSource和mybatis的配置文件
3.定义MapperScannerConfigurer------mapper扫描器,扫描dao层的接口,不然会报错。
4.视图解析器
5.开启注解配置,扫描service层、controller层的类,让ioc容器创建管理 。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值