SpringMVC_Spring_Mybatis环境搭建

SpringMVC_Spring_Mybatis环境搭建_第1张图片

一、引入jar包:

1、引入spring的jar包
2、引入mybatis的jar包
3、引入spring与mybatis的整合包
mybatis-spring-1.2.4.jar
4、引入springmvc的jar包
spring-web.jar
spring-webmvc.jar

二、将Spring的IOC容器交给Web容器管理:

在 在web.xml配置spring提供的监听器(ContextLoaderListener)以及制定spring配置文件applicationContext.xml的位置

web.xml:


  <context-param>
    <param-name>contextConfigLocationparam-name>
    <param-value>classpath:applicationContext.xmlparam-value>
  context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
  listener>

三、在web.xml中配置SpringMVC的前端控制器

(1)、在web.xml加入springmvc前端控制器的配置,并指定springmvc配置文件的位置


  <servlet>
    <servlet-name>springmvcservlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    <init-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:springmvc.xmlparam-value>
    init-param>
  servlet>

  <servlet-mapping>
    <servlet-name>springmvcservlet-name>
    <url-pattern>*.dourl-pattern>
  servlet-mapping> 

(2)、配置spring自带的编码过滤器解决乱码问题:

<filter>
      <filter-name>CharacterEncodingFilterfilter-name>
      <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
      <init-param>
          <param-name>encodingparam-name>
          <param-value>UTF-8param-value>
      init-param>
filter>
<filter-mapping>
        <filter-name>CharacterEncodingFilterfilter-name>
        <url-pattern>/*url-pattern>
filter-mapping>   

四、SpringMVC中的文件配置:

(1)、在springmvc.xml中配置处理器映射器(RequestMappingHandlerMapping)和处理器适配器(RequestMappingHandlerAdapter):


    <bean class = "org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">bean> 

    
    <bean class = "org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">bean> 

注意:如果在springmvc中开启了注解驱动,则默认配置好以上两个

开启注解驱动的配置(mvc:annotation-driven):


    <mvc:annotation-driven>mvc:annotation-driven>

(2)、配置视图解析器(InternationnalResourceViewResolver)

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

prefix表示我们在Controller中请求页面路径的通用前缀。
suffix表示我们在Controller中请求页面文件路径的通用后缀。

(3)开启springmvc的组件扫描,在环境配置中与spring的组件扫描冲突,只扫描Controller的组件


    <context:component-scan base-package="Controller包名,可以放大扫描包的位置" use-default-filters="false">
        <context:include-filter type="annotation" 
    expression="org.springframework.stereotype.Controller"/>
    context:component-scan>

五、spring的文件配置(applicationContext.xml)

(1)、开启组件扫描,过滤掉在springmvc中配置的Controller。

<context:component-scan base-package="spring的扫描包文件" >
        
        <context:exclude-filter type="annotation" 
            expression="org.springframework.stereotype.Controller"/>
    context:component-scan>

(2)、配置数据库连接池(c3p0)为例:

property-placeholder location = "classpath:jdbc.properties"/>

    id= "dataSource" 
            class= "com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}">property>    
        <property name="jdbcUrl" value="${jdbc.url}">property>       
        <property name="user" value="${jdbc.user}">property>     
        <property name="password" value="${jdbc.password}">property>                     
     

(3)、配置管理sqlSessionFactory


    <bean id = "sqlSessionFactory" class = "org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref = "dataSource">property>
        <property name="configLocation" value="classpath:mybatis-config.xml">property>     
    bean>

(4)、配置到持久层开发的包

 class= "org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="持久层开发的包">property>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory">property>
 

将持久层开发的接口和相关的sql.xml放在该包下进行开发

六、mybatis-config主文件配置:

<configuration>

    <settings>
        
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    settings>

    
    <mappers>
        
         <package name=".java接口文件和.xml的sql文件包名"/>
    mappers>


configuration>

控制层组件的三大问题:
1、怎么匹配请求
2、怎么返回
3、怎么获取URL请求的参数

SpringMVC_Spring_Mybatis环境搭建_第2张图片

你可能感兴趣的:(springmvc)