还不会整合框架?SpringMVC Spring Mybatis 框架整合看这个就够了!

看过之前的蜕变系列文章,相信你对SpringMVC 、Spring、 Mybatis有了一些应用上的感性认识。但是都还是单个使用,并没有放到一起来使用,今天我们讲讲怎样将这三个框架结合起来使用,学会搭建属于自己的开发框架——也就是俗称的SSM框架。

猿蜕变同样是一个原创系列文章,帮助你从一个普通的小白,开始掌握一些行业内通用的框架技术知识以及锻炼你对系统设计能力的提升,完成属于你的蜕变,更多精彩内容,敬请大家关注公主号猿人工厂,点击猿人养成获取

还不会整合框架?SpringMVC Spring Mybatis 框架整合看这个就够了!_第1张图片

还不会整合框架?SpringMVC Spring Mybatis 框架整合看这个就够了!_第2张图片

还不会整合框架?SpringMVC Spring Mybatis 框架整合看这个就够了!_第3张图片

还不会整合框架?SpringMVC Spring Mybatis 框架整合看这个就够了!_第4张图片

还不会整合框架?SpringMVC Spring Mybatis 框架整合看这个就够了!_第5张图片

 

到目前为止,我们已经学习过三个框架了,SpringMVC Spring Mybatis. Spring MVC 专注于MVC方面的事情,Spring作为IOC容器管理Bean的生命周期,MyBatis用于专注于数据持久层面的事情。三个框架分别负责处理三方面的问题,有条不紊,各有分工。今天我们就来将三个框架融合到一起,让他们协同工作,发挥各自的优势。这也是以后大家会在工作中经常使用的框架——SSM(Spring MVC Spring   Mybatis的缩写),不过嘛SpringMVC属于Spring的一部分,你叫SM也不是不可以哒。Spring MVC本来就是Spring生态圈的一部分,这两者间天生就是融合的,不存在整合一说,我们主要做的事情,是将Mybatis和Spring融合到一起,就好了——将SqlSessionFactory和事务处理方面的事情交给Spring来管理。

还不会整合框架?SpringMVC Spring Mybatis 框架整合看这个就够了!_第6张图片

如果你学到今天都还不会创建web应用?你真滴可能不太适合做一只猿类,放弃吧。

还不会整合框架?SpringMVC Spring Mybatis 框架整合看这个就够了!_第7张图片

增加项目依赖

修改pom.xml增加SpringMVC Spring Mybatis相关项目依赖


 
       xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
       4.0.0
 
       com.pz.study.frame
       ssmDemo
       0.0.1-SNAPSHOT
       war
 
       mybatis Maven Webapp
      
       http://www.example.com
 
 
      
              UTF-8
              1.7
              1.7
      

 
      
             
             
                     junit
                     junit
                     4.11
                     test
             

 
             
             
             
                     javax.servlet
                     javax.servlet-api
                     3.1.0
                     provided
             

             
             
                     com.alibaba
                     fastjson
                     1.2.8
             

 
             
             
                     joda-time
                     joda-time
                     2.9
             

             
             
                     org.hibernate
                     hibernate-validator
                     5.0.2.Final
             

 
             
                     org.springframework
                     spring-webmvc
                     3.2.4.RELEASE
             

 
 
             
                     commons-io
                     commons-io
                     2.6
             

 
 
             
                     commons-fileupload
                     commons-fileupload
                     1.3.3
             

             
 
             
             
                     org.springframework
                     spring-context
                     4.1.2.RELEASE
                     compile
             

 
             
             
                     org.aspectj
                     aspectjweaver
                     1.7.4
             

             
             
                     org.springframework
                     spring-aspects
                     4.1.2.RELEASE
             

             
 
             
 
             
 
             
                     mysql
                     mysql-connector-java
                     5.1.26
             

             
             
                     com.alibaba
                     druid
                     1.0.9
             

             
             
                     log4j
                     log4j
                     1.2.17
             

 
             
                     org.slf4j
                     slf4j-api
                     1.7.25
             

             
                     org.slf4j
                     slf4j-log4j12
                     1.7.25
             

 
 
      

 
      
              ssmDemo
             
                    
                            src/main/resources
                            true
                    

             

             
                    
                           
                           
                           
                                   org.apache.maven.plugins
                                   maven-compiler-plugin
                                  
                                          1.7
                                          1.7
                                          utf-8
                                  

                           

                           
                           
                                   org.apache.tomcat.maven
                                  
                                   tomcat7-maven-plugin
                                   2.1
                                  
                                         
                                          80
                                         
                                          /
                                          UTF-8
                                  

                           

                    

             

      

