总结51 整合SSM框架(将Mybatis交由Spring管理)

概念

既然说到整合SSM框架,这个’SSM’就是指三大框架
Spring和SpringMvc以及Mybatis框架
我们要将这三大框架整合到一起
甚至,我们通俗点来说,就是将Mybatis框架的一些功能交由Spring框架来接管

准备 导入jar包

我们需要除了导入Spring,SpringMvc等其它Jar包外,还需要导入’mybatis-spring’这个Jar包
它的作用就是用来将Myatis整合到Spring中.
格式:

 <!-- Spring整合Mybatis的Jar包 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.0</version>
        </dependency>

整合Mybatis框架

准备 - 删除冲突配置

要求:
1.删除sqlMapConfig.xml中的连接池环境信息,而在applicationContext.xml中配置连接池环境,以便给Spring管理
2.删除sqlMapConfig.xml中用于扫描sql操作包路径的配置,也就是’mappers’
‘标签

应用

应用规范和意义如下(在applicationContext中):
在这里插入图片描述
格式模板:


<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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/tx
			http://www.springframework.org/schema/tx/spring-tx.xsd
			http://www.springframework.org/schema/aop
			http://www.springframework.org/schema/aop/spring-aop.xsd
			http://www.springframework.org/schema/context
			http://www.springframework.org/schema/context/spring-context.xsd">


      <!--指定包路径
        (:如果你要使用springmvc,那么你还需要在springmvc.xml下也定义这样的扫描,但是指定的包路径必须为"controller")
        -->
          <context:component-scan base-package="指定一个用于spring的包路径,可以是宏观路径(cn.project),也可以是微观路径(cn.project.service)">

              <!-- 排除'Controller'注解,因为这个注解是用于springMvc的,而不是用于Spring的.否则会冲突 -->
              <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:exclude-filter>

          </context:component-scan>


                     <!-- Spring集成Mybatis -->
<!--.替代sqlMapConfig中的数据源(必须) -->
        <!-- 加载properties文件 -->
    <context:property-placeholder location="classpath:连接池配置文件名.properties"/>
        <!-- 配置数据连接池 此处以Druid连接池为核心,通过properties文件获取连接参数 -->
    <bean id="dataSourceId" class="com.alibaba.druid.pool.DruidDataSource"> <!-- 'dataSourceId'为你所自定义的数据库连接池id -->
        <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>


<!--.让Spring加载Mybatis 用于替代Service层中'sqlSession工厂'的代码 -->
    <bean class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSourceId"/> <!-- 让Spring通过Mybatis加载数据源 'sqlMapConfig.xml'核心配置文件中必须删除数据环境源 -->
        <property name="configLocation" value="classpath:sqlMapConfig.xml"/><!-- 让Spring同核心配置文件'sqlMapConfig.xml'耦合,以便引用其它配置(如核心配置文件中的分页查询插件) -->

    </bean>

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="指定一个Dao层的包路径(非接口绝对路径)"/> <!-- 指向一个Dao层的包路径,用于替代Service层sqlSession工厂的"getMapper"方法.
                                                                        而在指向Dao层路径同时,也替代了sqlMapConfig中用于加载映射SQL操作配置文件的'mappers'标签-->

    </bean>



<!-- 配置事务 如果你不需要事务管理,可以删除下面的配置信息 -->

    <!--.配置事务管理器 内核驱动为:Jdbc版本 -->
    <bean id="DataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

        <property name="dataSource" ref="dataSourceId"></property>

    </bean>

    <!--.定义Service层的哪些方法要应用到事务中,并对这些方法设置特定的传播规则等等.
            注意:.定义传播规则的’propagation’和定义超时时间的’timeout’可以省略.
                    比如: <tx:method name=“methodName”/>.name参数可以用星号’*'来模糊化匹配
            -->
    <tx:advice id="自定义事务规则ID" transaction-manager="DataSourceTransactionManager">
        <tx:attributes>
            <tx:method name="方法名或者模糊匹配表达式" propagation="自定义传播规则-可省略该参数" timeout="自定义超时时间-可省略该参数"/>
        </tx:attributes>
    </tx:advice>

    <!--.配置AOP事务切面 即:指定哪些类要归事务接管,需要用到切入点表达式. -->
    <aop:config>
        <aop:advisor advice-ref="自定义事务规则ID,要同上方一致" pointcut="execution(自定义切入点表达式)"></aop:advisor>
    </aop:config>

</beans>

扩展 - 设置别名

Spring加载Mybatis的applicaionContext文件中,Spring也能实现Mybatis的”起别名”功能
如下图:
在这里插入图片描述
其中:property name=”typeAliasesPackage” value=”com.itheima.domain”就是给特定包路径下的接实体类起别名.所有’com.itheima.domain’下的实体类文件都会被自动起别名.而别名就是以文件名大写或小写开头的名字.

扩展 - 集合Mybaits的插件(没必要)

注意:如果不想在sqlMapConfig中配置插件,我们也可以在Spring的applicationContext中配置插件,比如配置分页查询的pagehelper插件.但问题是,这样会让applicationContext配置文件变得臃肿.所以推荐把属于Mybatis的插件还是配置在sqlMapConfig中就好

在这里插入图片描述

SSM集成配置文件

在这里插入图片描述

需要配置4个配置文件
1.applicationContext.xml
2.applicationContext-service.xml
3.sping-mvc.xml
4.web.xml

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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/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">

    <!--配置数据源信息,使用druid连接池-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/ssmtest"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
    </bean>
    <!--配置spring整合mybatis框架的SQLSessionFactoryBean-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--扫描pojo包路径,为实体类创建别名-->
        <property name="typeAliasesPackage" value="cn.ssmtest.pojo"/>
    </bean>

    <!--mapper扫描器,用于产生代理对象(扫描dao层的包路径)-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="cn.ssmtest.dao"/>
    </bean>
</beans>

applicationContext-service.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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/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">

    <!--配置扫描器,扫描Service层包路径-->
    <context:component-scan base-package="cn.ssmtest.service"/>

    <!--.声明事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--事物注解驱动-->
    <tx:annotation-driven transaction-manager="transactionManager"/>


</beans>



spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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/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">

    <!--配置扫描器,扫描Controller层包路径-->
    <context:component-scan base-package="cn.ssmtest.controller"/>

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

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>

    <!--指定Spring配置文件位置-->
    <!-- 'classpath*'   意味着不光匹配本项目 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:applicationContext*.xml</param-value>
    </context-param>

    <!--配置Spring框架启动时使用的监听器-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!--配置SpringMVC的前端控制器-->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 指定SpringMvc配置文件的文件名(resources路径下) -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <!--让前端控制器随着项目的启动而加载-->
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.do</url-pattern><!-- 更改为*.do则表示,只要是url链接为.do结尾的,就不对其拦截(当然,jsp文件更不在拦截范围之内) -->
    </servlet-mapping>

</web-app>

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值