搭建SSM框架环境(spring整合mybatis整合springMVC)


1、SSM:spring+mybatis+springmvc

2、springMVC:视图层,界面层,负责接收请求,显示处理结果;

      spring:业务层,管理service、dao,工具类对象的;

     mybatis:持久层,访问数据库的;

用户发起请求---------springmvc接收----spring中的service对象-----mybatis处理数据

3、实现步骤:

《1》创建maven web项目

《2》加入依赖

      springmvc,spring,mybatis三个框架的依赖,Jackson依赖,mysql驱动,druid连接池,jsp,servlet依赖

《3》写web.xml文件

         1、注册DispatcherServlet,目的:创建springmvc的容器对象,才能创建controller类对象;创建的是servlet,才能接收用户的请求;

        2、注册spring的监听器:contextloaderlistener,目的:创建spring的容器对象,才能创建service,dao等对象

        3、注册字符集过滤器,解决post请求乱码的问题

《4》、创建包 : controller包,service,dao,实体类包

《5》、写springmvc、spring、mybatis的配置文件

    resources:springmvc配置文件

                        spring配置文件

                        mybatis配置文件

                        数据库的属性配置文件

《6》、写java代码 

《7》、写jsp页面

4、具体环境配置代码

pom.xml(ssm框架需要的依赖和resources插件)

  
    
      junit
      junit
      4.11
      test
    
    
      javax.servlet
      javax.servlet-api
      3.1.0
      provided
    
    
      javax.servlet.jsp
      jsp-api
      2.1
      provided
    
    
      org.springframework
      spring-webmvc
      4.2.9.RELEASE
    
    
      org.springframework
      spring-tx
      5.2.5.RELEASE
    
    
      org.springframework
      spring-jdbc
      4.2.9.RELEASE
    
    
      com.fasterxml.jackson.core
      jackson-core
      2.9.0
    
    
      com.fasterxml.jackson.core
      jackson-databind
      2.9.0
    
    
      org.mybatis
      mybatis-spring
      1.3.1
    
    
      org.mybatis
      mybatis
      3.5.1
    
    
      mysql
      mysql-connector-java
      5.1.9
    
    
      com.alibaba
      druid
      1.1.12
    
    
      com.github.pagehelper
      pagehelper
      5.3.0
    
  

  
    
      
        src/main/java
        
          **/*.properties
          **/*.xml
        
        false
      
    
  

web.xml

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

         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_3_0.xsd"
         version="3.0">
  
  
    /WEB-INF/jsp/zhuce.jsp
  



  Archetype Created Web Application
  
  
  
    springmvc
    class>org.springframework.web.servlet.DispatcherServletclass>
    
    
      contextConfigLocation
      classpath:dispatcherservlet.xml
    
    
    1
  
  
  
    springmvc
    /
  


    
        contextConfigLocation
        classpath:applicationContext.xml
    
    
        class>org.springframework.web.context.ContextLoaderListenerclass>
    

  
    
        CharacterEncodingFilter
        class>org.springframework.web.filter.CharacterEncodingFilterclass>
        
            encoding
            utf-8
        
        
            forceRequestEncoding
            true
        
        
            forceResponseEncoding
            true
        
    
    
        CharacterEncodingFilter
        /*
    


springMVC 配置文件

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

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

    package="com.itheima.controller"/>
    default-servlet-handler/>
    
    
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"  id="internalResourceViewResolver">
        
        
        
        

    

数据库属性配置文件

jdbc.properties
jdbc.url=jdbc:mysql://localhost:3306/donation
jdbc.username=root
jdbc.password=root
spring配置文件
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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/context http://www.springframework.org/schema/context/spring-context.xsd">


 
    class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        
        
        

    

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

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



  
    package="com.itheima.service"/>
 
 
 

                        

相关