JavaWeb笔记(30)-SSM整合

  1. 框架的整合(SpringMVC+Spring+Mybatis)
1. 由之前学习的三大框架进行整合开发:
    - 表现层(Web层):SpringMVC框架
    - 业务层:Spring框架
    - 持久层:Mybatis框架
2. 该三大框架的整合一定是由Spring框架去整合(配置)其他两个框架
    - 在整合时需要注意Mybatis和Spring版本匹配关系
    - 在开发过程中更多的使用:配置文件+注解 的方式进行配置
3. 项目结构:
    domain包:存放自定义Bean对象的包
    web包:存放自定义的web层控制器的包
    service包:存放自定义的service层方法
    dao包:存放自定义的数据库操作方法
4. 导包:
    pom.xml文件
    <properties>
    	<spring.version>5.0.2.RELEASE</spring.version>
    	<slf4j.version>1.6.6</slf4j.version>
    	<log4j.version>1.2.12</log4j.version>
    	<mysql.version>5.1.6</mysql.version>
    	<mybatis.version>3.4.5</mybatis.version>
    </properties>
        <!-- spring AOP -->
    aspectjweaver   1.6.8
    spring-aop      ${spring.version}
        <!-- Spring容器 -->
    spring-context  ${spring.version}
        <!-- Spring MVC -->
    spring-web      ${spring.version}
    spring-webmvc   ${spring.version}
        <!-- Spring 单元测试框架 -->
    spring-test     ${spring.version}
        <!-- Spring 事务管理 -->
    spring-tx       ${spring.version}
        <!-- Spring JdbcTemplate技术 -->
    spring-jdbc     ${spring.version}
        <!-- 单元测试 -->
    junit           4.12
        <!-- mysql驱动 -->
    mysql-connector-java ${mysql.version}
        <!-- servlet 和 jsp -->
    servlet-api     2.5                     provided
    jsp-api         2.0                     provided
        <!-- jsp页面EL表达式 -->
    jstl            1.2
        <!-- 日志相关技术 -->
    log4j           ${log4j.version}
    slf4j-api       ${slf4j.version}
    slf4j-log4j12   ${slf4j.version}
        <!-- mybatis和Spring整合Mybatis所使用的包 -->
    mybatis         ${mybatis.version}
    mybatis-spring  1.3.0
        <!-- 数据库连接池技术 -->
    c3p0            0.9.1.2                 compile
  1. Spring环境配置
1. 在resources目录下创建applicationContext.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: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">
2. Spring需要扫描service层和dao层的注解,并管理业务层对象添加到容器中
   web层对象和注解交由SpringMVC进行管理
    - 配置注解扫描
    <context:component-scan base-package="cn.mysilent">
        <!-- 配置要忽略web层的Controller注解 -->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
3. 结束符
    </beans>
  1. SpringMVC环境配置