分类建立配置文件目录

    在resources文件目录下建立两个子目录spring和mapper用于存放spring的各个模块配置以及mybatis的mapper文件。这一步你也可以省略,只是以后配置文件显得比较杂乱。建立项目总控文件applicationContext.xml


       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/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/context
       http://www.springframework.org/schema/context/spring-context.xsd"
        default-autowire="byName">
 
      
   
    
   
      
   

   
                  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
 
             
 
             
 
                    
 
                            classpath:db.properties
 
                    

 
             

 
      

   
   
   
     
     
     

 

 

还不会整合框架?SpringMVC Spring Mybatis 框架整合看这个就够了!_第8张图片

 

 

指定配置web.xml


       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
       order-center
   
   
      
        EncodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
       
            encoding
            UTF-8
       

       
            forceEncoding
            true
       

   

   
        EncodingFilter
        /*
   

   
     
   
              springmvc
              org.springframework.web.servlet.DispatcherServlet
             
                     contextConfigLocation
                     classpath:ApplicationContext.xml
             

              1
 
      

 
      
              springmvc
              /
      

      
      
              index.jsp
      

 
 

还不会整合框架?SpringMVC Spring Mybatis 框架整合看这个就够了!_第9张图片

配置mybatis总控文件


        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">

 
   
   
             
      

      
      
             
             
      

      
 

还不会整合框架?SpringMVC Spring Mybatis 框架整合看这个就够了!_第10张图片

配置spring整合mybatis的文件

spring-mybatis.xml


       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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/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"
        default-autowire="byName">
 
   
              destroy-method="close">
       
       
       
       
      
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
                                   value="0"/>
   

   
   
       
       
       
       
   

   
   
       
       
   

还不会整合框架?SpringMVC Spring Mybatis 框架整合看这个就够了!_第11张图片

配置spring-mvc相关文件

spring-mvc.xml


 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="
          http://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.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.xsd"
       default-autowire="byName">
 
      
      
 
 
      
                     class="org.springframework.web.servlet.view.InternalResourceViewResolver">
             
             
      
 
 
 
      
      
 
      
                     class="org.springframework.context.support.ConversionServiceFactoryBean">
             
      

 
      
                     class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
             
      

 
 
 
      
                     validator="hibernateValidator">
             
                                                 class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
                           
                                  
                                          text/html;charset=UTF-8
                                          application/json
                                          application/xml;charset=UTF-8
                                  

                           

                           
                                  
                                         
                                         
 
                                          WriteMapNullValue
 
                                          WriteNullNumberAsZero
                                          WriteNullListAsEmpty
                                          WriteNullStringAsEmpty
                                          WriteNullBooleanAsFalse
                                          WriteDateUseDateFormat
 
                                  

                           

                    

 
             
      
      
 
      
 
 
                     class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
             
             
             
             
             
             
             
             
             
             
      

 
 
   
 
 
 

还不会整合框架?SpringMVC Spring Mybatis 框架整合看这个就够了!_第12张图片

 

配置service层相关文件

spring-service.xml


 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="
          http://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.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.xsd"
       default-autowire="byName">
      
             
 
 

还不会整合框架?SpringMVC Spring Mybatis 框架整合看这个就够了!_第13张图片

 

配置事务管理相关文件

spring-tx.xml


       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/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/context
       http://www.springframework.org/schema/context/spring-context.xsd"
        default-autowire="byName">
 
   
              class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
       
       
   

   
   
       
           
           
           
           
           
           
           
           
           
           
       

   

   
   
       
                             pointcut="execution(*com.pz.web.study.ssm.service.*.*(..))" />
   

 

还不会整合框架?SpringMVC Spring Mybatis 框架整合看这个就够了!_第14张图片

配置数据库

db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/route?characterEncoding=utf8
jdbc.username=root
jdbc.password=123456

 

配置log4j

log4j.properties
log4j.rootLogger=trace,console
 
log4j.appender.console= org.apache.log4j.ConsoleAppender
log4j.appender.console.Target= System.out
log4j.appender.console.layout= org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=[%-5p][%d{yyyy-MM-ddHH:mm:ss}]%m%n

我建了一个技术群,群里有很多高手,加小编微信,备注:学习。带你见识更多的高手,帮你快速成长。

你可能感兴趣的:(猿进化)