SSM三大框架整合 Demo

一,整合思路及步驟

1. 搭建工程環境

   a. 創建一個動態的web工程

   b. 加入 Spring+Springmvc相關的jar包

   c. 加入 Mybatis 相關的jar包

   d. 加入 Mybatis 與Spring 的整合適配包

   e. 加入 連接池、 數據庫驅動、 日誌 等.

2. 搭建Spring + Springmvc的 環境:

  a. 在web.xml中配置 : 

      1). 配置創建Spring容器對象的監聽器  ContextLoaderListener

      2). 配置Springmvc的前端控制器   DispatcherServlet

      3). 配置解決中文亂碼過濾器   CharacterEncodingFilter

      4). 配置REST 過濾器   HiddenHttpMethodFilter

 

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">

  <!-- 字符編碼過濾器 -->

  <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>

 

  <!-- REST 過濾器 -->

  <filter>

    <filter-name>HiddenHttpMethodFilter</filter-name>

    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>

  </filter>

  <filter-mapping>

    <filter-name>HiddenHttpMethodFilter</filter-name>

    <url-pattern>/*</url-pattern>

  </filter-mapping>

  <!-- 創建Spring容器對象的監聽器 -->

    <context-param>

        <param-name>contextConfigLocation</param-name>

        <param-value>classpath:spring.xml</param-value>

    </context-param>

    <listener>

        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

    </listener>

 

  <!-- Springmvc的前端控制器 -->

    <servlet>

        <servlet-name>springDispatcherServlet</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>springDispatcherServlet</servlet-name>

        <url-pattern>/</url-pattern>

    </servlet-mapping>

 

</web-app>

 

 

 

b. 編寫Spring框架的配置文件  spring.xml

   1). 組件掃描

   2). 數據源

   3). 事務

   4). 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"

    xmlns:tx="http://www.springframework.org/schema/tx"

    xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"

    xsi:schemaLocation="http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd

        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-4.0.xsd

        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

    

    <!-- 組件掃描 -->

    <context:component-scan base-package="com.atguigu.ssm">

        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>

    </context:component-scan>

    

    <!-- 數據源 -->

    <context:property-placeholder location="classpath:db.properties"/>

    <bean id="myDataSource" 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>

    

    <!-- 事務管理 -->

    <bean id="transactionManager"

         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

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

    </bean>

    <tx:annotation-driven />

    

    <!-- 整合MyBatis -->

    <!-- 1.SqlSession對象的創建及管理

         MyBatis : SqlSessionFactory(mybatis-config.xml)==>openSession()==>SqlSession

         SSM     : SqlSessionFactoryBean ==>getObject()

     -->

     <bean class="org.mybatis.spring.SqlSessionFactoryBean">

        <!-- 1.注入Mybatis的配置文件 -->

        <property name="configLocation" value="classpath:mybatis-config.xml"></property>

        <!-- 2.注入數據源 -->

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

        <!-- 別名處理

        <property name="typeAliasesPackage" value="com.atguigu.ssm.beans"></property>

        -->

        <!-- 3.引入SQL映射文件 -->

        <property name="mapperLocations" value="classpath:mybatis/mapper/*.xml"></property>

     </bean>

    

     <!-- 2. Mapper接口代理實現類對象的創建及管理

          MyBatis: SqlSession.getMapper(xxxMapper.class)

          SSM    : MapperScannerConfigurer ,

                       爲指定包下的Mapper接口批量創建代理實現類對象並管理到Spring容器中

                      

                   eg: EmployeeMapper ==>$ProxyN  ==>  IOC容器中bean的id: employeeMapper

     -->

     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

        <property name="basePackage" value="com.atguigu.ssm.mapper"></property>

     </bean>

    

     <!-- <mybatis-spring:scan base-package="com.atguigu.ssm.mapper"/> -->

    

</beans>

 

 

c. 編寫Springmvc框架的配置文件  springmvc.xml

  1). 組件掃描

  2). 視圖解析器

  3). <mvc:xxx>

 

<?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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd

        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-4.0.xsd">

    

    <!-- 組件掃描 -->

    <context:component-scan base-package="com.atguigu.ssm" use-default-filters="false">

        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>

    </context:component-scan>

    

    <!-- 視圖解析器 -->

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

        <property name="prefix" value="/WEB-INF/views/"></property>

        <property name="suffix" value=".jsp"></property>

    </bean>

    

    <!-- mvc   解決靜態資源上傳問題-->

    <mvc:default-servlet-handler/>

    <mvc:annotation-driven/>

</beans>

 

 

 3. 搭建MyBatis框架的環境

    a. 全局配置文件  mybatis-config.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">

<configuration>

    

    

    <!-- 2. settings : 很多重要的設置項,可以改變Mybatis的運行行爲 -->

    <settings>

        <!-- 映射下劃線到駝峯命名     last_name==>lastName -->

        <setting name="mapUnderscoreToCamelCase" value="true"/>

        <!-- 開啓延遲加載 -->

        <setting name="lazyLoadingEnabled" value="true"/>

        <!-- 設置按需加載  還是 全部加載 -->

        <setting name="aggressiveLazyLoading" value="false"/>

        

        <!-- 二級緩存 -->

        <setting name="cacheEnabled" value="true"/>

    </settings>

    

    <!-- 3. typeAliases:別名處理

            alias: 指定別名。 如果不指定,默認的別名是類名(不區分大小寫)

            package: 爲指定包以及子包下的類取默認的別名

     -->

    <typeAliases>

        <package name="com.atguigu.ssm.beans"/>

    </typeAliases>

    

    <!-- 註冊插件 -->

    <plugins>

        <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>

    </plugins>

</configuration>

    b. JavaBean、 Mapper接口 、 映射文件

  4. Spring 整合 MyBatis:

  a. SqlSession對象的創建及管理(交由spring管理,在spring.xml文件中整合,上面有例子

    b. Mapper接口代理實現類對象的創建及管理(交由spring管理,在spring.xml文件中整合,上面有例子)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章