SSM整合(Spring+SpringMvc+Mybatis)

文章目录

  • SSM整合
    • 步骤说明:
      • 引入相关架包:
      • 配置web.xml
      • 配置SpringContext.xml
      • 配置springmvc.xml
      • 注意:

SSM整合

  • 文件说明:
    • springmvc.xml : 配置SpringMvc框架相关
    • springcontext.xml : 配置Spring容器 和整合mybatis

步骤说明:

引入相关架包:

     <dependencies>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-webmvcartifactId>
                <version>5.0.4.RELEASEversion>
            dependency>
    
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-ormartifactId>
                <version>5.0.4.RELEASEversion>
            dependency>
    
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-testartifactId>
                <version>5.0.4.RELEASEversion>
            dependency>
    
            
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-aspectsartifactId>
                <version>5.0.4.RELEASEversion>
            dependency>
    
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-jdbcartifactId>
                <version>5.0.4.RELEASEversion>
            dependency>
    
            <dependency>
                <groupId>com.fasterxml.jackson.coregroupId>
                <artifactId>jackson-annotationsartifactId>
                <version>2.11.2version>
            dependency>
    
            <dependency>
                <groupId>com.fasterxml.jackson.coregroupId>
                <artifactId>jackson-coreartifactId>
                <version>2.11.2version>
            dependency>
    
            <dependency>
                <groupId>com.fasterxml.jackson.coregroupId>
                <artifactId>jackson-databindartifactId>
                <version>2.11.2version>
            dependency>
    
            <dependency>
                <groupId>commons-logginggroupId>
                <artifactId>commons-loggingartifactId>
                <version>1.2version>
            dependency>
    
            <dependency>
                <groupId>org.aspectjgroupId>
                <artifactId>aspectjweaverartifactId>
                <version>1.9.1version>
            dependency>
    
            <dependency>
                <groupId>cglibgroupId>
                <artifactId>cglibartifactId>
                <version>3.2.5version>
            dependency>
    
            <dependency>
                <groupId>aopalliancegroupId>
                <artifactId>aopallianceartifactId>
                <version>1.0version>
            dependency>
    
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-context-supportartifactId>
                <version>5.0.4.RELEASEversion>
            dependency>
    
            <dependency>
                <groupId>mysqlgroupId>
                <artifactId>mysql-connector-javaartifactId>
                <version>5.1.47version>
            dependency>
    
            <dependency>
                <groupId>org.mybatisgroupId>
                <artifactId>mybatisartifactId>
                <version>3.5.4version>
            dependency>
    
            <dependency>
                <groupId>org.mybatisgroupId>
                <artifactId>mybatis-springartifactId>
                <version>2.0.4version>
            dependency>
    
    
    
    
    
    
            <dependency>
                <groupId>junitgroupId>
                <artifactId>junitartifactId>
                <version>4.13version>
                <scope>testscope>
            dependency>
    
            
            <dependency>
                <groupId>javax.servletgroupId>
                <artifactId>jstlartifactId>
                <version>1.1.2version>
            dependency>
    
            <dependency>
                <groupId>taglibsgroupId>
                <artifactId>standardartifactId>
                <version>1.1.2version>
            dependency>
    
            <dependency>
                <groupId>com.mchangegroupId>
                <artifactId>c3p0artifactId>
                <version>0.9.5.1version>
            dependency>
    
            <dependency>
                <groupId>commons-iogroupId>
                <artifactId>commons-ioartifactId>
                <version>2.6version>
            dependency>
    
            <dependency>
                <groupId>commons-fileuploadgroupId>
                <artifactId>commons-fileuploadartifactId>
                <version>1.3.1version>
            dependency>
    
            <dependency>
                <groupId>log4jgroupId>
                <artifactId>log4jartifactId>
                <version>1.2.17version>
            dependency>
    
        dependencies>
    

配置web.xml

  • 配置编码过滤器
        
          <filter>
              <filter-name>encodingFilterfilter-name>
              <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
              
              <init-param>
                  <param-name>encodingparam-name>
                  <param-value>utf-8param-value>
              init-param>
              
              <init-param>
                  <param-name>forceEncodingparam-name>
                  <param-value>trueparam-value>
              init-param>
      
          filter>
          <filter-mapping>
              <filter-name>encodingFilterfilter-name>
              <url-pattern>/url-pattern>
          filter-mapping>
  • 配置MVC 核心控制器 DispatcherServlet
          
          <servlet>
              <servlet-name>springmvcservlet-name>
              <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
              
              <init-param>
                  <param-name>contextConfigLocationparam-name>
                  <param-value>classpath:spring-conf/SpringMvc.xmlparam-value>
              init-param>
              
              <load-on-startup>1load-on-startup>
          servlet>
          <servlet-mapping>
              <servlet-name>springmvcservlet-name>
              <url-pattern>/url-pattern>
          servlet-mapping>
      
  • 初始化Spring容器 配置监听 ContextLoaderListener
        
          <context-param>
              <param-name>contextConfigLocationparam-name>
              <param-value>classpath:spring-conf/SpringContext.xmlparam-value>
          context-param>
      
          <listener>
              <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
          listener>

配置SpringContext.xml

该配置文件用于初始化Sprng容器 和 整合mybatis

  • 配置全局数据源

    <context:property-placeholder location="classpath:jdbc.properties"/>
    
        <bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="${driver}"/>
            <property name="jdbcUrl" value="${url}"/>
            <property name="user" value="${user}"/>
            <property name="password" value="${pwd}"/>
        bean>
    
  • 整合Mybatis

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="myDataSource"/>
            <property name="configLocation" value="classpath:mybatis-conf/mybatis-config.xml"/>
            <property name="typeAliasesPackage" value="edu.lsl.domain"/>
            <property name="mapperLocations" value="classpath:edu/lsl/mapper/**/*.xml"/>
        bean>
    
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="edu.lsl.mapper"/>
        bean>
    

配置springmvc.xml


<beans
        xmlns="http://www.springframework.org/schema/beans"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        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/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
">
    <context:component-scan base-package="edu.lsl.controller,edu.lsl.mapper,edu.lsl.service"/>
    <mvc:annotation-driven/>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"/>
        <property name="suffix" value=".jsp"/>
    bean>
    <mvc:default-servlet-handler/>
beans>

注意:

  • 记得将tomcat的servlet-API.jar 添加到工程

你可能感兴趣的:(Java,学习,spring,mybatis,web.xml,java)