SSM(SpringMVC,spring,myBatis)框架整合步骤

一.导包

    文件夹:所需jar包 下jar包导进去即可,https://pan.baidu.com/s/1K2rmSRiQ0jPKf3tjAoaggg

二.整合MyBatis与spring框架

  •     1.创建数据库基本配置信息文件jdbc.properties

        jdbc.driver=com.mysql.jdbc.Driver
        jdbc.url=jdbc:mysql://localhost:3306/ssm-crm?characterEncoding=utf-8
        jdbc.username=root
        jdbc.password=123456
        

  •     2.创建spring DAO层配置文件applicationContext-dao.xml配置数据源,配置SqlSessionFactory,开启Mapper的动态扫描,放到src下的spring文件夹下

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

            <!-- 配置 读取properties文件 jdbc.properties -->
            <context:property-placeholder location="classpath:jdbc.properties" />

            <!-- 配置 数据源 -->
            <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
                <property name="driverClassName" value="${jdbc.driver}" />
                <property name="url" value="${jdbc.url}" />
                <property name="username" value="${jdbc.username}" />
                <property name="password" value="${jdbc.password}" />
            </bean>

            <!-- 配置SqlSessionFactory -->
            <bean class="org.mybatis.spring.SqlSessionFactoryBean">
                <!-- 设置MyBatis核心配置文件 -->
                <property name="configLocation" value="classpath:SqlMapConfig.xml" />
                <!-- 设置数据源 -->
                <property name="dataSource" ref="dataSource" />
            </bean>

                 <!-- 注解配置事物 -->
                    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
                          <property name="dataSource" ref="dataSource"></property>
                   </bean>
                <!-- 开启注解 -->
                   <tx:annotation-driven transaction-manager="transactionManager"/>

                   <!-- 配置Mapper扫描 -->
                   <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
                    <!-- 设置Mapper扫描包 -->
                  <property name="basePackage" value="com.wenhao.mapper" />
                    </bean>
        </beans>
    
    

  •     3.创建SqlMapConfig.xml文件,并在applicationContext-dao.xml配置SqlSessionFactory工厂(第二步已配置)

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

        <!-- 为pojo类设置别名 -->
            <typeAliases>
                <!-- 2. 指定扫描包,会把包内所有的类都设置别名,别名的名称就是类名,大小写不敏感 -->
                <package name="com.wenhao.pojo" />
            </typeAliases>
            
            <!-- <mappers>
                <package name="com.wenhao.mapper"/>
            </mappers> -->
        </configuration>
        

  •     4.在web.xml文件中配置spring容器

        <!-- 配置spring -->
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/applicationContext-*.xml</param-value>
        </context-param>

        <!-- 配置监听器加载spring -->
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>


三.配置springmvc框架

 

  •     1.在spring文件夹下创建springmvc.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: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-4.0.xsd
                http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
            <!-- 配置Controller扫描 -->
            <context:component-scan base-package="com.wenhao.controller" />

            <!-- 处理器映射器 -->
            <!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>    -->
            <!-- 处理器适配器 -->
            <!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/> -->

            <!-- 配置注解驱动 -->
            <mvc:annotation-driven />
            
            <!-- 对静态资源放行(对应springmvc在web.xml中配置为<url-pattern>/</url-pattern>) -->
            <mvc:resources location="/css/" mapping="/css/**"></mvc:resources>
            <mvc:resources location="/fonts/" mapping="/fonts/**"></mvc:resources>
            <mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
            
            <!-- 配置Conveter转换器  转换工厂 (日期、去掉前后空格)。。 -->
            <bean id="conversionServiceFactoryBean" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
                <!-- 配置 多个转换器-->
                <property name="converters">
                    <list>
                        <bean class="com.wenhao.conversion.DateConveter" />
                    </list>
                </property>
            </bean>
            
            <!-- 配置拦截器 -->
            <mvc:interceptors>
                <mvc:interceptor>
                    <!-- 所有请求都进入拦截器 -->
                    <mvc:mapping path="/**"/>
                    <!-- 配置具体的拦截器 -->
                    <bean class="com.wenhao.interceptor.Interceptor1"/>
                </mvc:interceptor>
                <!-- <mvc:interceptor>
                    所有请求都进入拦截器
                    <mvc:mapping path="/**"/>
                    配置具体的拦截器
                    <bean class="com.wenhao.interceptor.Interceptor2"/>
                </mvc:interceptor> -->
            </mvc:interceptors>
            
            <!-- 上传图片配置实现类 -->
            <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
                <property name="maxUploadSize" value="50000000"></property>    
            </bean>
            
            <!-- springmvc异常处理器 -->
            <bean class="com.wenhao.exception.CustomerExceptionResolver"/>
                
                    
                    
            <!-- 配置视图解析器 -->
            <bean    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                <!-- 前缀 -->
                <property name="prefix" value="/WEB-INF/jsp/" />
                <!-- 后缀 -->
                <property name="suffix" value=".jsp" />
            </bean>
        </beans>
        

  •     2.在web.xml中配置springmvc前端控制器,解决post的乱码问题

        <!-- 配置过滤器,解决post的乱码问题 -->
        <filter>
            <filter-name>encoding</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>encoding</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
      
      
      <!-- 配置SpringMVC -->
        <servlet>
            <servlet-name>crm</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:spring/springmvc.xml</param-value>
            </init-param>
            <!-- 配置springmvc什么时候启动,参数必须为整数 -->
            <!-- 如果为0或者大于0,则springMVC随着容器启动而启动 -->
            <!-- 如果小于0,则在第一次请求进来的时候启动 -->
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>crm</servlet-name>
            <!-- 1. /* 拦截所有 jsp js png .css 真的全拦截 建议不使用 2. *.action *.do 拦截以do action
            结尾的请求 肯定能使用 ERP 3. / 拦截所有 (不包括jsp) (包含.js .png.css) 强烈建议使用 前台 面向消费者 www.jd.com/search
            /对静态资源放行 -->
            <url-pattern>/</url-pattern>
        </servlet-mapping>
        

  •     3.开启service层注解扫描    

        在spring文件夹下创建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-4.0.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

            <!-- 配置Service扫描 -->
            <context:component-scan base-package="com.wenhao.service" />
        </beans>
        

  •     4.配置日志log4j.properties

        # Global logging configuration
        log4j.rootLogger=info, stdout
        # Console output...
        log4j.appender.stdout=org.apache.log4j.ConsoleAppender
        log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
        log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

    
        

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值