4、集成
1、Spring 集成 Mybatis

applicationContext.xml
<!--连接池-->
<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/suns?useSSL=false"/>
    <property name="username" value="root"/>
    <property name="password" value="123456"/>
</bean>
<!--创建 SqlSessionFactoryBean(在 Mybatis 中叫 SqlSessionFactory)-->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="typeAliasesPackage" value="com.baizhiedu.entity"/>
    <property name="mapperLocations">
        <list>
            <value>classpath:com.zzw.mapper/*Mapper.xml</value>
        </list>
    </property>
</bean>
<!--创建 DAO 对象 MapperScannerConfigure-->
<bean id="scanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"/>
    <property name="basePackage" value="com.baizhiedu.dao"/>
</bean>
<!--配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
</bean>
<!--开启事务注解-->
<tx:annotation-driven  transaction-manager="transactionManager"/>
2、Spring 集成 SpringMVC
* Spring 和 SpringMVC 本身就是一家产品, 是不用整合的
* 但是现在的 Spring 容器自己无法启动, 我们需要在 web 容器启动的时候, 加载 Spring 的配置文件, 启动 Spring 容器
* 那么这个工作是在 spring-web 包中的一个监听器来做的, 这个包不用单独导入, 他已经在 spring-webmvc 包中了
* 它会监听 WEB 容器的启动和停止, 然后就可以控制 Spring 容器的启动和停止了
2.1、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">
    <!--spring 核心配置文件位置-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <!--
        spring Listener
        监听 ServletContext, 创建 Spring 容器放入 ServletContext 中 (Application 域)
        Spring 监听器: 当 tomcat 启动的时候, 唤醒 Spring 容器, 需要给监听器一个配置文件
    -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!--编码过滤器-->
    <filter>
        <filter-name>encFilter</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>encFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!--DispatcherServlet-->
    <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:springmvc.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>
2.2、spring-mvc.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:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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/util
       http://www.springframework.org/schema/util/spring-util.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
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!--扫描 controller-->
    <context:component-scan base-package="com.zzw.controller" />
    <!--这里配置三大组件-->
    <mvc:annotation-driven />
    <!--视图解析器, 它是用来将逻辑视图转换为物理视图, 如果是异步的话, 就不需要视图解析器了, 因为不需要返回页面-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
        <!--路径的前缀和后缀-->
    </bean>
    <!--配置静态资源放行-->
    <mvc:default-servlet-handler/>
</beans>
2.3、applicationContext.xml
核心:把 mybatis 的配置文件和 SqlSessionFactoryBean 扔到 spring 中,让 spring 管理 mapper 层
<?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:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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/util
       http://www.springframework.org/schema/util/spring-util.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
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!--扫描 service 层-->
    <context:component-scan base-package="com.zzw.service" />
    <!--加载外部属性文件-->
    <context:property-placeholder location="classpath:jdbc.properties" />
    <!--配置德鲁伊数据源-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="driverClassName" value="${jdbc.driver}"/>
    </bean>
    <!--配置 SqlSessionFactoryBean-->
    <bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--数据源-->
        <property name="dataSource" ref="dataSource"/>
        <!--实体类别名包扫描-->
        <property name="typeAliasesPackage" value="com.zzw.pojo"/>
    </bean>
    <!--配置 MapperScanner 扫描 mapper.xml 和 接口-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--配置 SQLSessionFactory-->
        <property name="sqlSessionFactoryBeanName" value="factory"/>
        <!--扫描 mapper-->
        <property name="basePackage" value="com.zzw.mapper"/>
    </bean>
    <!--配置事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--开启事务注解-->
    <tx:annotation-driven  transaction-manager="transactionManager"/>
</beans>
3、Spring 的父子容器
Spring 和 SpringMVC 的容器具有父子关系,Spring 容器为父容器,SpringMVC 为子容器
子容器可以引用父容器中的 Bean,而父容器不可以引用子容器中的 Bean
本文来自博客园,作者:lidongdongdong~,转载请注明原文链接:https://www.cnblogs.com/lidong422339/p/17550248.html

 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号