SSM整合

SSM框架整合规范、教程及意义(可直接copy)

一、Spring和SpringMVC整合过程的问题?

1、是否可以将SpringMVC的配置放到Spring配置文件中?

不行,这样会导致SpringMVC的配置文件被加载两次,SpringIOC容器初始化的时候会加载一次并创建所有对象,SpringMVC在服务器启动的时候也会初始化自己的容器并创建相关web层的对象,这样同一份配置文件就被加载两次导致冲突。

2、SpringMVC里面有IOC容器,为什么不把Spring配置放到SpringMVC中?

原因:SpringMVC是一个web层框架,如果把其他配置放到里面,以后如果更好mvc框架的时候,

就需要把其他的配置重新配置一遍,避免Spring的配置文件入侵到SpringMVC的配置文件中

框架:特定领域解决特点问题

3、Spring有自己的IOC容器(Service dao) SpringMVC也有自己的IOC容器

问题:为什么SpringMVC中的Controller能直接注入Spring容器中的对象

容器之间存在父子关系,在SpringMVC容器创建时会将Spring容器作为参数传入进去

4、SpringIOC容器和SpringMVC容器的关系?

Spring的容器和SpringMVC的关系为父子关系,SpringMVC容器在创建的时候会将Spring的容器设置为SpringMVC容器的父容器,自然子容器也就可以访问父容器中的元素,所以SpringMVC中的Controller类才能直接注入Spring容器中的对象。

二、SpringIOC容器和SpringMVC容器初始化流程

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bW1bXVFh-1641215483973)(F:\文档资料_框架\学习笔记\SSM笔记图片\Spring容器和SpringMVC容器关系.png)]

SSM框架整合规范、教程及意义(可直接

三、SSM整合web.xml配置模板
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <!--加载spring配置文件-->
    <param-value>classpath:spring-config.xml</param-value>
  </context-param>

  <filter>
    <!--配置处理post请求中文乱码的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>/*</url-pattern>
  </filter-mapping>

  <!--配置ioc监听器 自带创建ioc容器-->
  <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>
      <!--加载spring主配置文件-->
      <param-name>contextConfigLocation</param-name>
      <!--注意此处是加载web的配置文件也就是springMVC的配置文件 要和spring配置文件分开-->
      <param-value>classpath:spring-web-config.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>

四、SSM整合Spring主配置文件模板(包含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:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--扫描service层注解配置创建对象的类-->
    <context:component-scan base-package="com.oracle.service">
        <!--在spring和springMVC扫描注解冲突有重合的情况下 可以选择过滤掉某个注解或是只扫描指定的包-->
        <!--<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>-->
    </context:component-scan>
    <!--引入dao层的配置文件 不需要引入web层的配置文件 会直接在web.xml中配置加载-->
    <import resource="classpath:spring-dao-config.xml"></import>

    <!--  创建事务管理器  -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="druidDataSource"></property>
    </bean>
    <!--对事物管理器进行再次包装  设置事务的一些属性-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!--name属性设置为指定或所有设置事务 决定针对那些方法事务生效-->
            <!--isolation设置事务的隔离级别-->
            <!--propagation设置事务的传播-->
            <tx:method name="*" isolation="DEFAULT" propagation="SUPPORTS"/>
        </tx:attributes>
    </tx:advice>
    <!--设置事务的配置-->
    <aop:config>
        <!--设置切点   使用aop的切点表达式  可以设置在通知切面里面 但切面里面只能设置一个切点-->
        <aop:pointcut id="pt1" expression="execution(* com.oracle.service..*.*(..))"/>
        <!--设置切面 spring自带aop实现-->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"></aop:advisor>
    </aop:config>
    <!--使用注解配置事务的时候,需要配置开启事务的注解驱动 并将事务管理器注册在事务驱动中-->
    <!--<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>-->
</beans>
五、SSM整合-----Spring整合mybatis模板
<?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="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--注入连接池-->
        <property name="dataSource" ref="druidDataSource"></property>
        <!--<property name="configLocation" value="mybatis-config.xml"></property>-->
        <!--设置实体类的别名-->
        <property name="typeAliasesPackage" value="com.oracle.domain"></property>
        <!--扫描mapper的映射文件  包含结果集映射和xml配置的sql语句-->
        <property name="mapperLocations" value="classpath:mapper/*.xml"></property>
    </bean>
    <!--连接池配置-->
    <bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

    <!--扫描dao文件 创建dao 并创建dao类对象-->
    <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sessionFactory"></property>
        <!--指定要扫描的dao包-->
        <property name="basePackage" value="com.oracle.dao"></property>
    </bean>
</beans>

以及数据库连接需要的四个基本属性单独封装在properties文件中:

#数据库驱动
jdbc.driver=com.mysql.jdbc.Driver
#数据库连接的url
jdbc.url=jdbc:mysql://localhost:3306/数据库名
#数据库用户名
jdbc.username=root
#数据库密码
jdbc.password=123
六、SSM整合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">
    <!--扫描包创建对象的时候,注意不要和spring扫描有重合-->
    <context:component-scan base-package="com.oracle.web"></context:component-scan>

    <!--配置视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--配置前缀-->
        <property name="suffix" value=""></property>
        <!--配置后缀-->
        <property name="prefix" value=".jsp"></property>
    </bean>
    <!--一般会先在web中设置一个专门用于页面跳转的Controller-->
    <!--配置让dispatcherServlet拦截器在拦截请求的时候忽略访问js包下的请求  可根据需要配置-->
    <mvc:resources mapping="/js/**" location="/js/"></mvc:resources>


    <!--注册格式转换器   自定义的格式转化器需要在此处进行注册-->
    <bean id="conversionService2" class="org.springframework.context.support.ConversionServiceFactoryBean">
        <property name="converters">
            <bean class="com.oracle.util.StringToDateConverter"></bean>
        </property>
    </bean>

    <!--开启注解驱动  不然无法开启处理器映射器 和处理器适配器 开启spring才能正常工作-->
    <!--配置了数据格式转换器需要在此处进行注册  和使用除jackson以外的json解析器也要在mvc注解驱动中进行注册-->
    <mvc:annotation-driven conversion-service="conversionService2">
        <mvc:message-converters>
            <!--配置jackson以外的json转换器-->
            <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
                <!--设置fastjson支持的媒体类型-->
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/html;charset=utf-8</value>
                        <value>application/json;charset=utf-8</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
</beans>

附上自定义数据格式转化器的模板:

package com.oracle.util;


import org.springframework.core.convert.converter.Converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

//自定义转换器  不只针对日期转换 根据需要更改

//以下为一个自定义的日期字符串转化为Date对象的模板代码
public class StringToDateConverter implements Converter<String,Date> {

    @Override
    public Date convert(String source) {
        if (source==null||source==""){
            return null;
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date=null;
        try {
            date = format.parse((String) source);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
}
eDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date=null;
        try {
            date = format.parse((String) source);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值