1. web.xml文件配置
    a. 在web.xml配置文件中配置前端控制器,所有请求将交由前端控制器管理
        - 在<web-app>标签中添加<servlet>配置
        <servlet>
            <servlet-name>dispatcherServlet</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <!-- 配置初始化参数,创建完DispatcherServlet对象,加载springmvc.xml配置文件 -->
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:springmvc.xml</param-value>
            </init-param>
            <!-- 服务器启动的时候,让DispatcherServlet对象创建 -->
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>dispatcherServlet</servlet-name>
            <!-- 配置拦截所有 -->
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    b. 在web.xml配置文件中配置过滤器,解决中文乱码问题
        - 在<web-app>标签中添加<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>/*</url-pattern>
        </filter-mapping>
2. 在resources目录下创建springmvc.xml配置文件
    a. 约束头
        <?xml version="1.0" encoding="UTF-8"?>
        <beans xmlns="http://www.springframework.org/schema/beans"
            xmlns:mvc="http://www.springframework.org/schema/mvc"
            xmlns:context="http://www.springframework.org/schema/context"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd">
    b. 配置注解扫描,仅扫描web层的Controller注解
        <context:component-scan base-package="cn.mysilent">
            <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        </context:component-scan>
    c. 配置视图解析器,使得方法返回字符串可以自动搜索应目录下的同名jsp文件作为跳转页面
        <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        	<!-- JSP文件所在的目录 -->
        	<property name="prefix" value="/WEB-INF/pages/" />
        	<!-- 文件的后缀名 -->
	        <property name="suffix" value=".jsp" />
        </bean>
    d. 设置静态资源不过滤
        <mvc:resources location="/css/" mapping="/css/**" />
        <mvc:resources location="/images/" mapping="/images/**" />
        <mvc:resources location="/js/" mapping="/js/**" />
    e. 开启SpringMVC的注解扫描
        <mvc:annotation-driven />
    f. 结束符
        </beans>
  1. Spring整合SpringMVC:为了能在Controller的方法中调用service层的方法
1. Spring管理对象都是放在容器中,为了能调用service层的方法,首先需要想到定义对象,然后进行注入
    - 当前状况:在web.xml文件中仅配置了扫描springmvc.xml配置文件,没有加载Spring的配置文件
    - 解决方案:在Tomcat服务器启动时需要加载Spring配置文件
        - 一个web应用的核心对象ServletContext域对象仅有唯一的一个,在服务器启动时创建,在服务器关闭时销毁
        - 存在一系列监听器用于监听ServletContext对象的创建和销毁
        - 于是在配置对象创建监听器时,读取Spring的配置文件
        - Spring提供了这样一个监听器用于创建web版的工厂,存储在ServletContext域对象中(ContextLoaderListener监听器)
        - ContextLoaderListener监听器默认加载WEB-INF目录下的applicationContext.xml配置文件,原配置文件放在resources目录下
        - 配置初始化参数,修改配置文件加载路径
2. 在web.xml配置文件中添加<listener>标签
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listenerclass>
    </listener>
    <!-- 配置加载类路径的配置文件 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
3. 在web表现层中定义service层对象,使用@Autowired注解自动注入
  1. Mybatis配置
1. 在resources目录下创建sqlMapConfig.xml配置文件
    - 约束头
        <?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">
2. 在<configuration>标签中插入<environments>标签配置数据库环境
    <environments default="mysql">
        <environment id="mysql">
        <transactionManager type="JDBC"/>
        <dataSource type="POOLED">
            <property name="driver" value="com.mysql.jdbc.Driver"/>
            <property name="url" value="jdbc:mysql:///ssm"/>
            <property name="username" value="root"/>
            <property name="password" value="root"/>
        </dataSource>
        </environment>
    </environments>
3. 配置需要扫描的dao包,在<configuration>标签中添加<mappers>标签
    <mappers>
        <!-- 该包下所有的dao接口都可以使用 -->
        <package name="cn.mysilent.dao"/>
    </mappers>
4. 结束符
    </configuration>
  1. Spring整合Mybatis:为了在service层调用dao层的方法,实际整合过程就是将sqlMapConfig.xml配置文件中的内容转移到applicationContext.xml配置文件中去。
1. 正常使用Mybatis是
    a. 通过读取sqlMapConfig.xml文件来读取数据库信息
    b. 通过创建SqlSessionFactory工厂来获取SqlSession对象
    c. 通过SqlSession对象来创建dao的代理对象
    d. 通过代理对象来调用dao方法
2. Spring整合Mybatis同样是考虑自动注入的方式来获取代理对象
    a. 第一步:在Spring配置文件中配置连接池(在这里使用c3p0连接池技术)
        在<beans>标签中添加<bean>标签配置数据库连接池
        <bean id="dataSource" class="org.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClassName" value="com.mysql.jdbc.Driver" />
            <property name="url" value="jdbc:mysql:///ssm" />
            <property name="username" value="root" />
            <property name="password" value="root" />
        </bean>
    b. 第二步:在Spring配置文件中配置SqlSessionFactory工厂,存入IOC容器中
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
        </bean>
    c. 第三步:配置注解扫描的包
        <bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="cn.mysilent.dao"/>
        </bean>
3. 在dao层的接口编写中添加@Repository注解,由Spring创建代理对象并添加到容器中
4. 在service层的实现类中定义dao接口对象,并使用@AutoWired注解自动注入
  1. Spring整合Mybatis的事务管理
1. 在没有配置事物管理时,查找功能可以正确执行,但是增删改功能并不能对数据库进行实质操作,需要手动调用commit方法进行提交
2. 在Spring中配置事务管理
    a. 配置事务管理器,在applicationContext.xml配置文件<beans>标签中插入<bean>标签
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
            <property name="dataSource" ref="dataSource"></property>
        </bean>
    b. 配置事务的通知,在applicationContext.xml中使用tx的标签约束
        <!-- 配置事务的通知 --> 
        <tx:advice id="txAdvice" transaction-manager="transactionManager">
            <tx:attributes>
                <!-- 使用通配符来标识事务的管理 -->
                <tx:method name="*" propagation="REQUIRED" read-only="false"/>
                <!-- 标明find打头的方法(查找类)不需要进行事务管理 -->
                <!-- 编程时需要遵循命名规范 -->
                <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
            </tx:attributes>
        </tx:advice>
    c. 配置AOP的增强,将事务管理的前,后,异常通知使用注解标注并使用动态代理的技术来管理实现
        <aop:config>
            <!-- 配置切入点表达式,表示该包下任意实现类的任意方法都是切入点 -->
            <aop:pointcut expression="execution(* com.mysilent.service.impl.*.*(..))" id="pt1"/>
            <!-- 建立通知和切入点表达式的关系 -->
            <aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"/>
        </aop:config>
3. spring框架基于注解的AOP控制(除环绕通知手动控制)在执行顺序上存在瑕疵
        前置--业务方法--最终--后置
        前置--业务方法--最终--异常
        因此使用注解,建议使用环绕通知
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值