通过一个非常简单的SSM项目来将SpringMVC配置整理清晰。

所有的文件在Git上面都能找到,由于把代码搞上来看的很不舒服,结构不清晰。

第一步:确定环境

  • IDEA

  • MySQL 5.7.19

  • Tomcat 9

  • Maven 3.6

第二步:创建数据库

  参考GIT上面的SQL文件

 

第三步:新建Maven项目,基本目录搭建

  1.导入Springmvc,Mybatis,spring,Junit相关依赖

  2.建立基本结构和配置架构  

  • com.cll.pojo  
  • com.cll.dao
  • com.cll.service
  • com.cll.controller
  • mybatis-config.xml
  • appplicationContext.xml
  • database.properties
  • spring-dao.xml
  • spring-service.xml
  • spring-mvc.xml

第四步:编写Controller,和JSP文件(这些都很简单了,可以直接到Git上面下载)。

第五:主要来讲一下配置文件的分层。

  1.首先:application.xml.将其分为三层导入,整合speing配置文件。

    

   <import resource="spring-dao.xml"/>
   <import resource="spring-service.xml"/>
   <import resource="spring-mvc.xml"/>

   2.spring-dao.xml.这是Spring整合Mybatis的配置文件。

    

 <!-- 1.关联数据库文件 -->
   <context:property-placeholder location="classpath:database.properties"/>

    

<!-- 2.数据库连接池 -->
<!-- 3.配置SqlSessionFactory对象 -->
<!-- 4.配置扫描Dao接口包,动态实现Dao接口注入到spring容器中 -->

  3.spring-service层。spring整合service层的配置文件

     3.1 扫描service相关的bean

     3.2 将***serviceImpl注入到IOC容器中

   3.4 配置事务管理器,注入数据库连接池。

  到这里Spring层的配置结束,开始SpringMVC的配置。

       4.web.xml

    4.1配置DispatcherFilter,并且关联总的配置文件applicationContext.xml

<init-param>
           <param-name>contextConfigLocation</param-name>
           <!--一定要注意:我们这里加载的是总的配置文件,之前被这里坑了!-->  
           <param-value>classpath:applicationContext.xml</param-value>
       </init-param>

   4.2解决乱码,创建过滤器。

<!--encodingFilter-->
   <filter>
       <filter-name>encodingFilter</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>encodingFilter</filter-name>
       <url-pattern>/*</url-pattern>
   </filter-mapping>

   4.3设置session过期时间  

<session-config>
       <session-timeout>15</session-timeout>
   </session-config>
   
</web-app>

  5.spring-mvc.xml

    5.1开启现在常用的注解开发驱动

<mvc:annotation-driven />

    5.2静态资源默认Servlet配置

 <mvc:default-servlet-handler/>

       5.3配置视图解析器

   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
       <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
       <property name="prefix" value="/WEB-INF/jsp/" />
       <property name="suffix" value=".jsp" />
   </bean>

     5.4配置web相关的bean

   <context:component-scan base-package="com.kuang.controller" />  

好的,这基本就是配置文件的分层了。

Git地址:https://github.com/lele-Chang/order/tree/master/ssm

猜你喜欢

转载自www.cnblogs.com/leleChang/p/12938986.html
今日推荐